TrueReality  v0.1.1912
SmrtPtr.h
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * @author Maxim Serebrennik
20 */
21 
22 #pragma once
23 
24 #include <trUtil/Hash.h>
25 
26 #include <osg/ref_ptr>
27 
28 #include <iostream>
29 
30 namespace trBase
31 {
35  template<class T>
36  class SmrtPtr
37  {
38  public:
39 
45  SmrtPtr() { mOSGSmartPtr = nullptr; }
46 
54  SmrtPtr(T* t) { mOSGSmartPtr = t;}
55 
56 
64  SmrtPtr(const SmrtPtr& smPt) { mOSGSmartPtr = smPt.Get(); }
65 
73  T* Get() const { return mOSGSmartPtr.get(); }
74 
85  T* Release() { return mOSGSmartPtr.release(); }
86 
94  void Swap(SmrtPtr& sp) { mOSGSmartPtr.swap(sp); }
95 
103  bool Valid() const { return mOSGSmartPtr.valid(); }
104 
112  bool operator ! () const { return mOSGSmartPtr.operator!(); }
113 
121  T& operator * () const { return mOSGSmartPtr.operator*(); }
122 
130  T* operator -> () const { return mOSGSmartPtr.get(); }
131 
139  operator T* () const { return mOSGSmartPtr; }
140 
150  operator osg::ref_ptr<T> () const { return mOSGSmartPtr;}
151 
161  operator osg::ref_ptr<T>& () { return mOSGSmartPtr; }
162 
172  operator const osg::ref_ptr<T>& () const { return mOSGSmartPtr; }
173 
183  operator osg::ref_ptr<T>* () { return &mOSGSmartPtr; }
184 
195  friend inline std::ostream& operator<<(std::ostream& ios, const SmrtPtr& smPt)
196  {
197  ios << smPt.get();
198  return ios;
199  }
200 
201  private:
202 
203  osg::ref_ptr<T> mOSGSmartPtr; //Pointer to the internal OSG smart pointer
204 
205  };
206 }
207 
208 namespace trUtil
209 {
213  template<class _Key> struct hash<trBase::SmrtPtr<_Key> >
214  {
215  size_t operator()(const trBase::SmrtPtr<_Key>& keyPtr) const
216  {
217  return size_t(keyPtr.Get());
218  }
219  };
220 
224  template<class _Key> struct hash<osg::ref_ptr<_Key> >
225  {
226  size_t operator()(const trBase::SmrtPtr<_Key>& keyPtr) const
227  {
228  return size_t(keyPtr.Get());
229  }
230  };
231 }
SmrtPtr()
Default constructor.
Definition: SmrtPtr.h:45
Smart pointer for handling referenced counted objects.
Definition: SmrtPtr.h:36
size_t operator()(const trBase::SmrtPtr< _Key > &keyPtr) const
Definition: SmrtPtr.h:226
SmrtPtr(T *t)
Constructor.
Definition: SmrtPtr.h:54
bool operator!() const
Logical negation operator.
Definition: SmrtPtr.h:112
This Hash class and its implementations are taken from the libstdc++ hash_fun.h.
Definition: Hash.h:45
size_t operator()(const trBase::SmrtPtr< _Key > &keyPtr) const
Definition: SmrtPtr.h:215
T * operator->() const
Access to the internal pointer.
Definition: SmrtPtr.h:130
T * Release()
Release the pointer from ownership by this SmrtPtr<>, decrementing the objects refenced count to prev...
Definition: SmrtPtr.h:85
void Swap(SmrtPtr &sp)
Swaps the internal pointer of this and the passed in SmartPtr.
Definition: SmrtPtr.h:94
T & operator*() const
Dereferences the internal pointer.
Definition: SmrtPtr.h:121
osg::ref_ptr< T > mOSGSmartPtr
Definition: SmrtPtr.h:203
bool Valid() const
Returns True if the smart pointer has a valid internal pointer set.
Definition: SmrtPtr.h:103
Definition: FrameStamp.h:37
Namespace that holds various utility classes for the engine.
Definition: SmrtPtr.h:208
T * Get() const
Returns the stored internal pointer.
Definition: SmrtPtr.h:73
SmrtPtr(const SmrtPtr &smPt)
Copy constructor.
Definition: SmrtPtr.h:64
friend std::ostream & operator<<(std::ostream &ios, const SmrtPtr &smPt)
Prints out the memory address of the held pointer.
Definition: SmrtPtr.h:195