BOSS 7.0.9
BESIII Offline Software System
Loading...
Searching...
No Matches
BesEvtGen-00-04-08/src/phokhara/eemmg-lib/src/pointer.h
Go to the documentation of this file.
1/*
2 * pointer.h - smart pointer and array classes
3 *
4 * this file is part of PJFry library
5 * Copyright 2011 Valery Yundin
6 */
7
8#ifndef QUL_POINTER_H
9#define QUL_POINTER_H
10
11#include <memory>
12#include <cstring>
13
14class SRefCnt
15{
16 public:
17 SRefCnt() : count(0) { }
18
19 protected:
20 int count;
21};
22
23template <typename T>
24class SPtr
25{
26 public:
27 T* operator-> () const { return pObj; }
28 bool operator== (const T* pobj) const { return pobj==pObj; }
29 bool operator!= (const T* pobj) const { return pobj!=pObj; }
30
31 bool operator== (const SPtr<T> &spobj) const { return spobj.pObj==pObj; }
32 bool operator!= (const SPtr<T> &spobj) const { return spobj.pObj!=pObj; }
33
34 SPtr(T* pobj=0) : pObj(pobj) {
35 if (pObj) { pObj->count++; }
36 }
37 SPtr(const SPtr &ptr) : pObj(ptr.pObj) {
38 if (pObj) { pObj->count++; }
39 }
40
41 SPtr& operator= (const SPtr& ptr) {
42 if (this == &ptr) { return *this; }
43 if (pObj && --(pObj->count) == 0) { delete pObj; }
44 if ( (pObj=ptr.pObj) ) { pObj->count++; }
45 return *this;
46 }
47
49 if (pObj && --(pObj->count) == 0) { delete pObj; }
50 }
51
52 private:
53 T* pObj;
54};
55
56template <typename T, int N> class DArray;
57// Iterator for DArray class
58template <typename T, int N>
59class NIter
60{
61 friend class DArray<T, N>;
62 public:
63 inline T& operator* () { return ptr[idx%N]; }
64 inline T* operator-> () { return &ptr[idx%N]; }
65 inline NIter& operator++ () { idx++; return *this;}
66 inline NIter& operator+= (int n) { idx+=n; return *this;}
67
68 inline bool operator== (const NIter& iter) { return idx==iter.idx && ptr==iter.ptr; }
69 inline bool operator!= (const NIter& iter) { return idx!=iter.idx || ptr!=iter.ptr; }
70
71 NIter(T* begin, int last) : ptr(begin), idx(last) {}
72 private:
73 T* ptr;
74 int idx;
75};
76
77// DArray - static array with stack-like iterator
78template <typename T, int N>
79class DArray
80{
81 public:
82 DArray () : last(N), len(0) {}
83
85 iterator begin() { return iterator(elems, last); }
86 iterator end() { return iterator(elems, last+len); }
87
88 T& insert(const T& el) {
89 len = (len == N ? len : len+1);
90 last=((last-1)+N)%N;
91 elems[last]=el;
92 return elems[last];
93 }
94
95#ifdef USE_SMART_INSERT
96 void remove(iterator &it) {
97// assert(it.ptr==elems);
98 int i=it.idx%N;
99 elems[i]=T();
100 if (i>=last) {
101 memmove(&elems[last+1],&elems[last],(i-last)*sizeof(T));
102 memset(&elems[last],0,sizeof(T));
103 last=(last+1)%N;
104 } else {
105 memmove(&elems[i],&elems[i+1],(last-i-1)*sizeof(T));
106 memset(&elems[last-1],0,sizeof(T));
107 }
108 len=len-1;
109 }
110#endif
111
112 void reset() {
113#ifndef USE_DIRTY_RESET
114 for (int i=0; i<len; i++) {
115 elems[i]=T();
116 }
117#endif
118 last=N;
119 len=0;
120 }
121
122 static const int size = N;
123 const T& operator [](const int idx) const { return elems[idx]; }
124 T& operator [](const int idx) { return elems[idx]; }
125
126 private:
127 T elems[N];
128 int last;
129 int len;
130};
131
132// CArray - simple array with trivial iterator
133template <typename T, int N>
134class CArray
135{
136 public:
137 typedef std::auto_ptr<CArray> Ptr;
138
139 CArray (T dval=T()) {
140 for (iterator i=begin(); i!=end(); ++i) {
141 *i=dval;
142 }
143 }
144
145 typedef T* iterator;
146 iterator begin() { return &elems[0]; }
147 iterator end() { return &elems[N]; }
148
149
150 static const int size = N;
151
152 const T& operator [](const int idx) const { return elems[idx]; }
153 T& operator [](const int idx) { return elems[idx]; }
154
155 private:
156 T elems[N];
157};
158
159#endif /* QUL_POINTER_H */
160
161/*
162class Test: public SRefCnt
163{
164 public:
165 friend class SPtr<Test>;
166 typedef SPtr<Test> Ptr;
167
168 static Ptr create(int x) { return Ptr(new Test(x)); };
169
170 ~Test() { };
171
172 private:
173 Test() { };
174};
175
176*/
const Int_t n
EvtStreamInputIterator< typename Generator::result_type > iter(Generator gen, int N=0)