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)
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 
29 #ifndef META_TYPELIST_DIAGNOSTICS_H
30 #define META_TYPELIST_DIAGNOSTICS_H
31 
32 
33 #include "lib/meta/typelist.hpp"
34 #include "lib/meta/generator.hpp"
35 #include "lib/format-string.hpp"
36 #include "lib/format-cout.hpp"
37 #include "lib/meta/util.hpp"
38 
39 #include <string>
40 
41 
42 using std::string;
43 
44 
45 namespace lib {
46 namespace meta {
47 
49  struct Numz
50  {
51  uint o_;
52  Numz (uint x =0) : o_(x) { }
53  operator uint () const { return o_; }
54  };
55 
56 
60  template<int I>
61  struct Num : Numz
62  {
63  enum{ VAL=I };
64 
65  Num (uint x = uint(I)) : Numz(x) { }
66  };
67 
68 
69 
70  /* some forwards used by config-flags-test.cpp */
71  template<uint bit> struct Flag;
72  template< uint f1
73  , uint f2
74  , uint f3
75  , uint f4
76  , uint f5
77  >
78  struct Config;
79 
80 
81 
83  template<class X> struct CountDown { typedef NullType List; };
84  template<> struct CountDown<Num<0>> { typedef Node<Num<0>, NullType> List; };
85  template<int I> struct CountDown<Num<I>> { typedef Node<Num<I>, typename CountDown<Num<I-1>>::List> List; };
86 
87 
88 
89 
90  namespace test{ // unit tests covering typelist manipulating templates
91  namespace { // internals to support diagnostics in unit tests....
92 
93 
94  using util::_Fmt;
95 
96  struct NullP
97  {
98  static string print () { return "-"; }
99  };
100 
104  template<class T=NullType, class BASE=NullP>
105  struct Printer
106  : BASE
107  {
108  static string print () { return _Fmt("-<%s>%s") % typeStr<T>() % BASE::print(); }
109  };
110 
111  template<class BASE>
112  struct Printer<NullType, BASE>
113  : BASE
114  {
115  static string print () { return _Fmt("-<%u>%s") % "·" % BASE::print(); }
116  };
117 
118  template<class BASE, int I>
119  struct Printer<Num<I>, BASE>
120  : BASE
121  {
122  static string print () { return _Fmt("-<%u>%s") % uint(Num<I>::VAL) % BASE::print(); }
123  };
124 
125  template<class BASE, uint Fl>
126  struct Printer<Flag<Fl>, BASE>
127  : BASE
128  {
129  static string print () { return _Fmt("-<%u>%s") % uint(Fl) % BASE::print(); }
130  };
131 
132  template<class BASE>
133  struct Printer<int, BASE>
134  : BASE
135  {
136  static string print () { return _Fmt("-<%u>%s") % 'i' % BASE::print(); }
137  };
138 
139 
140 
143  template<class L>
144  string
146  {
148  return SubList::print();
149  }
150 
152  template<class TY, class TYPES, class BASE>
153  struct Printer<Node<TY,TYPES>, BASE>
154  : BASE
155  {
156  static string print ()
157  {
158  typedef Node<TY,TYPES> List;
159  return string("\n\t+--") + printSublist<List>()+"+"
160  + BASE::print();
161  }
162  };
163 
164  template<uint f1, uint f2, uint f3, uint f4, uint f5, class BASE>
165  struct Printer<Config<f1,f2,f3,f4,f5>, BASE>
166  : BASE
167  {
168  static string print ()
169  {
170  typedef typename Config<f1,f2,f3,f4,f5>::Flags FlagList;
171  return string("\n\t+-Conf-[") + printSublist<FlagList>()+"]"
172  + BASE::print();
173  }
174  };
175 
176  } // (End) internal defs
177 
178 
179 
180  /* ===== printing types and contents ===== */
181 
182  template<typename TYPES>
184  string >
185  showType ()
186  {
188  return DumpPrinter::print();
189  }
190 
191  // Note: we define overloads of this function for other types, especially Tuples
192 
193 
194 #define DISPLAY(_IT_) \
195  cout << STRINGIFY(_IT_) << "\t:" << showType<_IT_>() << endl;
196 
197 #define DUMPVAL(_IT_) \
198  cout << STRINGIFY(_IT_) << "\t:" << util::toString(_IT_) << endl;
199 
200 
201 
202 
203 
204 }}} // namespace lib::meta::test
205 #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:40
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:83
debugging template, printing the "number" used for instantiation on ctor call
Build a single inheritance chain of template instantiations.
Definition: generator.hpp:120
dummy interface / baseclass for diagnostics
helper for generating test lists
< distinct type representing a configuration
Definition: configflags.hpp:81
constant-wrapper type for debugging purposes, usable for generating lists of distinguishable types ...