]> git.cworth.org Git - vogl/blob - src/extlib/loki/include/loki/DataGenerators.h
Initial vogl checkin
[vogl] / src / extlib / loki / include / loki / DataGenerators.h
1 ////////////////////////////////////////////////////////////////////////////////
2 // The Loki Library
3 // Data Generator by Shannon Barber
4 // This code DOES NOT accompany the book:
5 // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
6 //     Patterns Applied". Copyright (c) 2001. Addison-Wesley.
7 //
8 // Code covered by the MIT License
9 // The author makes no representations about the suitability of this software
10 //  for any purpose. It is provided "as is" without express or implied warranty.
11 ////////////////////////////////////////////////////////////////////////////////
12 #ifndef LOKI_DATAGENERATORS_INC_
13 #define LOKI_DATAGENERATORS_INC_
14
15 // $Id: DataGenerators.h 751 2006-10-17 19:50:37Z syntheticpp $
16
17
18 #include "Typelist.h"
19
20 //Reference version
21
22 /************************************************************************************
23 // class template GenData
24 // Iteratates a Typelist, and invokes the functor GenFunc<T>
25 // for each type in the list, passing a functor along the way.
26 // The functor is designed to be an insertion iterator which GenFunc<T>
27 // can use to output information about the types in the list.
28 //
29
30 Example Use
31
32 template<typename T>
33 struct ExtractDataType
34     {
35     some_type operator()()
36         {
37         return create_value_from_type<T>;
38         }
39     };
40
41 Loki::IterateTypes<parameter_tl, ExtractDataType> gendata;
42 std::vector<some_type> stuff;
43 gendata(std::back_inserter(stuff));
44 *******************************************************************************/
45 namespace Loki
46 {
47 namespace TL
48 {
49 template<typename T>
50 struct nameof_type
51 {
52         const char *operator()()
53         {
54                 return typeid(T).name();
55         }
56 };
57 template<typename T>
58 struct sizeof_type
59 {
60         size_t operator()()
61         {
62                 return sizeof(T);
63         }
64 };
65 template <class TList, template <class> class GenFunc>
66 struct IterateTypes;
67
68 template <class T1, class T2, template <class> class GenFunc>
69 struct IterateTypes<Typelist<T1, T2>, GenFunc>
70 {
71         typedef IterateTypes<T1, GenFunc> head_t;
72         head_t head;
73         typedef IterateTypes<T2, GenFunc> tail_t;
74         tail_t tail;
75         template<class II>
76         void operator()(II ii)
77         {
78                 head.operator()(ii);
79                 tail.operator()(ii);
80         }
81 };
82
83 template <class AtomicType, template <class> class GenFunc>
84 struct IterateTypes
85 {
86         template<class II>
87         void operator()(II ii)
88         {
89                 GenFunc<AtomicType> genfunc;
90                 *ii = genfunc();
91                 ++ii; //Is this even needed?
92         }
93 };
94
95 template <template <class> class GenFunc>
96 struct IterateTypes<NullType, GenFunc>
97 {
98         template<class II>
99         void operator()(II ii)
100         {}
101 };
102
103 template<typename Types, template <class> class UnitFunc, typename II>
104 void iterate_types(II ii)
105 {
106         Loki::TL::IterateTypes<Types, UnitFunc> it;
107         it(ii);
108 }
109 }//ns TL
110 }//ns Loki
111
112 #endif // end file guardian
113