ADTF
object_ptr.h
Go to the documentation of this file.
1 
7 #ifndef _ADTF_UCOM_ANT_OBJECT_PTR_IMPLEMENTATION_HEADER_
8 #define _ADTF_UCOM_ANT_OBJECT_PTR_IMPLEMENTATION_HEADER_
9 
10 #ifndef _WIN32
11 // see https://gitlab.com/digitalwerk/solutions/adtf_content/adtf_base/adtf_core/-/issues/2802
12 #pragma GCC diagnostic push
13 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
14 #endif
15 namespace adtf
16 {
17 namespace ucom
18 {
19 namespace ant
20 {
21 
23 template<typename T>
25 {
26 protected:
31 public:
32 
37 #ifndef _MSC_VER
38  // see ACORE-9344, workaround for gcc de-virtualization behaviour
39  __attribute__((noinline))
40 #endif
41  operator const iobject_ptr<const T>& () const override
42  {
43  return *reinterpret_cast<const object_ptr_const_conversion<const T>*>(this);
44  }
45 };//template<typename T> class object_ptr_const_conversion
46 
48 template<typename T>
49 class object_ptr_const_conversion<const T> : public iobject_ptr<const T>
50 {
51 protected:
56 };//template<typename T> class object_ptr_const_conversion<const T>
57 
64 template<typename T,
65  bool = std::is_class<T>::value &&
66  detail::has_interface_info_type<typename std::remove_cv<T>::type>::value,
67  typename = object_ptr<T>
68  >
70 
80 template<typename T, typename TChild>
81 class object_ptr_base<T, false, TChild> : public object_ptr_const_conversion<T>
82 {
84 
85 public:
92  operator const iobject_ptr<object_type>& () const
93  { //use CRTP to access the private area of the object_ptr (which is the child of this)
94  return static_cast<const TChild* const>(this)->m_oDelegateObject;
95  }
96 };//template<typename T, typename TChild> class object_ptr_base<T, false, TChild>
97 
102 template<typename T, typename TChild>
103 class object_ptr_base<T, true, TChild> : public object_ptr_const_conversion<T>
104 
105 {
107 
108 public: //implement implicit cast with CRTP
116  { //use CRTP to access the private area of the object_ptr (which is the child of this)
117  return static_cast<TChild* const>(this)->m_oDelegateObject;
118  }
119 
126  operator const iobject_ptr<object_type>& () const
127  { //use CRTP to access the private area of the object_ptr (which is the child of this)
128  return static_cast<const TChild* const>(this)->m_oDelegateObject;
129  }
130 
131 protected:
133  constexpr object_ptr_base()
134  {
135  static_assert(!std::is_same<TChild, object_ptr<object_type>>::value,
136  "object_ptr<IObject> already implicitly convertible to iobject_ptr<IObject>");
137  }
138 };//template<typename T, typename TChild> class object_ptr_base<T, true, TChild>
139 
141 template<> class object_ptr_base<IObject> : public object_ptr_const_conversion<IObject> {};
142 
144 template<> class object_ptr_base<const IObject> : public iobject_ptr<const IObject> {};
145 
161 template<typename T>
162 class object_ptr : public object_ptr_base<T>
163 {
165  template<typename> friend class detail::delegate_object_ptr;
166 
167  template<typename Implementation, typename ...Args> friend
168  typename std::enable_if
169  <
170  !std::is_base_of
171  <
173  typename std::remove_cv<Implementation>::type
174  >::value,
176  >::type
177  make_object_ptr(Args&&... args);
178 
179  template<typename Implementation, typename ...Args> friend
180  typename std::enable_if
181  <
182  std::is_base_of
183  <
185  typename std::remove_cv<Implementation>::type
186  >::value,
188  >::type
189  make_object_ptr(Args&&... args);
190 
191  friend class object_ptr_base<T, true, object_ptr<T>>;
192  friend class object_ptr_base<T, false, object_ptr<T>>;
193  friend class weak_object_ptr<T>;
194 
195 private: //member section
204  template<typename U>
205  object_ptr(U* pSharedObject) :
206  m_oDelegateObject(nullptr != pSharedObject
207  ? new (std::nothrow) detail::object_reference_counter(pSharedObject)
208  : nullptr,
209  ucom_cast<T*>(pSharedObject))
210  {
211  }
212 
214  typedef detail::object_reference_counter::size_type size_type;
215 
220  virtual detail::iobject_ptr_ref<size_type>* GetObjPtrRef() const
221  {
222  return m_oDelegateObject.m_pRefer;
223  }
224 
225 private: //data section
227  typedef detail::delegate_object_ptr<T> storage_type;
229  storage_type m_oDelegateObject;
232 public:
234  object_ptr() : m_oDelegateObject()
235  {
236  }
237 
239  virtual ~object_ptr() = default;
240 
242  object_ptr(std::nullptr_t) : object_ptr()
243  {
244  }
245 
250  object_ptr(const object_ptr& i_oOther) :
251  object_ptr(static_cast<const iobject_ptr<T>&>(i_oOther))
252  {
253  }
254 
259  object_ptr(const iobject_ptr<T>& i_oOther) :
260  m_oDelegateObject(i_oOther.GetObjPtrRef(), i_oOther.Get())
261  {
262  }
263 
269  template<typename U>
270  object_ptr(const iobject_ptr<U>& i_oOther) :
271  object_ptr(ucom_object_ptr_cast<T, U>(i_oOther))
272  {
273  }
274 
281  template<typename U>
282  object_ptr(const iobject_ptr<U>& i_oOther, T* pSharedObject)
283  : m_oDelegateObject(i_oOther.GetObjPtrRef(), pSharedObject)
284  {
285  }
286 
292  {
293  Swap(i_oOther); //leave move-from object in valid but unspecified state
294  }
295 
304  template<typename U>
305  object_ptr(const weak_object_ptr<U>& i_pWeak) :
306  m_oDelegateObject(i_pWeak)
307  {
308  }
309 
319  {
320  Swap(i_oOther);
321  return *this;
322  }
323 
330  template<typename U>
332  {
333  *this = ucom_object_ptr_cast<T, U>(i_oOther);
334  return *this;
335  }
336 
341  T& operator*() const
342  {
343  return *Get();
344  }
345 
350  explicit operator bool() const
351  {
352  return (Get() != nullptr);
353  }
354 
361  void Reset()
362  {
363  *this = object_ptr();
364  }
365 
371  void Swap(object_ptr& o_oOther)
372  {
373  m_oDelegateObject.Swap(o_oOther.m_oDelegateObject);
374  }
375 
376 public: //implement iobject_ptr
381  virtual T* Get() const
382  {
383  return m_oDelegateObject.m_pSharedObject;
384  }
385 
390  virtual T* operator->() const
391  {
392  return Get();
393  }
394 
402  virtual tResult Reset(const iobject_ptr<T>& i_oOther)
403  {
404  *this = i_oOther;
405  return ERR_NOERROR;
406  }
407 };//class object_ptr<>
408 
409 }//ns ant
410 
412 template<class T>
414 
415 }//ns ucom
416 }//ns adtf
417 
418 //specializing the swap function to enable best match for compiler
420 namespace std
421 {
422  template<typename T>
423  inline void swap(adtf::ucom::ant::object_ptr<T>& i_oLHS,
425  {
426  i_oLHS.Swap(i_oRHS);
427  }
428 }//ns std
430 
431 #ifndef _WIN32
432 #pragma GCC diagnostic pop
433 #endif
434 
435 #endif //_ADTF_UCOM_ANT_OBJECT_PTR_IMPLEMENTATION_HEADER_
Base class for every interface type within the uCOM.
Definition: object_intf.h:31
Safely retrieve a valid object_ptr<> instance to *this when all we have is *this.
Base object pointer to realize binary compatible reference counting in interface methods.
Base object pointer to realize binary compatible reference counting in interface methods.
iobject_ptr< T >::object_type object_type
const correct IObject type
Definition: object_ptr.h:83
constexpr object_ptr_base()
Used to test whether object_ptr<IObject> is not inherited from this base.
Definition: object_ptr.h:133
iobject_ptr< T >::object_type object_type
const correct IObject type
Definition: object_ptr.h:106
Base class for object_ptr<>
Definition: object_ptr.h:69
Disables implicit conversion operators to iobject_ptr<const T> (implemented through inheritance)
Definition: object_ptr.h:50
Enables implicit conversion operators to iobject_ptr<const T>
Definition: object_ptr.h:25
__attribute__((noinline)) operator const iobject_ptr< const T > &() const override
Enable implicit conversion to const value type for pointer-like behavior.
Definition: object_ptr.h:39
Object pointer implementation used for reference counting on objects of type IObject.
Definition: object_ptr.h:163
virtual tResult Reset(const iobject_ptr< T > &i_oOther)
Reset this object_ptr<> with the content of another iobject_ptr<>
Definition: object_ptr.h:402
void Reset()
Reset this object_ptr.
Definition: object_ptr.h:361
object_ptr & operator=(object_ptr i_oOther)
Assignment operator.
Definition: object_ptr.h:318
object_ptr(std::nullptr_t)
nullptr_t construction - the object_ptr<T> is created empty
Definition: object_ptr.h:242
object_ptr(object_ptr &&i_oOther)
Move constructor.
Definition: object_ptr.h:291
object_ptr()
Default constructor - the object_ptr<T> is created empty.
Definition: object_ptr.h:234
object_ptr(const weak_object_ptr< U > &i_pWeak)
Construct from weak_object_ptr<>
Definition: object_ptr.h:305
T & operator*() const
Operator* overload to treat object_ptr<> types like real pointers.
Definition: object_ptr.h:341
object_ptr(const iobject_ptr< U > &i_oOther, T *pSharedObject)
Aliasing constructor for iobject_ptr containing a different type.
Definition: object_ptr.h:282
virtual T * operator->() const
Operator-> overload to treat object_ptr<> types like real pointers.
Definition: object_ptr.h:390
virtual T * Get() const
Get pointer to shared object.
Definition: object_ptr.h:381
void Swap(object_ptr &o_oOther)
Swap content of *this with o_oOther (only shallow copies are performed!)
Definition: object_ptr.h:371
object_ptr(const object_ptr &i_oOther)
Copy constructor.
Definition: object_ptr.h:250
object_ptr & operator=(const object_ptr< U > &i_oOther)
Assignment operator for object_ptr containing a different type.
Definition: object_ptr.h:331
object_ptr(const iobject_ptr< T > &i_oOther)
Copy constructor for base type.
Definition: object_ptr.h:259
virtual ~object_ptr()=default
default destruction
object_ptr(const iobject_ptr< U > &i_oOther)
Copy constructor for iobject_ptr containing a different type.
Definition: object_ptr.h:270
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.
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.
Namespace for entire ADTF SDK.