ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/ObjArray.h
Revision: 1.10
Committed: Tue Dec 9 17:42:20 2008 UTC (16 years, 4 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.9: +2 -1 lines
Log Message:
Fix mem leak with iterator.

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.10 // $Id: ObjArray.h,v 1.9 2008/12/08 15:26:11 loizides Exp $
3 loizides 1.1 //
4     // ObjArray
5     //
6 loizides 1.6 // Implementation of Collection interface using TObjArray class. By default, the
7     // array does not own its elements, and hence will not delete them. Use SetOwner(1)
8     // to turn on ownership.
9 loizides 1.1 //
10     // Authors: C.Loizides
11     //--------------------------------------------------------------------------------------------------
12    
13 loizides 1.2 #ifndef MITANA_DATACONT_OBJARRAY_H
14     #define MITANA_DATACONT_OBJARRAY_H
15 loizides 1.1
16     #include <TObjArray.h>
17     #include "MitAna/DataCont/interface/Collection.h"
18    
19     namespace mithep
20     {
21     template<class ArrayElement>
22     class ObjArray : public Collection<ArrayElement>
23     {
24     public:
25     ObjArray(UInt_t size=0, const char *name=0);
26     ~ObjArray() {}
27    
28 loizides 1.6 void Add(const ArrayElement *ae);
29 loizides 1.4 void Add(const TCollection *col);
30 loizides 1.6 void AddOwned(ArrayElement *ae);
31 loizides 1.1 const TObjArray &Arr() const { return fArray; }
32     TObjArray &Arr() { return fArray; }
33     ArrayElement *At(UInt_t idx);
34     const ArrayElement *At(UInt_t idx) const;
35     void Clear(Option_t */*opt*/="") { fArray.~TObjArray(); }
36     UInt_t Entries() const { return fNumEntries; }
37     UInt_t GetEntries() const { return fNumEntries; }
38     const char* GetName() const { return fArray.GetName(); }
39 loizides 1.6 UInt_t GetSize() const { return fArray.GetSize(); }
40 bendavid 1.7 Bool_t HasObject(const ArrayElement *obj) const;
41 loizides 1.1 const ArrayElement *Find(const char *name) const;
42     ArrayElement *Find(const char *name);
43 bendavid 1.7 void Print(Option_t *opt="") const;
44 loizides 1.1 void Remove(UInt_t idx);
45     void Remove(const char *name);
46     void Remove(ArrayElement *ae);
47     Bool_t IsOwner() const { return fArray.IsOwner(); }
48     TIterator *Iterator(Bool_t dir = kIterForward) const;
49     void Reset();
50 loizides 1.6 void SetName(const char *name) { fArray.SetName(name); }
51 bendavid 1.8 void SetOwner(Bool_t o);
52 loizides 1.1 void Sort() { fArray.Sort(); }
53 loizides 1.5 void Trim() { fArray.Compress();}
54 loizides 1.1 ArrayElement *UncheckedAt(UInt_t idx);
55     const ArrayElement *UncheckedAt(UInt_t idx) const;
56     ArrayElement *operator[](UInt_t idx);
57     const ArrayElement *operator[](UInt_t idx) const;
58    
59     protected:
60 loizides 1.6 void AddLast(const ArrayElement *e);
61 loizides 1.1
62     TObjArray fArray; //array for storage
63     UInt_t fNumEntries; //number of entries in the array
64    
65     private:
66     ObjArray(const ObjArray &a);
67    
68     ClassDefT(ObjArray,1) // Wrapper around TClonesArray class
69     };
70     }
71    
72     //--------------------------------------------------------------------------------------------------
73     template<class ArrayElement>
74     inline mithep::ObjArray<ArrayElement>::ObjArray(UInt_t size, const char *name) :
75     fArray(size),
76     fNumEntries(0)
77     {
78     // Default constructor.
79    
80     if (name)
81     fArray.SetName(name);
82     }
83    
84     //--------------------------------------------------------------------------------------------------
85     template<class ArrayElement>
86 loizides 1.4 inline void mithep::ObjArray<ArrayElement>::Add(const TCollection *col)
87     {
88 loizides 1.5 // Add objects from collection to array.
89    
90 loizides 1.4 if (!col)
91     return;
92    
93     if (IsOwner()) {
94     TObject::Error("Add","Can not add collection since IsOwner() returns kTRUE.");
95     return;
96     }
97    
98     TIterator *iter = col->MakeIterator();
99     if (iter) {
100     ArrayElement *to = dynamic_cast<ArrayElement *>(iter->Next());
101     while (to) {
102     AddLast(to);
103     to = dynamic_cast<ArrayElement *>(iter->Next());
104     }
105 loizides 1.10 delete iter;
106 loizides 1.4 }
107     }
108    
109     //--------------------------------------------------------------------------------------------------
110     template<class ArrayElement>
111 loizides 1.6 inline void mithep::ObjArray<ArrayElement>::Add(const ArrayElement *ae)
112     {
113     // Add object to array. This function should be used in cases the array does not own the objects.
114    
115     if (IsOwner()) {
116     TObject::Error("Add","Can not add object since IsOwner() returns kTRUE.");
117     return;
118     }
119    
120     AddLast(ae);
121     }
122    
123     //--------------------------------------------------------------------------------------------------
124     template<class ArrayElement>
125     inline void mithep::ObjArray<ArrayElement>::AddLast(const ArrayElement *ae)
126 loizides 1.1 {
127     // Add new entry at the end of array.
128    
129 loizides 1.6 fArray.AddLast(const_cast<ArrayElement*>(ae));
130 loizides 1.1 fNumEntries++;
131     }
132    
133     //--------------------------------------------------------------------------------------------------
134     template<class ArrayElement>
135 loizides 1.6 inline void mithep::ObjArray<ArrayElement>::AddOwned(ArrayElement *ae)
136     {
137     // Add object to array. This function should be used in cases the array owns the objects.
138    
139 bendavid 1.8 if (!IsOwner()) {
140     TObject::Error("AddOwned","Can not add object since IsOwner() returns kFALSE.");
141     return;
142     }
143    
144 loizides 1.6 AddLast(ae);
145     }
146    
147     //--------------------------------------------------------------------------------------------------
148     template<class ArrayElement>
149 loizides 1.1 inline ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx)
150     {
151     // Return entry at given index.
152    
153     if (idx<fNumEntries)
154     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
155    
156 loizides 1.5 TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
157     idx, fNumEntries, GetName(), ArrayElement::Class_Name());
158 loizides 1.1 return 0;
159     }
160    
161     //--------------------------------------------------------------------------------------------------
162     template<class ArrayElement>
163     inline const ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx) const
164     {
165     // Return entry at given index.
166    
167     if (idx<fNumEntries)
168     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
169    
170 loizides 1.5 TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
171     idx, fNumEntries, GetName(), ArrayElement::Class_Name());
172 loizides 1.1 return 0;
173     }
174    
175     //--------------------------------------------------------------------------------------------------
176     template<class ArrayElement>
177     inline const ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name) const
178     {
179     // Find object by name.
180    
181     return static_cast<const ArrayElement*>(fArray.FindObject(name));
182     }
183    
184     //--------------------------------------------------------------------------------------------------
185     template<class ArrayElement>
186     inline ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name)
187     {
188     // Find object by name.
189    
190     return static_cast<ArrayElement*>(fArray.FindObject(name));
191     }
192    
193     //--------------------------------------------------------------------------------------------------
194     template<class ArrayElement>
195 bendavid 1.7 inline Bool_t mithep::ObjArray<ArrayElement>::HasObject(const ArrayElement *obj) const
196     {
197     // Check whether object is in array.
198    
199     return fArray.FindObject(obj);
200     }
201    
202     //--------------------------------------------------------------------------------------------------
203     template<class ArrayElement>
204 loizides 1.1 inline void mithep::ObjArray<ArrayElement>::Remove(UInt_t idx)
205     {
206     // Remove object at given index from array. You probably want to call Trim as well.
207    
208     fArray.RemoveAt(idx);
209     }
210    
211     //--------------------------------------------------------------------------------------------------
212     template<class ArrayElement>
213     inline void mithep::ObjArray<ArrayElement>::Remove(const char *name)
214     {
215     // Remove object from array. You probably want to call Trim as well.
216    
217     TObject *obj = fArray.FindObject(name);
218     if (obj) fArray.Remove(obj);
219     }
220    
221     //--------------------------------------------------------------------------------------------------
222     template<class ArrayElement>
223     inline void mithep::ObjArray<ArrayElement>::Remove(ArrayElement *ae)
224     {
225     fArray.Remove(ae);
226     }
227    
228     //--------------------------------------------------------------------------------------------------
229     template<class ArrayElement>
230     inline void mithep::ObjArray<ArrayElement>::Reset()
231     {
232     // Default implementation for clearing the array.
233    
234     fArray.Clear(); //will delete objects if owner
235    
236     fNumEntries = 0;
237     }
238    
239     //--------------------------------------------------------------------------------------------------
240     template<class ArrayElement>
241 bendavid 1.8 inline void mithep::ObjArray<ArrayElement>::SetOwner(Bool_t o)
242     {
243     // Set ownership of array. Must do this before adding any objects
244    
245     if (fNumEntries) {
246     TObject::Error("SetOwner","Cannot safely change ownership of non-empty array.");
247     return;
248     }
249    
250     fArray.SetOwner(o);
251    
252     }
253    
254    
255     //--------------------------------------------------------------------------------------------------
256     template<class ArrayElement>
257 loizides 1.1 inline TIterator *mithep::ObjArray<ArrayElement>::Iterator(Bool_t dir) const
258     {
259     // Return ROOT collection iterator.
260    
261     return fArray.MakeIterator(dir);
262     }
263    
264     //--------------------------------------------------------------------------------------------------
265     template<class ArrayElement>
266 loizides 1.5 void mithep::ObjArray<ArrayElement>::Print(Option_t *opt) const
267     {
268     // Print out elements of array.
269    
270     printf("%s: Contains %d (out of %d) objs of name %s\n",
271     GetName(), GetEntries(), GetSize(), ArrayElement::Class_Name());
272    
273 loizides 1.9 if (opt && opt[0]=='l') {
274     const UInt_t N = GetEntries();
275     for (UInt_t i=0; i<N; ++i) {
276     printf("%4d: ",i);
277     At(i)->Print(opt);
278     }
279 loizides 1.5 }
280     }
281    
282     //--------------------------------------------------------------------------------------------------
283     template<class ArrayElement>
284     inline ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx)
285 loizides 1.1 {
286     // Return entry at given index.
287    
288 loizides 1.5 return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
289 loizides 1.1 }
290    
291     //--------------------------------------------------------------------------------------------------
292     template<class ArrayElement>
293 loizides 1.5 inline const ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx) const
294 loizides 1.1 {
295     // Return entry at given index.
296    
297 loizides 1.5 return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
298 loizides 1.1 }
299    
300     //--------------------------------------------------------------------------------------------------
301     template<class ArrayElement>
302 loizides 1.5 inline const ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx) const
303 loizides 1.1 {
304     // Return entry at given index.
305    
306 loizides 1.5 return At(idx);
307 loizides 1.1 }
308    
309     //--------------------------------------------------------------------------------------------------
310     template<class ArrayElement>
311 loizides 1.5 inline ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx)
312 loizides 1.1 {
313     // Return entry at given index.
314    
315 loizides 1.5 return At(idx);
316 loizides 1.1 }
317     #endif