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 _TYPE_INFO_H_ |
16 |
#define _TYPE_INFO_H_ |
17 |
#include <typeinfo> |
18 |
using namespace std; |
19 |
|
20 |
class TypeInfo |
21 |
{ |
22 |
public: |
23 |
// Constructors |
24 |
TypeInfo(); // needed for containers |
25 |
TypeInfo(const std::type_info&); // non-explicit |
26 |
|
27 |
// Access for the wrapped std::type_info |
28 |
const std::type_info& Get() const; |
29 |
// Compatibility functions |
30 |
bool before(const TypeInfo& rhs) const; |
31 |
const char* name() const; |
32 |
|
33 |
private: |
34 |
const std::type_info* pInfo_; |
35 |
}; |
36 |
|
37 |
// Implementation |
38 |
|
39 |
inline TypeInfo::TypeInfo(){ |
40 |
class Nil {}; |
41 |
pInfo_ = &typeid(Nil); |
42 |
} |
43 |
|
44 |
inline TypeInfo::TypeInfo(const std::type_info& ti): pInfo_(&ti) |
45 |
{ } |
46 |
|
47 |
inline bool TypeInfo::before(const TypeInfo& rhs) const{ |
48 |
return pInfo_->before(*rhs.pInfo_); |
49 |
} |
50 |
|
51 |
inline const std::type_info& TypeInfo::Get() const{ |
52 |
return *pInfo_; |
53 |
} |
54 |
|
55 |
inline const char* TypeInfo::name() const{ |
56 |
return pInfo_->name(); |
57 |
} |
58 |
|
59 |
// Comparison operators |
60 |
|
61 |
inline bool operator==(const TypeInfo& lhs, const TypeInfo& rhs){ |
62 |
return lhs.Get() == rhs.Get(); |
63 |
} |
64 |
|
65 |
inline bool operator<(const TypeInfo& lhs, const TypeInfo& rhs){ |
66 |
return lhs.before(rhs); |
67 |
} |
68 |
|
69 |
inline bool operator!=(const TypeInfo& lhs, const TypeInfo& rhs){ |
70 |
return !(lhs == rhs); |
71 |
} |
72 |
|
73 |
inline bool operator>(const TypeInfo& lhs, const TypeInfo& rhs){ |
74 |
return rhs < lhs; |
75 |
} |
76 |
|
77 |
inline bool operator<=(const TypeInfo& lhs, const TypeInfo& rhs){ |
78 |
return !(lhs > rhs); |
79 |
} |
80 |
|
81 |
inline bool operator>=(const TypeInfo& lhs, const TypeInfo& rhs){ |
82 |
return !(lhs < rhs); |
83 |
} |
84 |
|
85 |
|
86 |
|
87 |
#endif //iindef _TYPE_INFO_H_ |