FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairContFact.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 #//*-- AUTHOR : Ilse Koenig
9 //*-- Created : 21/10/2004
10 
12 //
13 // FairContFact
14 //
15 // Base class of all factories for the parameter containers
16 //
18 
19 #include "FairContFact.h"
20 
21 #include "FairLogger.h" // for FairLogger
22 #include "FairRuntimeDb.h" // for FairRuntimeDb
23 
24 #include <TCollection.h> // for TIter
25 #include <TObjString.h> // for TObjString
26 #include <iostream> // for operator<<, ostream, cout, etc
27 #include <string.h> // for strlen
28 
29 using std::cout;
30 
33 
34 FairContainer::FairContainer()
35  : TNamed()
36  , contexts(nullptr)
37  , actualContext("")
38  , fLogger(FairLogger::GetLogger())
39 {}
40 // Default constructor
41 
42 FairContainer::FairContainer(const char* name, const char* title, const char* defContext)
43  : TNamed(name, title)
44  , contexts(new TList())
45  , actualContext("")
46  , fLogger(FairLogger::GetLogger())
47 {
48  // Constructor
49  // Arguments: name = name of the corresponding parameter container
50  // title = title of this parameter container
51  // defContext = default context of this parameter container
52  addContext(defContext);
53 }
54 
56 {
57  // Destructor deletes the list of accepted contexts
58  if (contexts) {
59  contexts->Delete();
60  delete contexts;
61  }
62 }
63 
64 void FairContainer::addContext(const char* name)
65 {
66  // Adds a context to the list of accepted contexts
67  TObjString* c = new TObjString(name);
68  contexts->Add(c);
69 }
70 
71 Bool_t FairContainer::setActualContext(const char* c)
72 {
73  // The function sets the actual context for the container, if it is in the list of
74  // accepted contexts. When the actual context was already set before, it prints a warning
75  // and ignores the second setting.
76  // The function returns kFALSE, when the context is not in the list.
77  if (contexts->FindObject(c)) {
78  if (actualContext.IsNull()) {
79  actualContext = c;
80  } else
81  Warning("addContext",
82  "Actual context of parameter container %s already defined as %s",
83  GetName(),
84  actualContext.Data());
85  return kTRUE;
86  }
87  return kFALSE;
88 }
89 
91 {
92  // Returns the default context
93  return (static_cast<TObjString*>(contexts->At(0)))->String().Data();
94 }
95 
97 {
98  // prints the name, title of the container together with the actual context set
99  // or all possible contexts, when the actual context was not set
100  cout << fName << "\t" << fTitle << "\n";
101  if (!actualContext.IsNull()) {
102  cout << " actual context: " << actualContext << "\n";
103  } else {
104  TIter next(contexts);
105  Int_t i = 0;
106  TObjString* c;
107  cout << " all contexts:"
108  << "\n";
109  while ((c = static_cast<TObjString*>(next()))) {
110  if (c->String().IsNull()) {
111  cout << " \"\"";
112  } else {
113  cout << " " << c->String();
114  }
115  if (i == 0) {
116  cout << "\t default";
117  }
118  cout << "\n";
119  i++;
120  }
121  }
122 }
123 
125 {
126  // Returns the name of the parameter container used in the constructor and the
127  // runtime database.
128  // When the parameter container supportes different contexts (not only an empty string)
129  // and the actual context set is not the default context, the new name of the parameter
130  // container is concatinated as
131  // original container name + _ + actualcontext
132  TString cn = fName;
133  if (!actualContext.IsNull() && actualContext != (static_cast<TObjString*>(contexts->At(0)))->String()) {
134  cn += "_";
135  cn += actualContext;
136  }
137  return cn;
138 }
139 
141 {
142  // return the actual context, if set, or the default context
143  if (!actualContext.IsNull()) {
144  return actualContext.Data();
145  } else {
146  return getDefaultContext();
147  }
148 }
149 
151  : TNamed()
152  , containers(new TList)
153  , fLogger(FairLogger::GetLogger())
154 {
155  // Constructor creates a list to store objects of type FairContainer
156  // containers=new TList;
157 }
158 
160 {
161  // Destructor deletes the container list and its elements
163  containers->Delete();
164  delete containers;
165 }
166 
167 Bool_t FairContFact::addContext(const char* name)
168 {
169  // Set the actual context in all containers, which accept this context
170  FairContainer* c = 0;
171  Bool_t found = kFALSE;
172  TIter next(containers);
173  while ((c = static_cast<FairContainer*>(next()))) {
174  if (c->setActualContext(name)) {
175  found = kTRUE;
176  }
177  }
178  return found;
179 }
180 
182 {
183  // Returns the pointer to the parameter container in the runtime database
184  // If this parameter container does not yet exit, it calls the function
185  // createContainer(FairContainer*), which is implemented in the derived classes
186  // and calls the corresponding constructor. Then the pointer it added in the
187  // runtime database.
188  FairContainer* c = static_cast<FairContainer*>((containers->FindObject(name)));
189 
190  FairParSet* cont = 0;
191  if (c) {
192  TString cn = c->getConcatName();
194  if (!(cont = rtdb->findContainer(c->getConcatName().Data()))) {
195  if (strlen(c->getActualContext()) == 0) {
197  }
198  cont = createContainer(c);
199  if (cont) {
200  rtdb->addContainer(cont);
201  }
202  }
203  }
204  return cont;
205 }
206 
208 {
209  // Loops over all containers in the list and calls their print() function
210  cout << "---------------------------------------------------------------------------"
211  << "\n";
212  cout << GetName() << ": " << GetTitle() << "\n";
213  cout << "---------------------------------------------------------------------------"
214  << "\n";
215  FairContainer* c;
216  TIter next(containers);
217  while ((c = static_cast<FairContainer*>(next()))) {
218  c->print();
219  }
220 }
221 
223 {
224  // Check if a container already exist in the List of containers
225  // If it alread exist print an error message and return kFALSE
226  // such that the user can handle the issue
227  if (nullptr != containers->FindObject(cont)) {
228  LOG(error) << "The container " << cont->GetName() << " already exist in the "
229  << "container factory " << GetName() << ".\n"
230  << "Duplicate container is not added.";
231  return kFALSE;
232  }
233 
234  containers->Add(cont);
235  return kTRUE;
236 }
Bool_t AddContainer(FairContainer *)
virtual FairParSet * createContainer(FairContainer *)
Definition: FairContFact.h:56
virtual ~FairContFact()
list of container factories
Definition: FairRuntimeDb.h:24
static FairRuntimeDb * instance(void)
FairParSet * getContainer(const char *)
FairParSet * findContainer(const char *)
ClassImp(FairEventBuilder)
const char * getDefaultContext()
TList * containers
Definition: FairContFact.h:62
TList * contexts
Definition: FairContFact.h:29
const char * getContext()
TString getConcatName()
Bool_t addContainer(FairParSet *)
TString actualContext
Definition: FairContFact.h:31
Bool_t setActualContext(const char *c)
void addContext(const char *)
const char * getActualContext()
Definition: FairContFact.h:41
Bool_t addContext(const char *name)
void removeContFactory(FairContFact *fact)