Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-12 03:09:56

0001 #ifndef Tracker_ProxyBase11_H
0002 #define Tracker_ProxyBase11_H
0003 
0004 #include "FWCore/Utilities/interface/Visibility.h"
0005 #include "FWCore/Utilities/interface/Likely.h"
0006 
0007 #ifdef TR_DEBUG
0008 #include <iostream>
0009 #endif
0010 
0011 #include "ChurnAllocator.h"
0012 
0013 /** A base class for reference counting proxies.
0014  *  The class to which this one is proxy must inherit from
0015  *  ReferenceCounted.
0016  *  A ProxyBase has value semantics, in contrast to ReferenceCountingPointer,
0017  *  which has pointer semantics.
0018  *  The Proxy class inheriting from ProxyBase must duplicate the
0019  *  part of the interface of the reference counted class that it whiches to expose.
0020  */
0021 
0022 template <class T>
0023 class ProxyBase11 {
0024 public:
0025   using pointer = std::shared_ptr<T>;
0026 
0027   // protected:
0028 
0029   ProxyBase11() {}
0030 
0031   explicit ProxyBase11(T* p) : theData(p) {}
0032   template <typename U>
0033   ProxyBase11(std::shared_ptr<U> p) : theData(std::move(p)) {}
0034   template <typename U>
0035   ProxyBase11& operator=(std::shared_ptr<U> p) {
0036     theData = std::move(p);
0037     return *this;
0038   }
0039 
0040   ~ProxyBase11() noexcept { destroy(); }
0041 
0042   void swap(ProxyBase11& other) noexcept { std::swap(theData, other.theData); }
0043 
0044   ProxyBase11(ProxyBase11&& other) noexcept = default;
0045   ProxyBase11& operator=(ProxyBase11&& other) noexcept = default;
0046   ProxyBase11(ProxyBase11 const& other) = default;
0047   ProxyBase11& operator=(const ProxyBase11& other) = default;
0048 
0049   void reset() { theData.reset(); }
0050 
0051   const T& data() const {
0052     check();
0053     return *theData;
0054   }
0055 
0056   T& unsharedData() {
0057     check();
0058     if (references() > 1) {
0059       theData = theData->clone();
0060     }
0061     return *theData;
0062   }
0063 
0064   T& sharedData() {
0065     check();
0066     return *theData;
0067   }
0068 
0069   bool isValid() const { return bool(theData); }
0070 
0071   void check() const {
0072 #ifdef TR_DEBUG
0073     if UNLIKELY (!theData)
0074       std::cout << "dead proxyBase11 " << references() << std::endl;
0075 #endif
0076   }
0077 
0078   void destroy() noexcept {}
0079   int references() const { return theData.use_count(); }
0080 
0081 private:
0082   std::shared_ptr<T> theData;
0083 };
0084 
0085 template <class T>
0086 inline void swap(ProxyBase11<T>& lh, ProxyBase11<T>& rh) noexcept {
0087   lh.swap(rh);
0088 }
0089 
0090 #endif  // Tracker_ProxyBase11_H