ADTF
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
weak_object_ptr.h
Go to the documentation of this file.
1
7#pragma once
8
9#include "object_ptr_intf.h"
10
11namespace adtf
12{
13namespace ucom
14{
15namespace ant
16{
17
18namespace detail
19{
20template<typename StorageType>
22}
23
32template<typename T>
34{
36 template<typename>
37 friend class detail::object_ptr_storage;
39 typedef size_t size_type;
40
41private:
51 template<typename U>
52 weak_object_ptr(U* i_pObject, detail::iobject_ptr_ref<size_type>* i_pRefer) noexcept:
53 m_pObject(ucom_cast<T*>(i_pObject)), m_pRefer(i_pRefer)
54 {
55 if (nullptr != m_pRefer)
56 {
57 m_pRefer->IncreaseWeakCount();
58 }
59 }
61
62public:
66 constexpr weak_object_ptr() noexcept: m_pObject(nullptr), m_pRefer(nullptr)
67 {
68 }
69
76 {
77 Reset();
78 }
79
83 constexpr weak_object_ptr(std::nullptr_t) noexcept: weak_object_ptr()
84 {
85 }
86
94 weak_object_ptr(const weak_object_ptr& i_oOther) noexcept:
95 weak_object_ptr(i_oOther.m_pObject, i_oOther.m_pRefer) // Delegate increases weak count
96 {
97 }
98
108 {
109 Swap(oOther);
110 return *this;
111 }
112
118 {
119 Swap(oOther); // leave oOther in valid but unspecified state
120 oOther.Reset();
121 }
122
131 template<typename U>
132 weak_object_ptr(const iobject_ptr<U>& i_pOther) noexcept:
133 weak_object_ptr(i_pOther.Get(), i_pOther.GetObjPtrRef()) // delegate increases weak count
134 {
135 }
136
146 template<typename U>
147 weak_object_ptr& operator=(const iobject_ptr<U>& i_pOther) noexcept
148 {
149 // delegate increases weak count
150 *this = weak_object_ptr(i_pOther.Get(), i_pOther.GetObjPtrRef());
151 return *this;
152 }
153
163 template<typename U>
164 tResult Reset(const iobject_ptr<U>& i_oOther) noexcept
165 {
166 *this = i_oOther;
167 return m_pObject || !i_oOther.Get() ? tResult() : tResult(ERR_NO_INTERFACE);
168 }
169
177 tResult Reset(const iobject_ptr<T>& i_oOther) noexcept
178 {
179 *this = i_oOther;
180 return ERR_NOERROR;
181 }
182
188 void Reset() noexcept
189 {
190 if (nullptr != m_pRefer)
191 {
192 m_pRefer->DecreaseWeakCount();
193 m_pRefer = nullptr;
194 m_pObject = nullptr;
195 }
196 }
197
203 void Swap(weak_object_ptr& o_oOther) noexcept
204 {
205 using std::swap; // ADL for swap()
206 swap(o_oOther.m_pObject, m_pObject);
207 swap(o_oOther.m_pRefer, m_pRefer);
208 }
209
218 object_ptr<T> Lock() const noexcept
219 {
220 return object_ptr<T>(*this);
221 }
222
223private:
225 detail::iobject_ptr_ref<size_type>* m_pRefer;
227};
228
229} // namespace ant
230
232template<class T>
234
235} // namespace ucom
236} // namespace adtf
237
238// specializing the swap function to enable best match for compiler
240namespace std
241{
242template<typename T>
243inline void swap(adtf::ucom::ant::weak_object_ptr<T>& i_oLHS, adtf::ucom::ant::weak_object_ptr<T>& i_oRHS) noexcept
244{
245 i_oLHS.Swap(i_oRHS);
246}
247} // namespace std
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
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.
T * m_pObject
The managed object.
constexpr weak_object_ptr(std::nullptr_t) noexcept
Construct the weak_ptr from a nullptr - for empty construction.
~weak_object_ptr() noexcept
Destructs the weak_object_ptr.
tResult Reset(const iobject_ptr< T > &i_oOther) noexcept
Reset this weak_object_ptr<> with the content of another iobject_ptr<>
detail::iobject_ptr_ref< size_type > * m_pRefer
Refer to control the weak counting.
weak_object_ptr(const weak_object_ptr &i_oOther) noexcept
Copy constructor.
void Reset() noexcept
Reset to an empty weak_ptr object.
tResult Reset(const iobject_ptr< U > &i_oOther) noexcept
Reset this weak_object_ptr<> with the content of another iobject_ptr<>
object_ptr< T > Lock() const noexcept
Create an object_ptr<> from *this weak pointer (thread safe).
void Swap(weak_object_ptr &o_oOther) noexcept
Swap content of *this with o_oOther (only shallow copies are performed!)
constexpr weak_object_ptr() noexcept
Default constructor for empty construction.
weak_object_ptr & operator=(const iobject_ptr< U > &i_pOther) noexcept
Assignment from an iobject_ptr.
weak_object_ptr(weak_object_ptr &&oOther) noexcept
Move constructor.
weak_object_ptr & operator=(weak_object_ptr oOther) noexcept
Assignment operator.
weak_object_ptr(const iobject_ptr< U > &i_pOther) noexcept
Copy construction from an iobject_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
Definition d_ptr.h:237
Namespace for all internally used uCOM functionality implemented.
Namespace for all functionality provided since v3.0.
InterfacePointerType ucom_cast(ObjectPointerType i_pObject)
Used to cast arbitrary interface types within the UCOM.
Definition ucom_cast.h:126
Namespace for the ADTF uCOM3 SDK.
ant::weak_object_ptr< T > weak_object_ptr
Alias always bringing the latest version of ant::weak_object_ptr into scope.
Namespace for entire ADTF SDK.
Copyright © Audi Electronics Venture GmbH.