21#ifdef AUI_SHARED_PTR_FIND_INSTANCES
23#include <AUI/Util/APimpl.h>
27#include <AUI/Reflect/AClass.h>
31namespace aui::impl::shared_ptr {
32 struct InstancesDict {
33 std::recursive_mutex sync;
34 std::map<void*, std::set<void*>> map;
37 API_AUI_CORE InstancesDict& instances() noexcept;
38 API_AUI_CORE
void printAllInstancesOf(
void* ptrToSharedPtr) noexcept;
51struct _weak:
public std::weak_ptr<T> {
53 using super = std::weak_ptr<T>;
56 using super::weak_ptr;
59 _weak(
_weak<T>&& v)
noexcept: std::weak_ptr<T>(std::move(v)) {}
60 _weak(
const std::weak_ptr<T>& v): std::weak_ptr<T>(v) {}
61 _weak(std::weak_ptr<T>&& v)
noexcept: std::weak_ptr<T>(std::move(v)) {}
63 _<T> lock()
const noexcept {
64 return static_cast<_<T>>(super::lock());
67 _weak& operator=(
const std::weak_ptr<T>& v)
noexcept {
68 super::weak_ptr::operator=(v);
72 _weak& operator=(std::weak_ptr<T>&& v)
noexcept {
73 super::weak_ptr::operator=(std::move(v));
78 super::weak_ptr::operator=(v);
83 super::weak_ptr::operator=(std::move(v));
87 _weak& operator=(
const std::shared_ptr<T>& v)
noexcept {
88 super::weak_ptr::operator=(v);
92 _weak& operator=(std::shared_ptr<T>&& v)
noexcept {
93 super::weak_ptr::operator=(std::move(v));
97 _weak& operator=(
const _<T>& v)
noexcept {
98 super::weak_ptr::operator=(v);
103 super::weak_ptr::operator=(std::move(v));
108template<
typename T,
typename Deleter = std::default_delete<T>>
109using _unique = std::unique_ptr<T, Deleter>;
124 template<
typename T,
typename Deleter = std::default_delete<T>>
126 return {
ptr, std::move(deleter) };
136 static _<T> manage(T* raw);
146 template<
typename T,
typename Deleter>
147 static _<T> manage(T* raw, Deleter deleter);
155 static _unique<T> unique(T* raw);
165 static _<T> fake(T* raw);
176class _ :
public std::shared_ptr<T>
180 using super = std::shared_ptr<T>;
182 _(T* raw, std::nullopt_t): std::shared_ptr<T>(raw) {
186#ifdef AUI_SHARED_PTR_FIND_INSTANCES
187 friend API_AUI_CORE
void aui::impl::shared_ptr::printAllInstancesOf(
void* ptrToSharedPtr)
noexcept;
188 struct InstanceDescriber;
192 struct InstanceDescriber {
198 if (pointingTo ==
nullptr)
return;
199 std::unique_lock lock(aui::impl::shared_ptr::instances().sync);
200 aui::impl::shared_ptr::instances().map[pointingTo].insert(
self);
204 InstanceDescriber(
const InstanceDescriber&) =
delete;
205 InstanceDescriber(InstanceDescriber&&) =
delete;
207 ~InstanceDescriber() {
208 if (pointingTo ==
nullptr)
return;
209 auto& inst = aui::impl::shared_ptr::instances();
210 std::unique_lock lock(inst.sync);
211 if (
auto mapIt = inst.map.find(pointingTo); mapIt != inst.map.end()) {
212 auto setIt = mapIt->second.find(
self);
214 mapIt->second.erase(setIt);
215 if (mapIt->second.empty()) {
221 } mInstanceDescriber =
this;
229#ifdef AUI_SHARED_PTR_FIND_INSTANCES
230 void printAllInstances() {
231 aui::impl::shared_ptr::printAllInstancesOf(
this);
233 void printAllInstances()
const {
234 const_cast<_<T>&
>(*this).printAllInstances();
238 using std::shared_ptr<T>::shared_ptr;
240 _(
const std::shared_ptr<T>& v): std::shared_ptr<T>(v) {}
241 _(std::shared_ptr<T>&& v)
noexcept: std::shared_ptr<T>(std::move(v)) {}
242 _(
const _& v): std::shared_ptr<T>(v) {}
243 _(
_&& v)
noexcept: std::shared_ptr<T>(std::move(v)) {}
244 _(
const std::weak_ptr<T>& v): std::shared_ptr<T>(v) {}
245 _(
const _weak<T>& v): std::shared_ptr<T>(v) {}
247 _& operator=(
const _& rhs)
noexcept {
248 std::shared_ptr<T>::operator=(rhs);
252 _& operator=(
_&& rhs)
noexcept {
253 std::shared_ptr<T>::operator=(std::forward<_>(rhs));
272 template<
typename SignalField,
typename Object,
typename Function>
273 inline _<T>& connect(SignalField signalField, Object
object, Function&&
function);
275 template<
typename SignalField,
typename Function>
276 inline _<T>& connect(SignalField signalField, Function&&
function);
279 template <
typename Functor>
280 inline _<T>& operator^(Functor&& functor) {
286 return super::operator->()->begin();
289 return super::operator->()->end();
292 return super::operator->()->begin();
295 return super::operator->()->end();
300 template<
typename Arg>
301 const _<T>& operator<<(Arg&& value)
const {
302 (*super::get()) << std::forward<Arg>(value);
306 template<
typename Arg>
307 _<T>& operator<<(Arg&& value) {
308 (*super::get()) << std::forward<Arg>(value);
312 template<
typename... Arg>
313 auto operator()(Arg&&... value)
const requires std::is_invocable_v<T, Arg...> {
314 return (*super::get())(std::forward<Arg>(value)...);
317 template<
typename Arg>
318 const _<T>& operator+(Arg&& value)
const {
319 (*super::get()) + std::forward<Arg>(value);
323 template<
typename Arg>
324 _<T>& operator+(Arg&& value) {
325 (*super::get()) + std::forward<Arg>(value);
329 template<
typename Arg>
330 const _<T>& operator*(Arg&& value) {
331 (*super::get()) * std::forward<Arg>(value);
336 std::add_lvalue_reference_t<T> operator*() const noexcept {
337 return super::operator*();
340 template<
typename Arg>
341 const _<T>& operator-(Arg&& value)
const {
342 (*super::get()) - std::forward<Arg>(value);
346 template<
typename Arg>
347 _<T>& operator-(Arg&& value) {
348 (*super::get()) - std::forward<Arg>(value);
351 template<
typename Arg>
352 _<T>& operator>>(Arg&& value) {
353 (*super::get()) >> std::forward<Arg>(value);
357 template<
typename...Args>
358 _<T>& operator()(
const Args&... value) {
359 (*super::get())(value...);
362 template<
typename...Args>
363 auto operator()(Args&&... value) {
364 return (*super::get())(std::forward<Args>(value)...);
372 return _<T>(raw, std::nullopt);
377 return _<T>(std::shared_ptr<void>{}, raw);
380 template<
typename T,
typename Deleter>
382 return _<T>(raw, deleter);
386 return _unique<T>(raw);
390template<
typename TO,
typename FROM>
391inline TO* _cast(
const _unique<FROM>&
object)
393 return dynamic_cast<TO*
>(
object.get());
396template<
typename TO,
typename FROM>
399 return std::dynamic_pointer_cast<TO, FROM>(
object);
402template<
typename TO,
typename FROM>
405 return std::dynamic_pointer_cast<TO, FROM>(std::move(
object));
408template<
typename TO,
typename FROM>
409inline _<TO> _cast(
const std::shared_ptr<FROM>&
object)
411 return std::dynamic_pointer_cast<TO, FROM>(
object);
415template<
typename TO,
typename FROM>
416inline _<TO> _cast(std::shared_ptr<FROM>&&
object)
418 return std::dynamic_pointer_cast<TO, FROM>(std::move(
object));
464#define AUI_NULLSAFE(s) if(decltype(auto) _tmp = (s))_tmp
A base object class.
Definition: AObject.h:49
Stacktrace consisting of a collection of stack function frames.
Definition: AStacktrace.h:28
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
_weak< T > weak() const
Definition: SharedPtrTypes.h:268
#define AUI_ASSERT(condition)
Asserts that the passed condition evaluates to true.
Definition: Assert.h:55
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:51
Utility wrapper implementing the stack-allocated (fast) pimpl idiom.
Definition: APimpl.h:31
Definition: SharedPtrTypes.h:114
static _unique< T > unique(T *raw)
Definition: SharedPtrTypes.h:385
static _unique< T, Deleter > make_unique_with_deleter(T *ptr, Deleter deleter=Deleter{})
Creates unique_ptr from raw pointer and a deleter.
Definition: SharedPtrTypes.h:125
static _< T > manage(T *raw)
Definition: SharedPtrTypes.h:371
static _< T > fake(T *raw)
Definition: SharedPtrTypes.h:376