CMS 3D CMS Logo

clone_ptr.h
Go to the documentation of this file.
1 #ifndef FWCore_Utilities_clone_ptr_h
2 #define FWCore_Utilities_clone_ptr_h
3 
4 #include <memory>
5 
6 namespace extstd {
7 
8  /* modify unique_ptr behaviour adding a "cloning" copy-constructor and assignment-operator
9  */
10  template <typename T>
11  struct clone_ptr {
12  clone_ptr() noexcept : ptr_() {}
13  template <typename... Args>
14  explicit clone_ptr(Args&&... args) noexcept : ptr_(std::forward<Args>(args)...) {}
15 
16  clone_ptr(clone_ptr const& rh) : ptr_(rh ? rh->clone() : nullptr) {}
17  clone_ptr(clone_ptr&& rh) noexcept : ptr_(std::move(rh.ptr_)) {}
18 
20  if (&rh != this)
21  this->reset(rh ? rh->clone() : nullptr);
22  return *this;
23  }
24  clone_ptr& operator=(clone_ptr&& rh) noexcept {
25  if (&rh != this)
26  ptr_ = std::move(rh.ptr_);
27  return *this;
28  }
29 
30  operator bool() const { return static_cast<bool>(ptr_); }
31 
32  template <typename U>
33  clone_ptr(clone_ptr<U> const& rh) : ptr_(rh ? rh->clone() : nullptr) {}
34  template <typename U>
35  clone_ptr(clone_ptr<U>&& rh) noexcept : ptr_(std::move(rh.ptr_)) {}
36 
37  template <typename U>
39  if (&rh != this)
40  this->reset(rh ? rh->clone() : nullptr);
41  return *this;
42  }
43  template <typename U>
44  clone_ptr& operator=(clone_ptr<U>&& rh) noexcept {
45  if (&rh != this)
46  ptr_ = std::move(rh.ptr_);
47  return *this;
48  }
49 
50  T* operator->() const { return ptr_.get(); }
51 
52  T& operator*() const { return *ptr_; }
53 
54  T* get() const { return ptr_.get(); }
55 
56  void reset(T* iValue) { ptr_.reset(iValue); }
57 
58  private:
59  std::unique_ptr<T> ptr_;
60  };
61 
62 } // namespace extstd
63 
64 #endif
clone_ptr & operator=(clone_ptr &&rh) noexcept
Definition: clone_ptr.h:24
clone_ptr & operator=(clone_ptr< U > const &rh)
Definition: clone_ptr.h:38
clone_ptr & operator=(clone_ptr const &rh)
Definition: clone_ptr.h:19
void reset(T *iValue)
Definition: clone_ptr.h:56
clone_ptr(clone_ptr const &rh)
Definition: clone_ptr.h:16
clone_ptr(clone_ptr< U > &&rh) noexcept
Definition: clone_ptr.h:35
std::unique_ptr< T > ptr_
Definition: clone_ptr.h:59
clone_ptr(clone_ptr< U > const &rh)
Definition: clone_ptr.h:33
T & operator*() const
Definition: clone_ptr.h:52
T * operator->() const
Definition: clone_ptr.h:50
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
clone_ptr & operator=(clone_ptr< U > &&rh) noexcept
Definition: clone_ptr.h:44
clone_ptr(Args &&... args) noexcept
Definition: clone_ptr.h:14
clone_ptr() noexcept
Definition: clone_ptr.h:12
long double T
def move(src, dest)
Definition: eostools.py:511
clone_ptr(clone_ptr &&rh) noexcept
Definition: clone_ptr.h:17