]> git.cworth.org Git - vogl/blob - src/extlib/loki/include/loki/LokiTypeInfo.h
Initial vogl checkin
[vogl] / src / extlib / loki / include / loki / LokiTypeInfo.h
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 #ifndef LOKI_LOKITYPEINFO_INC_
16 #define LOKI_LOKITYPEINFO_INC_
17
18 // $Id: LokiTypeInfo.h 748 2006-10-17 19:49:08Z syntheticpp $
19
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 {
61         assert(pInfo_);
62 }
63
64 inline bool TypeInfo::before(const TypeInfo &rhs) const
65 {
66         assert(pInfo_);
67         // type_info::before return type is int in some VC libraries
68         return pInfo_->before(*rhs.pInfo_) != 0;
69 }
70
71 inline const std::type_info &TypeInfo::Get() const
72 {
73         assert(pInfo_);
74         return *pInfo_;
75 }
76
77 inline const char *TypeInfo::name() const
78 {
79         assert(pInfo_);
80         return pInfo_->name();
81 }
82
83 // Comparison operators
84
85 inline bool operator==(const TypeInfo &lhs, const TypeInfo &rhs)
86 // type_info::operator== return type is int in some VC libraries
87 {
88         return (lhs.Get() == rhs.Get()) != 0;
89 }
90
91 inline bool operator<(const TypeInfo &lhs, const TypeInfo &rhs)
92 {
93         return lhs.before(rhs);
94 }
95
96 inline bool operator!=(const TypeInfo &lhs, const TypeInfo &rhs)
97 {
98         return !(lhs == rhs);
99 }
100
101 inline bool operator>(const TypeInfo &lhs, const TypeInfo &rhs)
102 {
103         return rhs < lhs;
104 }
105
106 inline bool operator<=(const TypeInfo &lhs, const TypeInfo &rhs)
107 {
108         return !(lhs > rhs);
109 }
110
111 inline bool operator>=(const TypeInfo &lhs, const TypeInfo &rhs)
112 {
113         return !(lhs < rhs);
114 }
115 }
116
117 #endif // end file guardian