CaloCellGeometryMayOwnPtr

no_delete

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#ifndef Geometry_CaloGeometry_CaloCellGeometryMayOwnPtr_h
#define Geometry_CaloGeometry_CaloCellGeometryMayOwnPtr_h
// -*- C++ -*-
//
// Package:     Geometry/CaloGeometry
// Class  :     CaloCellGeometryMayOwnPtr
//
/**\class CaloCellGeometryMayOwnPtr CaloCellGeometryMayOwnPtr.h "Geometry/CaloGeometry/interface/CaloCellGeometryMayOwnPtr.h"

 Description: Type to hold pointer to CaloCellGeometry with possible ownership

 Usage:
    Used to either have single ownership or no ownership of the CaloCellGeometry

*/
//
// Original Author:  Christopher Jones
//         Created:  Wed, 23 Oct 2024 19:50:05 GMT
//

// system include files
#include <memory>

// user include files
#include "Geometry/CaloGeometry/interface/CaloCellGeometryPtr.h"
#include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"

// forward declarations

class CaloCellGeometryMayOwnPtr {
public:
  explicit CaloCellGeometryMayOwnPtr(std::unique_ptr<CaloCellGeometry const> iPtr) noexcept
      : ptr_{iPtr.release()}, own_{ptr_ != nullptr} {
    if (own_) {
      ptr_->increment();
    }
  }
  explicit CaloCellGeometryMayOwnPtr(CaloCellGeometryPtr const& iPtr) noexcept : ptr_{iPtr.get()}, own_{false} {}

  ~CaloCellGeometryMayOwnPtr() noexcept {
    if (own_ and ptr_->decrement()) {
      delete ptr_;
    }
  }
  CaloCellGeometryMayOwnPtr() noexcept = default;
  CaloCellGeometryMayOwnPtr(const CaloCellGeometryMayOwnPtr& iPtr) noexcept : ptr_{iPtr.ptr_}, own_{iPtr.own_} {
    if (own_) {
      ptr_->increment();
    }
  }
  CaloCellGeometryMayOwnPtr(CaloCellGeometryMayOwnPtr&& iPtr) noexcept : ptr_{iPtr.ptr_}, own_{iPtr.own_} {
    iPtr.ptr_ = nullptr;
    iPtr.own_ = false;
  }
  CaloCellGeometryMayOwnPtr& operator=(CaloCellGeometryMayOwnPtr const& iPtr) noexcept {
    //Even if someone does `foo = foo` this will work
    auto tmpPtr = iPtr.ptr_;
    auto tmpOwn = iPtr.own_;
    CaloCellGeometryMayOwnPtr temp(std::move(*this));
    ptr_ = tmpPtr;
    own_ = tmpOwn;
    if (own_) {
      ptr_->increment();
    }
    return *this;
  }
  CaloCellGeometryMayOwnPtr& operator=(CaloCellGeometryMayOwnPtr&& iPtr) noexcept {
    if (&iPtr != this) {
      CaloCellGeometryMayOwnPtr temp(std::move(*this));

      ptr_ = iPtr.ptr_;
      own_ = iPtr.own_;
      iPtr.ptr_ = nullptr;
      iPtr.own_ = false;
    }
    return *this;
  }

  CaloCellGeometry const* operator->() const { return ptr_; }
  CaloCellGeometry const* get() const { return ptr_; }
  CaloCellGeometry const& operator*() const { return *ptr_; }

  operator CaloCellGeometry const*() const { return ptr_; }

private:
  struct no_delete {
    void operator()(const void*) {}
  };

  const CaloCellGeometry* ptr_ = nullptr;
  bool own_ = false;
};

#endif