AbstractValue.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef UTILS__ABSTRACT_VALUE_H
00024 #define UTILS__ABSTRACT_VALUE_H
00025
00026 #include "Imports.h"
00027
00028 namespace Utils {
00029
00031
00123 template <typename AbstractType,
00124 typename NullObjectType,
00125 typename HierarchyRoot = AbstractType>
00126 class AbstractValue
00127 {
00128 public:
00130
00133 AbstractValue()
00134 : object_ (new NullObjectType())
00135 {}
00136
00138
00142 AbstractValue (const AbstractType &object)
00143 : object_ (object.copy())
00144 {}
00145
00147
00151 AbstractValue (const AbstractValue &other)
00152 : object_ (other.object_->copy())
00153 {}
00154
00156
00163 AbstractValue & operator= (const AbstractValue &other)
00164 {
00165 object_ = other.object_->copy();
00166 return *this;
00167 }
00168
00170
00178 bool operator== (const AbstractValue &other) const
00179 {
00180 return typeid (*object_) == typeid (*other.object_)
00181 && object_->isEqualTo (*other.object_);
00182 }
00183
00185
00193 bool operator!= (const AbstractValue &other) const
00194 {
00195 return !(*this == other);
00196 }
00197
00199 AbstractType & operator*()
00200 {
00201
00202 return static_cast <AbstractType &> (*object_);
00203 }
00204
00206 const AbstractType & operator*() const
00207 {
00208
00209 return static_cast <const AbstractType &> (*object_);
00210 }
00211
00213 AbstractType * operator->()
00214 {
00215
00216 return static_cast <AbstractType *> (object_.get());
00217 }
00218
00220 const AbstractType * operator->() const
00221 {
00222
00223 return static_cast <const AbstractType *> (object_.get());
00224 }
00225
00227
00242 template <typename RelatedAbstractType, typename RelatedNullType>
00243 operator AbstractValue <RelatedAbstractType,
00244 RelatedNullType,
00245 HierarchyRoot>() const
00246 {
00247
00248
00249 return AbstractValue <RelatedAbstractType,
00250 RelatedNullType,
00251 HierarchyRoot>
00252 (dynamic_cast <const RelatedAbstractType &> (*object_.get()));
00253 }
00254
00255 private:
00257 auto_ptr <HierarchyRoot> object_;
00258 };
00259
00260 }
00261
00262 #endif // UTILS__ABSTRACT_VALUE_H