name_of.h
Go to the documentation of this file.
1 #ifndef cetlib_name_of_h
2 #define cetlib_name_of_h
3 
4 // ======================================================================
5 //
6 // name_of: Define name_of<>::is() for each native type; can be
7 // specialized for arbitrary user-defined class/struct type
8 //
9 // TODO: specialize for function, ptr-to-mbr-obj, ptr-to-mbr_fctn
10 // TODO: consider specializing for more standard library types
11 //
12 // ======================================================================
13 
14 #include <cstddef>
15 #include <iosfwd>
16 #include <string>
17 #include <type_traits>
18 
19 namespace cet {
20 
21  // primary template:
22  template <class T>
23  struct name_of;
24 
25  // specializations for native types:
26  template <>
27  struct name_of<signed char>;
28  template <>
29  struct name_of<int>;
30  template <>
31  struct name_of<long>;
32  template <>
33  struct name_of<short>;
34  template <>
35  struct name_of<long long>;
36  template <>
37  struct name_of<unsigned char>;
38  template <>
39  struct name_of<unsigned int>;
40  template <>
41  struct name_of<unsigned long>;
42  template <>
43  struct name_of<unsigned short>;
44  template <>
45  struct name_of<unsigned long long>;
46  template <>
47  struct name_of<float>;
48  template <>
49  struct name_of<double>;
50  template <>
51  struct name_of<long double>;
52  template <>
53  struct name_of<void>;
54  template <>
55  struct name_of<bool>;
56  template <>
57  struct name_of<char>;
58 
59  // specializations for composite types:
60  template <class T>
61  struct name_of<T const>;
62  template <class T>
63  struct name_of<T volatile>;
64  template <class T>
65  struct name_of<T const volatile>;
66  template <class T>
67  struct name_of<T*>;
68  template <class T>
69  struct name_of<T&>;
70  template <class T, int N>
71  struct name_of<T[N]>;
72 
73  // specializations for selected library types:
74  template <>
75  struct name_of<std::istream>;
76  template <>
77  struct name_of<std::nullptr_t>;
78  template <>
79  struct name_of<std::ostream>;
80  template <>
81  struct name_of<std::string>;
82 }
83 
84 // ----------------------------------------------------------------------
85 // primary template:
86 
87 template <class T>
88 struct cet::name_of {
89  static std::string
90  is()
91  {
92  return "unknown-type";
93  }
94 };
95 
96 // ----------------------------------------------------------------------
97 // signed integral types:
98 
99 template <>
100 struct cet::name_of<signed char> {
101  static std::string
102  is()
103  {
104  return "schar";
105  }
106 };
107 
108 template <>
109 struct cet::name_of<int> {
110  static std::string
111  is()
112  {
113  return "int";
114  }
115 };
116 
117 template <>
118 struct cet::name_of<long> {
119  static std::string
120  is()
121  {
122  return "long";
123  }
124 };
125 
126 template <>
127 struct cet::name_of<short> {
128  static std::string
129  is()
130  {
131  return "short";
132  }
133 };
134 
135 template <>
136 struct cet::name_of<long long> {
137  static std::string
138  is()
139  {
140  return "llong";
141  }
142 };
143 
144 // ----------------------------------------------------------------------
145 // unsigned integral types:
146 
147 template <>
148 struct cet::name_of<unsigned char> {
149  static std::string
150  is()
151  {
152  return "uchar";
153  }
154 };
155 
156 template <>
157 struct cet::name_of<unsigned int> {
158  static std::string
159  is()
160  {
161  return "uint";
162  }
163 };
164 
165 template <>
166 struct cet::name_of<unsigned long> {
167  static std::string
168  is()
169  {
170  return "ulong";
171  }
172 };
173 
174 template <>
175 struct cet::name_of<unsigned short> {
176  static std::string
177  is()
178  {
179  return "ushort";
180  }
181 };
182 
183 template <>
184 struct cet::name_of<unsigned long long> {
185  static std::string
186  is()
187  {
188  return "ullong";
189  }
190 };
191 
192 // ----------------------------------------------------------------------
193 // floating types:
194 
195 template <>
197  static std::string
198  is()
199  {
200  return "float";
201  }
202 };
203 
204 template <>
205 struct cet::name_of<double> {
206  static std::string
207  is()
208  {
209  return "double";
210  }
211 };
212 
213 template <>
214 struct cet::name_of<long double> {
215  static std::string
216  is()
217  {
218  return "ldouble";
219  }
220 };
221 
222 // ----------------------------------------------------------------------
223 // remaining fundamental types:
224 
225 template <>
226 struct cet::name_of<void> {
227  static std::string
228  is()
229  {
230  return "void";
231  }
232 };
233 
234 template <>
236  static std::string
237  is()
238  {
239  return "bool";
240  }
241 };
242 
243 template <>
244 struct cet::name_of<char> {
245  static std::string
246  is()
247  {
248  return "char";
249  }
250 };
251 
252 // ----------------------------------------------------------------------
253 // cv-qualified types:
254 
255 template <class T>
257  static std::string
258  is()
259  {
260  return "c_" + name_of<T>::is();
261  }
262 };
263 
264 template <class T>
265 struct cet::name_of<T volatile> {
266  static std::string
267  is()
268  {
269  return "v_" + name_of<T>::is();
270  }
271 };
272 
273 template <class T>
274 struct cet::name_of<T const volatile> {
275  static std::string
276  is()
277  {
278  return "c-v_" + name_of<T>::is();
279  }
280 };
281 
282 // ----------------------------------------------------------------------
283 // pointer and reference types:
284 
285 template <class T>
286 struct cet::name_of<T*> {
287  static std::string
288  is()
289  {
290  return "ptr-to_" + name_of<T>::is();
291  }
292 };
293 
294 template <class T>
295 struct cet::name_of<T&> {
296  static std::string
297  is()
298  {
299  return "ref-to_" + name_of<T>::is();
300  }
301 };
302 
303 // ----------------------------------------------------------------------
304 // array types:
305 
306 template <class T, int N>
307 struct cet::name_of<T[N]> {
308  static std::string
309  is()
310  {
311  return "array[" + std::to_string(N) + "]-of_" + name_of<T>::is();
312  }
313 };
314 
315 // ----------------------------------------------------------------------
316 // selected library types::
317 
318 template <>
319 struct cet::name_of<std::istream> {
320  static std::string
321  is()
322  {
323  return "std::istream";
324  }
325 };
326 
327 template <>
328 struct cet::name_of<std::nullptr_t> {
329  static std::string
330  is()
331  {
332  return "std::nullptr_t";
333  }
334 };
335 
336 template <>
337 struct cet::name_of<std::ostream> {
338  static std::string
339  is()
340  {
341  return "std::ostream";
342  }
343 };
344 
345 template <>
347  static std::string
348  is()
349  {
350  return "std::string";
351  }
352 };
353 
354 // ======================================================================:
355 
356 #endif /* cetlib_name_of_h */
357 
358 // Local Variables:
359 // mode: c++
360 // End:
static std::string is()
Definition: name_of.h:276
static std::string is()
Definition: name_of.h:237
static std::string is()
Definition: name_of.h:321
static std::string is()
Definition: name_of.h:258
std::string string
Definition: nybbler.cc:12
static std::string is()
Definition: name_of.h:207
STL namespace.
static std::string is()
Definition: name_of.h:228
static std::string is()
Definition: name_of.h:186
static std::string is()
Definition: name_of.h:288
static std::string is()
Definition: name_of.h:339
static std::string is()
Definition: name_of.h:129
static std::string is()
Definition: name_of.h:348
static std::string is()
Definition: name_of.h:138
static std::string is()
Definition: name_of.h:198
static std::string is()
Definition: name_of.h:120
static std::string is()
Definition: name_of.h:330
static std::string is()
Definition: name_of.h:102
static std::string is()
Definition: name_of.h:309
static std::string is()
Definition: name_of.h:267
static std::string is()
Definition: name_of.h:150
static std::string is()
Definition: name_of.h:168
static std::string is()
Definition: name_of.h:246
static std::string is()
Definition: name_of.h:177
static std::string is()
Definition: name_of.h:90
static std::string is()
Definition: name_of.h:297
static std::string is()
Definition: name_of.h:111
int bool
Definition: qglobal.h:345
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
static std::string is()
Definition: name_of.h:216
static std::string is()
Definition: name_of.h:159