Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:47:59

0001 #ifndef FWCore_Utilities_clone_ptr_h
0002 #define FWCore_Utilities_clone_ptr_h
0003 
0004 #include <memory>
0005 
0006 namespace extstd {
0007 
0008   /* modify unique_ptr behaviour adding a "cloning" copy-constructor and assignment-operator
0009    */
0010   template <typename T>
0011   struct clone_ptr : public std::unique_ptr<T> {
0012     template <typename... Args>
0013     explicit clone_ptr(Args&&... args) noexcept : std::unique_ptr<T>(std::forward<Args>(args)...) {}
0014 
0015     clone_ptr(clone_ptr const& rh) : std::unique_ptr<T>(rh ? rh->clone() : nullptr) {}
0016     clone_ptr(clone_ptr&& rh) noexcept : std::unique_ptr<T>(std::move(rh)) {}
0017 
0018     clone_ptr& operator=(clone_ptr const& rh) {
0019       if (&rh != this)
0020         this->reset(rh ? rh->clone() : nullptr);
0021       return *this;
0022     }
0023     clone_ptr& operator=(clone_ptr&& rh) noexcept {
0024       if (&rh != this)
0025         std::unique_ptr<T>::operator=(std::move(rh));
0026       return *this;
0027     }
0028 
0029     template <typename U>
0030     clone_ptr(clone_ptr<U> const& rh) : std::unique_ptr<T>(rh ? rh->clone() : nullptr) {}
0031     template <typename U>
0032     clone_ptr(clone_ptr<U>&& rh) noexcept : std::unique_ptr<T>(std::move(rh)) {}
0033 
0034     template <typename U>
0035     clone_ptr& operator=(clone_ptr<U> const& rh) {
0036       if (&rh != this)
0037         this->reset(rh ? rh->clone() : nullptr);
0038       return *this;
0039     }
0040     template <typename U>
0041     clone_ptr& operator=(clone_ptr<U>&& rh) noexcept {
0042       if (&rh != this)
0043         std::unique_ptr<T>::operator=(std::move(rh));
0044       return *this;
0045     }
0046   };
0047 
0048 }  // namespace extstd
0049 
0050 #endif