GetFEM  5.5
getfem_copyable_ptr.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2012-2026 Andriy Andreykiv
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 /**@file getfem_copyable_ptr.h
31 @author Andriy Andreykiv <andriy.andreykiv@gmail.com>
32 @date October 6th, 2015.
33 @brief A smart pointer that copies the value it points to on copy operations
34 */
35 #pragma once
36 
37 #include <memory>
38 #include <cstddef>
39 
40 namespace getfem {
41 
42 /**
43  A wrapper around a unique_ptr that clones the value on copy
44 */
45 template<class T> class copyable_ptr{
46  std::unique_ptr<T> p_ = std::unique_ptr<T>(nullptr);
47 public:
48  copyable_ptr() = default;
49 
50  copyable_ptr(std::unique_ptr<T> p) : p_(std::move(p)) {}
51 
53  : p_(x.p_ ? std::make_unique<T>(*x.p_) : nullptr) {}
54 
55  copyable_ptr(copyable_ptr<T> &&x) : p_(std::move(*x.p_)) {}
56 
57  copyable_ptr<T> &operator=(const copyable_ptr<T> &x){
58  if (x.p_) p_ = std::make_unique<T>(*x.p_);
59  return *this;
60  }
61 
62  copyable_ptr<T> &operator=(copyable_ptr<T> &&x){
63  p_ = std::move(x.p_);
64  return *this;
65  }
66 
67  operator bool() const {return p_.operator bool();}
68 
69  T& operator*() const {return p_.operator*();}
70 
71  T* operator->() const {return p_.operator->();}
72 };
73 
74 } // namespace getfem
A wrapper around a unique_ptr that clones the value on copy.
GEneric Tool for Finite Element Methods.