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-Welsey 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 TYPEINFO_INC_
19 #define TYPEINFO_INC_
20
21 #include <typeinfo>
22 #include <cassert>
23 #include "Typelist.h"
24
25 namespace Loki
26 {
27 ////////////////////////////////////////////////////////////////////////////////
28 // class TypeInfo
29 // Purpose: offer a first-class, comparable wrapper over std::type_info
30 ////////////////////////////////////////////////////////////////////////////////
31
32 class TypeInfo
33 {
34 public:
35 // Constructors
36 TypeInfo(); // needed for containers
37 TypeInfo(const std::type_info&); // non-explicit
38
39 // Access for the wrapped std::type_info
40 const std::type_info& Get() const;
41 // Compatibility functions
42 bool before(const TypeInfo& rhs) const;
43 const char* name() const;
44
45 private:
46 const std::type_info* pInfo_;
47 };
48
49 // Implementation
50
51 inline TypeInfo::TypeInfo()
52 {
53 class Nil {};
54 pInfo_ = &typeid(Nil);
55 assert(pInfo_);
56 }
57
58 inline TypeInfo::TypeInfo(const std::type_info& ti)
59 : pInfo_(&ti)
60 { assert(pInfo_); }
61
62 inline bool TypeInfo::before(const TypeInfo& rhs) const
63 {
64 assert(pInfo_);
65 return pInfo_->before(*rhs.pInfo_);
66 }
67
68 inline const std::type_info& TypeInfo::Get() const
69 {
70 assert(pInfo_);
71 return *pInfo_;
72 }
73
74 inline const char* TypeInfo::name() const
75 {
76 assert(pInfo_);
77 return pInfo_->name();
78 }
79
80 // Comparison operators
81
82 inline bool operator==(const TypeInfo& lhs, const TypeInfo& rhs)
83 { return lhs.Get() == rhs.Get(); }
84
85 inline bool operator<(const TypeInfo& lhs, const TypeInfo& rhs)
86 { return lhs.before(rhs); }
87
88 inline bool operator!=(const TypeInfo& lhs, const TypeInfo& rhs)
89 { return !(lhs == rhs); }
90
91 inline bool operator>(const TypeInfo& lhs, const TypeInfo& rhs)
92 { return rhs < lhs; }
93
94 inline bool operator<=(const TypeInfo& lhs, const TypeInfo& rhs)
95 { return !(lhs > rhs); }
96
97 inline bool operator>=(const TypeInfo& lhs, const TypeInfo& rhs)
98 { return !(lhs < rhs); }
99 }
100
101 ////////////////////////////////////////////////////////////////////////////////
102 // Change log:
103 // June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
104 ////////////////////////////////////////////////////////////////////////////////
105
106 #endif // TYPEINFO_INC_