Boost logo

Boost Users :

From: Joel (joel_at_[hidden])
Date: 2004-09-23 22:10:53


Barak Simon wrote:

> Hi,
>
> I am trying to use the Tuple library in order to provide a logging interface which can get
> a variable number of parameters and handle them in a type-safe manner.
>
> However, though constructing a tuple by make_tuple seems easy enough (from a logging's user point of view)
> implementation wise, I am not sure how to actually iterate on the make_tuple_mapper which is being provided to my logging method, as
> generically getting the length and using invoking "get" on it does not work due to compile-time reasons (and there are no iterators available).
>
> I am sure it must be possible to do it somehow, or else the ostream serialisation capabilities would not have been possible to implement,
> but I guess I am just not sophisticated enough to fully grasp the MP intricacies...
>
> My logging method currently looks like that:
>
> template<class TheTuple>
> inline void logTuple(TheTuple theTuple)
> {
> }
>
> which might not be the correct way about it, but I was not sure how to explicitly provide a tuple type that's created by make_tuple,
> let alone how to iterate on the values.
>
> Any help with this would be greatly appreciated.

There's Fusion. It is currently a sub-library of Spirit
(boost/spirit/fusion). After the release of boost 1.32,
the plan is to upgrade the tuples library. Here's a
short synopsis:

Fusion Tuples Library

This is a re-implementation of the TR tuples with iterators, views and
algorithms. The structure is somewhat modeled after MPL. It is code-named
"fusion" because the library is the "fusion" of compile time
metaprogramming with runtime programming.

Overall structure:

     The library is composed of three sub-modules. Iterators, Sequences and
     Views and Algorithms.

Iterators:

     Generic iteration for heterogeneous types. The library is based on
     iterators.

     filter_view_iterator
         filtering iterator. Given an MPL predicate, skips elements to
         present only the items that passes the predicate. See filter_view
         below.

     joint_view_iterator
         Holds four iterators (two begin/end pairs). Iterates over the
         first pair then switches over to the next pair to present
         a contiguous whole. See joint_view below.

     single_view_iterator
         A single element iterator. See single_view below.

     transform_view_iterator
         Given a transform-function, transforms the elements being
         iterated. See transform_view below.

Sequences and Views:

     Holds a begin/end iterator. Sequences and views may be composed to form
     more complex sequences and views. View/sequence composition is a very
     nice concept. These are extremely lighweight classes and can be passed
     around by value quite inexpensively. For instance, rather than working
     directly on tuples, the algorithms work on sequences and return
     sequences.

     tuple
         The basic tuple structure

     tupleN
         Fixed sized tuples (where N = 0 to a predefined limit)

     filter_view
         Given an MPL predicate, filters the view to present only the items
         that passes the predicate.

     single_view
         A single element view

     joint_view
         A two-sequence view (concatenates two sequences)

     range
         Holds an iterator pair that represents a begin/end range.

     transform_view
         Transforms a sequence given a transform-function

     MPL sequences are also, automatically, fusion sequences. All algorithms
     and fusion functions that work on fusion sequences can also take in
     MPL seqneces.

Basic functions and meta_functions on sequences:

     I/O : TR1-tuples compatible I/O routines
     operator : tuple operators ==, !=, <, >, <=, >=
     begin : start of sequence
     end : end of sequence
     make-tuple : make a tuple
     tie : make a tuple of references
     generate : given a fusion sequence, generate a tuple
     get<N> : get the nth element of a tuple
     is_sequence : checks if a type is a fusion sequence
     tuple_element : get the nth type in a tuple
     tuple_size : get the number of elements in a tuple

Algorithms:

     With very complex composition of algorithms, it is not desirable to
     work directly on tuples. Like MPL, and unlike STL, the algorithms take
     in sequences/views and *return* the result by value; the algorithms are
     purely functional and do not (cannot) have side effects. We cannot have
     an out parameter that is passed by reference where the result is
     placed. The algorithms, instead, work on sequences and views and
     generate views.

     This strategy is very efficient. You can think of the algorithms as
     "lazy". The values are generated only wnen it is needed -for example
     when generating a tuple. When you are sure that you need a tuple, you
     can "generate" it from the sequence: generate(sequence);

         erase
         filter
         find
         find_if
         fold
         for_each
         insert
         push_back
         push_front
         remove
         remove_if
         replace
         transform

Cheers,

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net