Lumiera  0.pre.03
»edit your freedom«
typelist-diagnostics.hpp
Go to the documentation of this file.
1 /*
2  TYPELIST-DIAGNOSTICS - helper for testing the typelist based utilities
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
38 #ifndef META_TYPELIST_DIAGNOSTICS_H
39 #define META_TYPELIST_DIAGNOSTICS_H
40 
41 
42 #include "lib/meta/typelist.hpp"
43 #include "lib/meta/generator.hpp"
44 #include "lib/format-string.hpp"
45 #include "lib/format-cout.hpp"
46 #include "lib/meta/util.hpp"
47 
48 #include <string>
49 
50 
51 using std::string;
52 
53 
54 namespace lib {
55 namespace meta {
56 
58  struct Numz
59  {
60  uint o_;
61  Numz (uint x =0) : o_(x) { }
62  operator uint () const { return o_; }
63  };
64 
65 
69  template<int I>
70  struct Num : Numz
71  {
72  enum{ VAL=I };
73 
74  Num (uint x = uint(I)) : Numz(x) { }
75  };
76 
77 
78 
79  /* some forwards used by config-flags-test.cpp */
80  template<uint bit> struct Flag;
81  template< uint f1
82  , uint f2
83  , uint f3
84  , uint f4
85  , uint f5
86  >
87  struct Config;
88 
89 
90 
92  template<class X> struct CountDown { typedef NullType List; };
93  template<> struct CountDown<Num<0>> { typedef Node<Num<0>, NullType> List; };
94  template<int I> struct CountDown<Num<I>> { typedef Node<Num<I>, typename CountDown<Num<I-1>>::List> List; };
95 
96 
97 
98 
99  namespace test{ // unit tests covering typelist manipulating templates
100  namespace { // internals to support diagnostics in unit tests....
101 
102 
103  using util::_Fmt;
104 
105  struct NullP
106  {
107  static string print () { return "-"; }
108  };
109 
113  template<class T=NullType, class BASE=NullP>
114  struct Printer
115  : BASE
116  {
117  static string print () { return _Fmt("-<%s>%s") % typeStr<T>() % BASE::print(); }
118  };
119 
120  template<class BASE>
121  struct Printer<NullType, BASE>
122  : BASE
123  {
124  static string print () { return _Fmt("-<%u>%s") % "·" % BASE::print(); }
125  };
126 
127  template<class BASE, int I>
128  struct Printer<Num<I>, BASE>
129  : BASE
130  {
131  static string print () { return _Fmt("-<%u>%s") % uint(Num<I>::VAL) % BASE::print(); }
132  };
133 
134  template<class BASE, uint Fl>
135  struct Printer<Flag<Fl>, BASE>
136  : BASE
137  {
138  static string print () { return _Fmt("-<%u>%s") % uint(Fl) % BASE::print(); }
139  };
140 
141  template<class BASE>
142  struct Printer<int, BASE>
143  : BASE
144  {
145  static string print () { return _Fmt("-<%u>%s") % 'i' % BASE::print(); }
146  };
147 
148 
149 
152  template<class L>
153  string
155  {
157  return SubList::print();
158  }
159 
161  template<class TY, class TYPES, class BASE>
162  struct Printer<Node<TY,TYPES>, BASE>
163  : BASE
164  {
165  static string print ()
166  {
167  typedef Node<TY,TYPES> List;
168  return string("\n\t+--") + printSublist<List>()+"+"
169  + BASE::print();
170  }
171  };
172 
173  template<uint f1, uint f2, uint f3, uint f4, uint f5, class BASE>
174  struct Printer<Config<f1,f2,f3,f4,f5>, BASE>
175  : BASE
176  {
177  static string print ()
178  {
179  typedef typename Config<f1,f2,f3,f4,f5>::Flags FlagList;
180  return string("\n\t+-Conf-[") + printSublist<FlagList>()+"]"
181  + BASE::print();
182  }
183  };
184 
185  } // (End) internal defs
186 
187 
188 
189  /* ===== printing types and contents ===== */
190 
191  template<typename TYPES>
193  string >
194  showType ()
195  {
197  return DumpPrinter::print();
198  }
199 
200  // Note: we define overloads of this function for other types, especially Tuples
201 
202 
203 #define DISPLAY(_IT_) \
204  cout << STRINGIFY(_IT_) << "\t:" << showType<_IT_>() << endl;
205 
206 #define DUMPVAL(_IT_) \
207  cout << STRINGIFY(_IT_) << "\t:" << util::toString(_IT_) << endl;
208 
209 
210 
211 
212 
213 }}} // namespace lib::meta::test
214 #endif
string printSublist()
call the debug-print for a typelist utilising the Printer template
Automatically use custom string conversion in C++ stream output.
A template metaprogramming technique for manipulating collections of types.
Definition: run.hpp:49
Simple and lightweight helpers for metaprogramming and type detection.
Front-end for printf-style string template interpolation.
Helpers for working with lib::meta::Types (i.e.
A front-end for using printf-style formatting.
Implementation namespace for support and library code.
typename enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition: meta/util.hpp:92
debugging template, printing the "number" used for instantiation on ctor call
Build a single inheritance chain of template instantiations.
Definition: generator.hpp:129
dummy interface / baseclass for diagnostics
helper for generating test lists
< distinct type representing a configuration
Definition: configflags.hpp:90
constant-wrapper type for debugging purposes, usable for generating lists of distinguishable types ...