CMS 3D CMS Logo

ProxyBase11.h
Go to the documentation of this file.
1 #ifndef Tracker_ProxyBase11_H
2 #define Tracker_ProxyBase11_H
3 
6 
7 #ifdef TR_DEBUG
8 #include <iostream>
9 #endif
10 
11 #include "ChurnAllocator.h"
12 
22 template <class T>
23 class ProxyBase11 {
24 public:
25  using pointer = std::shared_ptr<T>;
26 
27  // protected:
28 
30 
31  explicit ProxyBase11(T* p) : theData(p) {}
32  template <typename U>
33  ProxyBase11(std::shared_ptr<U> p) : theData(std::move(p)) {}
34  template <typename U>
35  ProxyBase11& operator=(std::shared_ptr<U> p) {
36  theData = std::move(p);
37  return *this;
38  }
39 
40  ~ProxyBase11() noexcept { destroy(); }
41 
42  void swap(ProxyBase11& other) noexcept { std::swap(theData, other.theData); }
43 
44  ProxyBase11(ProxyBase11&& other) noexcept = default;
45  ProxyBase11& operator=(ProxyBase11&& other) noexcept = default;
46  ProxyBase11(ProxyBase11 const& other) = default;
47  ProxyBase11& operator=(const ProxyBase11& other) = default;
48 
49  void reset() { theData.reset(); }
50 
51  const T& data() const {
52  check();
53  return *theData;
54  }
55 
57  check();
58  if (references() > 1) {
59  theData = theData->clone();
60  }
61  return *theData;
62  }
63 
65  check();
66  return *theData;
67  }
68 
69  bool isValid() const { return bool(theData); }
70 
71  void check() const {
72 #ifdef TR_DEBUG
73  if UNLIKELY (!theData)
74  std::cout << "dead proxyBase11 " << references() << std::endl;
75 #endif
76  }
77 
78  void destroy() noexcept {}
79  int references() const { return theData.use_count(); }
80 
81 private:
82  std::shared_ptr<T> theData;
83 };
84 
85 template <class T>
86 inline void swap(ProxyBase11<T>& lh, ProxyBase11<T>& rh) noexcept {
87  lh.swap(rh);
88 }
89 
90 #endif // Tracker_ProxyBase11_H
void check() const
Definition: ProxyBase11.h:71
ProxyBase11(std::shared_ptr< U > p)
Definition: ProxyBase11.h:33
void reset()
Definition: ProxyBase11.h:49
int references() const
Definition: ProxyBase11.h:79
std::shared_ptr< T > theData
Definition: ProxyBase11.h:82
bool int lh
Definition: SIMDVec.h:20
std::shared_ptr< T > pointer
Definition: ProxyBase11.h:25
ProxyBase11 & operator=(std::shared_ptr< U > p)
Definition: ProxyBase11.h:35
ProxyBase11(T *p)
Definition: ProxyBase11.h:31
void swap(ProxyBase11 &other) noexcept
Definition: ProxyBase11.h:42
bool isValid() const
Definition: ProxyBase11.h:69
T & unsharedData()
Definition: ProxyBase11.h:56
const T & data() const
Definition: ProxyBase11.h:51
T & sharedData()
Definition: ProxyBase11.h:64
~ProxyBase11() noexcept
Definition: ProxyBase11.h:40
void destroy() noexcept
Definition: ProxyBase11.h:78
#define UNLIKELY(x)
Definition: Likely.h:21
long double T
void swap(ProxyBase11< T > &lh, ProxyBase11< T > &rh) noexcept
Definition: ProxyBase11.h:86
def move(src, dest)
Definition: eostools.py:511