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 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) return;
19  unsigned char buff[N]; memcpy(buff,&p,N);
20  assert((buff[N-1]&1)==0);
21  ++buff[N-1];
22  memcpy(&p,buff,N);
23  }
24 public:
25  bool isOwn() const {
26  unsigned char buff[N]; memcpy(buff,&p,N);
27  return 1==(buff[N-1]&1);
28  }
29 private:
30  T const * pointer() const {
31  unsigned char buff[N]; memcpy(buff,&p,N);
32  buff[N-1] &= 0xFE;
33  assert((buff[N-1]&1)==0);
34  T const * np;
35  memcpy(&np,buff,N);
36  return np;
37  }
38 
39  void destroy() {
40  if (isOwn()) delete const_cast<T*>(pointer());
41  }
42 public:
43  explicit mayown_ptr(T * ip=nullptr) : p(ip) { markOwn();}
44  explicit mayown_ptr(T const & ip) : p(&ip) {}
46  mayown_ptr(mayown_ptr &)=delete;
47  mayown_ptr(mayown_ptr && rh) : p(rh.p) { rh.p=nullptr;}
48  mayown_ptr& operator=(mayown_ptr &)=delete;
49  mayown_ptr& operator=(mayown_ptr && rh) { destroy(); p=rh.p; rh.p=nullptr; return *this;}
50 
51  T const & operator*() const { return *pointer();}
52  T const * operator->() const { return pointer();}
53  T const * get() const { return pointer();}
54  T const * release() { auto np=pointer(); p=nullptr; return np;}
55  void reset() { destroy(); p=nullptr;}
56  void reset(T * ip) { destroy(); p=ip; markOwn();}
57  void reset(T const & ip) { destroy(); p=&ip;}
58  bool empty() const { return nullptr==p;}
59 
60  T const * raw() const { return p;}
61 };
62 
63 template<typename T>
64 bool operator==(mayown_ptr<T> const & rh, mayown_ptr<T> const & lh) {
65  return rh.raw() == lh.raw();
66 }
67 template<typename T>
68 bool operator<(mayown_ptr<T> const & rh, mayown_ptr<T> const & lh) {
69  return rh.raw() < lh.raw();
70 }
71 
72 #endif
mayown_ptr & operator=(mayown_ptr &)=delete
T const * operator->() const
Definition: mayown_ptr.h:52
bool empty() const
Definition: mayown_ptr.h:58
bool isOwn() const
Definition: mayown_ptr.h:25
T const & operator*() const
Definition: mayown_ptr.h:51
mayown_ptr(mayown_ptr &&rh)
Definition: mayown_ptr.h:47
assert(m_qm.get())
void markOwn()
Definition: mayown_ptr.h:17
void reset(T *ip)
Definition: mayown_ptr.h:56
void destroy()
Definition: mayown_ptr.h:39
bool int lh
Definition: SIMDVec.h:21
void reset(T const &ip)
Definition: mayown_ptr.h:57
void reset()
Definition: mayown_ptr.h:55
T const * release()
Definition: mayown_ptr.h:54
mayown_ptr(T *ip=nullptr)
Definition: mayown_ptr.h:43
int np
Definition: AMPTWrapper.h:33
mayown_ptr & operator=(mayown_ptr &&rh)
Definition: mayown_ptr.h:49
bool operator==(const QGLikelihoodParameters &lhs, const QGLikelihoodCategory &rhs)
Test if parameters are compatible with category.
T const * raw() const
Definition: mayown_ptr.h:60
#define N
Definition: blowfish.cc:9
T const * p
Definition: mayown_ptr.h:15
T const * pointer() const
Definition: mayown_ptr.h:30
long double T
mayown_ptr(T const &ip)
Definition: mayown_ptr.h:44