Added LLVM notice.
[oota-llvm.git] / include / Support / TypeInfo.h
1 //===- Support/TypeInfo.h - Support class for type_info objects -*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This class makes std::type_info objects behave like first class objects that
11 // can be put in maps and hashtables.  This code is based off of code in the
12 // Loki C++ library from the Modern C++ Design book.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef SUPPORT_TYPEINFO_H
17 #define SUPPORT_TYPEINFO_H
18
19 #include <typeinfo>
20
21 struct TypeInfo {
22   TypeInfo() {                     // needed for containers
23     struct Nil {};  // Anonymous class distinct from all others...
24     Info = &typeid(Nil);
25   }
26
27   TypeInfo(const std::type_info &ti) : Info(&ti) { // non-explicit
28   }
29
30   // Access for the wrapped std::type_info
31   const std::type_info &get() const {
32     return *Info;
33   }
34
35   // Compatibility functions
36   bool before(const TypeInfo &rhs) const {
37     return Info->before(*rhs.Info);
38   }
39   const char *getClassName() const {
40     return Info->name();
41   }
42
43 private:
44   const std::type_info *Info;
45 };
46     
47 // Comparison operators
48 inline bool operator==(const TypeInfo &lhs, const TypeInfo &rhs) {
49   return lhs.get() == rhs.get();
50 }
51
52 inline bool operator<(const TypeInfo &lhs, const TypeInfo &rhs) {
53   return lhs.before(rhs);
54 }
55
56 inline bool operator!=(const TypeInfo &lhs, const TypeInfo &rhs) {
57   return !(lhs == rhs);
58 }
59
60 inline bool operator>(const TypeInfo &lhs, const TypeInfo &rhs) {
61   return rhs < lhs;
62 }
63     
64 inline bool operator<=(const TypeInfo &lhs, const TypeInfo &rhs) {
65   return !(lhs > rhs);
66 }
67      
68 inline bool operator>=(const TypeInfo &lhs, const TypeInfo &rhs) {
69   return !(lhs < rhs);
70 }
71
72 #endif