suanPan
ResourceHolder.h
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2024 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 explicit ResourceHolder(std::unique_ptr<T>&& obj)
39 : object(std::forward<std::unique_ptr<T>>(obj)) {}
40
41 ResourceHolder& operator=(const std::shared_ptr<T>& original_object) {
42 object = original_object->get_copy();
43 return *this;
44 }
45
46 ResourceHolder& operator=(std::unique_ptr<T>&& original_object) {
47 object = std::forward<std::unique_ptr<T>>(original_object);
48 return *this;
49 }
50
51 ResourceHolder(const ResourceHolder& old_holder)
52 : object(old_holder.object ? old_holder.object->get_copy() : nullptr) {}
53
54 ResourceHolder(ResourceHolder&& old_holder) noexcept { object = std::move(old_holder.object); }
55
58 ~ResourceHolder() = default;
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 & operator=(ResourceHolder &&) noexcept=delete
ResourceHolder()=default
ResourceHolder(std::unique_ptr< T > &&obj)
Definition: ResourceHolder.h:38
ResourceHolder(const ResourceHolder &old_holder)
Definition: ResourceHolder.h:51
ResourceHolder & operator=(const std::shared_ptr< T > &original_object)
Definition: ResourceHolder.h:41
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:54
bool operator==(const ResourceHolder &other) const
Definition: ResourceHolder.h:64
ResourceHolder & operator=(std::unique_ptr< T > &&original_object)
Definition: ResourceHolder.h:46
ResourceHolder & operator=(const ResourceHolder &)=delete