Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:11

0001 #ifndef FWCore_Utilities_ConstRespectingPtr_h
0002 #define FWCore_Utilities_ConstRespectingPtr_h
0003 
0004 // Package:     FWCore/Utilities
0005 // Class  :     ConstRespectingPtr
0006 //
0007 /**\class edm::ConstRespectingPtr
0008 
0009  Description: When this is a member of a class, const functions of the class
0010 can only call const functions of the object it points at. This aids in using
0011 the compiler to help maintain thread safety of const functions.
0012 
0013 Usage: WARNING: member data which uses this class must be made transient in
0014 the classes_def.xml file if it is a member of a persistent class!
0015 
0016 */
0017 // Original Author:  W. David Dagenhart
0018 //         Created:  20 March 2014
0019 
0020 #include <memory>
0021 
0022 #include "FWCore/Utilities/interface/propagate_const.h"
0023 
0024 namespace edm {
0025 
0026   template <typename T>
0027   class ConstRespectingPtr {
0028   public:
0029     ConstRespectingPtr();
0030     explicit ConstRespectingPtr(T*);
0031     ConstRespectingPtr(ConstRespectingPtr<T> const&) = delete;
0032     ConstRespectingPtr& operator=(ConstRespectingPtr<T> const&) = delete;
0033     ~ConstRespectingPtr();
0034 
0035     T const* operator->() const { return m_data; }
0036     T const& operator*() const { return *m_data; }
0037     T const* get() const { return m_data; }
0038 
0039     T* operator->() { return m_data; }
0040     T& operator*() { return *m_data; }
0041     T* get() { return m_data; }
0042 
0043     bool isSet() const;
0044 
0045     void set(std::unique_ptr<T> iNewValue);
0046 
0047     T* release();
0048     void reset();
0049 
0050   private:
0051     edm::propagate_const<T*> m_data;
0052   };
0053 
0054   template <typename T>
0055   ConstRespectingPtr<T>::ConstRespectingPtr() : m_data(nullptr) {}
0056 
0057   template <typename T>
0058   ConstRespectingPtr<T>::ConstRespectingPtr(T* v) : m_data(v) {}
0059 
0060   template <typename T>
0061   ConstRespectingPtr<T>::~ConstRespectingPtr() {
0062     delete m_data.get();
0063   }
0064 
0065   template <typename T>
0066   bool ConstRespectingPtr<T>::isSet() const {
0067     return nullptr != m_data;
0068   }
0069 
0070   template <typename T>
0071   void ConstRespectingPtr<T>::set(std::unique_ptr<T> iNewValue) {
0072     delete m_data;
0073     m_data = iNewValue.release();
0074   }
0075 
0076   template <typename T>
0077   T* ConstRespectingPtr<T>::release() {
0078     T* tmp = m_data;
0079     m_data = nullptr;
0080     return tmp;
0081   }
0082 
0083   template <typename T>
0084   void ConstRespectingPtr<T>::reset() {
0085     delete m_data;
0086     m_data = nullptr;
0087   }
0088 }  // namespace edm
0089 #endif