ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/StackArray.h
Revision: 1.6
Committed: Wed Dec 3 16:56:28 2008 UTC (16 years, 5 months ago) by bendavid
Content type: text/plain
Branch: MAIN
Changes since 1.5: +2 -12 lines
Log Message:
Require objects to be TObjects

File Contents

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2 bendavid 1.6 // $Id: StackArray.h,v 1.5 2008/12/01 17:17:20 bendavid Exp $
3 bendavid 1.1 //
4     // StackArray
5     //
6 loizides 1.4 // Implementation of an array on the stack as opposed to on the heap
7     // memory. For various reasons, the array can not be written in split
8     // mode. Maximum size is set by template parameter. Array is meant to
9     // store classes as opposed to StackArrayBasic which should be used to
10     // hold basic types.
11 bendavid 1.1 //
12     // Authors: C.Loizides, J.Bendavid
13     //--------------------------------------------------------------------------------------------------
14    
15     #ifndef MITANA_DATACONT_STACKARRAY
16     #define MITANA_DATACONT_STACKARRAY
17    
18     #include <TObject.h>
19     #include <TClass.h>
20     #include "MitAna/DataCont/interface/Collection.h"
21    
22     namespace mithep
23     {
24     template<class ArrayElement, UInt_t N>
25     class StackArray : public Collection<ArrayElement>
26     {
27     public:
28     StackArray();
29 bendavid 1.3 StackArray(const StackArray &a);
30 bendavid 1.1 ~StackArray() {}
31    
32     void AddCopy(const ArrayElement &ae);
33     ArrayElement *AddNew();
34     ArrayElement *Allocate();
35     ArrayElement *At(UInt_t idx);
36     const ArrayElement *At(UInt_t idx) const;
37     void Clear(Option_t */*opt*/="") {}
38     UInt_t Entries() const { return GetEntries(); }
39     UInt_t GetEntries() const { return fSize; }
40 loizides 1.4 UInt_t GetSize() const { return N; }
41 bendavid 1.5 Bool_t HasObject(const ArrayElement *obj) const;
42 bendavid 1.1 Bool_t IsOwner() const { return kTRUE; }
43     void Reset() { fSize = 0; }
44     void Trim() {}
45     ArrayElement *UncheckedAt(UInt_t idx);
46     const ArrayElement *UncheckedAt(UInt_t idx) const;
47     ArrayElement *operator[](UInt_t idx);
48     const ArrayElement *operator[](UInt_t idx) const;
49    
50     protected:
51 loizides 1.2 TClass *fClass; //!pointer to TClass object used by streamer
52     UShort_t fSize; //size of array
53 loizides 1.4 ArrayElement fArray[N]; //storage for objects
54 bendavid 1.1
55 loizides 1.4 ClassDef(StackArray,1) // Array on stack for arbitrary classes
56 bendavid 1.1 };
57     }
58    
59     //--------------------------------------------------------------------------------------------------
60     template<class ArrayElement, UInt_t N>
61     inline mithep::StackArray<ArrayElement, N>::StackArray() :
62 bendavid 1.3 fClass(TClass::GetClass(typeid(ArrayElement))),
63 bendavid 1.1 fSize(0)
64     {
65     // Default constructor.
66 bendavid 1.3 }
67    
68     //--------------------------------------------------------------------------------------------------
69     template<class ArrayElement, UInt_t N>
70     inline mithep::StackArray<ArrayElement, N>::StackArray(const StackArray &a) :
71     fClass(a.fClass),
72     fSize(a.fSize)
73     {
74 loizides 1.4 // Copy constructor. Copy only elements which are used.
75    
76 bendavid 1.3 for (UInt_t i=0; i<fSize; ++i)
77     fArray[i] = a.fArray[i];
78 bendavid 1.1 }
79    
80     //--------------------------------------------------------------------------------------------------
81     template<class ArrayElement, UInt_t N>
82     void mithep::StackArray<ArrayElement, N>::AddCopy(const ArrayElement &ae)
83     {
84     // Add a copy of an existing object.
85    
86     if(fSize>=N) {
87 loizides 1.4 TObject::Fatal("AddCopy", "Maximum number of slots reached (%d>=%d): "
88     "To support more requires a different template!", fSize, N);
89 bendavid 1.1 return;
90     }
91    
92     fArray[fSize] = ae;
93     ++fSize;
94     }
95    
96     //--------------------------------------------------------------------------------------------------
97     template<class ArrayElement, UInt_t N>
98     ArrayElement* mithep::StackArray<ArrayElement, N>::AddNew()
99     {
100     // Add new object.
101    
102     return new(Allocate()) ArrayElement();
103     }
104    
105     //--------------------------------------------------------------------------------------------------
106     template<class ArrayElement, UInt_t N>
107     ArrayElement* mithep::StackArray<ArrayElement, N>::Allocate()
108     {
109 loizides 1.4 // Return next slot in the array, *only* to be used in placement new operator.
110 bendavid 1.1
111     if(fSize>=N) {
112 loizides 1.4 TObject::Fatal("Allocate", "Maximum number of slots reached (%d>=%d): "
113     "To support more requires a different template!", fSize, N);
114 bendavid 1.1 return 0;
115     }
116    
117     ++fSize;
118     return &fArray[fSize-1];
119     }
120    
121     //--------------------------------------------------------------------------------------------------
122     template<class ArrayElement, UInt_t N>
123     inline ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx)
124     {
125     // Return entry at given index.
126    
127     if(idx<fSize)
128     return static_cast<ArrayElement*>(&fArray[idx]);
129    
130 loizides 1.4 ArrayElement tmp;
131     TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
132     idx, fSize, this->GetName(), typeid(tmp).name());
133 bendavid 1.1 return 0;
134     }
135    
136     //--------------------------------------------------------------------------------------------------
137     template<class ArrayElement, UInt_t N>
138     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx) const
139     {
140     // Return entry at given index.
141    
142     if(idx<fSize)
143     return static_cast<const ArrayElement*>(&fArray[idx]);
144    
145 loizides 1.4 ArrayElement tmp;
146     TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
147     idx, fSize, this->GetName(), typeid(tmp).name());
148 bendavid 1.1 return 0;
149     }
150    
151 bendavid 1.5 //--------------------------------------------------------------------------------------------------
152     template<class ArrayElement, UInt_t N>
153     inline Bool_t mithep::StackArray<ArrayElement, N>::HasObject(const ArrayElement *obj) const
154     {
155     // Check whether object is in array.
156    
157     for (UInt_t i=0; i<fSize; ++i) {
158 bendavid 1.6 if ( fArray[i].IsEqual(obj) )
159 bendavid 1.5 return true;
160     }
161    
162     return false;
163     }
164    
165 bendavid 1.1 //-------------------------------------------------------------------------------------------------
166     template<class ArrayElement, UInt_t N>
167     void mithep::StackArray<ArrayElement, N>::Streamer(TBuffer &b)
168     {
169     // Stream all objects in the array to or from the I/O buffer.
170    
171     if (b.IsReading()) {
172     b >> fSize;
173     if (fSize) {
174     b.ReadFastArray(fArray,fClass,fSize);
175     }
176     } else { /*writing*/
177     b << fSize;
178     if (fSize) {
179     b.WriteFastArray(fArray,fClass,fSize);
180     }
181     }
182     }
183    
184     //--------------------------------------------------------------------------------------------------
185     template<class ArrayElement, UInt_t N>
186     inline ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx)
187     {
188     // Return entry at given index.
189    
190     return static_cast<ArrayElement*>(&fArray[idx]);
191     }
192    
193     //--------------------------------------------------------------------------------------------------
194     template<class ArrayElement, UInt_t N>
195     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx) const
196     {
197     // Return entry at given index.
198    
199     return static_cast<const ArrayElement*>(&fArray[idx]);
200     }
201 loizides 1.4
202     //--------------------------------------------------------------------------------------------------
203     template<class ArrayElement, UInt_t N>
204     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx) const
205     {
206     // Return entry at given index.
207    
208     return At(idx);
209     }
210    
211     //--------------------------------------------------------------------------------------------------
212     template<class ArrayElement, UInt_t N>
213     inline ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx)
214     {
215     // Return entry at given index.
216    
217     return At(idx);
218     }
219 bendavid 1.1 #endif