Logo ROOT   6.08/07
Reference Guide
TCollection.h
Go to the documentation of this file.
1 // @(#)root/cont:$Id$
2 // Author: Fons Rademakers 13/08/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TCollection
13 #define ROOT_TCollection
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TCollection //
19 // //
20 // Collection abstract base class. This class inherits from TObject //
21 // because we want to be able to have collections of collections. //
22 // //
23 //////////////////////////////////////////////////////////////////////////
24 
25 #ifndef ROOT_TObject
26 #include "TObject.h"
27 #endif
28 
29 #ifndef ROOT_TIterator
30 #include "TIterator.h"
31 #endif
32 
33 #ifndef ROOT_TString
34 #include "TString.h"
35 #endif
36 
37 
38 class TClass;
39 class TObjectTable;
40 class TVirtualMutex;
41 class TIter;
42 
45 
47 
48 class TCollection : public TObject {
49 
50 private:
51  static TCollection *fgCurrentCollection; //used by macro R__FOR_EACH
52  static TObjectTable *fgGarbageCollection; //used by garbage collector
53  static Bool_t fgEmptyingGarbage; //used by garbage collector
54  static Int_t fgGarbageStack; //used by garbage collector
55 
56  TCollection(const TCollection &); //private and not-implemented, collections
57  void operator=(const TCollection &); //are too complex to be automatically copied
58 
59 protected:
60  enum { kIsOwner = BIT(14) };
61 
62  TString fName; //name of the collection
63  Int_t fSize; //number of elements in collection
64 
65  TCollection() : fName(), fSize(0) { }
66 
67  virtual void PrintCollectionHeader(Option_t* option) const;
68  virtual const char* GetCollectionEntryName(TObject* entry) const;
69  virtual void PrintCollectionEntry(TObject* entry, Option_t* option, Int_t recurse) const;
70 
71 public:
73 
74  virtual ~TCollection() { }
75  virtual void Add(TObject *obj) = 0;
76  void AddVector(TObject *obj1, ...);
77  virtual void AddAll(const TCollection *col);
78  Bool_t AssertClass(TClass *cl) const;
79  void Browse(TBrowser *b);
80  Int_t Capacity() const { return fSize; }
81  virtual void Clear(Option_t *option="") = 0;
82  virtual TObject *Clone(const char *newname="") const;
83  Int_t Compare(const TObject *obj) const;
84  Bool_t Contains(const char *name) const { return FindObject(name) != 0; }
85  Bool_t Contains(const TObject *obj) const { return FindObject(obj) != 0; }
86  virtual void Delete(Option_t *option="") = 0;
87  virtual void Draw(Option_t *option="");
88  virtual void Dump() const ;
89  virtual TObject *FindObject(const char *name) const;
90  TObject *operator()(const char *name) const;
91  virtual TObject *FindObject(const TObject *obj) const;
92  virtual Int_t GetEntries() const { return GetSize(); }
93  virtual const char *GetName() const;
94  virtual TObject **GetObjectRef(const TObject *obj) const = 0;
95  virtual Int_t GetSize() const { return fSize; }
96  virtual Int_t GrowBy(Int_t delta) const;
97  ULong_t Hash() const { return fName.Hash(); }
98  Bool_t IsArgNull(const char *where, const TObject *obj) const;
99  virtual Bool_t IsEmpty() const { return GetSize() <= 0; }
100  virtual Bool_t IsFolder() const { return kTRUE; }
101  Bool_t IsOwner() const { return TestBit(kIsOwner); }
102  Bool_t IsSortable() const { return kTRUE; }
103  virtual void ls(Option_t *option="") const ;
104  virtual TIterator *MakeIterator(Bool_t dir = kIterForward) const = 0;
106  virtual void Paint(Option_t *option="");
107  virtual void Print(Option_t *option="") const;
108  virtual void Print(Option_t *option, Int_t recurse) const;
109  virtual void Print(Option_t *option, const char* wildcard, Int_t recurse=1) const;
110  virtual void Print(Option_t *option, TPRegexp& regexp, Int_t recurse=1) const;
111  virtual void RecursiveRemove(TObject *obj);
112  virtual TObject *Remove(TObject *obj) = 0;
113  virtual void RemoveAll(TCollection *col);
114  void RemoveAll() { Clear(); }
115  void SetCurrentCollection();
116  void SetName(const char *name) { fName = name; }
117  virtual void SetOwner(Bool_t enable = kTRUE);
118  virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0);
119  virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0) const;
120 
122  static void StartGarbageCollection();
123  static void GarbageCollect(TObject *obj);
124  static void EmptyGarbageCollection();
125 
126  TIter begin() const;
127  TIter end() const;
128 
129  ClassDef(TCollection,3) //Collection abstract base class
130 };
131 
132 
133 //////////////////////////////////////////////////////////////////////////
134 // //
135 // TIter //
136 // //
137 // Iterator wrapper. Type of iterator used depends on type of //
138 // collection. //
139 // //
140 //////////////////////////////////////////////////////////////////////////
141 
142 class TIter {
143 
144 private:
145  TIterator *fIterator; //collection iterator
146 
147 protected:
148  TIter() : fIterator(nullptr) { }
149 
150 public:
152  : fIterator(col ? col->MakeIterator(dir) : 0) { }
153  TIter(TIterator *it) : fIterator(it) { }
154  TIter(const TIter &iter);
155  TIter &operator=(const TIter &rhs);
156  virtual ~TIter() { SafeDelete(fIterator); }
157  TObject *operator()() { return Next(); }
158  TObject *Next() { return fIterator ? fIterator->Next() : nullptr; }
159  const TCollection *GetCollection() const { return fIterator ? fIterator->GetCollection() : nullptr; }
160  Option_t *GetOption() const { return fIterator ? fIterator->GetOption() : ""; }
161  void Reset() { if (fIterator) fIterator->Reset(); }
162  TIter &operator++() { Next(); return *this; }
163  Bool_t operator==(const TIter &aIter) const {
164  if (fIterator == nullptr)
165  return aIter.fIterator == nullptr || **aIter.fIterator == nullptr;
166  if (aIter.fIterator == nullptr)
167  return fIterator == nullptr || **fIterator == nullptr;
168  return *fIterator == *aIter.fIterator;
169  }
170  Bool_t operator!=(const TIter &aIter) const {
171  return !(*this == aIter);
172  }
173  TObject *operator*() const { return fIterator ? *(*fIterator): nullptr; }
174  TIter &Begin();
175  static TIter End();
176 
177  ClassDef(TIter,0) //Iterator wrapper
178 };
179 
180 template <class T>
181 class TIterCategory: public TIter, public std::iterator_traits<typename T::Iterator_t> {
182 
183 public:
184  TIterCategory(const TCollection *col, Bool_t dir = kIterForward) : TIter(col, dir) { }
186  virtual ~TIterCategory() { }
187  TIterCategory &Begin() { TIter::Begin(); return *this; }
188  static TIterCategory End() { return TIterCategory(static_cast<TIterator*>(nullptr)); }
189 };
190 
191 
192 inline TIter TCollection::begin() const { return ++(TIter(this)); }
193 inline TIter TCollection::end() const { return TIter::End(); }
194 
195 
196 //---- R__FOR_EACH macro -------------------------------------------------------
197 
198 // Macro to loop over all elements of a list of type "type" while executing
199 // procedure "proc" on each element
200 
201 #define R__FOR_EACH(type,proc) \
202  SetCurrentCollection(); \
203  TIter _NAME3_(nxt_,type,proc)(TCollection::GetCurrentCollection()); \
204  type *_NAME3_(obj_,type,proc); \
205  while ((_NAME3_(obj_,type,proc) = (type*) _NAME3_(nxt_,type,proc)())) \
206  _NAME3_(obj_,type,proc)->proc
207 
208 #endif
TIter end() const
Definition: TCollection.h:193
virtual TObject * Remove(TObject *obj)=0
static Int_t fgGarbageStack
Definition: TCollection.h:54
void Begin(Int_t type)
virtual void Clear(Option_t *option="")=0
const TCollection * GetCollection() const
Definition: TCollection.h:159
virtual Option_t * GetOption() const
Definition: TIterator.h:42
virtual void Reset()=0
R__EXTERN TVirtualMutex * gCollectionMutex
Definition: TCollection.h:46
const char Option_t
Definition: RtypesCore.h:62
virtual const char * GetCollectionEntryName(TObject *entry) const
For given collection entry return the string that is used to identify the object and, potentially, perform wildcard/regexp filtering on.
Int_t Compare(const TObject *obj) const
Compare two TCollection objects.
TIter & Begin()
Pointing to the first element of the container.
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:157
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
#define BIT(n)
Definition: Rtypes.h:120
Bool_t IsArgNull(const char *where, const TObject *obj) const
Returns true if object is a null pointer.
virtual void Draw(Option_t *option="")
Draw all objects in this collection.
virtual Int_t GetEntries() const
Definition: TCollection.h:92
virtual void AddAll(const TCollection *col)
Add all objects from collection col to this collection.
Definition: TCollection.cxx:58
This class implements a mutex interface.
Definition: TVirtualMutex.h:34
static TIterCategory End()
Definition: TCollection.h:188
virtual TIterator * MakeReverseIterator() const
Definition: TCollection.h:105
virtual Bool_t IsFolder() const
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects)...
Definition: TCollection.h:100
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
static TCollection * fgCurrentCollection
Definition: TCollection.h:51
TString fName
Definition: TCollection.h:62
Option_t * GetOption() const
Definition: TCollection.h:160
Bool_t Contains(const char *name) const
Definition: TCollection.h:84
virtual TObject * Clone(const char *newname="") const
Make a clone of an collection using the Streamer facility.
Iterator abstract base class.
Definition: TIterator.h:32
void Reset()
Definition: TCollection.h:161
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition: TString.cxx:606
virtual void Delete(Option_t *option="")=0
Delete this object.
#define SafeDelete(p)
Definition: RConfig.h:507
TIterator * fIterator
Definition: TCollection.h:145
#define ClassDef(name, id)
Definition: Rtypes.h:254
void AddVector(TObject *obj1,...)
Add all arguments to the collection.
Definition: TCollection.cxx:71
TIterCategory(const TCollection *col, Bool_t dir=kIterForward)
Definition: TCollection.h:184
Bool_t Contains(const TObject *obj) const
Definition: TCollection.h:85
const Bool_t kIterForward
Definition: TCollection.h:43
Bool_t IsOwner() const
Definition: TCollection.h:101
virtual void PrintCollectionEntry(TObject *entry, Option_t *option, Int_t recurse) const
Print the collection entry.
Bool_t operator!=(const TIter &aIter) const
Definition: TCollection.h:170
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:41
virtual void RecursiveRemove(TObject *obj)
Remove object from this collection and recursively remove the object from all other objects (and coll...
virtual Int_t GrowBy(Int_t delta) const
Increase the collection&#39;s capacity by delta slots.
virtual ~TIter()
Definition: TCollection.h:156
TIter(TIterator *it)
Definition: TCollection.h:153
static TCollection * GetCurrentCollection()
Return the globally accessible collection.
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const =0
TIter & operator++()
Definition: TCollection.h:162
virtual ~TCollection()
Definition: TCollection.h:74
TObject * Next()
Definition: TCollection.h:158
Collection abstract base class.
Definition: TCollection.h:48
virtual void Paint(Option_t *option="")
Paint all objects in this collection.
Int_t fSize
Definition: TCollection.h:63
virtual const TCollection * GetCollection() const =0
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
static void StartGarbageCollection()
Set up for garbage collection.
TIter begin() const
Definition: TCollection.h:192
TObject * operator()()
Definition: TCollection.h:157
This class registers all instances of TObject and its derived classes in a hash table.
Definition: TObjectTable.h:37
void SetName(const char *name)
Definition: TCollection.h:116
void SetCurrentCollection()
Set this collection to be the globally accesible collection.
virtual void Dump() const
Dump all objects in this collection.
static Bool_t fgEmptyingGarbage
Definition: TCollection.h:53
static TIter End()
Pointing to the element after the last - to a nullptr value in our case.
void RemoveAll()
Definition: TCollection.h:114
static void GarbageCollect(TObject *obj)
Add to the list of things to be cleaned up.
virtual Bool_t IsEmpty() const
Definition: TCollection.h:99
virtual ~TIterCategory()
Definition: TCollection.h:186
TIter(const TCollection *col, Bool_t dir=kIterForward)
Definition: TCollection.h:151
virtual void ls(Option_t *option="") const
List (ls) all objects in this collection.
static TObjectTable * fgGarbageCollection
Definition: TCollection.h:52
unsigned long ULong_t
Definition: RtypesCore.h:51
void Browse(TBrowser *b)
Browse this collection (called by TBrowser).
virtual void Add(TObject *obj)=0
static void EmptyGarbageCollection()
Do the garbage collection.
Mother of all ROOT objects.
Definition: TObject.h:37
#define R__EXTERN
Definition: DllImport.h:27
void operator=(const TCollection &)
virtual void PrintCollectionHeader(Option_t *option) const
Print the collection header.
#define nullptr
Definition: Rtypes.h:87
Int_t Capacity() const
Definition: TCollection.h:80
virtual TObject * Next()=0
TObject * operator*() const
Definition: TCollection.h:173
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
virtual TObject * FindObject(const char *name) const
Find an object in this collection using its name.
TIterCategory & Begin()
Definition: TCollection.h:187
void End()
Bool_t AssertClass(TClass *cl) const
Make sure all objects in this collection inherit from class cl.
Definition: TCollection.cxx:86
virtual TObject ** GetObjectRef(const TObject *obj) const =0
const Bool_t kIterBackward
Definition: TCollection.h:44
Bool_t operator==(const TIter &aIter) const
Definition: TCollection.h:163
virtual void Print(Option_t *option="") const
Default print for collections, calls Print(option, 1).
Bool_t IsSortable() const
Definition: TCollection.h:102
virtual Int_t GetSize() const
Definition: TCollection.h:95
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * operator()(const char *name) const
Find an object in this collection by name.
TIterCategory(TIterator *it)
Definition: TCollection.h:185
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write all objects in this collection.
char name[80]
Definition: TGX11.cxx:109
virtual const char * GetName() const
Return name of this collection.
ULong_t Hash() const
Return hash value for this object.
Definition: TCollection.h:97