ADTF
Loading...
Searching...
No Matches
object_ptr_utilities.h
Go to the documentation of this file.
1
7#pragma once
8
9#include "object_ptr.h"
10#include "object_ptr_privates.h"
11
12#include <memory>
13#include <type_traits>
14
15namespace adtf
16{
17namespace ucom
18{
19namespace vision
20{
21template<typename Implementation, bool Fuse, typename Allocator, typename... Args>
22inline object_ptr<Implementation> allocate_object_ptr(Allocator&& allocator, Args&&... args);
23}
24namespace ant
25{
26
38template<typename T>
40{
41protected: // construction/destruction
50
51public:
58 {
59 return m_pWeak.Lock();
60 }
61
68 {
69 return m_pWeak.Lock();
70 }
71
72private:
73 // Only allocate_object_ptr has access to Assign()
74 template<typename Implementation, bool Fuse, typename Allocator, typename... Args>
75 friend object_ptr<Implementation> vision::allocate_object_ptr(Allocator&& allocator, Args&&... args);
76
82 void Assign(const object_ptr<T>& i_pObject)
83 {
84 m_pWeak.Reset(i_pObject);
85 }
86
87private: // private data
88 weak_object_ptr<T> m_pWeak;
89}; // template<typename T> class enable_object_ptr_from_this
90
91namespace detail
92{
94
101template<typename T>
102T* deduce_object_ptr_enabler(enable_object_ptr_from_this<T>&) noexcept;
103
104template<typename Implementation, typename = void>
105struct enables_object_ptr_from_this: ::std::false_type
106{
107};
108
111template<typename Implementation>
112struct enables_object_ptr_from_this<
113 Implementation,
114 ::std::void_t<decltype(deduce_object_ptr_enabler(::std::declval<Implementation&>()))>>:
115 ::std::is_convertible<Implementation*, decltype(deduce_object_ptr_enabler(::std::declval<Implementation&>()))>
116{
117};
118
120} // namespace detail
121
122// #################################################################################################
123// ############################### Special free functions ##########################################
124// #################################################################################################
134template<typename T>
136{
137 return oCasted;
138}
139
159template<typename Implementation, typename... Args>
161{
162 return vision::allocate_object_ptr<Implementation, true>(std::allocator<void>(), std::forward<Args>(args)...);
163}
164
176template<typename Implementation>
177inline object_ptr<std::decay_t<Implementation>> make_object_ptr(Implementation&& object_to_emplace)
178{
179 return vision::allocate_object_ptr<std::decay_t<Implementation>, true>(std::allocator<void>(),
180 std::forward<Implementation>(object_to_emplace));
181}
182
196template<typename T, typename U>
198{
199 // must be checked beforehand to determine error if ucom_object_ptr_cast returns empty
200 if (nullptr == i_oSrc.Get())
201 { // resetting empty is not an error
202 o_oDest.Reset(object_ptr<const T>());
203 return ERR_NOERROR;
204 }
205
206 // if the subsequent call returns empty, the ucom_cast<> failed
207 const auto pTmp = ucom_object_ptr_cast<const T>(i_oSrc);
208 if (!pTmp)
209 { // reset empty and return error
210 o_oDest.Reset(object_ptr<const T>());
211 return ERR_NO_INTERFACE;
212 }
213
214 return o_oDest.Reset(pTmp);
215}
216
226template<typename T>
228{
229 const object_ptr<const T> pTmp(i_oSrc);
230 return o_oDest.Reset(pTmp);
231}
232
233} // namespace ant
234
235namespace penguin
236{
237
244template<typename T>
246{
247 return ant::object_ptr<T>(i_oOther, const_cast<typename ant::object_ptr<T>::element_type*>(i_oOther.Get()));
248}
249
250} // namespace penguin
251
252namespace vision
253{
275template<typename Implementation, bool Fuse, typename Allocator, typename... Args>
276inline object_ptr<Implementation> allocate_object_ptr(Allocator&& allocator, Args&&... args)
277{
278 static_assert(!std::is_reference_v<Implementation> && !std::is_volatile_v<Implementation>);
279 using implementation_base_type = std::remove_const_t<Implementation>;
280 using allocator_base = typename std::allocator_traits<Allocator>::template rebind_alloc<void>;
281 using counter_type = detail::fused_object_reference_counter<implementation_base_type, allocator_base, Fuse>;
282
283 auto pCounter = counter_type::Create(std::forward<Allocator>(allocator), std::forward<Args>(args)...);
284 auto pSharedObject = pCounter->GetSharedObject();
285 object_ptr<implementation_base_type> oResult(static_cast<ant::detail::iobject_ptr_ref<size_t>*>(pCounter),
286 pSharedObject);
287
288 if constexpr (ant::detail::enables_object_ptr_from_this<implementation_base_type>::value)
289 {
290 // the base may be enabled for one of the base classes of the implementation, so the object_ptr
291 // to assign shares the reference count of the result and points to that base
292 using enabled_type =
293 ::std::remove_pointer_t<decltype(ant::detail::deduce_object_ptr_enabler(*pSharedObject))>;
294 enabled_type* pEnabled = pSharedObject;
295 ant::enable_object_ptr_from_this<enabled_type>* pEnabler = pSharedObject;
296 pEnabler->Assign(object_ptr<enabled_type>(oResult, pEnabled));
297 }
298
299 return oResult;
300}
301} // namespace vision
302
304template<class T>
306
309
312
315
318
321
322} // namespace ucom
323} // namespace adtf
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:775
Safely retrieve a valid object_ptr<> instance to *this when all we have is *this.
Definition object_ptr_utilities.h:40
~enable_object_ptr_from_this()=default
Default destructor.
object_ptr< T > object_ptr_from_this()
Retrieve an object_ptr with *this being the shared resource.
Definition object_ptr_utilities.h:57
enable_object_ptr_from_this()=default
Default constructor.
object_ptr< const T > object_ptr_from_this() const
Retrieve an object_ptr with *this being the shared resource - const correct.
Definition object_ptr_utilities.h:67
enable_object_ptr_from_this(const enable_object_ptr_from_this &)=default
Default copy construction.
enable_object_ptr_from_this & operator=(const enable_object_ptr_from_this &)=default
Default copy assignment.
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.
Definition object_ptr_intf.h:112
Definition object_ptr.h:384
Namespace for all functionality of the ADTF UCOM SDK provided since v3.0.
Definition test_runtime.h:14
tResult reset_object_ptr(iobject_ptr< const T > &o_oDest, const iobject_ptr< U > &i_oSrc)
Reset an iobject_ptr with const T.
Definition object_ptr_utilities.h:197
object_ptr< T > ucom_object_ptr_cast(object_ptr< T > oCasted)
Create an object_ptr with an already shared resource of implicitly convertible type.
Definition object_ptr_utilities.h:135
object_ptr< Implementation > make_object_ptr(Args &&... args)
Create an instance of type object_ptr with Implementation as the shared resource.
Definition object_ptr_utilities.h:160
Namespace for all functionality of the ADTF UCOM SDK provided since v3.21.
Definition plugin_logging_intf.h:48
object_ptr< Implementation > allocate_object_ptr(Allocator &&allocator, Args &&... args)
Create an instance of type object_ptr with Implementation as the shared resource.
Definition object_ptr_utilities.h:276
Namespace for the ADTF uCOM SDK.
Definition adtf_system.h:324
ant::weak_object_ptr< T > weak_object_ptr
Alias always bringing the latest version of ant::weak_object_ptr into scope.
Definition weak_object_ptr.h:232
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.
Definition object_ptr_utilities.h:305
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14
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.
Definition object_ptr_utilities.h:245