TCollection.h

Go to the documentation of this file.
00001 // @(#)root/cont:$Id: TCollection.h 37411 2010-12-08 17:42:11Z pcanal $
00002 // Author: Fons Rademakers   13/08/95
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TCollection
00013 #define ROOT_TCollection
00014 
00015 
00016 //////////////////////////////////////////////////////////////////////////
00017 //                                                                      //
00018 // TCollection                                                          //
00019 //                                                                      //
00020 // Collection abstract base class. This class inherits from TObject     //
00021 // because we want to be able to have collections of collections.       //
00022 //                                                                      //
00023 //////////////////////////////////////////////////////////////////////////
00024 
00025 #ifndef ROOT_TObject
00026 #include "TObject.h"
00027 #endif
00028 
00029 #ifndef ROOT_TIterator
00030 #include "TIterator.h"
00031 #endif
00032 
00033 #ifndef ROOT_TString
00034 #include "TString.h"
00035 #endif
00036 
00037 
00038 class TClass;
00039 class TObjectTable;
00040 class TVirtualMutex;
00041 
00042 const Bool_t kIterForward  = kTRUE;
00043 const Bool_t kIterBackward = !kIterForward;
00044 
00045 R__EXTERN TVirtualMutex *gCollectionMutex;
00046 
00047 class TCollection : public TObject {
00048 
00049 private:
00050    static TCollection  *fgCurrentCollection;  //used by macro R__FOR_EACH
00051    static TObjectTable *fgGarbageCollection;  //used by garbage collector
00052    static Bool_t        fgEmptyingGarbage;    //used by garbage collector
00053    static Int_t         fgGarbageStack;       //used by garbage collector
00054 
00055    TCollection(const TCollection &);    //private and not-implemented, collections
00056    void operator=(const TCollection &); //are too complex to be automatically copied
00057 
00058 protected:
00059    enum { kIsOwner = BIT(14) };
00060 
00061    TString   fName;               //name of the collection
00062    Int_t     fSize;               //number of elements in collection
00063 
00064    TCollection() : fName(), fSize(0) { }
00065 
00066    virtual void        PrintCollectionHeader(Option_t* option) const;
00067    virtual const char* GetCollectionEntryName(TObject* entry) const;
00068    virtual void        PrintCollectionEntry(TObject* entry, Option_t* option, Int_t recurse) const;
00069 
00070 public:
00071    enum { kInitCapacity = 16, kInitHashTableCapacity = 17 };
00072 
00073    virtual            ~TCollection() { }
00074    virtual void       Add(TObject *obj) = 0;
00075    void               AddVector(TObject *obj1, ...);
00076    virtual void       AddAll(const TCollection *col);
00077    Bool_t             AssertClass(TClass *cl) const;
00078    void               Browse(TBrowser *b);
00079    Int_t              Capacity() const { return fSize; }
00080    virtual void       Clear(Option_t *option="") = 0;
00081    virtual TObject   *Clone(const char *newname="") const;
00082    Int_t              Compare(const TObject *obj) const;
00083    Bool_t             Contains(const char *name) const { return FindObject(name) != 0; }
00084    Bool_t             Contains(const TObject *obj) const { return FindObject(obj) != 0; }
00085    virtual void       Delete(Option_t *option="") = 0;
00086    virtual void       Draw(Option_t *option="");
00087    virtual void       Dump() const ;
00088    virtual TObject   *FindObject(const char *name) const;
00089    TObject           *operator()(const char *name) const;
00090    virtual TObject   *FindObject(const TObject *obj) const;
00091    virtual Int_t      GetEntries() const { return GetSize(); }
00092    virtual const char *GetName() const;
00093    virtual TObject  **GetObjectRef(const TObject *obj) const = 0;
00094    virtual Int_t      GetSize() const { return fSize; }
00095    virtual Int_t      GrowBy(Int_t delta) const;
00096    ULong_t            Hash() const { return fName.Hash(); }
00097    Bool_t             IsArgNull(const char *where, const TObject *obj) const;
00098    virtual Bool_t     IsEmpty() const { return GetSize() <= 0; }
00099    virtual Bool_t     IsFolder() const { return kTRUE; }
00100    Bool_t             IsOwner() const { return TestBit(kIsOwner); }
00101    Bool_t             IsSortable() const { return kTRUE; }
00102    virtual void       ls(Option_t *option="") const ;
00103    virtual TIterator *MakeIterator(Bool_t dir = kIterForward) const = 0;
00104    virtual TIterator *MakeReverseIterator() const { return MakeIterator(kIterBackward); }
00105    virtual void       Paint(Option_t *option="");
00106    virtual void       Print(Option_t *option="") const;
00107    virtual void       Print(Option_t *option, Int_t recurse) const;
00108    virtual void       Print(Option_t *option, const char* wildcard, Int_t recurse=1) const;
00109    virtual void       Print(Option_t *option, TPRegexp& regexp, Int_t recurse=1) const;
00110    virtual void       RecursiveRemove(TObject *obj);
00111    virtual TObject   *Remove(TObject *obj) = 0;
00112    virtual void       RemoveAll(TCollection *col);
00113    void               RemoveAll() { Clear(); }
00114    void               SetCurrentCollection();
00115    void               SetName(const char *name) { fName = name; }
00116    virtual void       SetOwner(Bool_t enable = kTRUE);
00117    virtual Int_t      Write(const char *name=0, Int_t option=0, Int_t bufsize=0);
00118    virtual Int_t      Write(const char *name=0, Int_t option=0, Int_t bufsize=0) const;
00119 
00120    static TCollection  *GetCurrentCollection();
00121    static void          StartGarbageCollection();
00122    static void          GarbageCollect(TObject *obj);
00123    static void          EmptyGarbageCollection();
00124 
00125    ClassDef(TCollection,3)  //Collection abstract base class
00126 };
00127 
00128 
00129 //////////////////////////////////////////////////////////////////////////
00130 //                                                                      //
00131 // TIter                                                                //
00132 //                                                                      //
00133 // Iterator wrapper. Type of iterator used depends on type of           //
00134 // collection.                                                          //
00135 //                                                                      //
00136 //////////////////////////////////////////////////////////////////////////
00137 
00138 class TIter {
00139 
00140 private:
00141    TIterator    *fIterator;         //collection iterator
00142 
00143 protected:
00144    TIter() : fIterator(nullptr) { }
00145 
00146 public:
00147    TIter(const TCollection *col, Bool_t dir = kIterForward)
00148          : fIterator(col ? col->MakeIterator(dir) : 0) { }
00149    TIter(TIterator *it) : fIterator(it) { }
00150    TIter(const TIter &iter);
00151    TIter &operator=(const TIter &rhs);
00152    virtual ~TIter() { SafeDelete(fIterator); }
00153    TObject           *operator()() { return Next(); }
00154    TObject           *Next() { return fIterator ? fIterator->Next() : nullptr; }
00155    const TCollection *GetCollection() const { return fIterator ? fIterator->GetCollection() : nullptr; }
00156    Option_t          *GetOption() const { return fIterator ? fIterator->GetOption() : ""; }
00157    void               Reset() { if (fIterator) fIterator->Reset(); }
00158    TIter             &operator++() { Next(); return *this; }
00159    bool               operator!=(const TIter &aIter) const { return ((*fIterator) != *(aIter.fIterator)); }
00160    TObject           *operator*() const { return *(*fIterator); }
00161    TIter             &Begin();
00162    static TIter       End();
00163 
00164    ClassDef(TIter,0)  //Iterator wrapper
00165 };
00166 
00167 template <class T>
00168 class TIterCategory: public TIter, public std::iterator_traits<typename T::Iterator_t> {
00169 
00170 public:
00171    TIterCategory(const TCollection *col, Bool_t dir = kIterForward) : TIter(col, dir) { }
00172    TIterCategory(TIterator *it) : TIter(it) { }
00173    virtual ~TIterCategory() { }
00174    TIterCategory &Begin() { TIter::Begin(); return *this; }
00175    static TIterCategory End() { return TIterCategory(static_cast<TIterator*>(nullptr)); }
00176 };
00177 
00178 
00179 //---- R__FOR_EACH macro -------------------------------------------------------
00180 
00181 // Macro to loop over all elements of a list of type "type" while executing
00182 // procedure "proc" on each element
00183 
00184 #define R__FOR_EACH(type,proc) \
00185     SetCurrentCollection(); \
00186     TIter _NAME3_(nxt_,type,proc)(TCollection::GetCurrentCollection()); \
00187     type *_NAME3_(obj_,type,proc); \
00188     while ((_NAME3_(obj_,type,proc) = (type*) _NAME3_(nxt_,type,proc)())) \
00189        _NAME3_(obj_,type,proc)->proc
00190 
00191 #endif

Generated on Tue Jul 5 14:11:26 2011 for ROOT_528-00b_version by  doxygen 1.5.1