Jpp  15.0.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTypeInfo.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JTYPEINFO__
2 #define __JLANG__JTYPEINFO__
3 
4 #include <typeinfo>
5 
6 #include "JLang/JComparable.hh"
7 #include "JLang/JException.hh"
8 
9 
10 /**
11  * \author mdejong
12  */
13 
14 namespace JLANG {}
15 namespace JPP { using namespace JLANG; }
16 
17 namespace JLANG {
18 
19 
20  /**
21  * This class is a wrapper around the STL type_info class.
22  * It is possible to create a list of objects of this class.
23  *
24  * Source code is taken from reference:
25  * A. Alexandrescu, Modern C++ Design, Addison Wesley.
26  */
27  class JTypeInfo :
28  public JComparable<JTypeInfo>
29  {
30  public:
31  /**
32  * Default constructor.
33  */
35  {
36  class JNullType {};
37 
38  pType = &typeid(JNullType);
39  }
40 
41 
42  /**
43  * Constructor.
44  *
45  * \param type type information
46  */
47  JTypeInfo(const std::type_info& type) :
48  pType(&type)
49  {}
50 
51 
52  /**
53  * Get type information.
54  *
55  * \return type information
56  */
57  const std::type_info& get() const
58  {
59  if (pType == NULL)
60  throw JTypeInformationException("No type information.");
61  else
62  return *pType;
63  }
64 
65 
66  /**
67  * Less than operation.
68  *
69  * \param that object to be compared
70  * \return true if this type information less than that type information; else false
71  */
72  bool less(const JTypeInfo& that) const
73  {
74  return get().before(that.get()) != 0;
75  }
76 
77 
78  /**
79  * Evaluate order.
80  *
81  * \param type type information
82  * \return true if this type before given type; else false
83  */
84  bool before(const JTypeInfo& type) const
85  {
86  return get().before(type.get()) != 0;
87  }
88 
89 
90  /**
91  * Get type name.
92  *
93  * \return type name
94  */
95  const char* name() const
96  {
97  return get().name();
98  }
99 
100 
101  private:
102  const std::type_info* pType;
103  };
104 }
105 
106 #endif
Exception for absence of type information.
Definition: JException.hh:612
Exceptions.
This class is a wrapper around the STL type_info class.
Definition: JTypeInfo.hh:27
const char * name() const
Get type name.
Definition: JTypeInfo.hh:95
JTypeInfo()
Default constructor.
Definition: JTypeInfo.hh:34
bool before(const JTypeInfo &type) const
Evaluate order.
Definition: JTypeInfo.hh:84
const std::type_info & get() const
Get type information.
Definition: JTypeInfo.hh:57
bool less(const JTypeInfo &that) const
Less than operation.
Definition: JTypeInfo.hh:72
Auxiliary class for no type definition.
Definition: JNullType.hh:19
JTypeInfo(const std::type_info &type)
Constructor.
Definition: JTypeInfo.hh:47
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:24
const std::type_info * pType
Definition: JTypeInfo.hh:102