ADTF  3.18.2
weak_object_ptr.h
Go to the documentation of this file.
1 
7 #ifndef _ADTF_UCOM_ANT_WEAK_OBJECT_PTR_IMPLEMENTATION_HEADER_
8 #define _ADTF_UCOM_ANT_WEAK_OBJECT_PTR_IMPLEMENTATION_HEADER_
9 
10 namespace adtf
11 {
12 namespace ucom
13 {
14 namespace ant
15 {
16 
25 template < typename T >
27 {
29  template<typename> friend class detail::delegate_object_ptr;
31  typedef typename detail::object_reference_counter::size_type size_type;
32 
33 private:
43  template < typename U >
44  weak_object_ptr(U* i_pObject, detail::iobject_ptr_ref<size_type>* i_pRefer) :
45  m_pObject(ucom_cast<T*>(i_pObject)),
46  m_pRefer(i_pRefer)
47  {
48  IncreaseWeakCount();
49  }
52 public:
57  m_pObject(nullptr),
58  m_pRefer(nullptr)
59  {
60  }
61 
68  {
69  DecreaseWeakCount();
70  }
71 
75  weak_object_ptr(std::nullptr_t) : weak_object_ptr()
76  {
77  }
78 
86  weak_object_ptr(const weak_object_ptr& i_oOther) :
87  weak_object_ptr(i_oOther.m_pObject, i_oOther.m_pRefer) //Delegate increases weak count
88  {
89  }
100  {
101  Swap(oOther);
102  return *this;
103  }
104 
110  {
111  Swap(oOther); //leave oOther in valid but unspecified state
112  }
113 
122  template<typename U>
123  weak_object_ptr(const iobject_ptr<U>& i_pOther) :
124  weak_object_ptr(i_pOther.Get(), i_pOther.GetObjPtrRef()) //delegate increases weak count
125  {
126  }
127 
137  template<typename U>
139  {
140  //delegate increases weak count
141  *this = weak_object_ptr(i_pOther.Get(), i_pOther.GetObjPtrRef());
142  return *this;
143  }
144 
154  template<typename U>
155  tResult Reset(const iobject_ptr<U>& i_oOther)
156  {
157  *this = i_oOther;
158  return m_pObject || !i_oOther.Get() ? tResult() : tResult(ERR_NO_INTERFACE);
159  }
160 
168  tResult Reset(const iobject_ptr<T>& i_oOther)
169  {
170  *this = i_oOther;
171  return ERR_NOERROR;
172  }
173 
179  void Reset()
180  {
181  *this = weak_object_ptr();
182  }
183 
189  void Swap(weak_object_ptr& o_oOther)
190  {
191  using std::swap; //ADL for swap()
192  swap(o_oOther.m_pRefer, m_pRefer);
193  swap(o_oOther.m_pObject, m_pObject);
194  }
195 
205  {
206  return object_ptr<T>(*this);
207  }
208 
209 private:
211  void IncreaseWeakCount()
212  {
213  if (nullptr != m_pRefer)
214  {
215  m_pRefer->IncreaseWeakCount();
216  }
217  }
218 
219  void DecreaseWeakCount()
220  {
221  if (nullptr != m_pRefer)
222  {
223  m_pRefer->DecreaseWeakCount();
224  m_pRefer = nullptr;
225  m_pObject = nullptr;
226  }
227  }
228 
229 private:
230  T* m_pObject;
231  detail::iobject_ptr_ref<size_type>* m_pRefer;
233 };
234 
235 }//ns ant
236 
238 template<class T>
240 
241 }//ns ucom
242 }//ns adtf
243 
244 //specializing the swap function to enable best match for compiler
246 namespace std
247 {
248  template<typename T>
249  inline void swap(adtf::ucom::ant::weak_object_ptr<T>& i_oLHS,
251  {
252  i_oLHS.Swap(i_oRHS);
253  }
254 }//ns std
256 
257 #endif //_ADTF_UCOM_ANT_WEAK_OBJECT_PTR_IMPLEMENTATION_HEADER_
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
virtual detail::iobject_ptr_ref< size_t > * GetObjPtrRef() const =0
Get the reference counter.
virtual T * Get() const =0
Get raw pointer to shared object.
Base object pointer to realize binary compatible reference counting in interface methods.
Object pointer implementation used for reference counting on objects of type IObject.
Definition: object_ptr.h:163
Implementation of a weak pointer for usage with iobject_ptr and object_ptr.
weak_object_ptr & operator=(weak_object_ptr oOther)
Assignment operator.
tResult Reset(const iobject_ptr< T > &i_oOther)
Reset this weak_object_ptr<> with the content of another iobject_ptr<>
~weak_object_ptr()
Destructs the weak_object_ptr.
object_ptr< T > Lock() const
Create an object_ptr<> from *this weak pointer (thread safe).
void Reset()
Reset to an empty weak_ptr object.
weak_object_ptr(const weak_object_ptr &i_oOther)
Copy constructor.
weak_object_ptr()
Default constructor for empty construction.
weak_object_ptr(std::nullptr_t)
Construct the weak_ptr from a nullptr - for empty construction.
void Swap(weak_object_ptr &o_oOther)
Swap content of *this with o_oOther (only shallow copies are performed!)
weak_object_ptr(const iobject_ptr< U > &i_pOther)
Copy construction from an iobject_ptr.
tResult Reset(const iobject_ptr< U > &i_oOther)
Reset this weak_object_ptr<> with the content of another iobject_ptr<>
weak_object_ptr(weak_object_ptr &&oOther)
Move constructor.
weak_object_ptr & operator=(const iobject_ptr< U > &i_pOther)
Assignment from an iobject_ptr.
Namespace for entire ADTF SDK.