suanPan
Loading...
Searching...
No Matches
ResourceHolder.h
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2023 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> class ResourceHolder final {
32 std::unique_ptr<T> object = nullptr;
33
34public:
35 ResourceHolder() = default;
36
37 ResourceHolder& operator=(const std::shared_ptr<T>& original_object) {
38 object = original_object->make_copy();
39 return *this;
40 }
41
42 ResourceHolder(const ResourceHolder& old_holder)
43 : object(old_holder.object ? old_holder.object->make_copy() : nullptr) {}
44
45 ResourceHolder(ResourceHolder&&) noexcept = delete;
46 ResourceHolder& operator=(const ResourceHolder&) = delete;
47 ResourceHolder& operator=(ResourceHolder&&) noexcept = delete;
48 ~ResourceHolder() = default;
49
50 T* operator->() const { return object.get(); }
51
52 explicit operator bool() const { return object != nullptr; }
53};
54
55#endif
56
Definition: ResourceHolder.h:31
ResourceHolder()=default
ResourceHolder(const ResourceHolder &old_holder)
Definition: ResourceHolder.h:42
ResourceHolder(ResourceHolder &&) noexcept=delete
ResourceHolder & operator=(const std::shared_ptr< T > &original_object)
Definition: ResourceHolder.h:37