Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/lang/type_info.h

Last change on this file was 9659, checked in by bensch, 18 years ago

some thoughts

File size: 3.4 KB
Line 
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
28class TypeInfo
29{
30public:
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
48private:
49  const std::type_info* pInfo_;
50};
51
52// Implementation
53
54inline TypeInfo::TypeInfo()
55{
56  class Nil {};
57  pInfo_ = &typeid(Nil);
58  assert(pInfo_);
59}
60
61inline TypeInfo::TypeInfo(const std::type_info& ti)
62    : pInfo_(&ti)
63{
64  assert(pInfo_);
65}
66
67inline 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
74inline const std::type_info& TypeInfo::get() const
75  {
76    assert(pInfo_);
77    return *pInfo_;
78  }
79
80inline const char* TypeInfo::name() const
81{
82  assert(pInfo_);
83  return pInfo_->name();
84}
85
86// Comparison operators
87
88inline 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
92inline bool TypeInfo::operator<(const TypeInfo& rhs) const
93{ return this->before(rhs); }
94
95inline bool TypeInfo::operator!=(const TypeInfo& rhs) const
96{ return !(*this == rhs); }
97
98inline bool TypeInfo::operator>(const TypeInfo& rhs) const
99{ return rhs < *this; }
100
101inline bool TypeInfo::operator<=(const TypeInfo& rhs) const
102{ return !(*this > rhs); }
103
104inline 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__ */
Note: See TracBrowser for help on using the repository browser.