CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
13 template<typename T, int N=sizeof(T*)>
14 class mayown_ptr {
15 private:
16  T const * p=nullptr;
17 
18  void markOwn() {
19  if (nullptr==p) return;
20  unsigned char buff[N]; memcpy(buff,&p,N);
21  assert((buff[N-1]&1)==0);
22  ++buff[N-1];
23  memcpy(&p,buff,N);
24  }
25 public:
26  bool isOwn() const {
27  unsigned char buff[N]; memcpy(buff,&p,N);
28  return 1==(buff[N-1]&1);
29  }
30 private:
31  T const * pointer() const {
32  unsigned char buff[N]; memcpy(buff,&p,N);
33  buff[N-1] &= 0xFE;
34  assert((buff[N-1]&1)==0);
35  T const * np;
36  memcpy(&np,buff,N);
37  return np;
38  }
39 
40  void destroy() {
41  if (isOwn()) delete const_cast<T*>(pointer());
42  }
43 public:
44  explicit mayown_ptr(T * ip=nullptr) : p(ip) { markOwn();}
45  explicit mayown_ptr(T const & ip) : p(&ip) {}
47  mayown_ptr(mayown_ptr &)=delete;
48  mayown_ptr(mayown_ptr && rh) : p(rh.p) { rh.p=nullptr;}
49  mayown_ptr& operator=(mayown_ptr &)=delete;
50  mayown_ptr& operator=(mayown_ptr && rh) { destroy(); p=rh.p; rh.p=nullptr; return *this;}
51 
52  T const & operator*() const { return *pointer();}
53  T const * operator->() const { return pointer();}
54  T const * get() const { return pointer();}
55  T const * release() { auto np=pointer(); p=nullptr; return np;}
56  void reset() { destroy(); p=nullptr;}
57  void reset(T * ip) { destroy(); p=ip; markOwn();}
58  void reset(T const & ip) { destroy(); p=&ip;}
59  bool empty() const { return nullptr==p;}
60 
61  T const * raw() const { return p;}
62 };
63 
64 template<typename T>
65 bool operator==(mayown_ptr<T> const & rh, mayown_ptr<T> const & lh) {
66  return rh.raw() == lh.raw();
67 }
68 template<typename T>
69 bool operator<(mayown_ptr<T> const & rh, mayown_ptr<T> const & lh) {
70  return rh.raw() < lh.raw();
71 }
72 #else
73 template<typename T, int N=sizeof(T*)>
74 class mayown_ptr {
75  mayown_ptr() : p(nullptr) {}
76  ~mayown_ptr() {}
77 private:
78  T const * p;
81 };
82 #endif
83 
84 #endif
mayown_ptr & operator=(mayown_ptr &)=delete
T const * operator->() const
Definition: mayown_ptr.h:53
bool empty() const
Definition: mayown_ptr.h:59
bool isOwn() const
Definition: mayown_ptr.h:26
T const & operator*() const
Definition: mayown_ptr.h:52
mayown_ptr(mayown_ptr &&rh)
Definition: mayown_ptr.h:48
assert(m_qm.get())
void markOwn()
Definition: mayown_ptr.h:18
void reset(T *ip)
Definition: mayown_ptr.h:57
void destroy()
Definition: mayown_ptr.h:40
bool int lh
Definition: SIMDVec.h:21
#define nullptr
void reset(T const &ip)
Definition: mayown_ptr.h:58
void reset()
Definition: mayown_ptr.h:56
T const * release()
Definition: mayown_ptr.h:55
mayown_ptr(T *ip=nullptr)
Definition: mayown_ptr.h:44
int np
Definition: AMPTWrapper.h:33
mayown_ptr & operator=(mayown_ptr &&rh)
Definition: mayown_ptr.h:50
bool operator==(const QGLikelihoodParameters &lhs, const QGLikelihoodCategory &rhs)
Test if parameters are compatible with category.
T const * raw() const
Definition: mayown_ptr.h:61
#define N
Definition: blowfish.cc:9
T const * p
Definition: mayown_ptr.h:16
T const * pointer() const
Definition: mayown_ptr.h:31
long double T
mayown_ptr(T const &ip)
Definition: mayown_ptr.h:45