GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StackOfObjects.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_StackOfObjects
24 #define ADF_StackOfObjects
25 
26 #include "ADFConfig.h"
27 #include "PtrStack.h"
28 
29 namespace ADF
30 {
31 
33 
43 template <typename T>
45 {
46 private:
47  PtrStack<T, UShort_t> fStack;
48 
49  UShort_t fCurrentSize;
50  UShort_t fMaxSize;
51 
52 public:
53  StackOfObjects(UShort_t max_size = 100u):
54  fStack(), fCurrentSize(0), fMaxSize(max_size) {;}
56  { fStack.Delete(); }
57 
59 
64  T *New(Bool_t do_reset = true);
65 
67 
70  T *At(UShort_t i) const
71  {
72  if ( i < fCurrentSize )
73  return fStack.At(i);
74  return 0x0;
75  }
76 
78 
86  Bool_t SetSize(UShort_t how_many, Bool_t do_reset = true)
87  {
88  // 0 means reset the stack
89  if ( how_many == 0u )
90  { fCurrentSize = 0u; return true; }
91 
92  // cannot allocate so many
93  if ( how_many > GetMaxSize() )
94  return false;
95 
96  // already big enough
97  if ( how_many <= GetSize() ) {
98 
99  fCurrentSize = how_many;
100 
101  // call reset for the objects already in the stack
102  if ( do_reset == true ) {
103  for (UShort_t i = 0u; i < GetSize(); i++ )
104  At(i)->Reset();
105  }
106  return true;
107  }
108  // new objects have to be added on the top
109  UShort_t toadd = how_many - GetSize();
110  for (UShort_t i = 0u; i < toadd; i++ )
111  New(do_reset);
112 
113  return how_many == GetSize();
114  }
116  UShort_t GetSize() const
117  { return fCurrentSize; }
118 
120  UShort_t GetMaxSize() const
121  { return fMaxSize; }
122 
124 
128  void Reset()
129  { fCurrentSize = 0u; }
130 };
131 template <typename T> T *StackOfObjects<T>::New(Bool_t do_reset)
132 {
133  T *t = NULL;
134 
135  if ( fCurrentSize < fStack.GetSize() ) {
136  t = fStack.At(fCurrentSize);
137  if ( do_reset )
138  t->Reset();
139  fCurrentSize++;
140 
141  return t;
142  }
143 
144  if ( fCurrentSize < fMaxSize ) {
145  t = new T();
146  fStack.Add(t); fCurrentSize++;
147  }
148  return t;
149 }
150 
151 } // namespace ADF
152 #endif
153 
154 
155 
156 
157 
T * At(UShort_t i) const
To get the object at slot #i.
UShort_t GetSize() const
To know the current size of the stack.
void Reset()
Reset the stack.
template header file
void Delete()
Definition: PtrStack.h:155
to set informations about the ADF configuration
Bool_t SetSize(UShort_t how_many, Bool_t do_reset=true)
Change the current size.
StackOfObjects(UShort_t max_size=100u)
T * At(S which) const
Definition: PtrStack.h:90
Stack of pointers to ADFObjects.
UShort_t GetMaxSize() const
To know the max size of the stack.
T * New(Bool_t do_reset=true)
It pops up a new object on the top of the stack.