Logo ROOT   6.10/09
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 #include "TObject.h"
26 
27 #include "TIterator.h"
28 
29 #include "TString.h"
30 
31 
32 class TClass;
33 class TObjectTable;
34 class TVirtualMutex;
35 class TIter;
36 
39 
41 
42 class TCollection : public TObject {
43 
44 private:
45  static TCollection *fgCurrentCollection; //used by macro R__FOR_EACH
46  static TObjectTable *fgGarbageCollection; //used by garbage collector
47  static Bool_t fgEmptyingGarbage; //used by garbage collector
48  static Int_t fgGarbageStack; //used by garbage collector
49 
50  TCollection(const TCollection &); //private and not-implemented, collections
51  void operator=(const TCollection &); //are too complex to be automatically copied
52 
53 protected:
54  enum { kIsOwner = BIT(14) };
55 
56  TString fName; //name of the collection
57  Int_t fSize; //number of elements in collection
58 
59  TCollection() : fName(), fSize(0) { }
60 
61  virtual void PrintCollectionHeader(Option_t* option) const;
62  virtual const char* GetCollectionEntryName(TObject* entry) const;
63  virtual void PrintCollectionEntry(TObject* entry, Option_t* option, Int_t recurse) const;
64 
65 public:
67 
68  virtual ~TCollection() { }
69  virtual void Add(TObject *obj) = 0;
70  void AddVector(TObject *obj1, ...);
71  virtual void AddAll(const TCollection *col);
72  Bool_t AssertClass(TClass *cl) const;
73  void Browse(TBrowser *b);
74  Int_t Capacity() const { return fSize; }
75  virtual void Clear(Option_t *option="") = 0;
76  virtual TObject *Clone(const char *newname="") const;
77  Int_t Compare(const TObject *obj) const;
78  Bool_t Contains(const char *name) const { return FindObject(name) != 0; }
79  Bool_t Contains(const TObject *obj) const { return FindObject(obj) != 0; }
80  virtual void Delete(Option_t *option="") = 0;
81  virtual void Draw(Option_t *option="");
82  virtual void Dump() const ;
83  virtual TObject *FindObject(const char *name) const;
84  TObject *operator()(const char *name) const;
85  virtual TObject *FindObject(const TObject *obj) const;
86  virtual Int_t GetEntries() const { return GetSize(); }
87  virtual const char *GetName() const;
88  virtual TObject **GetObjectRef(const TObject *obj) const = 0;
89  virtual Int_t GetSize() const { return fSize; }
90  virtual Int_t GrowBy(Int_t delta) const;
91  ULong_t Hash() const { return fName.Hash(); }
92  Bool_t IsArgNull(const char *where, const TObject *obj) const;
93  virtual Bool_t IsEmpty() const { return GetSize() <= 0; }
94  virtual Bool_t IsFolder() const { return kTRUE; }
95  Bool_t IsOwner() const { return TestBit(kIsOwner); }
96  Bool_t IsSortable() const { return kTRUE; }
97  virtual void ls(Option_t *option="") const ;
98  virtual Bool_t Notify();
99  virtual TIterator *MakeIterator(Bool_t dir = kIterForward) const = 0;
101  virtual void Paint(Option_t *option="");
102  virtual void Print(Option_t *option="") const;
103  virtual void Print(Option_t *option, Int_t recurse) const;
104  virtual void Print(Option_t *option, const char* wildcard, Int_t recurse=1) const;
105  virtual void Print(Option_t *option, TPRegexp& regexp, Int_t recurse=1) const;
106  virtual void RecursiveRemove(TObject *obj);
107  virtual TObject *Remove(TObject *obj) = 0;
108  virtual void RemoveAll(TCollection *col);
109  void RemoveAll() { Clear(); }
110  void SetCurrentCollection();
111  void SetName(const char *name) { fName = name; }
112  virtual void SetOwner(Bool_t enable = kTRUE);
113  virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0);
114  virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0) const;
115 
117  static void StartGarbageCollection();
118  static void GarbageCollect(TObject *obj);
119  static void EmptyGarbageCollection();
120 
121  TIter begin() const;
122  TIter end() const;
123 
124  ClassDef(TCollection,3) //Collection abstract base class
125 };
126 
127 
128 //////////////////////////////////////////////////////////////////////////
129 // //
130 // TIter //
131 // //
132 // Iterator wrapper. Type of iterator used depends on type of //
133 // collection. //
134 // //
135 //////////////////////////////////////////////////////////////////////////
136 
137 class TIter {
138 
139 private:
140  TIterator *fIterator; //collection iterator
141 
142 protected:
143  TIter() : fIterator(nullptr) { }
144 
145 public:
147  : fIterator(col ? col->MakeIterator(dir) : 0) { }
148  TIter(TIterator *it) : fIterator(it) { }
149  TIter(const TIter &iter);
150  TIter &operator=(const TIter &rhs);
151  virtual ~TIter() { SafeDelete(fIterator); }
152  TObject *operator()() { return Next(); }
153  TObject *Next() { return fIterator ? fIterator->Next() : nullptr; }
154  const TCollection *GetCollection() const { return fIterator ? fIterator->GetCollection() : nullptr; }
155  Option_t *GetOption() const { return fIterator ? fIterator->GetOption() : ""; }
156  void Reset() { if (fIterator) fIterator->Reset(); }
157  TIter &operator++() { Next(); return *this; }
158  Bool_t operator==(const TIter &aIter) const {
159  if (fIterator == nullptr)
160  return aIter.fIterator == nullptr || **aIter.fIterator == nullptr;
161  if (aIter.fIterator == nullptr)
162  return fIterator == nullptr || **fIterator == nullptr;
163  return *fIterator == *aIter.fIterator;
164  }
165  Bool_t operator!=(const TIter &aIter) const {
166  return !(*this == aIter);
167  }
168  TObject *operator*() const { return fIterator ? *(*fIterator): nullptr; }
169  TIter &Begin();
170  static TIter End();
171 
172  ClassDef(TIter,0) //Iterator wrapper
173 };
174 
175 template <class T>
176 class TIterCategory: public TIter, public std::iterator_traits<typename T::Iterator_t> {
177 
178 public:
179  TIterCategory(const TCollection *col, Bool_t dir = kIterForward) : TIter(col, dir) { }
181  virtual ~TIterCategory() { }
182  TIterCategory &Begin() { TIter::Begin(); return *this; }
183  static TIterCategory End() { return TIterCategory(static_cast<TIterator*>(nullptr)); }
184 };
185 
186 
187 inline TIter TCollection::begin() const { return ++(TIter(this)); }
188 inline TIter TCollection::end() const { return TIter::End(); }
189 
190 
191 //---- R__FOR_EACH macro -------------------------------------------------------
192 
193 // Macro to loop over all elements of a list of type "type" while executing
194 // procedure "proc" on each element
195 
196 #define R__FOR_EACH(type,proc) \
197  SetCurrentCollection(); \
198  TIter _NAME3_(nxt_,type,proc)(TCollection::GetCurrentCollection()); \
199  type *_NAME3_(obj_,type,proc); \
200  while ((_NAME3_(obj_,type,proc) = (type*) _NAME3_(nxt_,type,proc)())) \
201  _NAME3_(obj_,type,proc)->proc
202 
203 #endif
TIter end() const
Definition: TCollection.h:188
virtual TObject * Remove(TObject *obj)=0
static Int_t fgGarbageStack
Definition: TCollection.h:48
void Begin(Int_t type)
virtual void Clear(Option_t *option="")=0
const TCollection * GetCollection() const
Definition: TCollection.h:154
virtual Option_t * GetOption() const
Definition: TIterator.h:40
virtual void Reset()=0
R__EXTERN TVirtualMutex * gCollectionMutex
Definition: TCollection.h:40
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:159
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:75
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:86
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:32
static TIterCategory End()
Definition: TCollection.h:183
virtual TIterator * MakeReverseIterator() const
Definition: TCollection.h:100
virtual Bool_t IsFolder() const
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects)...
Definition: TCollection.h:94
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
static TCollection * fgCurrentCollection
Definition: TCollection.h:45
TString fName
Definition: TCollection.h:56
Option_t * GetOption() const
Definition: TCollection.h:155
Bool_t Contains(const char *name) const
Definition: TCollection.h:78
virtual TObject * Clone(const char *newname="") const
Make a clone of an collection using the Streamer facility.
Float_t delta
Iterator abstract base class.
Definition: TIterator.h:30
void Reset()
Definition: TCollection.h:156
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition: TString.cxx:616
virtual void Delete(Option_t *option="")=0
Delete this object.
#define SafeDelete(p)
Definition: RConfig.h:499
TIterator * fIterator
Definition: TCollection.h:140
#define ClassDef(name, id)
Definition: Rtypes.h:297
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:179
Bool_t Contains(const TObject *obj) const
Definition: TCollection.h:79
const Bool_t kIterForward
Definition: TCollection.h:37
Bool_t IsOwner() const
Definition: TCollection.h:95
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:165
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
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:151
TIter(TIterator *it)
Definition: TCollection.h:148
static TCollection * GetCurrentCollection()
Return the globally accessible collection.
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const =0
TIter & operator++()
Definition: TCollection.h:157
virtual ~TCollection()
Definition: TCollection.h:68
TObject * Next()
Definition: TCollection.h:153
Collection abstract base class.
Definition: TCollection.h:42
virtual void Paint(Option_t *option="")
Paint all objects in this collection.
Int_t fSize
Definition: TCollection.h:57
virtual const TCollection * GetCollection() const =0
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:71
static void StartGarbageCollection()
Set up for garbage collection.
TIter begin() const
Definition: TCollection.h:187
TObject * operator()()
Definition: TCollection.h:152
This class registers all instances of TObject and its derived classes in a hash table.
Definition: TObjectTable.h:35
void SetName(const char *name)
Definition: TCollection.h:111
virtual Bool_t Notify()
&#39;Notify&#39; all objects in this collection.
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:47
static TIter End()
Pointing to the element after the last - to a nullptr value in our case.
void RemoveAll()
Definition: TCollection.h:109
static void GarbageCollect(TObject *obj)
Add to the list of things to be cleaned up.
virtual Bool_t IsEmpty() const
Definition: TCollection.h:93
virtual ~TIterCategory()
Definition: TCollection.h:181
TIter(const TCollection *col, Bool_t dir=kIterForward)
Definition: TCollection.h:146
virtual void ls(Option_t *option="") const
List (ls) all objects in this collection.
static TObjectTable * fgGarbageCollection
Definition: TCollection.h:46
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.
Int_t Capacity() const
Definition: TCollection.h:74
virtual TObject * Next()=0
TObject * operator*() const
Definition: TCollection.h:168
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:182
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:38
Bool_t operator==(const TIter &aIter) const
Definition: TCollection.h:158
virtual void Print(Option_t *option="") const
Default print for collections, calls Print(option, 1).
Bool_t IsSortable() const
Definition: TCollection.h:96
virtual Int_t GetSize() const
Definition: TCollection.h:89
TObject * operator()(const char *name) const
Find an object in this collection by name.
const Bool_t kTRUE
Definition: RtypesCore.h:91
TIterCategory(TIterator *it)
Definition: TCollection.h:180
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write all objects in this collection.
virtual const char * GetName() const
Return name of this collection.
ULong_t Hash() const
Return hash value for this object.
Definition: TCollection.h:91