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
00024
00025
00026
00027
00034 #ifndef KEYVALUE_VALUE_OBJECTPTR_H_
00035 #define KEYVALUE_VALUE_OBJECTPTR_H_
00036
00037 #include <boost/type_traits/is_base_of.hpp>
00038 #include <boost/utility/enable_if.hpp>
00039
00040 #include "keyvalue/extern/SharedPtr.h"
00041 #include "keyvalue/value/Object.h"
00042 #include "keyvalue/value/PtrTraits.h"
00043
00044 namespace keyvalue {
00045 namespace value {
00046
00062 class ObjectPtr {
00063
00064 public:
00065
00072 ObjectPtr();
00073
00080 template <typename ObjectType>
00081 ObjectPtr(const shared_ptr<ObjectType>& ptr, typename
00082 ::boost::enable_if< ::boost::is_base_of<Object, ObjectType> >::type*
00083 dummy = 0);
00084
00095 template <typename ObjectType, template <typename> class SmartPtr>
00096 ObjectPtr(const SmartPtr<ObjectType>& ptr, typename
00097 ::boost::disable_if< ::boost::is_base_of<Object, ObjectType> >::type*
00098 dummy = 0);
00099
00107 template <typename Rhs>
00108 ObjectPtr(const Rhs& rhs);
00109
00119 template <typename Rhs>
00120 ObjectPtr&
00121 operator =(const Rhs& rhs);
00122
00128 operator bool() const;
00129
00140 template <typename ObjectType>
00141 shared_ptr<ObjectType>
00142 cast(typename
00143 ::boost::enable_if< ::boost::is_base_of<Object, ObjectType> >::type*
00144 dummy = 0) const;
00145
00156 template <typename ObjectType>
00157 typename PtrTraits<ObjectType>::Type_
00158 cast(typename
00159 ::boost::disable_if< ::boost::is_base_of<Object, ObjectType> >::type*
00160 dummy = 0) const;
00161
00162 private:
00163
00169 void
00170 swap(ObjectPtr& other);
00171
00172 shared_ptr<Object> kvPtr_;
00173 PtrTraits<void>::Type_ ptr_;
00174
00175 };
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 template <typename Rhs>
00190 struct Parent;
00191
00192 template <typename ObjectType, template <typename> class SmartPtr>
00193 struct Parent<SmartPtr<ObjectType> > {
00194 typedef ObjectPtr Type_;
00195 };
00196
00197 template <typename ObjectType,
00198 bool isObject = ::boost::is_base_of<Object, ObjectType>::value>
00199 struct PointerParent;
00200
00201 template <typename ObjectType>
00202 struct PointerParent<ObjectType, true> {
00203 typedef shared_ptr<Object> Type_;
00204 };
00205
00206 template <typename ObjectType>
00207 struct PointerParent<ObjectType, false> {
00208 typedef typename PtrTraits<ObjectType>::Type_ Type_;
00209 };
00210
00211 template <typename ObjectType>
00212 struct Parent<ObjectType*> {
00213 typedef typename PointerParent<ObjectType>::Type_ Type_;
00214 };
00215
00216
00217
00218
00219
00220 template <typename ObjectType>
00221 ObjectPtr::ObjectPtr(const shared_ptr<ObjectType>& ptr, typename
00222 ::boost::enable_if< ::boost::is_base_of<Object, ObjectType> >::type*) :
00223 kvPtr_(ptr) {
00224 }
00225
00226 template <typename ObjectType, template <typename> class SmartPtr>
00227 ObjectPtr::ObjectPtr(const SmartPtr<ObjectType>& ptr, typename
00228 ::boost::disable_if< ::boost::is_base_of<Object, ObjectType> >::type*) :
00229 ptr_(ptr) {
00230 }
00231
00232 template <typename Rhs>
00233 ObjectPtr::ObjectPtr(const Rhs& rhs) {
00234
00235
00236
00237
00238
00239
00240 typename Parent<Rhs>::Type_ tmp(rhs);
00241 ObjectPtr other(tmp);
00242 swap(other);
00243 }
00244
00245
00246
00247
00248
00249 template <typename Rhs>
00250 ObjectPtr&
00251 ObjectPtr::operator =(const Rhs& rhs) {
00252
00253
00254
00255
00256
00257
00258 return *this = typename Parent<Rhs>::Type_(rhs);
00259 }
00260
00261
00262
00263
00264
00265 template <typename ObjectType>
00266 shared_ptr<ObjectType>
00267 ObjectPtr::cast(typename
00268 ::boost::enable_if< ::boost::is_base_of<Object, ObjectType> >::type*)
00269 const {
00270 return dynamic_pointer_cast<ObjectType>(kvPtr_);
00271 }
00272
00273 template <typename ObjectType>
00274 typename PtrTraits<ObjectType>::Type_
00275 ObjectPtr::cast(typename
00276 ::boost::disable_if< ::boost::is_base_of<Object, ObjectType> >::type*)
00277 const {
00278 return dynamic_pointer_cast<ObjectType>(ptr_);
00279 }
00280
00281 }
00282 }
00283
00284 #endif // KEYVALUE_VALUE_OBJECTPTR_H_