suanPan
🧮 An Open Source, Parallel and Heterogeneous Finite Element Analysis Framework
Loading...
Searching...
No Matches
ResourceHolder.h
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2026 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->unique_copy(); }
32class ResourceHolder final {
33 std::unique_ptr<T> object = nullptr;
34
35public:
36 ResourceHolder() = default;
37
39 : object(old.object ? old.object->unique_copy() : nullptr) {}
40
41 ResourceHolder(ResourceHolder&& old) noexcept { object = std::move(old.object); }
42
45 ~ResourceHolder() = default;
46
47 explicit ResourceHolder(std::unique_ptr<T>&& old)
48 : object(std::move(old)) {}
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>& old) {
56 if(old) object = old->unique_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==(std::nullptr_t null) const { return object == null; }
65};
66
67#endif
68
Definition ResourceHolder.h:32
ResourceHolder & operator=(const std::shared_ptr< T > &old)
Definition ResourceHolder.h:55
ResourceHolder()=default
T * operator->() const
Definition ResourceHolder.h:60
ResourceHolder & operator=(U &&other)
Definition ResourceHolder.h:50
~ResourceHolder()=default
ResourceHolder & operator=(ResourceHolder &&)=delete
bool operator==(std::nullptr_t null) const
Definition ResourceHolder.h:64
ResourceHolder(ResourceHolder &&old) noexcept
Definition ResourceHolder.h:41
ResourceHolder(std::unique_ptr< T > &&old)
Definition ResourceHolder.h:47
ResourceHolder(const ResourceHolder &old)
Definition ResourceHolder.h:38
ResourceHolder & operator=(const ResourceHolder &)=delete