DataGenerators.h

00001 
00002 // The Loki Library
00003 // Data Generator by Shannon Barber
00004 // This code DOES NOT accompany the book:
00005 // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design 
00006 //     Patterns Applied". Copyright (c) 2001. Addison-Wesley.
00007 //
00008 // Code covered by the MIT License
00009 // The author makes no representations about the suitability of this software
00010 //  for any purpose. It is provided "as is" without express or implied warranty.
00012 #ifndef LOKI_DATAGENERATORS_INC_
00013 #define LOKI_DATAGENERATORS_INC_
00014 
00015 // $Id: DataGenerators.h 751 2006-10-17 19:50:37Z syntheticpp $
00016 
00017 
00018 #include "Typelist.h"
00019 
00020 //Reference version
00021 
00022 /************************************************************************************
00023 // class template GenData
00024 // Iteratates a Typelist, and invokes the functor GenFunc<T>
00025 // for each type in the list, passing a functor along the way.
00026 // The functor is designed to be an insertion iterator which GenFunc<T>
00027 // can use to output information about the types in the list.
00028 //
00029 
00030 Example Use
00031 
00032 template<typename T>
00033 struct ExtractDataType
00034     {
00035     some_type operator()()
00036         {
00037         return create_value_from_type<T>;
00038         }
00039     };
00040 
00041 Loki::IterateTypes<parameter_tl, ExtractDataType> gendata;
00042 std::vector<some_type> stuff;
00043 gendata(std::back_inserter(stuff));
00044 *******************************************************************************/
00045 namespace Loki
00046 {
00047     namespace TL
00048         {
00049         template<typename T>
00050         struct nameof_type
00051             {
00052             const char* operator()()
00053                 {
00054                 return typeid(T).name();
00055                 }
00056             };
00057         template<typename T>
00058         struct sizeof_type
00059             {
00060             size_t operator()()
00061                 {
00062                 return sizeof(T);
00063                 }
00064             };
00065     template <class TList, template <class> class GenFunc>
00066     struct IterateTypes;
00067      
00068     template <class T1, class T2, template <class> class GenFunc>
00069     struct IterateTypes<Typelist<T1, T2>, GenFunc>
00070     {
00071     typedef IterateTypes<T1, GenFunc> head_t;
00072     head_t head;
00073     typedef IterateTypes<T2, GenFunc> tail_t;
00074     tail_t tail;
00075     template<class II>
00076     void operator()(II ii)
00077         {
00078         head.operator()(ii);
00079         tail.operator()(ii);
00080         }
00081     };
00082      
00083     template <class AtomicType, template <class> class GenFunc>
00084     struct IterateTypes
00085     {
00086     template<class II>
00087     void operator()(II ii)
00088         {
00089         GenFunc<AtomicType> genfunc;
00090         *ii = genfunc();
00091         ++ii; //Is this even needed?
00092         }
00093     };
00094     
00095     template <template <class> class GenFunc>
00096     struct IterateTypes<NullType, GenFunc>
00097     {
00098     template<class II>
00099     void operator()(II ii)
00100         {}
00101     };
00102     
00103     template<typename Types, template <class> class UnitFunc, typename II>
00104     void iterate_types(II ii)
00105         {
00106         Loki::TL::IterateTypes<Types, UnitFunc> it;
00107         it(ii);
00108         }
00109     }//ns TL
00110 }//ns Loki
00111 
00112 #endif // end file guardian
00113 

Generated on Sun Feb 25 16:52:21 2007 for Loki by  doxygen 1.5.1-p1