7 #ifndef _ADTF_UCOM_ANT_OBJECT_PTR_PRIVATES_IMPLEMENTATION_HEADER_
8 #define _ADTF_UCOM_ANT_OBJECT_PTR_PRIVATES_IMPLEMENTATION_HEADER_
27 class object_ptr_const_conversion;
30 template<
typename T,
typename U>
38 class delegate_object_ptr;
41 template<
typename T,
typename Enable =
void>
42 class delegate_object_ptr_resetter;
46 class delegate_object_ptr_resetter<
48 typename std::enable_if<std::is_class<T>::value && has_interface_info_type<typename std::remove_cv<T>::type>::value && std::is_same<typename std::remove_cv<T>::type, typename T::interface_type>::value>::type
50 public object_ptr_const_conversion<typename iobject_ptr<T>::object_type>
55 virtual tResult Reset(
const iobject_ptr<object_type>& i_oOther)
57 using child_type = delegate_object_ptr<T>;
58 const auto pTmp = ucom_cast<T*>(i_oOther.Get());
61 static_cast<child_type&
>(*this) = child_type(i_oOther.GetObjPtrRef(), pTmp);
65 static_cast<child_type&
>(*this) = child_type();
70 const bool bSuccess =
static_cast<child_type&
>(*this).Get() || !i_oOther.Get();
73 RETURN_ERROR_DESC(ERR_NO_INTERFACE,
"Object does not implement interface '%s'.", get_iid<T>());
81 class delegate_object_ptr_resetter<
83 typename std::enable_if<std::is_class<T>::value && has_interface_info_type<typename std::remove_cv<T>::type>::value && !std::is_same<typename std::remove_cv<T>::type, typename T::interface_type>::value>::type
85 public object_ptr_const_conversion<typename iobject_ptr<T>::object_type>
90 virtual tResult Reset(
const iobject_ptr<object_type>&)
97 class delegate_object_ptr_resetter<
99 typename std::enable_if<!std::is_class<T>::value || !has_interface_info_type<typename std::remove_cv<T>::type>::value>::type
101 public object_ptr_const_conversion<typename iobject_ptr<T>::object_type>
106 virtual tResult Reset(
const iobject_ptr<object_type>&)
115 template<
typename TSize>
116 class DOEXPORT iobject_ptr_ref
119 template<
typename>
friend class delegate_object_ptr;
122 typedef TSize size_type;
126 ~iobject_ptr_ref() =
default;
129 virtual void IncreaseUseCount() = 0;
130 virtual void DecreaseUseCount() = 0;
132 virtual void IncreaseWeakCount() = 0;
133 virtual void DecreaseWeakCount() = 0;
135 virtual bool IncreaseUseCountNonZero() = 0;
139 class object_reference_counter :
public iobject_ptr_ref<size_t>
143 typedef iobject_ptr_ref<size_t>::size_type size_type;
147 explicit object_reference_counter(U* pObjImpl) :
154 virtual ~object_reference_counter()
158 virtual void IncreaseUseCount()
163 virtual void DecreaseUseCount()
166 if (0 == --m_szUseCount)
173 virtual void IncreaseWeakCount()
178 virtual void DecreaseWeakCount()
181 if (0 == --m_szWeakCount)
187 virtual bool IncreaseUseCountNonZero()
189 size_type szExpected = m_szUseCount;
194 while (0 != szExpected &&
195 !m_szUseCount.compare_exchange_weak(szExpected, szExpected + 1));
197 return 0 != szExpected;
203 m_pObjImpl->Destroy();
207 std::atomic<size_type> m_szUseCount;
208 std::atomic<size_type> m_szWeakCount;
209 const IObject*
const m_pObjImpl;
216 class delegate_object_ptr :
public delegate_object_ptr_resetter<T>
222 template<
typename,
typename >
friend class delegate_object_ptr_resetter;
224 template<
typename Implementation,
typename ...Args>
friend
225 typename std::enable_if
229 enable_object_ptr_from_this<typename std::remove_cv<Implementation>::type>,
230 typename std::remove_cv<Implementation>::type
232 object_ptr<Implementation>
238 typedef object_reference_counter::size_type size_type;
240 iobject_ptr_ref<size_type>* m_pRefer;
245 delegate_object_ptr() :
247 m_pSharedObject(nullptr)
252 delegate_object_ptr(iobject_ptr_ref<size_type>* pRefer, T* pObject)
253 : m_pRefer(pRefer), m_pSharedObject(pObject)
258 delegate_object_ptr(
const delegate_object_ptr& oOther)
259 : m_pRefer(oOther.m_pRefer),
260 m_pSharedObject(oOther.m_pSharedObject)
265 delegate_object_ptr(delegate_object_ptr&& oOther)
271 delegate_object_ptr(
const weak_object_ptr<U>& i_pWeak) : delegate_object_ptr()
273 if (
nullptr != i_pWeak.m_pRefer && i_pWeak.m_pRefer->IncreaseUseCountNonZero())
275 m_pRefer = i_pWeak.m_pRefer;
276 m_pSharedObject = i_pWeak.m_pObject;
280 delegate_object_ptr& operator= (delegate_object_ptr i_oOther)
286 ~delegate_object_ptr()
293 if (
nullptr != m_pRefer)
295 m_pRefer->IncreaseUseCount();
301 if (
nullptr != m_pRefer)
303 m_pRefer->DecreaseUseCount();
304 m_pSharedObject =
nullptr;
309 void Swap(delegate_object_ptr& o_oOther)
312 swap(m_pRefer, o_oOther.m_pRefer);
313 swap(m_pSharedObject, o_oOther.m_pSharedObject);
317 virtual object_type* Get()
const
319 return ucom_cast<object_type*>(m_pSharedObject);
322 virtual object_type* operator->()
const
324 return ucom_cast<object_type*>(m_pSharedObject);
328 virtual iobject_ptr_ref<size_type>* GetObjPtrRef()
const
344 inline void swap(adtf::ucom::ant::detail::delegate_object_ptr<T>& i_oLHS,
345 adtf::ucom::ant::detail::delegate_object_ptr<T>& i_oRHS)
#define RETURN_ERROR_DESC(_code,...)
Same as RETURN_ERROR(_error) using a printf like parameter list for detailed error description.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
A common result class usable as return value throughout.
IObject object_type
Provided for single point of const correct type access.
Object pointer implementation used for reference counting on objects of type IObject.
Implementation of a weak pointer for usage with iobject_ptr and object_ptr.
tVoid swap(A_UTILS_NS::d_ptr< _PARENT, _PRIVATE_D > &i_oLHS, A_UTILS_NS::d_ptr< _PARENT, _PRIVATE_D > &i_oRHS)
std::swap specialization for AUTILSDPtr for perfect fit on ADL
InterfacePointerType ucom_cast(ObjectPointerType i_pObject)
Used to cast arbitrary interface types within the UCOM.
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.
ant::weak_object_ptr< T > weak_object_ptr
Alias always bringing the latest version of ant::weak_object_ptr into scope.
ant::object_ptr< T > object_ptr
Alias always bringing the latest version of ant::object_ptr into scope.
ant::iobject_ptr< T > iobject_ptr
Alias always bringing the latest version of ant::iobject_ptr into scope.
ant::enable_object_ptr_from_this< T > enable_object_ptr_from_this
Alias always bringing the latest version of ant::enable_object_ptr_from_this into scope.
ant::IObject IObject
Alias always bringing the latest version of ant::IObject into scope.
Namespace for entire ADTF SDK.