Writing a singleton to a root file

Dear ROOT experts,

I have a singleton class which contains a map of custom class and I would like to write it to a root file:

class Volume : public TObject
{
   public:
      Volume();
      /* other stuff */
     ClassDef(Volume,1)
}

class Info : public TObject
{
   private:
      map<const char *, Volume *> volumes;
      void init(); // fill volumes
      Info() { init(); }
   public:
      Info &Instance() { static Info instance; return instance; } 
   ClassDef(Info,1)
}

I set up all the code and the makefile in order to compile with cint and it works, except that when I try

Info::Instance().Write("Info");

it says

I understand that Info need a public constructor in order to be read from the file, but a singleton cannot have a public constructor…

What I need is the following:

  1. At the beginning of the program, the volumes map must be filled and must be globally available to all the program (hence Info must be a singleton: it must happen only once during the program and anyone can call it without worrying of its initialization)
  2. The volumes map is saved to a root file
  3. Another program open the root file and load back in memory the volumes map which must be globally available

I was thinking of using the Info class for both the writing and reading part, but the singleton is a problem.
How can I solve this problem?

Thanks,
Claudio

I see your problem as quite artificial and forced - first you create a singleton with all its problems and ugliness and now you meet another problem - ROOT’s I/O and requirement to have a public default ctor.

Well, after all, why not just use a global map/THashTable and read/write it ?

Anyway, if you really like the idea of singleton but facing the problem with public ctor … make it public and make a dtor private + overload as private static operators new and delete (preventing a user from creating your object on a stack or in dynamic memory, though he can still use ‘::new Info’).

I’m not quite sure that IO will not complain about new/delete/dtor being private.

Hi tpochep, thanks for your suggestion.
I ended up using a temporary TClonesArray to write the map to the file and read it back, while retaining the singleton pattern.