suanPan
Loading...
Searching...
No Matches
ResourceHolder.h
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2025 Theodore Chang
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
26#ifndef RESOURCEHOLDER_H
27#define RESOURCEHOLDER_H
28
29#include <memory>
30
31template<typename T> requires requires(T* copyable) { copyable->get_copy(); }
32class ResourceHolder final {
33 std::unique_ptr<T> object = nullptr;
34
35public:
36 ResourceHolder() = default;
37
38 ResourceHolder(const ResourceHolder& old_holder)
39 : object(old_holder ? old_holder.object->get_copy() : nullptr) {}
40
41 ResourceHolder(ResourceHolder&& old_holder) noexcept { object = std::move(old_holder.object); }
42
45 ~ResourceHolder() = default;
46
47 explicit ResourceHolder(std::unique_ptr<T>&& original_object)
48 : object(std::move(original_object)) {}
49
50 template<typename U> requires std::is_base_of_v<T, U> ResourceHolder& operator=(U&& other) {
51 object = std::make_unique<U>(std::forward<U>(other));
52 return *this;
53 }
54
55 ResourceHolder& operator=(const std::shared_ptr<T>& original_object) {
56 if(nullptr != original_object) object = original_object->get_copy();
57 return *this;
58 }
59
60 T* operator->() const { return object.get(); }
61
62 explicit operator bool() const { return object != nullptr; }
63
64 bool operator==(const ResourceHolder& other) const { return object == other.object; }
65
66 bool operator==(const T& other) const { return object == other; }
67
68 bool operator==(std::nullptr_t null) const { return object == null; }
69};
70
71#endif
72
Definition ResourceHolder.h:32
ResourceHolder()=default
ResourceHolder(std::unique_ptr< T > &&original_object)
Definition ResourceHolder.h:47
T * operator->() const
Definition ResourceHolder.h:60
ResourceHolder & operator=(U &&other)
Definition ResourceHolder.h:50
ResourceHolder(const ResourceHolder &old_holder)
Definition ResourceHolder.h:38
~ResourceHolder()=default
ResourceHolder & operator=(ResourceHolder &&)=delete
ResourceHolder & operator=(const std::shared_ptr< T > &original_object)
Definition ResourceHolder.h:55
bool operator==(const T &other) const
Definition ResourceHolder.h:66
bool operator==(std::nullptr_t null) const
Definition ResourceHolder.h:68
ResourceHolder(ResourceHolder &&old_holder) noexcept
Definition ResourceHolder.h:41
bool operator==(const ResourceHolder &other) const
Definition ResourceHolder.h:64
ResourceHolder & operator=(const ResourceHolder &)=delete