autoPtr.h

Engine/source/core/util/autoPtr.h

More...

Classes:

class

A simple smart pointer.

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _AUTOPTR_H_
 25#define _AUTOPTR_H_
 26
 27#ifndef _TYPETRAITS_H_
 28#  include "platform/typetraits.h"
 29#endif
 30
 31
 32template<class T>
 33struct AutoPtrRef
 34{
 35   T* _ptr;
 36   AutoPtrRef(T *ptr)
 37      : _ptr(ptr)
 38   {}
 39};
 40
 41/// A simple smart pointer.
 42/// An extended version of std::auto_ptr which supports a deletion policy.
 43/// The delete policy indicates how the ptr is to be deleted. DeleteSingle,
 44/// the default, is used to delete individual objects. DeleteArray can
 45/// can be used to delete arrays.
 46/// <code>
 47///    AutoPtr<Object> ptr(new Object);
 48///    AutoPtr<Object,DeleteSingle> ptr(new Object);
 49///    AutoPtr<Object,DeleteArray> ptr(new Object[10]);
 50/// </code>
 51/// AutoPtrs do not perform reference counting and assume total ownership
 52/// of any object assigned to them.  Assigning an AutoPtr to another transfers
 53/// that ownership and resets the source AutoPtr to 0.
 54template<class T, class P = DeleteSingle>
 55class AutoPtr
 56{
 57public:
 58   typedef T ValueType;
 59
 60   explicit AutoPtr(T *ptr = 0): _ptr(ptr) {}
 61   ~AutoPtr()
 62   {
 63       P::destroy(_ptr);
 64   }
 65
 66   // Copy constructors
 67   AutoPtr(AutoPtr &rhs): _ptr(rhs.release()) {}
 68
 69   template<class U>
 70   AutoPtr(AutoPtr<U,P> &rhs): _ptr(rhs.release()) { }
 71
 72   /// Transfer ownership, any object currently be referenced is deleted and
 73   /// rhs is set to 0.
 74   AutoPtr& operator=(AutoPtr &rhs)
 75   {
 76       reset(rhs.release());
 77       return *this;
 78   }
 79
 80   template<class U>
 81   AutoPtr& operator=(AutoPtr<U,P> &rhs)
 82   {
 83       reset(rhs.release());
 84       return *this;
 85   }
 86
 87   // Access
 88   T* ptr() const { return _ptr; }
 89   T& operator*() const { return *_ptr; }
 90   T* operator->() const { return _ptr; }
 91   T& operator[](size_t index) { return (_ptr)[index]; }
 92
 93   /// Release ownership of the object without deleting it.
 94   T* release()
 95   {
 96       T* tmp(_ptr);
 97       _ptr = 0;
 98       return tmp;
 99   }
100
101   /// Equivalent to *this = (T*)ptr, except that operator=(T*) isn't provided for.
102   void reset(T* ptr = 0)
103   {
104       if (_ptr != ptr)
105       {
106           P::destroy(_ptr);
107           _ptr = ptr;
108       }
109   }
110
111   // Conversion to/from ref type
112   AutoPtr(AutoPtrRef<T> ref): _ptr(ref._ptr) {}
113   AutoPtr& operator=(AutoPtrRef<T> ref)
114   {
115        reset(ref._ptr);
116        return *this;
117   }
118   
119   bool isNull() const { return _ptr == NULL; }
120   bool isValid() const { return !isNull(); }
121
122   template<class U>
123   operator AutoPtrRef<U>() { return AutoPtrRef<U>(release()); }
124
125   template<class U>
126   operator AutoPtr<U,P>() { return AutoPtr<U,P>(release()); }
127
128private:
129   T  *_ptr;
130};
131
132#endif
133