gwnavruntime/kernel/SF_AutoPtr.h Source File

SF_AutoPtr.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 /**************************************************************************
8 
9 PublicHeader: Kernel
10 Filename : KY_AutoPtr.h
11 Content : Simple auto pointers: AutoPtr, ScopePtr
12 Created : 2008
13 Authors : MaximShemanarev
14 
15 **************************************************************************/
16 
17 #ifndef INC_KY_Kernel_AutoPtr_H
18 #define INC_KY_Kernel_AutoPtr_H
19 
21 
22 namespace Kaim {
23 
24 // ***** AutoPtr
25 //
26 // AutoPtr similar to std::auto_ptr. It keeps a boolean ownership
27 // flag, so can be correctly assigned and copy-constructed, but
28 // generates extra code.
29 // DO NOT use in performance critical code.
30 // DO NOT store in containers (Array, Hash, etc).
31 //------------------------------------------------------------------------
32 template<class T> class AutoPtr
33 {
34 public:
35  AutoPtr(T* p = 0) : pObject(p), Owner(true) {}
36  AutoPtr(T* p, bool owner) : pObject(p), Owner(owner) {}
37  AutoPtr(const AutoPtr<T>& p) : pObject(0), Owner(p.Owner)
38  {
39  pObject = p.constRelease();
40  }
41  ~AutoPtr(void) { Reset(); }
42 
43  AutoPtr<T>& operator=(const AutoPtr<T>& p)
44  {
45  if (this != &p)
46  {
47  bool owner = p.Owner;
48  Reset(p.constRelease());
49  Owner = owner;
50  }
51  return *this;
52  }
53 
54  AutoPtr<T>& operator=(T* p)
55  {
56  Reset(p);
57  return *this;
58  }
59 
60  T& operator* () const { return *pObject; }
61  T* operator->() const { return pObject; }
62  T* GetPtr() const { return pObject; }
63  operator bool(void) const { return pObject != 0; }
64 
65  T* Release()
66  {
67  Owner = false;
68  return pObject;
69  }
70 
71  void Reset(T* p = 0, bool owner = true)
72  {
73  if (pObject != p)
74  {
75  if (pObject && Owner)
76  delete Release();
77  pObject = p;
78  }
79  Owner = p != 0 && owner;
80  }
81 
82 private:
83  T* pObject;
84  bool Owner;
85 
86  // Release for const object.
87  T* constRelease(void) const
88  {
89  return const_cast<AutoPtr<T>*>(this)->Release();
90  }
91 };
92 
93 
94 
95 // ***** ScopePtr
96 //
97 // A simple wrapper used for automatic object deleting
98 // No ownership, no assignment operator, no copy-constructor,
99 // so that no extra code is generated compared with manual new/delete
100 //------------------------------------------------------------------------
101 template<class T> class ScopePtr
102 {
103 public:
104  ScopePtr(T* p = 0) : pObject(p) {}
105  ~ScopePtr(void) { delete pObject; }
106 
107  T& operator* () const { return *pObject; }
108  T* operator->() const { return pObject; }
109  T* GetPtr() const { return pObject; }
110  operator bool() const { return pObject != 0; }
111 
112 private:
113  // Copying is prohibited
114  ScopePtr(const ScopePtr<T>&);
115  const ScopePtr<T>& operator=(const ScopePtr<T>&);
116 
117  T* pObject;
118 };
119 
120 } // Scaleform
121 
122 #endif
123 
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
Vec2f operator*(KyFloat32 s, const Vec2f &v)
scalar * vec operator
Definition: vec2f.h:120