<<< Stack primitive | Lambda Home | Constant scalar >>> |
Common Text Transformation Library http://cttl.sourceforge.net/
As an adaptable generic component, scalar and other types of lambda primitives communicate through a simple, uniform public interface, named scalar interface. Scalar and stack lambda classes assume the protocol of a general-purpose stack. Alongside with a simple, push/pop mechanism for the deposit and retrieval of data, stack interface is restricted by its singular point of access to the top element only:
Member function | Description |
---|---|
Such interface gives a programmatic way to manipulate primitives outside the lambda context, by a direct access to the encapsulated value. For example, program scalar_primitive.cpp creates and modifies an integer scalar via its stack interface:
Keywords: scalar_primitive.cpp
// scalar_primitive.cpp // Program demonstrates the interface of scalar primitive. #include "cttl/cttl.h" #include "lambda/lambda.h" using namespace cttl; int main(/*int argc, char* argv[]*/) { // C++ interface of scalar primitive lambda< int >::scalar Variable( 4 ); // instantiate integer scalar assert( Variable.top() == 4 ); // read access to top element Variable.push( 3 ); // push new value assert( Variable.top() == 3 ); // verify result Variable.top() = 2; // write access to top element assert( Variable.top() == 2 ); // verify result Variable.pop(); // does nothing unless Variable is a stack assert( Variable.size() == 1 ); // non-stack scalar has constant size return 0; }
From the expression standpoint, scalar primitive behaves a lot like a variable, and, for the most part, its native interface is not the main focus of programmer's attention. Instead, while in use by a lambda expression,
scalars of built-in and numeric types are manipulated via arithmetic operators;
scalar encapsulating object of the user-defined type uses closure calls to the object's member functions;
scalar wrapping a closure or higher-order function relies on overloaded operators to trigger the actual function call. For example, closure scalar is dereferenced to invoke the function.
From the implementation standpoint, the stack model and its LIFO (last-in first-out) context is a good choice for an interface, intended to connect objects hosted inside expression templates that support either
The interface has no run-time overhead or execution-time penalty. Internally, a specially designated compile-time policy class, which implements the required behavior, controls every part of the expression-template formulas, including the aspects of storage. This is a widespread technique among policy-based implementations of expression-template classes.
In addition to general-purpose stack interface, lambda primitive classes have member functions to
provide access to the encapsulated instance of the stack (for stack primitives), and
generate a copy of the reference-based lambda primitive for already existing primitive.
Member function | Description |
---|---|
Because make_reference() is often used in-line, knowing the exact reference_T of the resulting object is unimportant, as shown by the scalar_make_reference.cpp sample:
Keywords: scalar_make_reference.cpp, make_reference
// scalar_make_reference.cpp // Program demonstrates usage of make_reference() // member function for scalar lambda primitives. #define CTTL_TRACE_RULES // automatically turns lambda tracing on #include "cttl/cttl.h" #include "lambda/lambda.h" using namespace cttl; template< typename ScalarRefT > void push_data( ScalarRefT depository_, typename ScalarRefT::value_T value_ ) { depository_.push( value_ ); } int main(/*int argc, char* argv[]*/) { cttl::lambda< int >::scalar var = 1234; assert( var.top() == 1234 ); push_data( var.make_reference(), 5678 ); assert( var.top() == 5678 ); push_data( var.make_reference().make_reference(), 9999 ); assert( var.top() == 9999 ); return 0; }
Notice that make_reference() member of the reference-based lambda primitive returns an exact copy of the original object without penalty:
push_data( var.make_reference().make_reference(), 9999 );
Copyright © 1997-2009 Igor Kholodov mailto:cttl@users.sourceforge.net.
Permission to copy and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.
<<< Stack primitive | Lambda Home | Constant scalar >>> |