Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:50

0001 #ifndef DataFormats_Common_CloningPtr_h
0002 #define DataFormats_Common_CloningPtr_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     Common
0006 // Class  :     CloningPtr
0007 //
0008 /**\class CloningPtr CloningPtr.h DataFormats/Common/interface/CloningPtr.h
0009 
0010  Description: A smart pointer that owns its pointer and clones it when necessary
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Mon Apr  3 16:43:29 EDT 2006
0019 //
0020 
0021 // system include files
0022 #include <algorithm>
0023 #include <memory>
0024 
0025 // user include files
0026 #include "DataFormats/Common/interface/ClonePolicy.h"
0027 
0028 // forward declarations
0029 namespace edm {
0030   template <class T, class P = ClonePolicy<T> >
0031   class CloningPtr {
0032   public:
0033     CloningPtr() : ptr_(nullptr) {}
0034     CloningPtr(const T& iPtr) : ptr_(P::clone(iPtr)) {}
0035     CloningPtr(std::unique_ptr<T> iPtr) : ptr_(iPtr.release()) {}
0036     CloningPtr(const CloningPtr<T, P>& iPtr) : ptr_(P::clone(*(iPtr.ptr_))) {}
0037 
0038     CloningPtr<T, P>& operator=(const CloningPtr<T, P>& iRHS) {
0039       CloningPtr<T, P> temp(iRHS);
0040       swap(temp);
0041       return *this;
0042     }
0043 
0044     void swap(CloningPtr<T, P>& iPtr) { std::swap(ptr_, iPtr.ptr_); }
0045 
0046     ~CloningPtr() { delete ptr_; }
0047 
0048     // ---------- const member functions ---------------------
0049     T& operator*() { return *ptr_; }
0050 
0051     T* operator->() { return ptr_; }
0052 
0053     T* get() { return ptr_; }
0054 
0055   private:
0056     T* ptr_;
0057   };
0058 
0059   // Free swap function
0060   template <class T, class P>
0061   inline void swap(CloningPtr<T, P>& a, CloningPtr<T, P>& b) {
0062     a.swap(b);
0063   }
0064 }  // namespace edm
0065 #endif