ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/StackArray.h
Revision: 1.2
Committed: Thu Oct 23 18:22:27 2008 UTC (16 years, 6 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.1: +8 -14 lines
Log Message:
Cleanup to have TObject::Fatal rather than Fatal to avoid some warnings.

File Contents

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.2 // $Id: StackArray.h,v 1.1 2008/09/19 11:56:08 bendavid Exp $
3 bendavid 1.1 //
4     // StackArray
5     //
6     // Implementation of a TStackArray using stack (and not heap) memory.
7     // For various reasons, the array can not be written in split mode.
8     // Maximum size of references is set to 1024 (but this could be
9     // changed if there is need for it).
10     //
11     // Authors: C.Loizides, J.Bendavid
12     //--------------------------------------------------------------------------------------------------
13    
14     #ifndef MITANA_DATACONT_STACKARRAY
15     #define MITANA_DATACONT_STACKARRAY
16    
17     #include <TObject.h>
18     #include <TError.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     ~StackArray() {}
30    
31     void AddCopy(const ArrayElement &ae);
32     ArrayElement *AddNew();
33     ArrayElement *Allocate();
34     ArrayElement *At(UInt_t idx);
35     const ArrayElement *At(UInt_t idx) const;
36     void Clear(Option_t */*opt*/="") {}
37     UInt_t Entries() const { return GetEntries(); }
38     UInt_t GetEntries() const { return fSize; }
39     Bool_t IsOwner() const { return kTRUE; }
40     void Reset() { fSize = 0; }
41     void Trim() {}
42     ArrayElement *UncheckedAt(UInt_t idx);
43     const ArrayElement *UncheckedAt(UInt_t idx) const;
44    
45     ArrayElement *operator[](UInt_t idx);
46     const ArrayElement *operator[](UInt_t idx) const;
47    
48     protected:
49 loizides 1.2 TClass *fClass; //!pointer to TClass object used by streamer
50     UShort_t fSize; //size of array
51     ArrayElement fArray[N]; //storage of uids of referenced objects
52 bendavid 1.1
53     private:
54     StackArray(const StackArray &a);
55    
56     ClassDef(StackArray,1) // Implementation of our own TStackArray
57     };
58     }
59    
60     //--------------------------------------------------------------------------------------------------
61     template<class ArrayElement, UInt_t N>
62     inline mithep::StackArray<ArrayElement, N>::StackArray() :
63     fSize(0)
64     {
65     // Default constructor.
66     fClass = TClass::GetClass(typeid(ArrayElement));
67     }
68    
69     //--------------------------------------------------------------------------------------------------
70     template<class ArrayElement, UInt_t N>
71     void mithep::StackArray<ArrayElement, N>::AddCopy(const ArrayElement &ae)
72     {
73     // Add a copy of an existing object.
74    
75     if(fSize>=N) {
76 loizides 1.2 TObject::Fatal("Add",
77     "Maximum number of references reached: To support more requires change code!");
78 bendavid 1.1 return;
79     }
80    
81     fArray[fSize] = ae;
82     ++fSize;
83     }
84    
85     //--------------------------------------------------------------------------------------------------
86     template<class ArrayElement, UInt_t N>
87     ArrayElement* mithep::StackArray<ArrayElement, N>::AddNew()
88     {
89     // Add new object.
90    
91     return new(Allocate()) ArrayElement();
92     }
93    
94     //--------------------------------------------------------------------------------------------------
95     template<class ArrayElement, UInt_t N>
96     ArrayElement* mithep::StackArray<ArrayElement, N>::Allocate()
97     {
98     // Allocate a slot in the array, *only* to be used in placement new operator.
99    
100     if(fSize>=N) {
101 loizides 1.2 TObject::Fatal("Add",
102     "Maximum number of references reached: To support more requires change code!");
103 bendavid 1.1 return 0;
104     }
105    
106     ++fSize;
107     return &fArray[fSize-1];
108     }
109    
110     //--------------------------------------------------------------------------------------------------
111     template<class ArrayElement, UInt_t N>
112     inline ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx)
113     {
114     // Return entry at given index.
115    
116     if(idx<fSize)
117     return static_cast<ArrayElement*>(&fArray[idx]);
118    
119     Error("At", "Given index (%ud) is larger than array size (%ud)", idx, fSize);
120     return 0;
121     }
122    
123     //--------------------------------------------------------------------------------------------------
124     template<class ArrayElement, UInt_t N>
125     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::At(UInt_t idx) const
126     {
127     // Return entry at given index.
128    
129     if(idx<fSize)
130     return static_cast<const ArrayElement*>(&fArray[idx]);
131    
132     Error("At", "Given index (%ud) is larger than array size (%ud)", idx, fSize);
133     return 0;
134     }
135    
136     //--------------------------------------------------------------------------------------------------
137     template<class ArrayElement, UInt_t N>
138     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx) const
139     {
140     // Return entry at given index.
141    
142     return At(idx);
143     }
144    
145     //--------------------------------------------------------------------------------------------------
146     template<class ArrayElement, UInt_t N>
147     inline ArrayElement *mithep::StackArray<ArrayElement, N>::operator[](UInt_t idx)
148     {
149     // Return entry at given index.
150    
151     return At(idx);
152     }
153    
154     //-------------------------------------------------------------------------------------------------
155     template<class ArrayElement, UInt_t N>
156     void mithep::StackArray<ArrayElement, N>::Streamer(TBuffer &b)
157     {
158     // Stream all objects in the array to or from the I/O buffer.
159    
160     if (b.IsReading()) {
161     b >> fSize;
162     if (fSize) {
163     b.ReadFastArray(fArray,fClass,fSize);
164     }
165     } else { /*writing*/
166     b << fSize;
167     if (fSize) {
168     b.WriteFastArray(fArray,fClass,fSize);
169     }
170     }
171     }
172    
173     //--------------------------------------------------------------------------------------------------
174     template<class ArrayElement, UInt_t N>
175     inline ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx)
176     {
177     // Return entry at given index.
178    
179     return static_cast<ArrayElement*>(&fArray[idx]);
180     }
181    
182     //--------------------------------------------------------------------------------------------------
183     template<class ArrayElement, UInt_t N>
184     inline const ArrayElement *mithep::StackArray<ArrayElement, N>::UncheckedAt(UInt_t idx) const
185     {
186     // Return entry at given index.
187    
188     return static_cast<const ArrayElement*>(&fArray[idx]);
189     }
190     #endif