CloningPtr

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#ifndef DataFormats_Common_CloningPtr_h
#define DataFormats_Common_CloningPtr_h
// -*- C++ -*-
//
// Package:     Common
// Class  :     CloningPtr
//
/**\class CloningPtr CloningPtr.h DataFormats/Common/interface/CloningPtr.h

 Description: A smart pointer that owns its pointer and clones it when necessary

 Usage:
    <usage>

*/
//
// Original Author:  Chris Jones
//         Created:  Mon Apr  3 16:43:29 EDT 2006
//

// system include files
#include <algorithm>
#include <memory>

// user include files
#include "DataFormats/Common/interface/ClonePolicy.h"

// forward declarations
namespace edm {
  template <class T, class P = ClonePolicy<T> >
  class CloningPtr {
  public:
    CloningPtr() : ptr_(nullptr) {}
    CloningPtr(const T& iPtr) : ptr_(P::clone(iPtr)) {}
    CloningPtr(std::unique_ptr<T> iPtr) : ptr_(iPtr.release()) {}
    CloningPtr(const CloningPtr<T, P>& iPtr) : ptr_(P::clone(*(iPtr.ptr_))) {}

    CloningPtr<T, P>& operator=(const CloningPtr<T, P>& iRHS) {
      CloningPtr<T, P> temp(iRHS);
      swap(temp);
      return *this;
    }

    void swap(CloningPtr<T, P>& iPtr) { std::swap(ptr_, iPtr.ptr_); }

    ~CloningPtr() { delete ptr_; }

    // ---------- const member functions ---------------------
    T& operator*() { return *ptr_; }

    T* operator->() { return ptr_; }

    T* get() { return ptr_; }

  private:
    T* ptr_;
  };

  // Free swap function
  template <class T, class P>
  inline void swap(CloningPtr<T, P>& a, CloningPtr<T, P>& b) {
    a.swap(b);
  }
}  // namespace edm
#endif