[ROOT] Nested classes

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Mon May 28 2001 - 15:16:02 MEST


Hi ROOT'ers, 

I'm starting to use nested classes for data storage, since that's
a very neat of encapsulating data types.  For example 


  class DataTable : public TObject {
  private:
     TClonesArray fTable; 
     UInt_t       fNEntries; 
     Float_t      fSumEnergy; 
     Float_t      fAvarageEnergy; 
     Flaot_t      fEnergyVariance; 
     
  public: 
     class Data : public TObject {
     private:
       UInt_t     fId; 
       Float_t    fEnergy; 
     public: 
       Data(UInt_t id=0, Float_t energy=0) : fId(id), fEnergy(energy) {} 
       virtual ~Data() {}
       UInt_t  GetId() const { return fId; }
       void    SetId(UInt_t id) { fId = id; }
       Float_t GetEnergy() const { return fEnergy; }
       void    SetEnergy(Float_t energy) { fEnergy = energy; }

       ClassDef(Data, 1) // 
     };
 
     DataTable() : fTable("DataTable::Data") {} 
     virtual ~DataTable() {} 

     Data* AddData(UInt_t id, Float_t energy) {
       return new (fTable[fEntries++]) Data(id, energy);
       fSumEnergy += energy; 
       ...
     } 
     Data* FindData(UInt_t id) { 
       TIter next(&fTable); 
       Data* data = 0;
       while ((data = (Data*)next())) 
         if (data->GetId() == id) 
	   return data; 
       return 0;
     }
 
     ClassDef(DataTable, 1) // Table of data 
   };

As I've pointed out earlier, 

  http://root.cern.ch/root/roottalk/roottalk01/1561.html
  http://root.cern.ch/root/roottalk/roottalk01/1562.html

one has to do a couple of tricks to make this work. 

However, there's another problem in all this, which has to do with
the MakeProject feature of TFile.  When asking to make the classes
from the StreamerInfo, ROOT makes the files: 

   DataTable.h 
   DataTable::Data.h 

but DataTable::Data isn't mentioned as a nested class in DataTable.h!,
so in essence DataTable::Data.h will define a namespace, not a nested
class, which I believe is the wrong behaviour.   There are other
problems too, pertaining to the '::' in the class name. 

So I suggest that ROOT makes a ClassDefNested(mother, child, version)
macro and maybe a ClassImpNested(mother, child) macro too, that will
take care of these things.  Perhaps the dictionary for a given class
should say wether it is a nested class or not, and perhaps also have a
list of class nested in it. 

Hope you find this useful. 

Yours, 

Christian  -----------------------------------------------------------
Holm Christensen                             Phone:  (+45) 35 35 96 91 
  Sankt Hansgade 23, 1. th.                  Office: (+45) 353  25 305 
  DK-2200 Copenhagen N                       Web:    www.nbi.dk/~cholm    
  Denmark                                    Email:       cholm@nbi.dk



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:47 MET