HYDRA_development_version
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hcontfact.cc
Go to the documentation of this file.
1 //*-- AUTHOR : Ilse Koenig
2 //*-- Modified last : 09/01/2002 by Ilse Koenig
3 
4 //_HADES_CLASS_DESCRIPTION
5 /////////////////////////////////////////////////////////////
6 //
7 // HContFact
8 //
9 // Base class of all factories for the parameter containers
10 //
11 /////////////////////////////////////////////////////////////
12 using namespace std;
13 #include "hcontfact.h"
14 #include "hades.h"
15 #include "hruntimedb.h"
16 #include "TObjString.h"
17 #include <iostream>
18 #include <iomanip>
19 
22 
23 HContainer::HContainer() { contexts=0; }
24  // Default constructor
25 
26 HContainer::HContainer(const Char_t* name, const Char_t* title,
27  const Char_t* defContext)
28  : TNamed(name, title) {
29  // Constructor
30  // Arguments: name = name of the corresponding parameter container
31  // title = title of this parameter container
32  // defContext = default context of this parameter container
33  contexts=new TList;
34  addContext(defContext);
35  actualContext="";
36 }
37 
39  // Destructor deletes the list of accepted contexts
40  if (contexts) {
41  contexts->Delete();
42  delete contexts;
43  }
44 }
45 
46 void HContainer::addContext(const Char_t* name) {
47  // Adds a context to the list of accepted contexts
48  TObjString* c=new TObjString(name);
49  contexts->Add(c);
50 }
51 
52 Bool_t HContainer::setActualContext(const Char_t* c) {
53  // The function sets the actual context for the container, if it is in the list of
54  // accepted contexts. When the actual context was already set before, it prints a warning
55  // and ignores the second setting.
56  // The function returns kFALSE, when the context is not in the list.
57  if (contexts->FindObject(c)) {
58  if (actualContext.IsNull()) actualContext=c;
59  else Warning("addContext",
60  "Actual context of parameter container %s already defined as %s",
61  GetName(),actualContext.Data());
62  return kTRUE;
63  }
64  return kFALSE;
65 }
66 
68  // Returns the default context
69  return ((TObjString*)contexts->At(0))->String().Data();
70 }
71 
73  // prints the name, title of the container together with the actual context set
74  // or all possible contexts, when the actual context was not set
75  cout<<fName<<"\t"<<fTitle<<"\n";
76  if (!actualContext.IsNull()) cout<<" actual context: "<<actualContext<<"\n";
77  else {
78  TIter next(contexts);
79  Int_t i=0;
80  TObjString* c;
81  cout<<" all contexts:"<<"\n";
82  while ((c=(TObjString*)next())) {
83  if (c->String().IsNull()) cout<<" \"\"";
84  else cout<<" "<<c->String();
85  if (i==0) cout<<"\t default";
86  cout<<"\n";
87  i++;
88  }
89  }
90 }
91 
93  // Returns the name of the parameter container used in the constructor and the
94  // runtime database.
95  // When the parameter container supportes different contexts (not only an empty string)
96  // and the actual context set is not the default context, the new name of the parameter
97  // container is concatinated as
98  // original container name + _ + actualcontext
99  TString cn=fName;
100  if (!actualContext.IsNull() && actualContext!=((TObjString*)contexts->At(0))->String()) {
101  cn+="_";
102  cn+=actualContext;
103  }
104  return cn;
105 }
106 
107 const Char_t* HContainer::getContext() {
108  // return the actual context, if set, or the default context
109  if (!actualContext.IsNull()) return actualContext.Data();
110  else return getDefaultContext();
111 }
112 
113 //------------------------------------------------------------------
114 
115 HContFact::HContFact() : TNamed() {
116  // Constructor creates a list to store objects of type HContainer
117  containers=new TList;
118 }
119 
121  // Destructor deletes the container list and its elements
122  containers->Delete();
123  delete containers;
124 }
125 
126 Bool_t HContFact::addContext(const Char_t* name) {
127  // Set the actual context in all containers, which accept this context
128  HContainer* c=0;
129  Bool_t found=kFALSE;
130  TIter next(containers);
131  while ((c=(HContainer*)next())) {
132  if (c->setActualContext(name)) found=kTRUE;
133  }
134  return found;
135 }
136 
137 HParSet* HContFact::getContainer(const Char_t* name) {
138  // Returns the pointer to the parameter container in the runtime database
139  // If this parameter container does not yet exit, it calls the function
140  // createContainer(HContainer*), which is implemented in the derived classes
141  // and calls the corresponding constructor. Then the pointer it added in the
142  // runtime database.
143  HContainer* c=(HContainer*)(containers->FindObject(name));
144  HParSet* cont=0;
145  if (c) {
146  TString cn=c->getConcatName();
147  HRuntimeDb* rtdb=gHades->getRuntimeDb();
148  if (!(cont=rtdb->findContainer(c->getConcatName().Data()))) {
149  if (strlen(c->getActualContext())==0) c->setActualContext(c->getDefaultContext());
150  cont=createContainer(c);
151  if (cont) rtdb->addContainer(cont);
152  else Error("getContainer(const Char_t* name)","Container %s not created",name);
153  }
154  }
155  return cont;
156 }
157 
159  // Loops over all containers in the list and calls their print() function
160  cout<<"---------------------------------------------------------------------------"<<"\n";
161  cout<<GetName()<<": "<<GetTitle()<<"\n";
162  cout<<"---------------------------------------------------------------------------"<<"\n";
163  HContainer* c;
164  TIter next(containers);
165  while ((c=(HContainer*)next())) c->print();
166 }
const Char_t * getDefaultContext()
Definition: hcontfact.cc:67
const Char_t * getContext()
Definition: hcontfact.cc:107
TList * containers
Definition: hcontfact.h:30
HParSet * getContainer(const Char_t *)
Definition: hcontfact.cc:137
Bool_t setActualContext(const Char_t *c)
Definition: hcontfact.cc:52
TList * contexts
Definition: hcontfact.h:13
HRuntimeDb * getRuntimeDb(void)
Definition: hades.h:111
TString getConcatName()
Definition: hcontfact.cc:92
void print()
Definition: hcontfact.cc:158
Bool_t addContainer(HParSet *)
Definition: hruntimedb.cc:103
void addContext(const Char_t *)
Definition: hcontfact.cc:46
const Char_t * getActualContext()
Definition: hcontfact.h:21
Definition: hparset.h:9
void print()
Definition: hcontfact.cc:72
Hades * gHades
Definition: hades.cc:1213
HParSet * findContainer(const Char_t *)
Definition: hruntimedb.cc:143
ClassImp(HContainer) ClassImp(HContFact) HContainer
Definition: hcontfact.cc:20
TString actualContext
Definition: hcontfact.h:14
Bool_t addContext(const Char_t *name)
Definition: hcontfact.cc:126
virtual HParSet * createContainer(HContainer *)
Definition: hcontfact.h:37
virtual ~HContFact()
Definition: hcontfact.cc:120