ADTF
Loading...
Searching...
No Matches
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:
224 T* m_pObject;
225 detail::iobject_ptr_ref<size_type>* m_pRefer;
226};
227
228} // namespace ant
229
231template<class T>
233
234} // namespace ucom
235} // namespace adtf
236
237// specializing the swap function to enable best match for compiler
239namespace std
240{
241template<typename T>
242inline void swap(adtf::ucom::ant::weak_object_ptr<T>& i_oLHS, adtf::ucom::ant::weak_object_ptr<T>& i_oRHS) noexcept
243{
244 i_oLHS.Swap(i_oRHS);
245}
246} // namespace std
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
Base object pointer to realize binary compatible reference counting in interface methods.
Definition object_ptr_intf.h:112
Definition object_ptr.h:384
Implementation of a weak pointer for usage with iobject_ptr and object_ptr.
Definition weak_object_ptr.h:34
weak_object_ptr(const iobject_ptr< U > &i_pOther) noexcept
Copy construction from an iobject_ptr.
Definition weak_object_ptr.h:132
tResult Reset(const iobject_ptr< T > &i_oOther) noexcept
Reset this weak_object_ptr<> with the content of another iobject_ptr<>
Definition weak_object_ptr.h:177
tResult Reset(const iobject_ptr< U > &i_oOther) noexcept
Reset this weak_object_ptr<> with the content of another iobject_ptr<>
Definition weak_object_ptr.h:164
constexpr weak_object_ptr() noexcept
Default constructor for empty construction.
Definition weak_object_ptr.h:66
void Reset() noexcept
Reset to an empty weak_ptr object.
Definition weak_object_ptr.h:188
~weak_object_ptr() noexcept
Destructs the weak_object_ptr.
Definition weak_object_ptr.h:75
void Swap(weak_object_ptr &o_oOther) noexcept
Swap content of *this with o_oOther (only shallow copies are performed!)
Definition weak_object_ptr.h:203
object_ptr< T > Lock() const noexcept
Create an object_ptr<> from *this weak pointer (thread safe).
Definition weak_object_ptr.h:218
weak_object_ptr & operator=(const iobject_ptr< U > &i_pOther) noexcept
Assignment from an iobject_ptr.
Definition weak_object_ptr.h:147
weak_object_ptr(weak_object_ptr &&oOther) noexcept
Move constructor.
Definition weak_object_ptr.h:117
constexpr weak_object_ptr(std::nullptr_t) noexcept
Construct the weak_ptr from a nullptr - for empty construction.
Definition weak_object_ptr.h:83
weak_object_ptr(const weak_object_ptr &i_oOther) noexcept
Copy constructor.
Definition weak_object_ptr.h:94
weak_object_ptr & operator=(weak_object_ptr oOther) noexcept
Assignment operator.
Definition weak_object_ptr.h:107
Namespace for all internally used functionality implemented.
Definition class_info.h:17
Namespace for all functionality of the ADTF UCOM SDK provided since v3.0.
Definition test_runtime.h:14
InterfacePointerType ucom_cast(ObjectPointerType i_pObject) noexcept
Definition ucom_cast.h:157
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
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14