Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

TCollection.h

Go to the documentation of this file.
00001 // @(#)root/cont:$Name:  $:$Id: TCollection.h,v 1.12 2003/02/14 08:23:14 brun Exp $
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 
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 //                                                                      //
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 class TClass;
00038 class TObjectTable;
00039 
00040 
00041 const Bool_t kIterForward  = kTRUE;
00042 const Bool_t kIterBackward = !kIterForward;
00043 
00044 
00045 class TCollection : public TObject {
00046 
00047 private:
00048    static TCollection  *fgCurrentCollection;  //used by macro ForEach
00049    static TObjectTable *fgGarbageCollection;  //used by garbage collector
00050    static Bool_t        fgEmptyingGarbage;    //used by garbage collector
00051    static Int_t         fgGarbageStack;       //used by garbage collector
00052 
00053    TCollection(const TCollection &);    // private and not-implemented, collections
00054    void operator=(const TCollection &); // are too sensitive to be automatically copied
00055 
00056 protected:
00057    enum { kIsOwner = BIT(14) };
00058 
00059    TString   fName;               //name of the collection
00060    Int_t     fSize;               //number of elements in collection
00061 
00062    TCollection() : fSize(0) { }
00063 
00064 public:
00065    enum { kInitCapacity = 16, kInitHashTableCapacity = 17 };
00066 
00067    virtual            ~TCollection() { }
00068    virtual void       Add(TObject *obj) = 0;
00069    void               AddVector(TObject *obj1, ...);
00070    virtual void       AddAll(const TCollection *col);
00071    Bool_t             AssertClass(TClass *cl) const;
00072    void               Browse(TBrowser *b);
00073    Int_t              Capacity() const { return fSize; }
00074    virtual void       Clear(Option_t *option="") = 0;
00075    Bool_t             Contains(const char *name) const { return FindObject(name) != 0; }
00076    Bool_t             Contains(const TObject *obj) const { return FindObject(obj) != 0; }
00077    virtual void       Delete(Option_t *option="") = 0;
00078    virtual void       Draw(Option_t *option="");
00079    virtual void       Dump() const ;
00080    virtual TObject   *FindObject(const char *name) const;
00081    TObject           *operator()(const char *name) const;
00082    virtual TObject   *FindObject(const TObject *obj) const;
00083    virtual const char *GetName() const;
00084    virtual TObject  **GetObjectRef(const TObject *obj) const = 0;
00085    virtual Int_t      GetSize() const { return fSize; }
00086    virtual Int_t      GrowBy(Int_t delta) const;
00087    Bool_t             IsArgNull(const char *where, const TObject *obj) const;
00088    virtual Bool_t     IsEmpty() const { return GetSize() <= 0; }
00089    virtual Bool_t     IsFolder() const { return kTRUE; }
00090    Bool_t             IsOwner() const { return TestBit(kIsOwner); }
00091    virtual void       ls(Option_t *option="") const ;
00092    virtual TIterator *MakeIterator(Bool_t dir = kIterForward) const = 0;
00093    virtual TIterator *MakeReverseIterator() const { return MakeIterator(kIterBackward); }
00094    virtual void       Paint(Option_t *option="");
00095    virtual void       Print(Option_t *option="") const;
00096    virtual void       RecursiveRemove(TObject *obj);
00097    virtual TObject   *Remove(TObject *obj) = 0;
00098    virtual void       RemoveAll(TCollection *col);
00099    void               RemoveAll() { Clear(); }
00100    void               SetCurrentCollection();
00101    void               SetName(const char *name) { fName = name; }
00102    void               SetOwner(Bool_t enable = kTRUE) { enable ? SetBit(kIsOwner) : ResetBit(kIsOwner); }
00103    virtual Int_t      Write(const char *name=0, Int_t option=0, Int_t bufsize=0);
00104 
00105    static TCollection  *GetCurrentCollection();
00106    static void          StartGarbageCollection();
00107    static void          GarbageCollect(TObject *obj);
00108    static void          EmptyGarbageCollection();
00109 
00110    ClassDef(TCollection,3)  //Collection abstract base class
00111 };
00112 
00113 
00115 //                                                                      //
00116 // TIter                                                                //
00117 //                                                                      //
00118 // Iterator wrapper. Type of iterator used depends on type of           //
00119 // collection.                                                          //
00120 //                                                                      //
00122 
00123 class TIter {
00124 
00125 private:
00126    TIterator    *fIterator;         //collection iterator
00127 
00128 protected:
00129    TIter() : fIterator(0) { }
00130 
00131 public:
00132    TIter(const TCollection *col, Bool_t dir = kIterForward)
00133         : fIterator(col ? col->MakeIterator(dir) : 0) { }
00134    TIter(TIterator *it) : fIterator(it) { }
00135    TIter(const TIter &iter);
00136    TIter &operator=(const TIter &rhs);
00137    virtual            ~TIter() { SafeDelete(fIterator) }
00138    TObject           *operator()() { return fIterator ? fIterator->Next() : 0; }
00139    TObject           *Next() { return fIterator ? fIterator->Next() : 0; }
00140    const TCollection *GetCollection() const { return fIterator ? fIterator->GetCollection() : 0; }
00141    Option_t          *GetOption() const { return fIterator ? fIterator->GetOption() : ""; }
00142    void               Reset() { if (fIterator) fIterator->Reset(); }
00143 
00144    ClassDef(TIter,0)  //Iterator wrapper
00145 };
00146 
00147 
00148 //---- ForEach macro -----------------------------------------------------------
00149 
00150 // Macro to loop over all elements of a list of type "type" while executing
00151 // procedure "proc" on each element
00152 
00153 #define ForEach(type,proc) \
00154     SetCurrentCollection(); \
00155     TIter _NAME3_(type,proc,_nxt)(TCollection::GetCurrentCollection()); \
00156     type *_NAME3_(type,proc,_obj); \
00157     while ((_NAME3_(type,proc,_obj) = (type*) _NAME3_(type,proc,_nxt)())) \
00158        _NAME3_(type,proc,_obj)->proc
00159 
00160 #endif

Generated on Thu Dec 18 14:52:17 2003 for ROOT by doxygen1.2.16