File indexing completed on 2024-04-06 12:03:50
0001 #ifndef DataFormats_Common_CloningPtr_h
0002 #define DataFormats_Common_CloningPtr_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <algorithm>
0023 #include <memory>
0024
0025
0026 #include "DataFormats/Common/interface/ClonePolicy.h"
0027
0028
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
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
0060 template <class T, class P>
0061 inline void swap(CloningPtr<T, P>& a, CloningPtr<T, P>& b) {
0062 a.swap(b);
0063 }
0064 }
0065 #endif