ADTF
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
89}; // template<typename T> class enable_object_ptr_from_this
90
91// #################################################################################################
92// ############################### Special free functions ##########################################
93// #################################################################################################
103template<typename T>
105{
106 return oCasted;
107}
108
128template<typename Implementation, typename... Args>
130{
131 return vision::allocate_object_ptr<Implementation, true>(std::allocator<void>(), std::forward<Args>(args)...);
132}
133
147template<typename T, typename U>
149{
150 // must be checked beforehand to determine error if ucom_object_ptr_cast returns empty
151 if (nullptr == i_oSrc.Get())
152 { // resetting empty is not an error
153 o_oDest.Reset(object_ptr<const T>());
154 return ERR_NOERROR;
155 }
156
157 // if the subsequent call returns empty, the ucom_cast<> failed
158 const auto pTmp = ucom_object_ptr_cast<const T>(i_oSrc);
159 if (!pTmp)
160 { // reset empty and return error
161 o_oDest.Reset(object_ptr<const T>());
162 return ERR_NO_INTERFACE;
163 }
164
165 return o_oDest.Reset(pTmp);
166}
167
177template<typename T>
179{
180 const object_ptr<const T> pTmp(i_oSrc);
181 return o_oDest.Reset(pTmp);
182}
183
184} // namespace ant
185
186namespace penguin
187{
188
195template<typename T>
197{
198 return ant::object_ptr<T>(i_oOther, const_cast<typename ant::object_ptr<T>::element_type*>(i_oOther.Get()));
199}
200
201} // namespace penguin
202
203namespace vision
204{
226template<typename Implementation, bool Fuse, typename Allocator, typename... Args>
227inline object_ptr<Implementation> allocate_object_ptr(Allocator&& allocator, Args&&... args)
228{
229 static_assert(!std::is_reference_v<Implementation> && !std::is_volatile_v<Implementation>);
230 using implementation_base_type = std::remove_const_t<Implementation>;
231 using allocator_base = typename std::allocator_traits<Allocator>::template rebind_alloc<void>;
232 using counter_type = detail::fused_object_reference_counter<implementation_base_type, allocator_base, Fuse>;
233
234 auto pCounter = counter_type::Create(std::forward<Allocator>(allocator), std::forward<Args>(args)...);
235 auto pSharedObject = pCounter->GetSharedObject();
236 object_ptr<implementation_base_type> oResult(static_cast<ant::detail::iobject_ptr_ref<size_t>*>(pCounter),
237 pSharedObject);
238
239 if constexpr (std::is_base_of_v<ant::enable_object_ptr_from_this<implementation_base_type>, implementation_base_type>)
240 {
241 const auto pEnabler = static_cast<ant::enable_object_ptr_from_this<implementation_base_type>*>(pSharedObject);
242 pEnabler->Assign(oResult);
243 }
244
245 return oResult;
246}
247} // namespace vision
248
250template<class T>
252
255
258
261
264
267
268} // namespace ucom
269} // namespace adtf
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Safely retrieve a valid object_ptr<> instance to *this when all we have is *this.
object_ptr< T > object_ptr_from_this()
Retrieve an object_ptr with *this being the shared resource.
~enable_object_ptr_from_this()=default
Default destructor.
void Assign(const object_ptr< T > &i_pObject)
Assign an existing shared resource in form of the managing object ptr.
object_ptr< const T > object_ptr_from_this() const
Retrieve an object_ptr with *this being the shared resource - const correct.
enable_object_ptr_from_this()=default
Default constructor.
enable_object_ptr_from_this & operator=(const enable_object_ptr_from_this &)=default
Default copy assignment.
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
virtual T * Get() const =0
Get raw pointer to shared object.
virtual tResult Reset(const iobject_ptr< T > &i_oOther)=0
Reset this object_ptr<> with the content of another iobject_ptr<>
Base object pointer to realize binary compatible reference counting in interface methods.
Implementation of a weak pointer for usage with iobject_ptr and object_ptr.
Namespace for all functionality provided since v3.0.
tResult reset_object_ptr(iobject_ptr< const T > &o_oDest, const iobject_ptr< U > &i_oSrc)
Reset an iobject_ptr with const T.
object_ptr< T > ucom_object_ptr_cast(object_ptr< T > oCasted)
Create an object_ptr with an already shared resource of implicitly convertible type.
object_ptr< Implementation > make_object_ptr(Args &&... args)
Create an instance of type object_ptr with Implementation as the shared resource.
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.
Namespace for the ADTF uCOM3 SDK.
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.
Namespace for entire ADTF SDK.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
object_ptr< Implementation > allocate_object_ptr(Allocator &&allocator, Args &&... args)
Create an instance of type object_ptr with Implementation as the shared resource.