Lumiera  0.pre.03
»edit your freedom«
configflags.hpp
Go to the documentation of this file.
1 /*
2  CONFIGFLAGS.hpp - Building classes based on configuration cases
3 
4  Copyright (C)
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
39 #ifndef LIB_META_CONFIGFLAGS_H
40 #define LIB_META_CONFIGFLAGS_H
41 
42 
43 #include "lib/meta/typelist.hpp"
44 
45 
46 namespace lib {
47 namespace meta{
48 
49  const size_t CONFIG_FLAGS_MAX = 5;
50 
51 
52  template<uint bit> struct Flag { typedef Flag ID; };
53  template<> struct Flag<0> { typedef NullType ID; };
54 
55 
56  template< uint f1=0
57  , uint f2=0
58  , uint f3=0
59  , uint f4=0
60  , uint f5=0
61  >
62  struct Flags
63  {
64  typedef typename Types< typename Flag<f1>::ID
65  , typename Flag<f2>::ID
66  , typename Flag<f3>::ID
67  , typename Flag<f4>::ID
68  , typename Flag<f5>::ID
69  >::List
70  Tuple;
71  typedef Tuple List;
72  };
73 
74 
75  template< uint f1=0
76  , uint f2=0
77  , uint f3=0
78  , uint f4=0
79  , uint f5=0
80  >
81  struct Config
82  {
83  typedef typename Flags<f1,f2,f3,f4,f5>::Tuple Flags;
84  typedef Flags List;
85  };
86 
87 
88 
89  template<uint Fl, class CONF>
90  struct ConfigSetFlag;
91 
92  template< uint Fl
93  , uint f1
94  , uint f2
95  , uint f3
96  , uint f4
97  , uint IGN
98  >
99  struct ConfigSetFlag<Fl, Config<f1,f2,f3,f4,IGN>>
100  {
102  };
103 
104 
105 
107  template<class FLAGS, class CONF=Config<> >
109  {
110  typedef CONF Config;
111  typedef Config Type;
112  };
113  template<uint Fl, class FLAGS, class CONF>
114  struct BuildConfigFromFlags< Node<Flag<Fl>,FLAGS>, CONF>
115  {
116  typedef typename ConfigSetFlag< Fl
117  , typename BuildConfigFromFlags<FLAGS,CONF>::Config
118  >::Config Config;
119  typedef Config Type;
120  };
121 
123  template<class FLAGS>
125 
126 
127  namespace {
129  template<uint ii, uint jj>
130  struct maxC
131  {
132  enum{ VAL = ii < jj? jj : ii };
133  };
134  }
135 
136 
148  template<class FLAGS>
149  struct FlagInfo;
150 
151  template<uint ff, class FLAGS>
152  struct FlagInfo<Node<Flag<ff>, FLAGS>>
153  {
154  enum{ BITS = maxC< ff, FlagInfo<FLAGS>::BITS> ::VAL
155  , CODE = (1<<ff) | FlagInfo<FLAGS>::CODE
156  };
157  };
158 
159  template<>
161  {
162  enum{ BITS = 0
163  , CODE = 0
164  };
165 
166  template<class FUNC>
167  static typename FUNC::Ret
168  accept (FUNC& functor)
169  {
170  return functor.done();
171  }
172  };
173 
174  template<class CONF, class TAIL>
175  struct FlagInfo<Node<CONF, TAIL>>
176  {
177  typedef typename CONF::Flags ThisFlags;
178  enum{
179  BITS = maxC< FlagInfo<ThisFlags>::BITS, FlagInfo<TAIL>::BITS > ::VAL
180  };
181 
182  template<class FUNC>
183  static typename FUNC::Ret
184  accept (FUNC& functor)
185  {
186  functor.template visit<CONF>(FlagInfo<ThisFlags>::CODE);
187  return FlagInfo<TAIL>::accept (functor);
188  }
189  };
190 
191 
192 
193 }} // namespace lib::meta
194 #endif
A template metaprogramming technique for manipulating collections of types.
Helper for calculating values and for invoking runtime code based on a given FlagTuple.
helper comparing enum values and flags
Implementation namespace for support and library code.
< distinct type representing a configuration
Definition: configflags.hpp:81
create a configuration type for the given list-of-flags
build a configuration type from a list-of-flags