1 | //////////////////////////////////////////////////////////////////////////////// |
---|
2 | // The Loki Library |
---|
3 | // Copyright (c) 2001 by Andrei Alexandrescu |
---|
4 | // This code accompanies the book: |
---|
5 | // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design |
---|
6 | // Patterns Applied". Copyright (c) 2001. Addison-Wesley. |
---|
7 | // Permission to use, copy, modify, distribute and sell this software for any |
---|
8 | // purpose is hereby granted without fee, provided that the above copyright |
---|
9 | // notice appear in all copies and that both that copyright notice and this |
---|
10 | // permission notice appear in supporting documentation. |
---|
11 | // The author or Addison-Wesley Longman make no representations about the |
---|
12 | // suitability of this software for any purpose. It is provided "as is" |
---|
13 | // without express or implied warranty. |
---|
14 | //////////////////////////////////////////////////////////////////////////////// |
---|
15 | |
---|
16 | // Last update: June 20, 2001 |
---|
17 | |
---|
18 | #ifndef __TYPE_INFO_H__ |
---|
19 | #define __TYPE_INFO_H__ |
---|
20 | |
---|
21 | #include <typeinfo> |
---|
22 | #include <cassert> |
---|
23 | //////////////////////////////////////////////////////////////////////////////// |
---|
24 | // class TypeInfo |
---|
25 | // Purpose: offer a first-class, comparable wrapper over std::type_info |
---|
26 | //////////////////////////////////////////////////////////////////////////////// |
---|
27 | |
---|
28 | class TypeInfo |
---|
29 | { |
---|
30 | public: |
---|
31 | // Constructors |
---|
32 | TypeInfo(); // needed for containers |
---|
33 | TypeInfo(const std::type_info&); // non-explicit |
---|
34 | |
---|
35 | bool operator==(const TypeInfo& typeInfo) const; |
---|
36 | bool operator!=(const TypeInfo& typeInfo) const; |
---|
37 | bool operator<(const TypeInfo& typeInfo) const; |
---|
38 | bool operator>(const TypeInfo& typeInfo) const; |
---|
39 | bool operator<=(const TypeInfo& typeInfo) const; |
---|
40 | bool operator>=(const TypeInfo& typeInfo) const; |
---|
41 | |
---|
42 | // Access for the wrapped std::type_info |
---|
43 | const std::type_info& get() const; |
---|
44 | // Compatibility functions |
---|
45 | bool before(const TypeInfo& rhs) const; |
---|
46 | const char* name() const; |
---|
47 | |
---|
48 | private: |
---|
49 | const std::type_info* pInfo_; |
---|
50 | }; |
---|
51 | |
---|
52 | // Implementation |
---|
53 | |
---|
54 | inline TypeInfo::TypeInfo() |
---|
55 | { |
---|
56 | class Nil {}; |
---|
57 | pInfo_ = &typeid(Nil); |
---|
58 | assert(pInfo_); |
---|
59 | } |
---|
60 | |
---|
61 | inline TypeInfo::TypeInfo(const std::type_info& ti) |
---|
62 | : pInfo_(&ti) |
---|
63 | { |
---|
64 | assert(pInfo_); |
---|
65 | } |
---|
66 | |
---|
67 | inline bool TypeInfo::before(const TypeInfo& rhs) const |
---|
68 | { |
---|
69 | assert(pInfo_); |
---|
70 | // type_info::before return type is int in some VC libraries |
---|
71 | return pInfo_->before(*rhs.pInfo_) != 0; |
---|
72 | } |
---|
73 | |
---|
74 | inline const std::type_info& TypeInfo::get() const |
---|
75 | { |
---|
76 | assert(pInfo_); |
---|
77 | return *pInfo_; |
---|
78 | } |
---|
79 | |
---|
80 | inline const char* TypeInfo::name() const |
---|
81 | { |
---|
82 | assert(pInfo_); |
---|
83 | return pInfo_->name(); |
---|
84 | } |
---|
85 | |
---|
86 | // Comparison operators |
---|
87 | |
---|
88 | inline bool TypeInfo::operator==(const TypeInfo& rhs) const |
---|
89 | // type_info::operator== return type is int in some VC libraries |
---|
90 | { return (this->get() == rhs.get()) != 0; } |
---|
91 | |
---|
92 | inline bool TypeInfo::operator<(const TypeInfo& rhs) const |
---|
93 | { return this->before(rhs); } |
---|
94 | |
---|
95 | inline bool TypeInfo::operator!=(const TypeInfo& rhs) const |
---|
96 | { return !(*this == rhs); } |
---|
97 | |
---|
98 | inline bool TypeInfo::operator>(const TypeInfo& rhs) const |
---|
99 | { return rhs < *this; } |
---|
100 | |
---|
101 | inline bool TypeInfo::operator<=(const TypeInfo& rhs) const |
---|
102 | { return !(*this > rhs); } |
---|
103 | |
---|
104 | inline bool TypeInfo::operator>=(const TypeInfo& rhs) const |
---|
105 | { return !(*this < rhs); } |
---|
106 | |
---|
107 | //////////////////////////////////////////////////////////////////////////////// |
---|
108 | // Change log: |
---|
109 | // June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!! |
---|
110 | //////////////////////////////////////////////////////////////////////////////// |
---|
111 | |
---|
112 | #endif /* __TYPE_INFO_H__ */ |
---|