Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-07-02 00:53:41

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 {
0012     clone_ptr() noexcept : ptr_() {}
0013     template <typename... Args>
0014     explicit clone_ptr(Args&&... args) noexcept : ptr_(std::forward<Args>(args)...) {}
0015 
0016     clone_ptr(clone_ptr const& rh) : ptr_(rh ? rh->clone() : nullptr) {}
0017     clone_ptr(clone_ptr&& rh) noexcept : ptr_(std::move(rh.ptr_)) {}
0018 
0019     clone_ptr& operator=(clone_ptr const& rh) {
0020       if (&rh != this)
0021         this->reset(rh ? rh->clone() : nullptr);
0022       return *this;
0023     }
0024     clone_ptr& operator=(clone_ptr&& rh) noexcept {
0025       if (&rh != this)
0026         ptr_ = std::move(rh.ptr_);
0027       return *this;
0028     }
0029 
0030     operator bool() const { return static_cast<bool>(ptr_); }
0031 
0032     template <typename U>
0033     clone_ptr(clone_ptr<U> const& rh) : ptr_(rh ? rh->clone() : nullptr) {}
0034     template <typename U>
0035     clone_ptr(clone_ptr<U>&& rh) noexcept : ptr_(std::move(rh.ptr_)) {}
0036 
0037     template <typename U>
0038     clone_ptr& operator=(clone_ptr<U> const& rh) {
0039       if (&rh != this)
0040         this->reset(rh ? rh->clone() : nullptr);
0041       return *this;
0042     }
0043     template <typename U>
0044     clone_ptr& operator=(clone_ptr<U>&& rh) noexcept {
0045       if (&rh != this)
0046         ptr_ = std::move(rh.ptr_);
0047       return *this;
0048     }
0049 
0050     T* operator->() const { return ptr_.get(); }
0051 
0052     T& operator*() const { return *ptr_; }
0053 
0054     T* get() const { return ptr_.get(); }
0055 
0056     void reset(T* iValue) { ptr_.reset(iValue); }
0057 
0058   private:
0059     std::unique_ptr<T> ptr_;
0060   };
0061 
0062 }  // namespace extstd
0063 
0064 #endif