Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-29 06:08:24

0001 #ifndef Geometry_CaloGeometry_CaloCellGeometryMayOwnPtr_h
0002 #define Geometry_CaloGeometry_CaloCellGeometryMayOwnPtr_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     Geometry/CaloGeometry
0006 // Class  :     CaloCellGeometryMayOwnPtr
0007 //
0008 /**\class CaloCellGeometryMayOwnPtr CaloCellGeometryMayOwnPtr.h "Geometry/CaloGeometry/interface/CaloCellGeometryMayOwnPtr.h"
0009 
0010  Description: Type to hold pointer to CaloCellGeometry with possible ownership
0011 
0012  Usage:
0013     Used to either have single ownership or no ownership of the CaloCellGeometry
0014 
0015 */
0016 //
0017 // Original Author:  Christopher Jones
0018 //         Created:  Wed, 23 Oct 2024 19:50:05 GMT
0019 //
0020 
0021 // system include files
0022 #include <memory>
0023 
0024 // user include files
0025 #include "Geometry/CaloGeometry/interface/CaloCellGeometryPtr.h"
0026 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
0027 
0028 // forward declarations
0029 
0030 class CaloCellGeometryMayOwnPtr {
0031 public:
0032   explicit CaloCellGeometryMayOwnPtr(std::unique_ptr<CaloCellGeometry const> iPtr) noexcept
0033       : ptr_{iPtr.release()}, own_{ptr_ != nullptr} {
0034     if (own_) {
0035       ptr_->increment();
0036     }
0037   }
0038   explicit CaloCellGeometryMayOwnPtr(CaloCellGeometryPtr const& iPtr) noexcept : ptr_{iPtr.get()}, own_{false} {}
0039 
0040   ~CaloCellGeometryMayOwnPtr() noexcept {
0041     if (own_ and ptr_->decrement()) {
0042       delete ptr_;
0043     }
0044   }
0045   CaloCellGeometryMayOwnPtr() noexcept = default;
0046   CaloCellGeometryMayOwnPtr(const CaloCellGeometryMayOwnPtr& iPtr) noexcept : ptr_{iPtr.ptr_}, own_{iPtr.own_} {
0047     if (own_) {
0048       ptr_->increment();
0049     }
0050   }
0051   CaloCellGeometryMayOwnPtr(CaloCellGeometryMayOwnPtr&& iPtr) noexcept : ptr_{iPtr.ptr_}, own_{iPtr.own_} {
0052     iPtr.ptr_ = nullptr;
0053     iPtr.own_ = false;
0054   }
0055   CaloCellGeometryMayOwnPtr& operator=(CaloCellGeometryMayOwnPtr const& iPtr) noexcept {
0056     //Even if someone does `foo = foo` this will work
0057     auto tmpPtr = iPtr.ptr_;
0058     auto tmpOwn = iPtr.own_;
0059     CaloCellGeometryMayOwnPtr temp(std::move(*this));
0060     ptr_ = tmpPtr;
0061     own_ = tmpOwn;
0062     if (own_) {
0063       ptr_->increment();
0064     }
0065     return *this;
0066   }
0067   CaloCellGeometryMayOwnPtr& operator=(CaloCellGeometryMayOwnPtr&& iPtr) noexcept {
0068     if (&iPtr != this) {
0069       CaloCellGeometryMayOwnPtr temp(std::move(*this));
0070 
0071       ptr_ = iPtr.ptr_;
0072       own_ = iPtr.own_;
0073       iPtr.ptr_ = nullptr;
0074       iPtr.own_ = false;
0075     }
0076     return *this;
0077   }
0078 
0079   CaloCellGeometry const* operator->() const { return ptr_; }
0080   CaloCellGeometry const* get() const { return ptr_; }
0081   CaloCellGeometry const& operator*() const { return *ptr_; }
0082 
0083   operator CaloCellGeometry const*() const { return ptr_; }
0084 
0085 private:
0086   struct no_delete {
0087     void operator()(const void*) {}
0088   };
0089 
0090   const CaloCellGeometry* ptr_ = nullptr;
0091   bool own_ = false;
0092 };
0093 
0094 #endif