Mobile API Reference  MicroStrategy 2019
SmartPtr.h
Go to the documentation of this file.
1 //==============================================================================================
2 // FILENAME : SmartPtr.h
3 // AUTHOR : Juan Pablo Muraira
4 // CREATION : 10/19/2001
5 // Copyright (C) MicroStrategy Incorporated 2001
6 //==============================================================================================
7 #ifndef MSynch_SmartPtr_h
8 #define MSynch_SmartPtr_h
9 
10 #include "SmartBase.h"
11 
12 #ifdef _MSC_VER
13 #pragma warning(disable:4284) // return type for 'identifier::operator ->' is not a UDT or reference to a UDT. Will produce errors if applied using infix notation
14 #endif
15 
16 namespace MSynch
17 {
19  // Use SmartPtr when you wish to manipulate a resource by using
20  // reference count technique, if and only if 1) that resource does
21  // not implement reference counting already and 2) you will never
22  // pass that resource outside the current constellation. If either 1)
23  // or 2) is not true, you must use SmartObjX.
24  // If you do not wish to use reference counting, you may use StrongObj.
25  // Remember that StrongObj doesn't work with collections and SmartPtr does.
26  //
27  // We _ASSERTE() when the reference count goes over 10000, which we consider
28  // corrupt.
29  // Notice that the constructor of a SmartPtr may throw, but it will
30  // cleanup the resource it is guarding if that happens.
31  // We verify errors when dereferencing, not when initializing, and we
32  // do so by asserting.
33  // SmartPtr does not have the & operator defined. This is on purpose, we cannot
34  // allow the user to modify the data without the reference count.
36 
37  template<class T,class DeleteOperator=MBase::DeleteC<T> >
38  class SmartPtr:
39  public SmartBase<T*,DeleteOperator>
40  {
41  public:
42  SmartPtr(T* const ipData = NULL): SmartBase<T*,DeleteOperator>(ipData)
43  {
44  }
45 
46  // copy constructor allowed on SmartPtr
47  // upcast should be implicit
48  template<class U,class DeleteOperatorU>
49  SmartPtr(const SmartPtr<U,DeleteOperatorU>& irSmartPtr) throw() :
51  irSmartPtr.mData,
52  reinterpret_cast<Int32*>(irSmartPtr.mpnRefCount))
53  {
54  }
55 
56  // Assignment operator of raw pointer
57  // upcast should be implicit
58  SmartPtr& operator=(T* const ipData)
59  {
60  this->Reset(ipData);
61  return *this;
62  }
63 
64 
65  // Assignment operator (template specialization)
66  // upcast should be implicit
67  SmartPtr& operator=(const SmartPtr& irSmartPtr) throw()
68  {
69  // only modify if not the same pointed object
70  if (this->mData != static_cast<T*>(irSmartPtr.mData))
71  {
72  this->ReplaceWith(irSmartPtr.mData,reinterpret_cast<Int32*>(irSmartPtr.mpnRefCount));
73  }
74  return *this;
75  }
76 
77  T* operator->() const throw()
78  {
79  _ASSERTE(this->mData!=NULL);
80  return this->mData;
81  }
82 
83  T& operator*() const throw()
84  {
85  _ASSERTE(this->mData!=NULL);
86  return *(this->mData);
87  }
88 
89  template<class U,class DeleteOperatorU>
90  void DownCastFrom(SmartPtr<U,DeleteOperatorU>& irSmartPtr) throw()
91  {
92  // Only modify if iData is different from the current member
93  if(static_cast<U*>(this->mData) != irSmartPtr.mData)
94  {
95  ReplaceWith(reinterpret_cast<T*>(irSmartPtr.mData),irSmartPtr.mpnRefCount);
96  }
97  }
98  };
99 
100 
101  // 2002-04-11 -- vovechkin: [SmartPtrArray vs. SmartArrayPtr]
102  // As I mentioned to Juan, the name SmartPtrArray gives an impression
103  // of an "array of smart pointers", which is not how this template works.
104  // In reality, it is a "smart pointer to an array of T".
105  // Hence, a better name: SmartPtrArray.
106 
108  // SmartArrayPtr is a specialization of SmartPtr for use with C++ arrays.
109  template<class T>
111  public SmartPtr<T, MBase::DeleteArray<T> >
112  {
113  public:
114  SmartArrayPtr(T* ipData = NULL):
115  SmartPtr<T,MBase::DeleteArray<T> >(ipData)
116  {
117  }
118 
119  // copy constructor allowed on SmartPtr
120  // upcast should be implicit
121  template<class U>
122  SmartArrayPtr(SmartArrayPtr<U>& irSmartPtr) throw():
124  {
125  }
126 
127  SmartArrayPtr& operator=(T* const ipData)
128  {
129  SmartPtr<T,MBase::DeleteArray<T> >::operator=(ipData);
130  return *this;
131  }
132 
133  template<class U>
134  SmartArrayPtr& operator=(const SmartArrayPtr<U>& irSmartPtr) throw()
135  {
136  SmartPtr<T,MBase::DeleteArray<T> >::operator=(irSmartPtr);
137  return *this;
138  }
139 
140  T& operator[](int i) const
141  {
142  _ASSERTE(this->mData!=NULL);
143  return (this->mData)[i];
144  }
145  };
146 }
147 
148 #endif // MSynch_SmartPtr_h
void ReplaceWith(T * iData, Int32 *ipRefCount)
Definition: SmartBase.h:147
void DownCastFrom(SmartPtr< U, DeleteOperatorU > &irSmartPtr)
Definition: SmartPtr.h:90
T * mData
Definition: SmartBase.h:167
SmartPtr & operator=(T *const ipData)
Definition: SmartPtr.h:58
SmartArrayPtr(T *ipData=NULL)
Definition: SmartPtr.h:114
#define _ASSERTE(x)
Definition: Asserte.h:40
Definition: ReferenceCountedImpl.h:18
T & operator[](int i) const
Definition: SmartPtr.h:140
SmartArrayPtr(SmartArrayPtr< U > &irSmartPtr)
Definition: SmartPtr.h:122
void Reset(T * iData=NULL)
Definition: SmartBase.h:32
SmartPtr(T *const ipData=NULL)
Definition: SmartPtr.h:42
#define Int32
Definition: BasicTypes.h:20
SmartPtr(const SmartPtr< U, DeleteOperatorU > &irSmartPtr)
Definition: SmartPtr.h:49
Definition: Allocator.h:47
Definition: SmartPtr.h:38
SmartArrayPtr & operator=(const SmartArrayPtr< U > &irSmartPtr)
Definition: SmartPtr.h:134
T & operator*() const
Definition: SmartPtr.h:83
SmartArrayPtr & operator=(T *const ipData)
Definition: SmartPtr.h:127
SmartPtr & operator=(const SmartPtr &irSmartPtr)
Definition: SmartPtr.h:67
T * operator->() const
Definition: SmartPtr.h:77
Definition: SmartPtr.h:110
#define NULL
Definition: Null.h:10
Definition: SmartBase.h:20