EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairContFact.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairContFact.cxx
1 #//*-- AUTHOR : Ilse Koenig
2 //*-- Created : 21/10/2004
3 
5 //
6 // FairContFact
7 //
8 // Base class of all factories for the parameter containers
9 //
11 
12 #include "FairContFact.h"
13 #include "FairRuntimeDb.h"
14 
15 #include "TObjString.h"
16 
17 
18 #include <iostream>
19 
20 using std::cout;
21 
24 
26  :TNamed(),
27  contexts(NULL),
28  actualContext(""),
29  fLogger(FairLogger::GetLogger())
30 {
31 }
32 // Default constructor
33 
34 FairContainer::FairContainer(const char* name, const char* title,
35  const char* defContext)
36  : TNamed(name, title),
37  contexts(new TList()),
38  actualContext(""),
39  fLogger(FairLogger::GetLogger())
40 {
41  // Constructor
42  // Arguments: name = name of the corresponding parameter container
43  // title = title of this parameter container
44  // defContext = default context of this parameter container
45  addContext(defContext);
46 }
47 
49 {
50  // Destructor deletes the list of accepted contexts
51  if (contexts) {
52  contexts->Delete();
53  delete contexts;
54  }
55 }
56 
58 {
59  // Adds a context to the list of accepted contexts
60  TObjString* c=new TObjString(name);
61  contexts->Add(c);
62 }
63 
65 {
66  // The function sets the actual context for the container, if it is in the list of
67  // accepted contexts. When the actual context was already set before, it prints a warning
68  // and ignores the second setting.
69  // The function returns kFALSE, when the context is not in the list.
70  if (contexts->FindObject(c)) {
71  if (actualContext.IsNull()) { actualContext=c; }
72  else Warning("addContext",
73  "Actual context of parameter container %s already defined as %s",
74  GetName(),actualContext.Data());
75  return kTRUE;
76  }
77  return kFALSE;
78 }
79 
81 {
82  // Returns the default context
83  return ((TObjString*)contexts->At(0))->String().Data();
84 }
85 
87 {
88  // prints the name, title of the container together with the actual context set
89  // or all possible contexts, when the actual context was not set
90  cout<<fName<<"\t"<<fTitle<<"\n";
91  if (!actualContext.IsNull()) { cout<<" actual context: "<<actualContext<<"\n"; }
92  else {
93  TIter next(contexts);
94  Int_t i=0;
95  TObjString* c;
96  cout<<" all contexts:"<<"\n";
97  while ((c=(TObjString*)next())) {
98  if (c->String().IsNull()) { cout<<" \"\""; }
99  else { cout<<" "<<c->String(); }
100  if (i==0) { cout<<"\t default"; }
101  cout<<"\n";
102  i++;
103  }
104  }
105 }
106 
108 {
109  // Returns the name of the parameter container used in the constructor and the
110  // runtime database.
111  // When the parameter container supportes different contexts (not only an empty string)
112  // and the actual context set is not the default context, the new name of the parameter
113  // container is concatinated as
114  // original container name + _ + actualcontext
115  TString cn=fName;
116  if (!actualContext.IsNull() && actualContext!=((TObjString*)contexts->At(0))->String()) {
117  cn+="_";
118  cn+=actualContext;
119  }
120  return cn;
121 }
122 
124 {
125  // return the actual context, if set, or the default context
126  if (!actualContext.IsNull()) { return actualContext.Data(); }
127  else { return getDefaultContext(); }
128 }
129 
130 //------------------------------------------------------------------
131 
133  : TNamed(),
134  containers(new TList),
135  fLogger(FairLogger::GetLogger())
136 {
137  // Constructor creates a list to store objects of type FairContainer
138  // containers=new TList;
139 }
140 
142 {
143  // Destructor deletes the container list and its elements
144  containers->Delete();
145  delete containers;
146 }
147 
148 Bool_t FairContFact::addContext(const char* name)
149 {
150  // Set the actual context in all containers, which accept this context
151  FairContainer* c=0;
152  Bool_t found=kFALSE;
153  TIter next(containers);
154  while ((c=(FairContainer*)next())) {
155  if (c->setActualContext(name)) { found=kTRUE; }
156  }
157  return found;
158 }
159 
161 {
162  // Returns the pointer to the parameter container in the runtime database
163  // If this parameter container does not yet exit, it calls the function
164  // createContainer(FairContainer*), which is implemented in the derived classes
165  // and calls the corresponding constructor. Then the pointer it added in the
166  // runtime database.
167  FairContainer* c=(FairContainer*)(containers->FindObject(name));
168 
169  FairParSet* cont=0;
170  if (c) {
171  TString cn=c->getConcatName();
173  if (!(cont=rtdb->findContainer(c->getConcatName().Data()))) {
174  if (strlen(c->getActualContext())==0) { c->setActualContext(c->getDefaultContext()); }
175  cont=createContainer(c);
176  if (cont) { rtdb->addContainer(cont); }
177  }
178  }
179  return cont;
180 }
181 
183 {
184  // Loops over all containers in the list and calls their print() function
185  cout<<"---------------------------------------------------------------------------"<<"\n";
186  cout<<GetName()<<": "<<GetTitle()<<"\n";
187  cout<<"---------------------------------------------------------------------------"<<"\n";
188  FairContainer* c;
189  TIter next(containers);
190  while ((c=(FairContainer*)next())) { c->print(); }
191 }