ROOT logo
// @(#)root/table:$Id: TGenericTable.h 27157 2009-01-15 14:05:12Z brun $
// Author: Valery Fine(fine@bnl.gov)   30/06/2001

#ifndef ROOT_TGenericTable
#define ROOT_TGenericTable

#include "TTable.h"

//////////////////////////////////////////////////////////////////////////
//                                                                      //
//  TGenericTable                                                       //
//                                                                      //
//  This is the class to represent the array of C-struct                //
//  defined at run-time                                                 //
//                                                                      //
//////////////////////////////////////////////////////////////////////////
class TGenericTable : public TTable {
protected:                                        
   TTableDescriptor *fColDescriptors;     
   virtual TTableDescriptor *GetDescriptorPointer() const { return fColDescriptors;}                 
   virtual void SetDescriptorPointer(TTableDescriptor *list) { fColDescriptors = list;}        
   void SetGenericType(){ TTable::SetType(GetDescriptorPointer()->GetName()); }
 
public:
   class iterator {
   protected:
      UInt_t  fRowSize;
      char   *fCurrentRow;
      iterator():  fRowSize(0), fCurrentRow(0) {}
   public:
      iterator(UInt_t size, char &rowPtr): fRowSize(size), fCurrentRow(&rowPtr){}
      iterator(const TTable &t, char &rowPtr): fRowSize(t.GetRowSize()), fCurrentRow(&rowPtr){}
      iterator(const TTable &t): fRowSize(t.GetRowSize()), fCurrentRow(0){}
      iterator(const iterator& iter) : fRowSize (iter.fRowSize), fCurrentRow(iter.fCurrentRow){}
      iterator &operator=(const iterator& iter) { fRowSize = iter.fRowSize; fCurrentRow = iter.fCurrentRow; return *this;}
      iterator &operator++()    { if (fCurrentRow) fCurrentRow+=fRowSize; return *this;}
      void     operator++(int) { if (fCurrentRow) fCurrentRow+=fRowSize;}
      iterator &operator--()    { if (fCurrentRow) fCurrentRow-=fRowSize; return *this;}
      void      operator--(int) { if (fCurrentRow) fCurrentRow-=fRowSize;}
      iterator &operator+(Int_t idx) { if (fCurrentRow) fCurrentRow+=idx*fRowSize; return *this;}
      iterator &operator-(Int_t idx) { if (fCurrentRow) fCurrentRow-=idx*fRowSize; return *this;}
      Int_t operator-(const iterator &it) const { return (fCurrentRow-it.fCurrentRow)/fRowSize; }
             char *operator *(){ return fCurrentRow;}
             Bool_t operator==(const iterator &t) const { return (fCurrentRow == t.fCurrentRow); }
      Bool_t operator!=(const iterator &t) const { return !operator==(t); }
   };                                        
   TGenericTable() : TTable("TGenericTable",-1), fColDescriptors(0) {SetType("generic");}      

   // Create TGenericTable by C structure name provided
   TGenericTable(const char *structName, const char *name);
   TGenericTable(const char *structName, Int_t n);
   TGenericTable(const char *structName, const char *name,Int_t n);

   // Create TGenericTable by TTableDescriptor pointer
   TGenericTable(const TTableDescriptor &dsc, const char *name);
   TGenericTable(const TTableDescriptor &dsc, Int_t n);
   TGenericTable(const TTableDescriptor &dsc,const char *name,Int_t n);

   virtual ~TGenericTable();

   char  *GetTable(Int_t i=0)   const { return ((char *)GetArray())+i*GetRowSize();}     
   TTableDescriptor  *GetTableDescriptors() const { return GetDescriptorPointer();}
   TTableDescriptor  *GetRowDescriptors()   const { return GetDescriptorPointer();}
   char &operator[](Int_t i){ assert(i>=0 && i < GetNRows()); return *GetTable(i); }             
   const char &operator[](Int_t i) const { assert(i>=0 && i < GetNRows()); return *((const char *)(GetTable(i))); } 
   iterator begin()        { return ((const TGenericTable *)this)->begin();}
   iterator begin() const  {                      return GetNRows() ? iterator(*this, *GetTable(0)):end();}
   iterator end()   { return ((const TGenericTable *)this)->end(); }
   iterator end()   const  {Long_t i = GetNRows(); return i? iterator(*this, *GetTable(i)):iterator(*this);}
   ClassDef(TGenericTable,4) // Generic array of C-structure (a'la STL vector)
};
 
#endif
 TGenericTable.h:1
 TGenericTable.h:2
 TGenericTable.h:3
 TGenericTable.h:4
 TGenericTable.h:5
 TGenericTable.h:6
 TGenericTable.h:7
 TGenericTable.h:8
 TGenericTable.h:9
 TGenericTable.h:10
 TGenericTable.h:11
 TGenericTable.h:12
 TGenericTable.h:13
 TGenericTable.h:14
 TGenericTable.h:15
 TGenericTable.h:16
 TGenericTable.h:17
 TGenericTable.h:18
 TGenericTable.h:19
 TGenericTable.h:20
 TGenericTable.h:21
 TGenericTable.h:22
 TGenericTable.h:23
 TGenericTable.h:24
 TGenericTable.h:25
 TGenericTable.h:26
 TGenericTable.h:27
 TGenericTable.h:28
 TGenericTable.h:29
 TGenericTable.h:30
 TGenericTable.h:31
 TGenericTable.h:32
 TGenericTable.h:33
 TGenericTable.h:34
 TGenericTable.h:35
 TGenericTable.h:36
 TGenericTable.h:37
 TGenericTable.h:38
 TGenericTable.h:39
 TGenericTable.h:40
 TGenericTable.h:41
 TGenericTable.h:42
 TGenericTable.h:43
 TGenericTable.h:44
 TGenericTable.h:45
 TGenericTable.h:46
 TGenericTable.h:47
 TGenericTable.h:48
 TGenericTable.h:49
 TGenericTable.h:50
 TGenericTable.h:51
 TGenericTable.h:52
 TGenericTable.h:53
 TGenericTable.h:54
 TGenericTable.h:55
 TGenericTable.h:56
 TGenericTable.h:57
 TGenericTable.h:58
 TGenericTable.h:59
 TGenericTable.h:60
 TGenericTable.h:61
 TGenericTable.h:62
 TGenericTable.h:63
 TGenericTable.h:64
 TGenericTable.h:65
 TGenericTable.h:66
 TGenericTable.h:67
 TGenericTable.h:68
 TGenericTable.h:69
 TGenericTable.h:70
 TGenericTable.h:71
 TGenericTable.h:72
 TGenericTable.h:73