CMS 3D CMS Logo

mayown_ptr.h
Go to the documentation of this file.
1 #ifndef CMSSW_mayown_ptr_H
2 #define CMSSW_mayown_ptr_H
3 
4 #include <cassert>
5 #include <cstring>
6 
7 // a smart pointer which may own
8 // can be implemented trivially with shared_ptr
9 // this is infinetely ligher
10 // assume alignment > 2....
11 
12 template <typename T, int N = sizeof(T*)>
13 class mayown_ptr {
14 private:
15  T const* p = nullptr;
16 
17  void markOwn() {
18  if (nullptr == p)
19  return;
20  unsigned char buff[N];
21  memcpy(buff, &p, N);
22  assert((buff[N - 1] & 1) == 0);
23  ++buff[N - 1];
24  memcpy(&p, buff, N);
25  }
26 
27 public:
28  bool isOwn() const {
29  unsigned char buff[N];
30  memcpy(buff, &p, N);
31  return 1 == (buff[N - 1] & 1);
32  }
33 
34 private:
35  T const* pointer() const {
36  unsigned char buff[N];
37  memcpy(buff, &p, N);
38  buff[N - 1] &= 0xFE;
39  assert((buff[N - 1] & 1) == 0);
40  T const* np;
41  memcpy(&np, buff, N);
42  return np;
43  }
44 
45  void destroy() {
46  if (isOwn())
47  delete const_cast<T*>(pointer());
48  }
49 
50 public:
51  explicit mayown_ptr(T* ip = nullptr) : p(ip) { markOwn(); }
52  explicit mayown_ptr(T const& ip) : p(&ip) {}
54  mayown_ptr(mayown_ptr&) = delete;
55  mayown_ptr(mayown_ptr&& rh) : p(rh.p) { rh.p = nullptr; }
56  mayown_ptr& operator=(mayown_ptr&) = delete;
58  destroy();
59  p = rh.p;
60  rh.p = nullptr;
61  return *this;
62  }
63 
64  T const& operator*() const { return *pointer(); }
65  T const* operator->() const { return pointer(); }
66  T const* get() const { return pointer(); }
67  T const* release() {
68  auto np = pointer();
69  p = nullptr;
70  return np;
71  }
72  void reset() {
73  destroy();
74  p = nullptr;
75  }
76  void reset(T* ip) {
77  destroy();
78  p = ip;
79  markOwn();
80  }
81  void reset(T const& ip) {
82  destroy();
83  p = &ip;
84  }
85  bool empty() const { return nullptr == p; }
86 
87  T const* raw() const { return p; }
88 };
89 
90 template <typename T>
91 bool operator==(mayown_ptr<T> const& rh, mayown_ptr<T> const& lh) {
92  return rh.raw() == lh.raw();
93 }
94 template <typename T>
95 bool operator<(mayown_ptr<T> const& rh, mayown_ptr<T> const& lh) {
96  return rh.raw() < lh.raw();
97 }
98 
99 #endif
mayown_ptr & operator=(mayown_ptr &)=delete
T const * operator->() const
Definition: mayown_ptr.h:65
T const & operator*() const
Definition: mayown_ptr.h:64
bool operator==(mayown_ptr< T > const &rh, mayown_ptr< T > const &lh)
Definition: mayown_ptr.h:91
mayown_ptr(mayown_ptr &&rh)
Definition: mayown_ptr.h:55
void markOwn()
Definition: mayown_ptr.h:17
void reset(T *ip)
Definition: mayown_ptr.h:76
void destroy()
Definition: mayown_ptr.h:45
bool int lh
Definition: SIMDVec.h:27
void reset(T const &ip)
Definition: mayown_ptr.h:81
assert(be >=bs)
void reset()
Definition: mayown_ptr.h:72
T const * release()
Definition: mayown_ptr.h:67
mayown_ptr(T *ip=nullptr)
Definition: mayown_ptr.h:51
int np
Definition: AMPTWrapper.h:43
mayown_ptr & operator=(mayown_ptr &&rh)
Definition: mayown_ptr.h:57
T const * raw() const
Definition: mayown_ptr.h:87
#define N
Definition: blowfish.cc:9
bool empty() const
Definition: mayown_ptr.h:85
T const * p
Definition: mayown_ptr.h:15
bool isOwn() const
Definition: mayown_ptr.h:28
long double T
T const * pointer() const
Definition: mayown_ptr.h:35
mayown_ptr(T const &ip)
Definition: mayown_ptr.h:52