00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef CAELUM__PRIVATE_PTR_H
00022 #define CAELUM__PRIVATE_PTR_H
00023
00024 #include "CaelumPrerequisites.h"
00025
00026 namespace Caelum
00027 {
00036 template<class PointedT>
00037 struct DefaultPrivatePtrTraits
00038 {
00040 typedef PointedT* InnerPointerType;
00041
00043 static inline const InnerPointerType getNullValue() {
00044 return 0;
00045 }
00046
00048 static inline PointedT* getPointer (const InnerPointerType& inner) {
00049 return inner;
00050 }
00051
00053 static void destroy (InnerPointerType& inner)
00054 {
00055 delete inner;
00056 inner = 0;
00057 }
00058 };
00059
00080 template<class PointedT, typename TraitsT = DefaultPrivatePtrTraits<PointedT> >
00081 class PrivatePtr
00082 {
00083 private:
00085 typedef typename TraitsT::InnerPointerType InnerPointerType;
00086
00088 InnerPointerType mInner;
00089
00090 public:
00094 void reset (const InnerPointerType& newInner = TraitsT::getNullValue()) {
00095 if (mInner == newInner) {
00096 return;
00097 }
00098 TraitsT::destroy (mInner);
00099 mInner = newInner;
00100 }
00101
00102 InnerPointerType release () {
00103 InnerPointerType result = mInner;
00104 mInner = TraitsT::getNullValue();
00105 return result;
00106 }
00107
00110 PrivatePtr () { mInner = TraitsT::getNullValue (); }
00111
00114 PrivatePtr (const InnerPointerType& inner) { mInner = inner; }
00115
00118 ~PrivatePtr () { setNull(); }
00119
00122 PrivatePtr (PrivatePtr& rhs)
00123 {
00124 if (&rhs != this) {
00125 this->reset (rhs.mInner);
00126 rhs.mInner = TraitsT::getNullValue ();
00127 }
00128 }
00129
00132 const PrivatePtr& operator= (PrivatePtr& rhs)
00133 {
00134 if (&rhs != this) {
00135 this->reset (rhs.mInner);
00136 rhs.mInner = TraitsT::getNullValue ();
00137 }
00138 return *this;
00139 }
00140
00142 bool isNull () const { return mInner == TraitsT::getNullValue (); }
00143
00145 void setNull () {
00146 TraitsT::destroy (mInner);
00147 assert(this->isNull());
00148 }
00149
00150 PointedT* getPointer () const { return TraitsT::getPointer (mInner); }
00151 PointedT* get () const { return getPointer (); }
00152 PointedT* operator-> () const { return getPointer (); }
00153 PointedT& operator* () const{ return *getPointer (); }
00154 };
00155
00159 template<class MovableT>
00160 struct MovableObjectPrivatePtrTraits: public DefaultPrivatePtrTraits<MovableT>
00161 {
00162 typedef MovableT* InnerPointerType;
00163
00164 static void destroy (InnerPointerType& inner)
00165 {
00166 if (inner != 0) {
00167
00168
00169 inner->_getManager ()->destroyMovableObject (inner);
00170 inner = 0;
00171 }
00172 }
00173 };
00174
00175 typedef PrivatePtr<Ogre::MovableObject, MovableObjectPrivatePtrTraits<Ogre::MovableObject> > PrivateMovableObjectPtr;
00176 typedef PrivatePtr<Ogre::BillboardChain, MovableObjectPrivatePtrTraits<Ogre::BillboardChain> > PrivateBillboardChainPtr;
00177 typedef PrivatePtr<Ogre::BillboardSet, MovableObjectPrivatePtrTraits<Ogre::BillboardSet> > PrivateBillboardSetPtr;
00178 typedef PrivatePtr<Ogre::Entity, MovableObjectPrivatePtrTraits<Ogre::Entity> > PrivateEntityPtr;
00179 typedef PrivatePtr<Ogre::Light, MovableObjectPrivatePtrTraits<Ogre::Light> > PrivateLightPtr;
00180 typedef PrivatePtr<Ogre::ManualObject, MovableObjectPrivatePtrTraits<Ogre::ManualObject> > PrivateManualObjectPtr;
00181 typedef PrivatePtr<Ogre::ParticleSystem, MovableObjectPrivatePtrTraits<Ogre::ParticleSystem> > PrivateParticleSystemPtr;
00182
00187 struct SceneNodePrivatePtrTraits: public DefaultPrivatePtrTraits<Ogre::SceneNode>
00188 {
00189 static void destroy (InnerPointerType& inner)
00190 {
00191 if (inner) {
00192
00193
00194 inner->getCreator ()->destroySceneNode (inner->getName ());
00195 inner = 0;
00196 }
00197 }
00198 };
00199
00200 typedef PrivatePtr<Ogre::SceneNode, SceneNodePrivatePtrTraits> PrivateSceneNodePtr;
00201
00219 template<class PointedT, class InnerT, class ManagerT>
00220 struct PrivateResourcePtrTraits
00221 {
00222 typedef InnerT InnerPointerType;
00223
00224 static const InnerT getNullValue () {
00225 return InnerT();
00226 }
00227
00228 static PointedT* getPointer (const InnerPointerType& inner) {
00229 return inner.getPointer ();
00230 }
00231
00232 static void destroy (InnerPointerType& inner) {
00233 if (!inner.isNull ()) {
00234
00235
00236
00237
00238 ManagerT::getSingletonPtr ()->remove (inner->getHandle ());
00239 assert (inner.unique () && "Resource pointer not unique after destruction");
00240 inner.setNull();
00241 }
00242 }
00243 };
00244
00245 typedef PrivatePtr <
00246 Ogre::Material,
00247 PrivateResourcePtrTraits <
00248 Ogre::Material,
00249 Ogre::MaterialPtr,
00250 Ogre::MaterialManager
00251 >
00252 > PrivateMaterialPtr;
00253
00254 typedef PrivatePtr <
00255 Ogre::Mesh,
00256 PrivateResourcePtrTraits <
00257 Ogre::Mesh,
00258 Ogre::MeshPtr,
00259 Ogre::MeshManager
00260 >
00261 > PrivateMeshPtr;
00262 }
00263
00264 #endif // CAELUM__PRIVATE_PTR_H