GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PtrStack.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #ifndef ADF_PtrStack
24 #define ADF_PtrStack
25 #define ADF_PTRSTACK_H ADF_PtrStack
26 
27 #include <memory.h>
28 
29 namespace ADF
30 {
32 
58 template <typename T, typename S = UInt_t>
59 class PtrStack
60 {
61 public:
62 // const S fDefaultCapacity = 16;
63 
64 private:
65 // public:
66  T **fStack;
67 
68  S fCurrentSize;
69  S fFilledSize;
70  S fMaxSize;
71 
72  Bool_t fIsOwner;
73 
74 protected:
75  void Init();
76  void Expand(S);
77 
78 public:
79  PtrStack(S max_size = 16):
80  fStack(0x0), fCurrentSize(0), fFilledSize(0), fMaxSize(max_size), fIsOwner(true) { Init(); }
81  ~PtrStack();
82 
84  void SetBottom()
85  { fCurrentSize = 0; }
86 
87  void SetOwner(Bool_t own = false)
88  { fIsOwner = own; }
89 
90  T *At(S which) const
91  { if ( which < fMaxSize ) return fStack[which]; return NULL; }
92 
94 
97  void Add(T *t)
98  {
99  if ( fFilledSize == fMaxSize )
100  Expand(fMaxSize+10);
101  if ( fFilledSize < fMaxSize ) { fStack[fFilledSize++] = t; fCurrentSize = fFilledSize; }
102  }
103 
104  // void Reserve(S size) { if ( size <= fMaxSize ) return; }
105 
107 
110  S GetSize() const
111  { return fCurrentSize; }
112 
114 
117  void Clear()
118  {
119  if ( fStack == NULL || fFilledSize == 0 ) return;
120  for (S i = 0; i < fFilledSize; i++)
121  fStack[i] = NULL ;
122  fFilledSize = fCurrentSize = 0;
123  }
124 
126 
129  void Delete();
130 };
131 template <typename T, typename S> void PtrStack<T,S>::Init()
132 {
133  if ( fMaxSize <= 0 ) fMaxSize = 1;
134 
135  if ( fStack )
136  { delete [] fStack; fStack = NULL; }
137 
138  fStack = new T*[fMaxSize]; ::memset(fStack,0,fMaxSize*sizeof(T*)); fFilledSize = 0; fCurrentSize = 0;
139 }
140 template <typename T, typename S> void PtrStack<T,S>::Expand(S new_size)
141 {
142  if ( new_size <= fMaxSize ) return;
143  T **tmp = new T*[new_size]; ::memset(tmp,0,new_size*sizeof(T*));
144 
145  for (S i = 0; i < fFilledSize; i++) tmp[i] = At(i);
146  if ( fStack )
147  { delete [] fStack; fStack = NULL; }
148 
149  fStack = tmp; fMaxSize = new_size;
150 }
151 template <typename T, typename S> PtrStack<T,S>::~PtrStack()
152 {
153  if ( fStack ) { Delete(); delete [] fStack; }
154 }
155 template <typename T, typename S> void PtrStack<T,S>::Delete()
156 {
157 
158  if ( fStack == NULL || fFilledSize == 0 ) return;
159 
160  for (S i = 0; i < fMaxSize; i++) {
161  if ( fStack[i] && fIsOwner ) delete fStack[i]; fStack[i] = NULL;
162  }
163  fFilledSize = fCurrentSize = 0;
164 }
165 }
166 #endif
167 
168 
169 
170 
171 
void SetOwner(Bool_t own=false)
Definition: PtrStack.h:87
void Clear()
Definition: PtrStack.h:117
void Init()
Definition: PtrStack.h:131
PtrStack.
Definition: PtrStack.h:59
void Delete()
Definition: PtrStack.h:155
S GetSize() const
to get the current position in the stack
Definition: PtrStack.h:110
void Add(T *t)
Add a new pointer on the stack.
Definition: PtrStack.h:97
PtrStack(S max_size=16)
Definition: PtrStack.h:79
T * At(S which) const
Definition: PtrStack.h:90
void SetBottom()
The object at position c is now on the top of the stack.
Definition: PtrStack.h:84
void Expand(S)
Definition: PtrStack.h:140