Cppcheck
valueptr.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 //---------------------------------------------------------------------------
20 #ifndef valueptrH
21 #define valueptrH
22 //---------------------------------------------------------------------------
23 
24 #include "config.h"
25 
26 #include <memory>
27 
28 template<class T>
30  template<class U>
31  struct cloner {
32  static T* apply(const T* x) {
33  return new U(*static_cast<const U*>(x));
34  }
35  };
36 
37 public:
38  using pointer = T*;
39  using element_type = T;
40  using cloner_type = decltype(&cloner<T>::apply);
41 
42  ValuePtr() : mPtr(nullptr), mClone() {}
43 
44  template<class U>
45  // cppcheck-suppress noExplicitConstructor
46  // NOLINTNEXTLINE(google-explicit-constructor)
47  ValuePtr(const U& value) : mPtr(cloner<U>::apply(&value)), mClone(&cloner<U>::apply)
48  {}
49 
50  ValuePtr(const ValuePtr& rhs) : mPtr(nullptr), mClone(rhs.mClone) {
51  if (rhs) {
52  mPtr.reset(mClone(rhs.get()));
53  }
54  }
55  ValuePtr(ValuePtr&& rhs) NOEXCEPT : mPtr(std::move(rhs.mPtr)), mClone(std::move(rhs.mClone)) {}
56 
57  /**
58  * Releases the shared_ptr's ownership of the managed object using the .reset() function
59  */
60  void release() {
61  mPtr.reset();
62  }
63 
64  T* get() NOEXCEPT {
65  return mPtr.get();
66  }
67  const T* get() const NOEXCEPT {
68  return mPtr.get();
69  }
70 
71  T& operator*() {
72  return *get();
73  }
74  const T& operator*() const {
75  return *get();
76  }
77 
79  return get();
80  }
81  const T* operator->() const NOEXCEPT {
82  return get();
83  }
84 
85  void swap(ValuePtr& rhs) {
86  using std::swap;
87  swap(mPtr, rhs.mPtr);
88  swap(mClone, rhs.mClone);
89  }
90 
92  swap(rhs);
93  return *this;
94  }
95 
96  // NOLINTNEXTLINE(google-explicit-constructor)
97  operator bool() const NOEXCEPT {
98  return !!mPtr;
99  }
100 
101 private:
102  std::shared_ptr<T> mPtr;
104 };
105 
106 #endif
ValuePtr()
Definition: valueptr.h:42
ValuePtr(ValuePtr &&rhs) NOEXCEPT
Definition: valueptr.h:55
T * get() NOEXCEPT
Definition: valueptr.h:64
void release()
Releases the shared_ptr's ownership of the managed object using the .reset() function.
Definition: valueptr.h:60
const T & operator*() const
Definition: valueptr.h:74
ValuePtr(const ValuePtr &rhs)
Definition: valueptr.h:50
const T * get() const NOEXCEPT
Definition: valueptr.h:67
decltype(&cloner< T >::apply) cloner_type
Definition: valueptr.h:40
std::shared_ptr< T > mPtr
Definition: valueptr.h:102
ValuePtr< T > & operator=(ValuePtr rhs)
Definition: valueptr.h:91
T * operator->() NOEXCEPT
Definition: valueptr.h:78
T element_type
Definition: valueptr.h:39
T & operator*()
Definition: valueptr.h:71
const T * operator->() const NOEXCEPT
Definition: valueptr.h:81
void swap(ValuePtr &rhs)
Definition: valueptr.h:85
cloner_type mClone
Definition: valueptr.h:103
ValuePtr(const U &value)
Definition: valueptr.h:47
T * pointer
Definition: valueptr.h:38
#define NOEXCEPT
Definition: config.h:68
#define CPPCHECKLIB
Definition: config.h:35
static T * apply(const T *x)
Definition: valueptr.h:32