Caelum
PrivatePtr.h
1 /*
2 This file is part of Caelum.
3 See http://www.ogre3d.org/wiki/index.php/Caelum
4 
5 Copyright (c) 2008 Caelum team. See Contributors.txt for details.
6 
7 Caelum is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Caelum is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with Caelum. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef CAELUM__PRIVATE_PTR_H
22 #define CAELUM__PRIVATE_PTR_H
23 
24 #include "CaelumPrerequisites.h"
25 
26 namespace Caelum
27 {
36  template<class PointedT>
38  {
40  typedef PointedT* InnerPointerType;
41 
43  static inline const InnerPointerType getNullValue() {
44  return 0;
45  }
46 
48  static inline PointedT* getPointer (const InnerPointerType& inner) {
49  return inner;
50  }
51 
53  static void destroy (InnerPointerType& inner)
54  {
55  delete inner;
56  inner = 0;
57  }
58  };
59 
80  template<class PointedT, typename TraitsT = DefaultPrivatePtrTraits<PointedT> >
81  class PrivatePtr
82  {
83  private:
85  typedef typename TraitsT::InnerPointerType InnerPointerType;
86 
88  InnerPointerType mInner;
89 
90  public:
94  void reset (const InnerPointerType& newInner = TraitsT::getNullValue()) {
95  if (mInner == newInner) {
96  return;
97  }
98  TraitsT::destroy (mInner);
99  mInner = newInner;
100  }
101 
102  InnerPointerType release () {
103  InnerPointerType result = mInner;
104  mInner = TraitsT::getNullValue();
105  return result;
106  }
107 
110  PrivatePtr () { mInner = TraitsT::getNullValue (); }
111 
114  PrivatePtr (const InnerPointerType& inner) { mInner = inner; }
115 
118  ~PrivatePtr () { setNull(); }
119 
123  {
124  if (&rhs != this) {
125  this->reset (rhs.mInner);
126  rhs.mInner = TraitsT::getNullValue ();
127  }
128  }
129 
132  const PrivatePtr& operator= (PrivatePtr& rhs)
133  {
134  if (&rhs != this) {
135  this->reset (rhs.mInner);
136  rhs.mInner = TraitsT::getNullValue ();
137  }
138  return *this;
139  }
140 
142  bool isNull () const { return mInner == TraitsT::getNullValue (); }
143 
145  void setNull () {
146  TraitsT::destroy (mInner);
147  assert(this->isNull());
148  }
149 
150  PointedT* getPointer () const { return TraitsT::getPointer (mInner); }
151  PointedT* get () const { return getPointer (); }
152  PointedT* operator-> () const { return getPointer (); }
153  PointedT& operator* () const{ return *getPointer (); }
154  };
155 
159  template<class MovableT>
161  {
162  typedef MovableT* InnerPointerType;
163 
164  static void destroy (InnerPointerType& inner)
165  {
166  if (inner != 0) {
167  //Ogre::LogManager::getSingletonPtr ()->logMessage (
168  // "PrivatePtr: Destroying movable object " + inner->getName ());
169  inner->_getManager ()->destroyMovableObject (inner);
170  inner = 0;
171  }
172  }
173  };
174 
182 
187  struct SceneNodePrivatePtrTraits: public DefaultPrivatePtrTraits<Ogre::SceneNode>
188  {
189  static void destroy (InnerPointerType& inner)
190  {
191  if (inner) {
192  //Ogre::LogManager::getSingletonPtr ()->logMessage (
193  // "PrivatePtr: Destroying scene node " + inner->getName ());
194  inner->getCreator ()->destroySceneNode (inner->getName ());
195  inner = 0;
196  }
197  }
198  };
199 
201 
219  template<class PointedT, class InnerT, class ManagerT>
221  {
222  typedef InnerT InnerPointerType;
223 
224  static const InnerT getNullValue () {
225  return InnerT();
226  }
227 
228  static PointedT* getPointer (const InnerPointerType& inner) {
229  return inner.getPointer ();
230  }
231 
232  static void destroy (InnerPointerType& inner) {
233  if (!inner.isNull ()) {
234  //Ogre::LogManager::getSingletonPtr ()->logMessage (
235  // "PrivateResourcePtrTraits: Destroying owned resource"
236  // " name=" + inner->getName () +
237  // " handle=" + Ogre::StringConverter::toString (inner->getHandle ()) );
238  ManagerT::getSingletonPtr ()->remove (inner->getHandle ());
239  assert (inner.unique () && "Resource pointer not unique after destruction");
240  inner.setNull();
241  }
242  }
243  };
244 
245  typedef PrivatePtr <
246  Ogre::Material,
248  Ogre::Material,
249  Ogre::MaterialPtr,
250  Ogre::MaterialManager
251  >
253 
254  typedef PrivatePtr <
255  Ogre::Mesh,
257  Ogre::Mesh,
258  Ogre::MeshPtr,
259  Ogre::MeshManager
260  >
261  > PrivateMeshPtr;
262 }
263 
264 #endif // CAELUM__PRIVATE_PTR_H
PrivatePtr traits for uniquely-owned resources.
Definition: PrivatePtr.h:220
~PrivatePtr()
Non-virtual destructor (don&#39;t derive from this).
Definition: PrivatePtr.h:118
static void destroy(InnerPointerType &inner)
Destroy the inner value (and set null).
Definition: PrivatePtr.h:53
PrivatePtr traits for a movable object.
Definition: PrivatePtr.h:160
PrivatePtr traits for a scene node.
Definition: PrivatePtr.h:187
static const InnerPointerType getNullValue()
Return an InnerPointerType repressenting a null value.
Definition: PrivatePtr.h:43
PrivatePtr(PrivatePtr &rhs)
Copy constructor; clears right-hand-side.
Definition: PrivatePtr.h:122
PrivatePtr(const InnerPointerType &inner)
Initializing constructur.
Definition: PrivatePtr.h:114
PrivatePtr()
Constructor; always initialize to 0.
Definition: PrivatePtr.h:110
PointedT * InnerPointerType
The type of the inner member to hold in PrivatePtr.
Definition: PrivatePtr.h:40
bool isNull() const
Check if this is null.
Definition: PrivatePtr.h:142
Template for smart pointers with strict unique ownership.
Definition: PrivatePtr.h:81
void reset(const InnerPointerType &newInner=TraitsT::getNullValue())
Change the inner value.
Definition: PrivatePtr.h:94
void setNull()
Set to null and destroy contents (if any).
Definition: PrivatePtr.h:145
static PointedT * getPointer(const InnerPointerType &inner)
Convert InnerPointerType to a naked PointedT.
Definition: PrivatePtr.h:48
Default traits for Caelum::PrivatePtr.
Definition: PrivatePtr.h:37
Caelum namespace.
Definition: Astronomy.cpp:24