ROOT logo
//*-- AUTHOR : Ilse Koenig
//*-- Modified last : 09/01/2002 by Ilse Koenig

//_HADES_CLASS_DESCRIPTION 
/////////////////////////////////////////////////////////////
//
//  HContFact
//
//  Base class of all factories for the parameter containers 
//
/////////////////////////////////////////////////////////////
using namespace std;
#include "hcontfact.h"
#include "hades.h"
#include "hruntimedb.h"
#include "TObjString.h"
#include <iostream> 
#include <iomanip>

ClassImp(HContainer)
ClassImp(HContFact)

HContainer::HContainer() { contexts=0; }
  // Default constructor

HContainer::HContainer(const Char_t* name, const Char_t* title,
                       const Char_t* defContext)
           : TNamed(name, title) {
  // Constructor
  // Arguments:  name       = name of the corresponding parameter container
  //             title      = title of this parameter container
  //             defContext = default context of this parameter container  
  contexts=new TList;
  addContext(defContext);
  actualContext="";
}

HContainer::~HContainer() {
  // Destructor deletes the list of accepted contexts
  if (contexts) {
    contexts->Delete();
    delete contexts;
  }
}

void HContainer::addContext(const Char_t* name) {
  // Adds a context to the list of accepted contexts 
  TObjString* c=new TObjString(name);
  contexts->Add(c);  
}

Bool_t HContainer::setActualContext(const Char_t* c) {
  // The function sets the actual context for the container, if it is in the list of
  // accepted contexts. When the actual context was already set before, it prints a warning
  // and ignores the second setting.
  // The function returns kFALSE, when the context is not in the list. 
  if (contexts->FindObject(c)) {
    if (actualContext.IsNull()) actualContext=c;
    else Warning("addContext",
                 "Actual context of parameter container %s already defined as %s",
                 GetName(),actualContext.Data());
    return kTRUE;
  }
  return kFALSE;
}

const Char_t* HContainer::getDefaultContext() {
  // Returns the default context
  return ((TObjString*)contexts->At(0))->String().Data();
}

void HContainer::print() {
  // prints the name, title of the container together with the actual context set
  // or all possible contexts, when the actual context was not set
  cout<<fName<<"\t"<<fTitle<<"\n";
  if (!actualContext.IsNull()) cout<<"     actual context:  "<<actualContext<<"\n";
  else {
    TIter next(contexts);
    Int_t i=0;
    TObjString* c;
    cout<<"  all contexts:"<<"\n";
    while ((c=(TObjString*)next())) {
      if (c->String().IsNull()) cout<<"     \"\""; 
      else cout<<"     "<<c->String();
      if (i==0) cout<<"\t default";
      cout<<"\n";
      i++;
    }
  }
}

TString HContainer::getConcatName() {
  // Returns the name of the parameter container used in the constructor and the
  // runtime database.
  // When the parameter container supportes different contexts (not only an empty string)
  // and the actual context set is not the default context, the new name of the parameter
  // container is concatinated as
  //      original container name  +  _  +  actualcontext  
  TString cn=fName;
  if (!actualContext.IsNull() && actualContext!=((TObjString*)contexts->At(0))->String()) {
    cn+="_";
    cn+=actualContext;
  }
  return cn;
}

const Char_t* HContainer::getContext() {
  // return the actual context, if set, or the default context
  if (!actualContext.IsNull()) return actualContext.Data();
  else return getDefaultContext();
}

//------------------------------------------------------------------

HContFact::HContFact() : TNamed() {
  // Constructor creates a list to store objects of type HContainer
  containers=new TList;
}

HContFact::~HContFact() {
  // Destructor deletes the container list and its elements
  containers->Delete();
  delete containers;
}

Bool_t HContFact::addContext(const Char_t* name) {
  // Set the actual context in all containers, which accept this context
  HContainer* c=0;
  Bool_t found=kFALSE;
  TIter next(containers);
  while ((c=(HContainer*)next())) {
    if (c->setActualContext(name)) found=kTRUE;
  }
  return found;
}

HParSet* HContFact::getContainer(const Char_t* name) {
  // Returns the pointer to the parameter container in the runtime database
  // If this parameter container does not yet exit, it calls the function
  // createContainer(HContainer*), which is implemented in the derived classes
  // and calls the corresponding constructor. Then the pointer it added in the
  // runtime database.
  HContainer* c=(HContainer*)(containers->FindObject(name));
  HParSet* cont=0;
  if (c) {
    TString cn=c->getConcatName();
    HRuntimeDb* rtdb=gHades->getRuntimeDb();
    if (!(cont=rtdb->findContainer(c->getConcatName().Data()))) {
      if (strlen(c->getActualContext())==0) c->setActualContext(c->getDefaultContext());
      cont=createContainer(c);
      if (cont) rtdb->addContainer(cont);
      else Error("getContainer(const Char_t* name)","Container %s not created",name);
    }
  }
  return cont;
}

void HContFact::print() {
  // Loops over all containers in the list and calls their print() function
  cout<<"---------------------------------------------------------------------------"<<"\n";
  cout<<GetName()<<":  "<<GetTitle()<<"\n";
  cout<<"---------------------------------------------------------------------------"<<"\n";
  HContainer* c;
  TIter next(containers);
  while ((c=(HContainer*)next())) c->print();
}  
 hcontfact.cc:1
 hcontfact.cc:2
 hcontfact.cc:3
 hcontfact.cc:4
 hcontfact.cc:5
 hcontfact.cc:6
 hcontfact.cc:7
 hcontfact.cc:8
 hcontfact.cc:9
 hcontfact.cc:10
 hcontfact.cc:11
 hcontfact.cc:12
 hcontfact.cc:13
 hcontfact.cc:14
 hcontfact.cc:15
 hcontfact.cc:16
 hcontfact.cc:17
 hcontfact.cc:18
 hcontfact.cc:19
 hcontfact.cc:20
 hcontfact.cc:21
 hcontfact.cc:22
 hcontfact.cc:23
 hcontfact.cc:24
 hcontfact.cc:25
 hcontfact.cc:26
 hcontfact.cc:27
 hcontfact.cc:28
 hcontfact.cc:29
 hcontfact.cc:30
 hcontfact.cc:31
 hcontfact.cc:32
 hcontfact.cc:33
 hcontfact.cc:34
 hcontfact.cc:35
 hcontfact.cc:36
 hcontfact.cc:37
 hcontfact.cc:38
 hcontfact.cc:39
 hcontfact.cc:40
 hcontfact.cc:41
 hcontfact.cc:42
 hcontfact.cc:43
 hcontfact.cc:44
 hcontfact.cc:45
 hcontfact.cc:46
 hcontfact.cc:47
 hcontfact.cc:48
 hcontfact.cc:49
 hcontfact.cc:50
 hcontfact.cc:51
 hcontfact.cc:52
 hcontfact.cc:53
 hcontfact.cc:54
 hcontfact.cc:55
 hcontfact.cc:56
 hcontfact.cc:57
 hcontfact.cc:58
 hcontfact.cc:59
 hcontfact.cc:60
 hcontfact.cc:61
 hcontfact.cc:62
 hcontfact.cc:63
 hcontfact.cc:64
 hcontfact.cc:65
 hcontfact.cc:66
 hcontfact.cc:67
 hcontfact.cc:68
 hcontfact.cc:69
 hcontfact.cc:70
 hcontfact.cc:71
 hcontfact.cc:72
 hcontfact.cc:73
 hcontfact.cc:74
 hcontfact.cc:75
 hcontfact.cc:76
 hcontfact.cc:77
 hcontfact.cc:78
 hcontfact.cc:79
 hcontfact.cc:80
 hcontfact.cc:81
 hcontfact.cc:82
 hcontfact.cc:83
 hcontfact.cc:84
 hcontfact.cc:85
 hcontfact.cc:86
 hcontfact.cc:87
 hcontfact.cc:88
 hcontfact.cc:89
 hcontfact.cc:90
 hcontfact.cc:91
 hcontfact.cc:92
 hcontfact.cc:93
 hcontfact.cc:94
 hcontfact.cc:95
 hcontfact.cc:96
 hcontfact.cc:97
 hcontfact.cc:98
 hcontfact.cc:99
 hcontfact.cc:100
 hcontfact.cc:101
 hcontfact.cc:102
 hcontfact.cc:103
 hcontfact.cc:104
 hcontfact.cc:105
 hcontfact.cc:106
 hcontfact.cc:107
 hcontfact.cc:108
 hcontfact.cc:109
 hcontfact.cc:110
 hcontfact.cc:111
 hcontfact.cc:112
 hcontfact.cc:113
 hcontfact.cc:114
 hcontfact.cc:115
 hcontfact.cc:116
 hcontfact.cc:117
 hcontfact.cc:118
 hcontfact.cc:119
 hcontfact.cc:120
 hcontfact.cc:121
 hcontfact.cc:122
 hcontfact.cc:123
 hcontfact.cc:124
 hcontfact.cc:125
 hcontfact.cc:126
 hcontfact.cc:127
 hcontfact.cc:128
 hcontfact.cc:129
 hcontfact.cc:130
 hcontfact.cc:131
 hcontfact.cc:132
 hcontfact.cc:133
 hcontfact.cc:134
 hcontfact.cc:135
 hcontfact.cc:136
 hcontfact.cc:137
 hcontfact.cc:138
 hcontfact.cc:139
 hcontfact.cc:140
 hcontfact.cc:141
 hcontfact.cc:142
 hcontfact.cc:143
 hcontfact.cc:144
 hcontfact.cc:145
 hcontfact.cc:146
 hcontfact.cc:147
 hcontfact.cc:148
 hcontfact.cc:149
 hcontfact.cc:150
 hcontfact.cc:151
 hcontfact.cc:152
 hcontfact.cc:153
 hcontfact.cc:154
 hcontfact.cc:155
 hcontfact.cc:156
 hcontfact.cc:157
 hcontfact.cc:158
 hcontfact.cc:159
 hcontfact.cc:160
 hcontfact.cc:161
 hcontfact.cc:162
 hcontfact.cc:163
 hcontfact.cc:164
 hcontfact.cc:165
 hcontfact.cc:166