stlab.adobe.com Adobe Systems Incorporated
typeinfo.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
4  or a copy at http://stlab.adobe.com/licenses.html)
5 */
6 
7 /**************************************************************************************************/
8 
9 #ifndef ADOBE_TYPEINFO_HPP
10 #define ADOBE_TYPEINFO_HPP
11 
12 /**************************************************************************************************/
13 
14 #include <adobe/config.hpp>
15 
16 #include <boost/operators.hpp>
17 #include <boost/range/as_literal.hpp>
18 
19 #include <cstddef>
20 #include <functional>
21 #include <string>
22 #include <typeinfo>
23 
24 #include <adobe/algorithm/copy.hpp>
25 
26 #ifndef NDEBUG
27 #include <iosfwd>
28 #endif
29 
30 /**************************************************************************************************/
31 
32 namespace adobe {
33 
42 /**************************************************************************************************/
43 
44 namespace implementation {
45 
46 /**************************************************************************************************/
47 
48 const char* cppfilt(const std::type_info& type);
49 
50 /**************************************************************************************************/
51 
52 struct type_instance_t
53 {
54  const std::type_info* type_info_m;
55  const char* name_m;
56  const type_instance_t* parameter_m[6];
57 
58  bool requires_std_rtti() const;
59 };
60 
61 template <typename O> // O models OutputIterator
62 // requires value_type(O) == char
63 O serialize(const type_instance_t& x, O out)
64 {
65  if (x.type_info_m)
66  { return copy_sentinal(cppfilt(*x.type_info_m), out).second; }
67 
68  out = copy_sentinal(x.name_m, out).second;
69  if (x.parameter_m[0]) {
70  *out++ = '<'; out = serialize(*x.parameter_m[0], out);
71  for (const type_instance_t* const* xp = &x.parameter_m[1]; *xp; ++xp) {
72  *out++ = ','; out = serialize(**xp, out);
73  }
74  *out++ = '>';
75  }
76  return out;
77 };
78 
79 bool before(const type_instance_t& x, const type_instance_t& y);
80 bool operator==(const type_instance_t& x, const type_instance_t& y);
81 
82 inline bool operator!=(const type_instance_t& x, const type_instance_t& y) { return !(x == y); }
83 
84 /**************************************************************************************************/
85 
86 } // namespace implementation
87 
88 /**************************************************************************************************/
89 
90 namespace version_1 {
91 
92 
93 #ifndef ADOBE_REQUIRES_STD_RTTI
94 #define ADOBE_REQUIRES_STD_RTTI 1
95 #endif
96 
97 #if !ADOBE_REQUIRES_STD_RTTI
98 
99 template <typename T, typename Any = void>
100 struct make_type_info
101 { };
102 
103 #else
104 
105 template <typename T, typename Any = void>
106 struct make_type_info { static const implementation::type_instance_t value; };
107 
108 template <typename T, typename Any>
109 const implementation::type_instance_t make_type_info<T, Any>::value = { &typeid(T*), 0, { 0 } };
110 
111 #endif
112 
113 /**************************************************************************************************/
114 
115 template <typename Any, typename T0, std::size_t Size>
116 struct make_type_info<T0[Size], Any>
117 {
118  static const implementation::type_instance_t value;
119  static const char name_s[256];
120 };
121 
122 template <typename Any, typename T0, std::size_t Size>
124  = { 'a', 'r', 'r', 'a', 'y', '['
125  , Size / 1000000000UL % 10 + '0'
126  , Size / 100000000UL % 10 + '0'
127  , Size / 10000000UL % 10 + '0'
128  , Size / 1000000UL % 10 + '0'
129  , Size / 100000UL % 10 + '0'
130  , Size / 10000UL % 10 + '0'
131  , Size / 1000UL % 10 + '0'
132  , Size / 100UL % 10 + '0'
133  , Size / 10UL % 10 + '0'
134  , Size / 1UL % 10 + '0'
135  ,']' };
136 
137 template <typename Any, typename T0, std::size_t Size>
138 const implementation::type_instance_t make_type_info<T0[Size], Any>::value
139  = { 0, &name_s[0], { &make_type_info<T0>::value} };
140 
141 /**************************************************************************************************/
142 
143 template <typename Any, typename T0, std::size_t Size>
144 struct make_type_info<const T0[Size], Any>
145 {
146  static const implementation::type_instance_t value;
147  static const char name_s[256];
148 };
149 
150 template <typename Any, typename T0, std::size_t Size>
152  = { 'a', 'r', 'r', 'a', 'y', '['
153  , Size / 1000000000UL % 10 + '0'
154  , Size / 100000000UL % 10 + '0'
155  , Size / 10000000UL % 10 + '0'
156  , Size / 1000000UL % 10 + '0'
157  , Size / 100000UL % 10 + '0'
158  , Size / 10000UL % 10 + '0'
159  , Size / 1000UL % 10 + '0'
160  , Size / 100UL % 10 + '0'
161  , Size / 10UL % 10 + '0'
162  , Size / 1UL % 10 + '0'
163  ,']' };
164 
165 template <typename Any, typename T0, std::size_t Size>
166 const implementation::type_instance_t make_type_info<const T0[Size], Any>::value
167  = { 0, &name_s[0], { &make_type_info<const T0>::value} };
168 
169 /**************************************************************************************************/
170 
175 class type_info_t : boost::equality_comparable<type_info_t>
176 {
177  public:
178 
182  const char* name() const
183  { return identity_m->type_info_m ? identity_m->type_info_m->name() : identity_m->name_m; }
184 
189  bool before(const type_info_t& x) const
190  { return adobe::implementation::before(*identity_m, *x.identity_m); }
191 
192  bool requires_std_rtti() const
193  { return identity_m->requires_std_rtti(); }
194 
199  friend bool inline operator==(const type_info_t& x, const type_info_t& y)
200  { return *x.identity_m == *y.identity_m; }
201 
202 #ifndef NDEBUG
203  friend std::ostream& operator<<(std::ostream& out, const type_info_t& x);
204 #endif
205 
206  template <typename T>
207  friend type_info_t type_info();
208 
209  friend struct aggregate_type_info_t;
210 
211  template <typename O>
212  friend inline O serialize(const type_info_t& x, O out)
213  { return serialize(*x.identity_m, out); }
214 
215  private:
216  explicit type_info_t(const implementation::type_instance_t* x) : identity_m(x) { }
217 
218  const implementation::type_instance_t* identity_m;
219 };
220 
225 template <typename T>
228 
233 template <typename T>
234 inline type_info_t type_info(const T&) { return type_info<const T>(); }
235 
240 template <typename T>
241 inline type_info_t type_info(T&) { return type_info<T>(); }
242 
243 /**************************************************************************************************/
244 
246 {
247  operator type_info_t() const { return type_info_t(&private_m); }
248 
249  const implementation::type_instance_t& private_m;
250 };
251 
252 /**************************************************************************************************/
253 
260 #define ADOBE_NAME_TYPE_0(name, ...) \
261 namespace adobe { namespace version_1 { \
262 template <typename Any> \
263 struct make_type_info<__VA_ARGS__, Any> { static const implementation::type_instance_t value; }; \
264 template <typename Any> \
265 const implementation::type_instance_t make_type_info<__VA_ARGS__, Any>::value \
266  = { 0, name, { 0 } }; \
267 } }
268 
269 /**************************************************************************************************/
270 
277 #define ADOBE_NAME_TYPE_1(name, ...) \
278 namespace adobe { namespace version_1 { \
279 template <typename Any, typename T0> \
280 struct make_type_info<__VA_ARGS__, Any> { static const implementation::type_instance_t value; }; \
281 template <typename Any, typename T0> \
282 const implementation::type_instance_t make_type_info<__VA_ARGS__, Any>::value \
283  = { 0, name, { &make_type_info<T0>::value } }; \
284 } }
285 
286 /**************************************************************************************************/
287 
295 #define ADOBE_NAME_TYPE_2(name, ...) \
296 namespace adobe { namespace version_1 { \
297 template <typename Any, typename T0, typename T1> \
298 struct make_type_info<__VA_ARGS__, Any> { static const implementation::type_instance_t value; }; \
299 template <typename Any, typename T0, typename T1> \
300 const implementation::type_instance_t make_type_info<__VA_ARGS__, Any>::value \
301  = { 0, name, { &make_type_info<T0>::value, &make_type_info<T1>::value } }; \
302 } }
303 
304 /**************************************************************************************************/
305 
312 #define ADOBE_NAME_TYPE_3(name, ...) \
313 namespace adobe { namespace version_1 { \
314 template <typename Any, typename T0, typename T1, typename T2> \
315 struct make_type_info<__VA_ARGS__, Any> { static const implementation::type_instance_t value; }; \
316 template <typename Any, typename T0, typename T1, typename T2> \
317 const implementation::type_instance_t make_type_info<__VA_ARGS__, Any>::value \
318  = { 0, name, { &make_type_info<T0>::value, &make_type_info<T1>::value, \
319  &make_type_info<T2>::value} }; \
320 } }
321 
322 /**************************************************************************************************/
323 
330 #define ADOBE_NAME_TYPE_4(name, ...) \
331 namespace adobe { namespace version_1 { \
332 template <typename Any, typename T0, typename T1, typename T2, typename T3> \
333 struct make_type_info<__VA_ARGS__, Any> { static const implementation::type_instance_t value; }; \
334 template <typename Any, typename T0, typename T1, typename T2, typename T3> \
335 const implementation::type_instance_t make_type_info<__VA_ARGS__, Any>::value \
336  = { 0, name, { &make_type_info<T0>::value, &make_type_info<T1>::value, \
337  &make_type_info<T2>::value, &make_type_info<T3>::value} }; \
338 } }
339 
340 /**************************************************************************************************/
341 
348 #define ADOBE_NAME_TYPE_5(name, ...) \
349 namespace adobe { namespace version_1 { \
350 template <typename Any, typename T0, typename T1, typename T2, typename T3, typename T4> \
351 struct make_type_info<__VA_ARGS__, Any> { static const implementation::type_instance_t value; }; \
352 template <typename Any, typename T0, typename T1, typename T2, typename T3, typename T4> \
353 const implementation::type_instance_t make_type_info<__VA_ARGS__, Any>::value \
354  = { 0, name, { &make_type_info<T0>::value, &make_type_info<T1>::value, \
355  &make_type_info<T2>::value, &make_type_info<T3>::value, \
356  &make_type_info<T4>::value} }; \
357 } }
358 
359 /**************************************************************************************************/
360 
361 } // namespace version_1
362 
363 using version_1::make_type_info;
364 using version_1::aggregate_type_info_t;
366 using version_1::type_info_t;
367 
368 /**************************************************************************************************/
369 
379 class bad_cast : public std::bad_cast
380 {
381  public:
382  bad_cast();
383  bad_cast(const std::type_info& from, const std::type_info& to);
388  bad_cast(const type_info_t& from, const type_info_t& to);
389  bad_cast(const bad_cast&);
390  bad_cast& operator=(const bad_cast&);
391  virtual ~bad_cast() throw();
398  virtual const char* what() const throw();
399 
400  private:
401  std::string what_m;
402 };
403 
404 /**************************************************************************************************/
405 
406 // Little endian - intended to read well in the debugger.
407 #define ADOBE_CHAR_INT(a, b, c, d) (int(a) | (int(b) << 8) | (int(c) << 16) | (int(d) << 24))
408 
409 template <typename T>
410 struct short_name { enum { value = ADOBE_CHAR_INT('u','n','k','n') }; };
411 
412 #define ADOBE_SHORT_NAME_TYPE(a, b, c, d, T) \
413 namespace adobe { \
414 template < > struct short_name<T> { enum { value = ADOBE_CHAR_INT(a, b, c, d) }; }; \
415 }
416 
417 /**************************************************************************************************/
418 
419 } // namespace adobe
420 
421 /**************************************************************************************************/
422 
423 namespace std {
424 
430 template <>
431 struct less<adobe::version_1::type_info_t> :
432  std::binary_function<adobe::version_1::type_info_t, adobe::version_1::type_info_t, bool>
433 {
435  const adobe::version_1::type_info_t& y) const
436  { return x.before(y); }
437 };
438 
439 } // namespace std
440 
441 /**************************************************************************************************/
442 
443 ADOBE_NAME_TYPE_0("double", double)
444 ADOBE_NAME_TYPE_0("float", float)
445 ADOBE_NAME_TYPE_0("int", int)
446 ADOBE_NAME_TYPE_0("short", short)
447 ADOBE_NAME_TYPE_0("long", long)
448 ADOBE_NAME_TYPE_0("unsigned_int", unsigned int)
449 ADOBE_NAME_TYPE_0("unsigned_short", unsigned short)
450 ADOBE_NAME_TYPE_0("unsigned_long", unsigned long)
451 ADOBE_NAME_TYPE_0("char", char)
452 ADOBE_NAME_TYPE_0("signed_char", signed char)
453 ADOBE_NAME_TYPE_0("unsigned_char", unsigned char)
454 ADOBE_NAME_TYPE_0("bool", bool)
455 
456 ADOBE_NAME_TYPE_1("pointer", T0*)
457 ADOBE_NAME_TYPE_1("const", const T0)
458 ADOBE_NAME_TYPE_1("reference", T0&)
459 
460 ADOBE_SHORT_NAME_TYPE('d','b','l','e', double)
461 ADOBE_SHORT_NAME_TYPE('f','l','o','t', float)
462 ADOBE_SHORT_NAME_TYPE('i','n','t','_', int)
463 ADOBE_SHORT_NAME_TYPE('s','h','r','t', short)
464 ADOBE_SHORT_NAME_TYPE('l','o','n','g', long)
465 ADOBE_SHORT_NAME_TYPE('u','i','n','t', unsigned int)
466 ADOBE_SHORT_NAME_TYPE('u','s','h','r', unsigned short)
467 ADOBE_SHORT_NAME_TYPE('u','l','n','g', unsigned long)
468 ADOBE_SHORT_NAME_TYPE('c','h','a','r', char)
469 ADOBE_SHORT_NAME_TYPE('s','c','h','r', signed char)
470 ADOBE_SHORT_NAME_TYPE('u','c','h','r', unsigned char)
471 ADOBE_SHORT_NAME_TYPE('b','o','o','l', bool)
472 
473 /**************************************************************************************************/
474 
475 #endif
476 
477 /**************************************************************************************************/
const char * name() const
Returns a null-terminated byte-string corresponding to the name of the type.
Definition: typeinfo.hpp:182
std::pair< I, O > copy_sentinal(I f, O o, const T &x)
copy implementation
Definition: copy.hpp:194
#define ADOBE_CHAR_INT(a, b, c, d)
Definition: typeinfo.hpp:407
STL namespace.
friend bool operator==(const type_info_t &x, const type_info_t &y)
Compares the current object with x. Returns true if the two values describe the same type...
Definition: typeinfo.hpp:199
bool operator==(const circular_queue< T > &x, const circular_queue< T > &y)
bool operator!=(const forest< T > &x, const forest< T > &y)
Definition: forest.hpp:719
friend O serialize(const type_info_t &x, O out)
Definition: typeinfo.hpp:212
const implementation::type_instance_t & private_m
Definition: typeinfo.hpp:249
An exception class thrown during ASL failures to cast.
Definition: typeinfo.hpp:379
Partial re-implementation of standard type_info .
Definition: typeinfo.hpp:175
static const implementation::type_instance_t value
Definition: typeinfo.hpp:146
bool operator()(const adobe::version_1::type_info_t &x, const adobe::version_1::type_info_t &y) const
Definition: typeinfo.hpp:434
#define ADOBE_SHORT_NAME_TYPE(a, b, c, d, T)
Definition: typeinfo.hpp:412
bool before(const type_info_t &x) const
Compares the current object with x. Returns true if *this precedes x in the implementationís collati...
Definition: typeinfo.hpp:189
static const implementation::type_instance_t value
Definition: typeinfo.hpp:118
static const implementation::type_instance_t value
Definition: typeinfo.hpp:106
std::ostream & operator<<(std::ostream &s, const extents_t &x)

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google