AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AComPtr.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
15#include <cassert>
16
22template<typename T>
23class AComPtr {
24public:
25
26 AComPtr() {
27 mValue = nullptr;
28 }
29
30 ~AComPtr() {
31 if (mValue) {
32 mValue->Release();
33 }
34 }
35
36 T** operator&() noexcept {
37 AUI_ASSERTX(mValue == nullptr, "value already set");
38 return &mValue;
39 }
40
41 AComPtr(T* value): mValue(value) {}
42 AComPtr(const AComPtr<T>& rhs): mValue(rhs.mValue) {
43 if (mValue) {
44 mValue->AddRef();
45 }
46 }
47
48 AComPtr(AComPtr<T>&& rhs): mValue(rhs.mValue) {
49 rhs.mValue = nullptr;
50 }
51
52 [[nodiscard]]
53 T* value() const noexcept {
54 AUI_ASSERT(mValue != nullptr);
55 return mValue;
56 }
57
58 [[nodiscard]]
59 T* operator*() const noexcept {
60 return value();
61 }
62
63 [[nodiscard]]
64 T* operator->() const noexcept {
65 return value();
66 }
67
68
69 [[nodiscard]]
70 operator T*() const noexcept {
71 return value();
72 }
73
74 [[nodiscard]]
75 operator bool() const noexcept {
76 return mValue != nullptr;
77 }
78
79private:
80 T* mValue;
81};
82
83
#define AUI_ASSERT(condition)
Asserts that the passed condition evaluates to true.
Definition Assert.h:55
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74