ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/ObjArray.h
Revision: 1.8
Committed: Wed Dec 3 16:57:08 2008 UTC (16 years, 5 months ago) by bendavid
Content type: text/plain
Branch: MAIN
Changes since 1.7: +23 -2 lines
Log Message:
Add checks to make setting ownership safer

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 bendavid 1.8 // $Id: ObjArray.h,v 1.7 2008/12/01 17:17:20 bendavid 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     }
106     }
107    
108     //--------------------------------------------------------------------------------------------------
109     template<class ArrayElement>
110 loizides 1.6 inline void mithep::ObjArray<ArrayElement>::Add(const ArrayElement *ae)
111     {
112     // Add object to array. This function should be used in cases the array does not own the objects.
113    
114     if (IsOwner()) {
115     TObject::Error("Add","Can not add object since IsOwner() returns kTRUE.");
116     return;
117     }
118    
119     AddLast(ae);
120     }
121    
122     //--------------------------------------------------------------------------------------------------
123     template<class ArrayElement>
124     inline void mithep::ObjArray<ArrayElement>::AddLast(const ArrayElement *ae)
125 loizides 1.1 {
126     // Add new entry at the end of array.
127    
128 loizides 1.6 fArray.AddLast(const_cast<ArrayElement*>(ae));
129 loizides 1.1 fNumEntries++;
130     }
131    
132     //--------------------------------------------------------------------------------------------------
133     template<class ArrayElement>
134 loizides 1.6 inline void mithep::ObjArray<ArrayElement>::AddOwned(ArrayElement *ae)
135     {
136     // Add object to array. This function should be used in cases the array owns the objects.
137    
138 bendavid 1.8 if (!IsOwner()) {
139     TObject::Error("AddOwned","Can not add object since IsOwner() returns kFALSE.");
140     return;
141     }
142    
143 loizides 1.6 AddLast(ae);
144     }
145    
146     //--------------------------------------------------------------------------------------------------
147     template<class ArrayElement>
148 loizides 1.1 inline ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx)
149     {
150     // Return entry at given index.
151    
152     if (idx<fNumEntries)
153     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
154    
155 loizides 1.5 TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
156     idx, fNumEntries, GetName(), ArrayElement::Class_Name());
157 loizides 1.1 return 0;
158     }
159    
160     //--------------------------------------------------------------------------------------------------
161     template<class ArrayElement>
162     inline const ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx) const
163     {
164     // Return entry at given index.
165    
166     if (idx<fNumEntries)
167     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
168    
169 loizides 1.5 TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
170     idx, fNumEntries, GetName(), ArrayElement::Class_Name());
171 loizides 1.1 return 0;
172     }
173    
174     //--------------------------------------------------------------------------------------------------
175     template<class ArrayElement>
176     inline const ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name) const
177     {
178     // Find object by name.
179    
180     return static_cast<const ArrayElement*>(fArray.FindObject(name));
181     }
182    
183     //--------------------------------------------------------------------------------------------------
184     template<class ArrayElement>
185     inline ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name)
186     {
187     // Find object by name.
188    
189     return static_cast<ArrayElement*>(fArray.FindObject(name));
190     }
191    
192     //--------------------------------------------------------------------------------------------------
193     template<class ArrayElement>
194 bendavid 1.7 inline Bool_t mithep::ObjArray<ArrayElement>::HasObject(const ArrayElement *obj) const
195     {
196     // Check whether object is in array.
197    
198     return fArray.FindObject(obj);
199     }
200    
201     //--------------------------------------------------------------------------------------------------
202     template<class ArrayElement>
203 loizides 1.1 inline void mithep::ObjArray<ArrayElement>::Remove(UInt_t idx)
204     {
205     // Remove object at given index from array. You probably want to call Trim as well.
206    
207     fArray.RemoveAt(idx);
208     }
209    
210     //--------------------------------------------------------------------------------------------------
211     template<class ArrayElement>
212     inline void mithep::ObjArray<ArrayElement>::Remove(const char *name)
213     {
214     // Remove object from array. You probably want to call Trim as well.
215    
216     TObject *obj = fArray.FindObject(name);
217     if (obj) fArray.Remove(obj);
218     }
219    
220     //--------------------------------------------------------------------------------------------------
221     template<class ArrayElement>
222     inline void mithep::ObjArray<ArrayElement>::Remove(ArrayElement *ae)
223     {
224     fArray.Remove(ae);
225     }
226    
227     //--------------------------------------------------------------------------------------------------
228     template<class ArrayElement>
229     inline void mithep::ObjArray<ArrayElement>::Reset()
230     {
231     // Default implementation for clearing the array.
232    
233     fArray.Clear(); //will delete objects if owner
234    
235     fNumEntries = 0;
236     }
237    
238     //--------------------------------------------------------------------------------------------------
239     template<class ArrayElement>
240 bendavid 1.8 inline void mithep::ObjArray<ArrayElement>::SetOwner(Bool_t o)
241     {
242     // Set ownership of array. Must do this before adding any objects
243    
244     if (fNumEntries) {
245     TObject::Error("SetOwner","Cannot safely change ownership of non-empty array.");
246     return;
247     }
248    
249     fArray.SetOwner(o);
250    
251     }
252    
253    
254     //--------------------------------------------------------------------------------------------------
255     template<class ArrayElement>
256 loizides 1.1 inline TIterator *mithep::ObjArray<ArrayElement>::Iterator(Bool_t dir) const
257     {
258     // Return ROOT collection iterator.
259    
260     return fArray.MakeIterator(dir);
261     }
262    
263     //--------------------------------------------------------------------------------------------------
264     template<class ArrayElement>
265 loizides 1.5 void mithep::ObjArray<ArrayElement>::Print(Option_t *opt) const
266     {
267     // Print out elements of array.
268    
269     printf("%s: Contains %d (out of %d) objs of name %s\n",
270     GetName(), GetEntries(), GetSize(), ArrayElement::Class_Name());
271    
272     const UInt_t N = GetEntries();
273     for (UInt_t i=0; i<N; ++i) {
274     printf("%4d: ",i);
275     At(i)->Print(opt);
276     }
277     }
278    
279     //--------------------------------------------------------------------------------------------------
280     template<class ArrayElement>
281     inline ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx)
282 loizides 1.1 {
283     // Return entry at given index.
284    
285 loizides 1.5 return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
286 loizides 1.1 }
287    
288     //--------------------------------------------------------------------------------------------------
289     template<class ArrayElement>
290 loizides 1.5 inline const ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx) const
291 loizides 1.1 {
292     // Return entry at given index.
293    
294 loizides 1.5 return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
295 loizides 1.1 }
296    
297     //--------------------------------------------------------------------------------------------------
298     template<class ArrayElement>
299 loizides 1.5 inline const ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx) const
300 loizides 1.1 {
301     // Return entry at given index.
302    
303 loizides 1.5 return At(idx);
304 loizides 1.1 }
305    
306     //--------------------------------------------------------------------------------------------------
307     template<class ArrayElement>
308 loizides 1.5 inline ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx)
309 loizides 1.1 {
310     // Return entry at given index.
311    
312 loizides 1.5 return At(idx);
313 loizides 1.1 }
314     #endif