ADTF  3.18.2
object_ptr_utilities.h
Go to the documentation of this file.
1 
7 #ifndef _ADTF_UCOM_ANT_OBJECT_PTR_UTILITIES_IMPLEMENTATION_HEADER_
8 #define _ADTF_UCOM_ANT_OBJECT_PTR_UTILITIES_IMPLEMENTATION_HEADER_
9 
10 namespace adtf
11 {
12 namespace ucom
13 {
14 namespace ant
15 {
16 
28 template<typename T>
30 {
31 protected: //construction/destruction
40 
41 public:
48  {
49  return m_pWeak.Lock();
50  }
51 
58  {
59  return m_pWeak.Lock();
60  }
61 
62 private:
63  //Only make_object_ptr has access to Assign()
64  template<typename Implementation, typename ...Args> friend
65  typename std::enable_if
66  <
67  std::is_base_of
68  <
70  typename std::remove_cv<Implementation>::type
71  >::value,
73  >::type
74  make_object_ptr(Args&&... args);
75 
81  void Assign(const object_ptr<T>& i_pObject)
82  {
83  m_pWeak.Reset(i_pObject);
84  }
85 
86 private: //private data
88 };//template<typename T> class enable_object_ptr_from_this
89 
90 //#################################################################################################
91 //############################### Special free functions ##########################################
92 //#################################################################################################
102 template<typename T, typename U>
104 {
105  const auto p = ucom_cast<typename object_ptr<T>::element_type*>(i_oOther.Get());
106  if (nullptr != p)
107  {
108  return object_ptr<T>(i_oOther, p);
109  }
110  return object_ptr<T>();
111 }
112 
132 template<typename Implementation, typename ...Args>
133 inline
134  typename std::enable_if
135  <
136  !std::is_base_of
137  <
139  typename std::remove_cv<Implementation>::type
140  >::value,
142  >::type
143 make_object_ptr(Args&&... args)
144 {
146  new (std::nothrow) Implementation(std::forward<Args>(args)...));
147 }
148 
169 template<typename Implementation, typename ...Args>
170 inline
171  typename std::enable_if
172  <
173  std::is_base_of
174  <
176  typename std::remove_cv<Implementation>::type
177  >::value,
179  >::type
180 make_object_ptr(Args&&... args)
181 {
182  using namespace std; //remove_cv, forward, nothrow
183  //as a workaround we need to create non const version to assign the delegate
185  new (nothrow) typename remove_cv<Implementation>::type(forward<Args>(args)...));
186  if (pTmp)
187  {
188  auto& pEnablerTmp = static_cast<enable_object_ptr_from_this
189  <
190  typename std::remove_cv<Implementation>::type
191  >&>(*pTmp);
192  pEnablerTmp.Assign(pTmp);
193  }
194  return pTmp;
195 }
196 
212 template<
213  typename T,
214  typename U,
215  template<typename> class TDest = iobject_ptr,
216  template<typename> class TSrc = iobject_ptr>
217 tResult reset_object_ptr(TDest<const T>& o_oDest, const TSrc<U>& i_oSrc)
218 {
219  //must be checked beforehand to determine error if ucom_object_ptr_cast returns empty
220  if (nullptr == i_oSrc.Get())
221  { //resetting empty is not an error
222  o_oDest.Reset(object_ptr<const T>());
223  return ERR_NOERROR;
224  }
225 
226  //if the subsequent call returns empty, the ucom_cast<> failed
227  const auto pTmp = ucom_object_ptr_cast<const T>(i_oSrc);
228  if (!pTmp)
229  { //reset empty and return error
230  o_oDest.Reset(object_ptr<const T>());
231  return ERR_NO_INTERFACE;
232  }
233 
234  return o_oDest.Reset(pTmp);
235 }
236 
246 template<typename T, template<typename> class TSrc = iobject_ptr>
247 tResult reset_object_ptr(iobject_ptr<const T>& o_oDest, const TSrc<T>& i_oSrc)
248 {
249  const object_ptr<const T> pTmp(i_oSrc);
250  return o_oDest.Reset(pTmp);
251 }
252 
253 }//ns ant
254 
255 namespace penguin
256 {
257 
264 template<typename T>
266 {
267  const auto p = const_cast<typename ant::object_ptr<T>::element_type*>(i_oOther.Get());
268  if (nullptr != p)
269  {
270  return ant::object_ptr<T>(i_oOther, p);
271  }
272  return ant::object_ptr<T>();
273 }
274 
275 }
276 
278 template<class T>
280 
283 
286 
289 
292 
293 }//ns ucom
294 }//ns adtf
295 
296 #endif //_ADTF_UCOM_ANT_OBJECT_PTR_UTILITIES_IMPLEMENTATION_HEADER_
Safely retrieve a valid object_ptr<> instance to *this when all we have is *this.
~enable_object_ptr_from_this()=default
Default destructor.
object_ptr< const T > object_ptr_from_this() const
Retrieve an object_ptr with *this being the shared resource - const correct.
friend std::enable_if< std::is_base_of< enable_object_ptr_from_this< typename std::remove_cv< Implementation >::type >, typename std::remove_cv< Implementation >::type >::value, object_ptr< Implementation > >::type make_object_ptr(Args &&... args)
Create an instance of type object_ptr with Implementation as the shared resource.
enable_object_ptr_from_this & operator=(const enable_object_ptr_from_this &)=default
Default copy assignment.
void Assign(const object_ptr< T > &i_pObject)
Assign an existing shared resource in form of the managing object ptr.
enable_object_ptr_from_this()=default
Default constructor.
object_ptr< T > object_ptr_from_this()
Retrieve an object_ptr with *this being the shared resource.
enable_object_ptr_from_this(const enable_object_ptr_from_this &)=default
Default copy construction.
weak_object_ptr< T > m_pWeak
weak pointer managing the object_ptr to enable
Base object pointer to realize binary compatible reference counting in interface methods.
virtual tResult Reset(const iobject_ptr< T > &i_oOther)=0
Reset this object_ptr<> with the content of another iobject_ptr<>
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.
std::enable_if< !std::is_base_of< enable_object_ptr_from_this< typename std::remove_cv< Implementation >::type >, typename std::remove_cv< Implementation >::type >::value, object_ptr< Implementation > >::type make_object_ptr(Args &&... args)
Create an instance of type object_ptr with Implementation as the shared resource.
tResult reset_object_ptr(TDest< const T > &o_oDest, const TSrc< U > &i_oSrc)
Reset an iobject_ptr with const T.
object_ptr< T > ucom_object_ptr_cast(const iobject_ptr< U > &i_oOther)
Create an object_ptr with an already shared resource of implicitly convertible type.
ant::object_ptr< T > ucom_object_ptr_const_cast(const ant::iobject_ptr< const T > &i_oOther)
Create an object_ptr with a mutable shared resource of an existing const resource.
ant::iobject_ptr< T > iobject_ptr
Alias always bringing the latest version of ant::iobject_ptr into scope.
Namespace for entire ADTF SDK.