Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:33

0001 #ifndef CondFormats_SerializationHelper_unique_void_ptr_h
0002 #define CondFormats_SerializationHelper_unique_void_ptr_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     CondFormats/SerializationHelper
0006 // Class  :     unique_void_ptr
0007 //
0008 /**\class unique_void_ptr unique_void_ptr.h "CondFormats/SerializationHelper/interface/unique_void_ptr.h"
0009 
0010  Description: Provides ownership of a const void*
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Christopher Jones
0018 //         Created:  Wed, 31 May 2023 14:34:15 GMT
0019 //
0020 
0021 // system include files
0022 #include <functional>
0023 
0024 // user include files
0025 
0026 // forward declarations
0027 
0028 namespace cond::serialization {
0029   class unique_void_ptr {
0030   public:
0031     unique_void_ptr() noexcept : ptr_{nullptr} {}
0032     unique_void_ptr(const void* iPtr, std::function<void(const void*)> iDestructor) noexcept
0033         : ptr_(iPtr), destructor_(std::move(iDestructor)) {}
0034 
0035     ~unique_void_ptr() noexcept {
0036       if (destructor_) {
0037         destructor_(ptr_);
0038       }
0039     }
0040 
0041     unique_void_ptr(const unique_void_ptr&) = delete;             // stop default
0042     unique_void_ptr& operator=(const unique_void_ptr&) = delete;  // stop default
0043 
0044     unique_void_ptr(unique_void_ptr&& iOld) noexcept : ptr_(iOld.ptr_), destructor_(std::move(iOld.destructor_)) {
0045       iOld.ptr_ = nullptr;
0046     }
0047     unique_void_ptr& operator=(unique_void_ptr&& iOld) noexcept {
0048       unique_void_ptr tmp(std::move(*this));
0049       std::swap(ptr_, iOld.ptr_);
0050       std::swap(destructor_, iOld.destructor_);
0051       return *this;
0052     }
0053     // ---------- const member functions ---------------------
0054     const void* get() const noexcept { return ptr_; }
0055 
0056     // ---------- member functions --------------------------------
0057     const void* release() noexcept {
0058       auto tmp = ptr_;
0059       ptr_ = nullptr;
0060       return tmp;
0061     }
0062 
0063   private:
0064     // ---------- member data --------------------------------
0065     const void* ptr_;
0066     std::function<void(const void*)> destructor_;
0067   };
0068 }  // namespace cond::serialization
0069 #endif