My Project
Loading...
Searching...
No Matches
CopyablePtr.hpp
1/*
2 Copyright 2022 Equinor ASA.
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef OPM_COPYABLE_PTR_HPP
21#define OPM_COPYABLE_PTR_HPP
22namespace Opm {
23namespace Utility {
24// Wraps std::unique_ptr and makes it copyable.
25//
26// WARNING: This template should not be used with polymorphic classes.
27// That would require a virtual clone() method to be implemented.
28// It will only ever copy the static class type of the pointed to class.
29template <class T>
31public:
32 CopyablePtr() : ptr_(nullptr) {}
33 CopyablePtr(const CopyablePtr& other) {
34 if (other) { // other does not contain a nullptr
35 ptr_ = std::make_unique<T>(*other.get());
36 }
37 else {
38 ptr_ = nullptr;
39 }
40 }
41 // assignment operator
42 CopyablePtr<T>& operator=(const CopyablePtr<T>& other) {
43 if (other) {
44 ptr_ = std::make_unique<T>(*other.get());
45 }
46 else {
47 ptr_ = nullptr;
48 }
49 return *this;
50 }
51 // assign directly from a unique_ptr
52 CopyablePtr<T>& operator=(std::unique_ptr<T>&& uptr) {
53 ptr_ = std::move(uptr);
54 return *this;
55 }
56 // member access operator
57 T* operator->() const {return ptr_.get(); }
58 // boolean context operator
59 explicit operator bool() const noexcept {
60 return ptr_ ? true : false;
61 }
62 // get a pointer to the stored value
63 T* get() const {return ptr_.get();}
64 T* release() const {return ptr_.release();}
65private:
66 std::unique_ptr<T> ptr_;
67};
68
69} // namespace Utility
70} // namespace Opm
71#endif
Definition CopyablePtr.hpp:30
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30