Diana Software
QGlobalDataManager.cc
Go to the documentation of this file.
1 #include "QGlobalDataManager.hh"
2 #include "QStringHandler.hh"
3 #include "QDianaVersion.hh"
4 #include <sstream>
5 
6 
8 
10 
12 
14 {
15  if(!fUniqueInstance) {
16  if(owner.empty()) DianaThrow("QGlobalDataManager::GetInstance() needs owner specification at first call")
18  if(!fUniqueInstance) DianaThrow("QGlobalDataManager::GetInstance(): cannot allocate object");
19  }
20  fUniqueInstance->SetOwner(owner);
21  return *fUniqueInstance;
22 }
23 
24 
26 {
27  fOwner.clear();
28  fCacheEnabled = true;
29  fInstances++;
30 /* if(fInstances == 1)
31  QMessageHandler::Warn("QGlobalDataManager::QGlobalDataManager()","Multiple instances of QGlobalDataManager can generate errors, use only one instance.");
32  */
33 
34 // See discussion in issue #274
35 // fOutputVersion = QSWInfo::GetDianaVersionTag();
38 }
39 
41 {
42 
43  Clear();
44  fInstances--;
45 }
46 
48 {
49  std::map<QGlobalLabel, CachedObject>::iterator iter = fDataMap.begin();
50  while(iter != fDataMap.end()) {
51  delete iter->second.object;
52  iter++;
53  }
54  fDataMap.clear();
55  fDefaultMap.clear();
56 }
57 
58 void QGlobalDataManager::Dump(std::ostream& o) const
59 {
60  o<<"Dump of cached global QObjects. Caller: \"";
61  if(!fOwner.empty()) o<<fOwner;
62  o<<"\""<<std::endl;
63  std::map<QGlobalLabel,CachedObject>::const_iterator iter = fDataMap.begin();
64  int len = 15;
65  o<<" "<<QStringHandler::Resize("Owner",20)
66  <<" "<<QStringHandler::Resize("Name",len)
67  <<" "<<QStringHandler::Resize("BeginRun",10)
68  <<" "<<QStringHandler::Resize("EndRun",10)
69  <<" "<<QStringHandler::Resize("Channel",10)
70  <<" "<<QStringHandler::Resize("IsValid",8)
71  <<" "<<QStringHandler::Resize("Dump",len)
72  <<std::endl;
73  for(int i = 0; i < 88; i++) o<<"-";
74  o<<std::endl;
75  while(iter != fDataMap.end()) {
76  const QGlobalLabel& label = iter->first;
77  const QObject* evData = 0;
78  if((iter->second.object)) evData = (iter->second.object);
79 
80  o<<" "<<QStringHandler::Resize(label.fOwner,20)
81  <<" "<<QStringHandler::Resize(label.fName,len);
82  char buf[128];
83  snprintf(buf,128,"%06d",label.fBeginRun);
84  o<<" "<<QStringHandler::Resize(buf,10);
85  snprintf(buf,128,"%06d",label.fEndRun);
86  o<<" "<<QStringHandler::Resize(buf,10);
87  snprintf(buf,128,"%04d",label.fChannel);
88  o<<" "<<QStringHandler::Resize(buf,10);
89  bool isValid = false;
90  if(evData) isValid = evData->IsValid();
91  o<<" "<<QStringHandler::Resize((isValid ? "true" : "false"),8);
92  if(isValid) {
93  o<<" ";
94  std::stringstream msgStream;
95  evData->Dump(msgStream);
96  std::string line;
97  std::getline(msgStream,line);
98  o<<line<<std::endl;
99  while(std::getline(msgStream,line)) {
100  o << QStringHandler::Resize(" ",80) << line << std::endl;
101  }
102 
103  } else {
104  o << std::endl;
105  }
106  iter++;
107  }
108  o<<"End of dump of global QObjects "<<std::endl;
109 
110 
111 }
112 
113 
114 std::vector<QGlobalLabel> QGlobalDataManager::GetWriteLabels() const
115 {
116  std::vector<QGlobalLabel> labels;
117  std::map<QGlobalLabel, CachedObject>::const_iterator iter = fDataMap.begin();
118  while(iter != fDataMap.end()) {
119  labels.push_back(iter->first);
120  iter++;
121  }
122  return labels;
123 }
124 
126 {
127 
128  QObject* obj = 0;
129  std::map<QGlobalLabel, CachedObject>::const_iterator iter = fDataMap.find(label);
130  if(iter != fDataMap.end() ) {
131  obj = iter->second.object;
132  return obj;
133  }
134 // QError err = QERR_GLOBAL_GENERIC;
135 // err.SetDescription("Object " + label.GetStringLabel() + " not found");
136 // QMessageHandler::Debug("QGlobalDataManager::GetByLabel()",err.GetDescription());
137 
138  return 0;
139 }
140 
141 
142 
#define DianaThrow(obj)
Definition: QDianaDebug.hh:26
#define Q_END_NAMESPACE
Definition: QDiana.hh:22
#define Q_BEGIN_NAMESPACE
Definition: QDiana.hh:20
Object to manage I/O (DB, file, or memory) of diana global quantities.
std::vector< QGlobalLabel > GetWriteLabels() const
get labels
static QGlobalDataManager & GetInstance(const std::string &owner="")
singleton interface provided for use in non diana executables. Do not use it within diana or root mac...
std::string fOutputVersion
Version tag of this release, used to write onto the DB. Parameter not accessible to the user.
bool fCacheEnabled
cache enabled
std::string fOwner
the module that is accessing this object
static QGlobalDataManager * fUniqueInstance
singleton instance
std::string fInputVersion
Version tag of the objects to be read, used to read from the DB. Parameter not accessible to the user...
void SetOwner(const std::string &owner)
set the module that is accessing this object
std::map< QGlobalLabel, CachedObject > fDataMap
cached QObject 's
void Clear()
clear cache
void Dump(std::ostream &o) const
Dump cached objects to stream.
QGlobalDataManager()
constructor
std::map< QGlobalLabel, std::string > fDefaultMap
default owners
const QObject * GetByLabel(const QGlobalLabel &) const
get object by label (not-recommended). Works only on cached object's
static int fInstances
number of instances
Label for global QObject's.
Definition: QGlobalLabel.hh:19
std::string fName
Object name.
Definition: QGlobalLabel.hh:44
std::string fOwner
Object owner.
Definition: QGlobalLabel.hh:42
int fBeginRun
begin validity
Definition: QGlobalLabel.hh:46
base class for objects handled by QEvent and QGlobalDataManager.
Definition: QObject.hh:76
virtual void Dump(std::ostream &o) const
print content to stream (intended for screen print or log files)
Definition: QObject.cc:53
bool IsValid() const
check wheter object is valid
Definition: QObject.hh:114
const char * GetDianaCleanVersionTag()
Return the DIANA version tag cleaned.
std::string Resize(const std::string &s, size_t len)
resize a string to len, adding spaces if necessary