<<< Higher-order functions     Lambda Home     make_translator( ) helper >>>

Common Text Transformation Library http://cttl.sourceforge.net/

Value translators

The type of X The resulting type of value translator T^X
  • Any C++ type to be converted
    to another type:

  • Type conversion translator from TypeFrom to TypeTo:

     // conversion function:
     TypeTo convert( TypeFrom value_ ) { /*...*/ }
     TypeFrom var_a; // variable of one type
     TypeTo   var_b; // variable of another type
        scalar( &var_b )^convert = var_a

    See also: discussion about scalar instantiation, and scalar_types_mix.cpp sample.

  • String literal

     char const*
     wchar_t const*
  • Literal translator for a const literal:

     ++( scalar( &var )^atof^"3.14159" )
     ++( scalar( 0 )^atoi^"100" )

    See also: literal translator examples

  • CTTL substring

     const_edge< PolicyT, StringT > const&
     edge< PolicyT, StringT > const&
  • Substring translator. The substring is converted to a zero-terminated string, which is then passed as an argument to the chain of higher-order function calls encapsulaed by T:

     ++( scalar( &var )^atof^edge_ )
     ++( scalar( 0 )^atoi^edge_ )

    See also: substring translator examples

  • STL Sequence

     std::vector< ValueT >&
     std::pair< SequenceT, int >&
     std::pair< SequenceT*, int >&
  • Sequence translator has a form T^X^V, where X is a sequence, and V represents a reference to an input symbol of SequenceT::value_type. The sequence translator converts an input symbol V (such as "abc") to an integral type by

    • first, inserting the symbol into the sequence container, and

    • second, returning symbol's position in the sequence.

  • To connect with the input symbol, the sequence translator is usually combined with a literal translator, or a substring translator:

     ++( scalar( &var )^vect^"abc" ) // V is "abc"
     ++( scalar( 0 )^vect^edge_ )    // V is a substring

    The two additional std::pair formats exist for the reason of being template selectors by operator^ in T^X, and the std::pair::second element is never used. This is a hack that allows sequence translator to accept sequences other than std::vector, such as STL std::list, or a user-defined container class.

    Sequence translator implementation relies on the presence of two member functions in the sequence interface,

        SequenceT::push_back( SequenceT::value_type );

    See also: sequence translator examples

  • Dictionary pair

     std::pair< SequenceT, MapT >&
     std::pair< SequenceT*, MapT* >&
  • Dictionary translator algorithm is based on a sequence and an associative container, together forming a dictionary.

  • Dictionary translator has form T^X^V, where V represents a reference to an input symbol of MapT::key_type. The algorithm converts input symbol from MapT::key_type to MapT::data_type. The algorithm compares the input value to an existing key, and inserts new element into both containers, if necessary. The value for MapT::data_type is generated automatically based on the element position in the sequence container.

  • To connect with the input symbol, the dictionary translator is usually combined with a literal translator, or a substring translator:

     ++( scalar( 0 )^symbol_table^"abc" ),
     ++( scalar( 0 )^symbol_table^"def" )

    See also: dictionary translator examples

  • Closure

     // MACROS:
     CTTL_MEMBER_ACTION( obj, mf2, arg )
     CTTL_STATIC_ACTION( f1, arg )
     // Overloaded functions:

    Here, action() is a family of overloaded helper functions supporting all of the above macro formats. For detailed description see closure documentation.

  • Each helper returns an appropriate closure object. The parameters are:

  • obj is an object that implements the member function to be invoked. Note: obj is not a scalar type. Constant objects are passed by const reference; mutable objects - by address.

  • f, f1, mf1, and mf2 are function pointers, or STL function pointer adaptors for the static or member function to be invoked.

  • arg is the function argument, if any. const arg is passed by reference, mutable - by address.

  • X is a template parameter specifying the return type of a static or global function.

  • Closure translator T^X creates a representation of a function call, while using the closure X to accommodate a function pointer and an optional function argument:

    // transform substring using closure translator:
        scalar( &edge_ )
            std::mem_fun( &digit_parser::get_digit_name ),

    The same result using assignment operator:

    // transform substring using assignment:
    scalar( &edge_ ) = 
        *scalar( CTTL_MEMBER_ACTION(
            std::mem_fun( &digit_parser::get_digit_name ),

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.



<<< Higher-order functions     Lambda Home     make_translator( ) helper >>>