ionClass.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <ionConfig.h>
11 
12 #include <typeinfo>
13 
14 
23 template <typename T, typename U>
24 bool InstanceOf(U * Object)
25 {
26  return dynamic_cast<T const *>(Object) != 0;
27 }
28 
37 template <typename T, typename U>
38 bool InstanceOf(U & Object)
39 {
40  return dynamic_cast<T const *>(& Object) != 0;
41 }
42 
48 template <typename T, typename U>
49 T * As(U * Object)
50 {
51  return dynamic_cast<T *>(Object);
52 }
53 
59 template <typename T, typename U>
60 T const * As(U const * Object)
61 {
62  return dynamic_cast<T const *>(Object);
63 }
64 
70 template <typename T, typename U>
71 T & As(U & Object)
72 {
73  return * dynamic_cast<T *>(& Object);
74 }
75 
81 template <typename T, typename U>
82 T const & As(U const & Object)
83 {
84  return * dynamic_cast<T const *>(& Object);
85 }
86 
89 template <class Implementation>
90 class Singleton
91 {
92 
93  Singleton(Singleton const &) = delete;
94  Singleton & operator = (Singleton const &) = delete;
95 
96 public:
97 
98  static Implementation & Get()
99  {
100  static Implementation * Instance = 0;
101 
102  if (! Instance)
103  Instance = new Implementation();
104 
105  return * Instance;
106  }
107 
108  static Implementation * GetPointer()
109  {
110  return & Get();
111  }
112 
113 protected:
114 
116  {}
117 
118 };
119 
122 template <class T>
124 {
125 
126 public:
127 
128  T * operator ->()
129  {
130  return GetReference();
131  }
132 
133  T * Get()
134  {
135  return GetReference();
136  }
137 
138  operator T*()
139  {
140  return GetReference();
141  }
142 
143  T const * operator ->() const
144  {
145  return GetReference();
146  }
147 
148  T const * Get() const
149  {
150  return GetReference();
151  }
152 
153  operator T const *() const
154  {
155  return GetReference();
156  }
157 
158 private:
159 
160  mutable T * Reference = 0;
161 
162  T * GetReference()
163  {
164  if (! Reference)
165  Reference = T::GetPointer();
166  return Reference;
167  }
168 
169  T const * GetReference() const
170  {
171  if (! Reference)
172  Reference = T::GetPointer();
173  return Reference;
174  }
175 
176 };
177 
180 struct Type
181 {
182  Type(std::type_info const & value)
183  : Value(value)
184  {}
185 
186  bool operator <(Type const & other) const
187  {
188  return Value.before(other.Value);
189  }
190 
191  std::type_info const & Value;
192 };
T * Get()
Definition: ionClass.h:133
static Implementation & Get()
Definition: ionClass.h:98
static Implementation * GetPointer()
Definition: ionClass.h:108
Type(std::type_info const &value)
Definition: ionClass.h:182
Wraps a pointer to a singleton object.
Definition: ionClass.h:123
T const * Get() const
Definition: ionClass.h:148
std::type_info const & Value
Definition: ionClass.h:191
Singleton()
Definition: ionClass.h:115
T * As(U *Object)
Casts an object as a different type, performing a validity check.
Definition: ionClass.h:49
Lightweight wrapper for std::type_info that enables sorting (e.g. for use in a std::map) ...
Definition: ionClass.h:180
bool InstanceOf(U *Object)
Check whether an object is an instance of a type.
Definition: ionClass.h:24
Base class for Singleton design pattern implementation.
Definition: ionClass.h:90