AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
SharedPtr.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14#include "SharedPtrTypes.h"
15
16#include <AUI/Common/AObject.h>
17#include <AUI/Thread/AThread.h>
18#include <AUI/Reflect/AClass.h>
19
20template<typename T, typename... Args>
21inline _<T> _new(Args&& ... args)
22{
23 return static_cast<_<T>>(std::make_shared<T>(std::forward<Args>(args)...));
24}
25
26template<typename T, typename E>
27inline _<T> _new(std::initializer_list<E> il) {
28
29 if constexpr (std::is_base_of_v<AObject, T>) {
30 auto o = new T(il.begin(), il.end());
31 return _<T>(o, [](T* obj)
32 {
33 static_cast<AObject*>(obj)->getThread()->enqueue([obj]()
34 {
35 obj->clearSignals();
36 static_cast<AObject*>(obj)->getThread()->enqueue([obj]()
37 {
38 delete obj;
39 });
40 });
41 });
42 }
43 else {
44 return static_cast<_<T>>(std::make_shared<T>(il.begin(), il.end()));
45 }
46}
47
48
49template<typename T>
50std::ostream& operator<<(std::ostream& os, const _<T>& rhs) noexcept {
51 return os << "[" << AClass<T>::name() << " " << rhs.get() << "]";
52}
53
54// gtest printer
55template<typename T>
56inline void PrintTo(const _<T>& ptr, std::ostream* stream) {
57 *stream << ptr;
58}
59
60template<typename T>
61template<typename SignalField, typename Object, typename Function>
62inline _<T>& _<T>::connect(SignalField signalField, Object object, Function&& function) {
63 AObject::connect(super::get()->*signalField, object, std::forward<Function>(function));
64 return *this;
65}
66
67template<typename T>
68template<typename SignalField, typename Function>
69inline _<T>& _<T>::connect(SignalField signalField, Function&& function) {
70 AObject::connect(super::get()->*signalField, super::get(), std::forward<Function>(function));
71 return *this;
72}
73
74
75#ifdef AUI_SHARED_PTR_FIND_INSTANCES
76template<typename T>
78 return AStacktrace::capture(3);
79}
80#endif
Definition: AClass.h:18
A base object class.
Definition: AObject.h:49
Stacktrace consisting of a collection of stack function frames.
Definition: AStacktrace.h:28
static AStacktrace capture(unsigned skipFrames=0, unsigned maxFrames=128) noexcept
Creates stacktrace of the current thread.
Definition: AStacktraceImpl.cpp:156
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
API_AUI_CORE const ACommandLineArgs & args() noexcept
Definition: OSAndroid.cpp:29