CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/DataFormats/Common/interface/CloningPtr.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_CloningPtr_h
00002 #define DataFormats_Common_CloningPtr_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     Common
00006 // Class  :     CloningPtr
00007 // 
00016 //
00017 // Original Author:  Chris Jones
00018 //         Created:  Mon Apr  3 16:43:29 EDT 2006
00019 // $Id: CloningPtr.h,v 1.6 2011/11/16 22:34:34 wmtan Exp $
00020 //
00021 
00022 // system include files
00023 #include <algorithm>
00024 #include <memory>
00025 
00026 // user include files
00027 #include "DataFormats/Common/interface/ClonePolicy.h"
00028 
00029 // forward declarations
00030 namespace edm {
00031   template< class T, class P = ClonePolicy<T> >
00032   class CloningPtr {
00033 public:
00034     CloningPtr(): ptr_(0) {}
00035     CloningPtr(const T& iPtr) : ptr_(P::clone(iPtr)) {}
00036     CloningPtr(std::auto_ptr<T> iPtr) : ptr_(iPtr.release()) {}
00037     CloningPtr(const CloningPtr<T,P>& iPtr) : ptr_(P::clone(*(iPtr.ptr_))) {}
00038     
00039     CloningPtr<T,P>& operator=(const CloningPtr<T,P>& iRHS) {
00040       CloningPtr<T,P> temp(iRHS);
00041       swap(temp);
00042       return *this;
00043     }
00044     
00045     void swap(CloningPtr<T,P>& iPtr) {
00046       std::swap(ptr_, iPtr.ptr_);
00047     }
00048     
00049     ~CloningPtr() { delete ptr_;}
00050     
00051     // ---------- const member functions ---------------------
00052     T& operator*() const { return *ptr_; }
00053     
00054     T* operator->() const { return ptr_; }
00055     
00056     T* get() const { return ptr_; }
00057     
00058 private:
00059     T* ptr_;
00060   };
00061 
00062   // Free swap function
00063   template <class T, class P>
00064   inline
00065   void
00066   swap(CloningPtr<T,P>& a, CloningPtr<T,P>& b) 
00067   {
00068     a.swap(b);
00069   }
00070 }
00071 #endif