Diana Software
QEvent.cc
Go to the documentation of this file.
1 #include "QEvent.hh"
2 #include "QStringHandler.hh"
3 #include "QBaseType.hh"
4 #include "QBool.hh"
5 
7 
9 {
10  fObjectMap.clear();
11  fDefaultMap.clear();
12  fModuleName = 0;
13  fReadNumber = 0;
14  fOverWrite = false;
15 }
16 
18 {
19  QObjectMap::const_iterator iter = fObjectMap.begin();
20  while(iter != fObjectMap.end()) {
21  QObject** evData = (iter->second);
22  delete *evData;
23  delete evData;
24  iter++;
25  }
26  fObjectMap.clear();
27  fDefaultMap.clear();
28  fModuleName = 0;
29  fReadNumber = 0;
30  fOverWrite = false;
31 }
32 
34 {
35  fObjectMap.clear();
36  fDefaultMap.clear();
37  fModuleName = 0;
38  fReadNumber = 0;
39  fOverWrite = false;
40  this->operator=(ev);
41 }
42 
43 const QEvent& QEvent::operator=(const QEvent& otherEv){
44  if(this == &otherEv) return *this;
45  Clear();
46  QObjectMap::const_iterator iter = otherEv.fObjectMap.begin();
47  // MV FIXME: remove other objects...
48  while(iter != otherEv.fObjectMap.end()) {
49  const QEventLabel& label = iter->first;
50  const QObject* otherEvData = *(iter->second);
51  QObject** thisEvData = 0;
52  QObjectMap::iterator thisFound = fObjectMap.find(label);
53  if(thisFound == fObjectMap.end()) {
54  thisEvData = new (QObject*);
55  *thisEvData = otherEvData->Duplicate();
56  fObjectMap[label] = thisEvData;
57  } else {
58  thisEvData = thisFound->second;
59  }
60  if(otherEvData->IsValid()) otherEvData->FullyFill(*thisEvData);
61  else (*thisEvData)->InValidate();
62  iter++;
63  }
64 
65  fDefaultMap = otherEv.fDefaultMap;
66  fModuleName = otherEv.fModuleName;
67  fReadNumber = otherEv.fReadNumber;
68  fOverWrite = otherEv.fOverWrite;
69 
70  return *this;
71 }
72 
73 
75 {
76  QObjectMap::const_iterator iter = fObjectMap.begin();
77  while(iter != fObjectMap.end()) {
78  (*(iter->second))->Reset();
79  iter++;
80  }
81 
82 }
83 
85 {
86  QObjectMap::const_iterator iter = fObjectMap.begin();
87  QObjectMap newMap;
88  while(iter != fObjectMap.end()) {
89  if((*(iter->second))->IsJustAdded() || (*(iter->second))->IsRequired()) {
90  newMap[iter->first] = iter->second;
91  }
92  iter++;
93  }
94  fObjectMap.clear();
95  fObjectMap = newMap;
96 }
97 
98 void QEvent::Dump(std::ostream& o) const
99 {
100  o<<"Dump of QEvent read number: "<<fReadNumber<<" Caller: \"";
101  if(fModuleName) o<<*fModuleName;
102  o<<"\""<<std::endl;
103  QObjectMap::const_iterator iter = fObjectMap.begin();
104  int len = 15;
105  o<<" "<<QStringHandler::Resize("Owner@Name",30)
106  <<" "<<QStringHandler::Resize("Exists/Valid/Req",16)
107  <<" "<<QStringHandler::Resize("Type",30)
108  <<" "<<QStringHandler::Resize("Dump",len)
109  <<std::endl;
110  for(int i = 0; i < 30+16+30+len; i++) o<<"-";
111  o<<std::endl;
112  while(iter != fObjectMap.end()) {
113  const QEventLabel& label = iter->first;
114  const QObject* evData = 0;
115  if(*(iter->second)) evData = *(iter->second);
116 
117  o<<" "<<QStringHandler::Resize(label.GetStringLabel(),30);
118 
119  bool exists = false;
120  bool isValid = false;
121  bool isRequired = false;
122  std::string type = "n/a";
123  if(evData) {
124  exists = true;
125  isValid = evData->IsValid();
126  isRequired = evData->IsRequired();
127  type = evData->ClassName();
128  }
129 
130  o<<" "<<QStringHandler::Resize((exists ? "t" : "f"),3);
131  o<<"/";
132  o<<" "<<QStringHandler::Resize((isValid ? "t" : "f"),3);
133  o<<"/";
134  o<<" "<<QStringHandler::Resize((isRequired ? "t" : "f"),2);
135  o<<" "<<QStringHandler::Resize(type,30);
136  if(isValid) {
137  o<<" ";
138  std::stringstream msgStream;
139  evData->Dump(msgStream);
140  std::string line;
141  std::getline(msgStream,line);
142  o<<line<<std::endl;
143  while(std::getline(msgStream,line)) {
144  o << QStringHandler::Resize(" ",47) << line << std::endl;
145  }
146 
147  } else {
148  o << std::endl;
149  }
150  iter++;
151  }
152  o<<"End of dump of QEvent read number: "<<fReadNumber<<std::endl;
153 
154 }
155 
156 std::vector<QEventLabel> QEvent::GetLabels() const
157 {
158  std::vector<QEventLabel> labels;
159  QObjectMap::const_iterator iter = fObjectMap.begin();
160  while(iter != fObjectMap.end()) {
161  labels.push_back(iter->first);
162  iter++;
163  }
164  return labels;
165 }
166 
167 QObject** QEvent::GetObject(const char* owner, const char* name) const
168 {
169  //std::cout<<"QEvent::Get(): "<<label<<" From: "<<*fModuleName<<std::endl;
170  QEventLabel label;
171  GetLabel(owner,name,label);
172  return GetObjectByLabel(label);
173 }
174 
176 {
177  QObjectMap::const_iterator iter = fObjectMap.find(label);
178  if(iter != fObjectMap.end()) {
179  return iter->second;
180  }
181  return 0;
182 }
183 
184 void QEvent::GetLabel(const char* owner, const char* name, QEventLabel& label) const
185 {
186  label.name = name;
187  if(strcmp(owner,"") == 0 && fDefaultMap.find(name) != fDefaultMap.end()) {
188  label.owner = fDefaultMap.find(name)->second;
189  } else {
190  label.owner = owner;
191  }
192  return;
193 }
194 
195 void QEvent::Add(const char* owner, const char* name, QObject** evData)
196 {
197  // std::cout<<"QEvent::Add: "<<label<<" From: "<<*fModuleName<<std::endl;
198  // if found and we are not in overwrite mode we throw an error.
199  if(strcmp(owner,"") == 0) {
200  std::stringstream msg;
201  msg<<"Object: "<<name<<" cannot be added to the event since no owner has been specified ";
202  throw QError(QERR_EVENT_GENERIC,__FILE__,__LINE__,msg.str());
203  }
204  QObject** address = GetObject(owner,name);
205  QEventLabel label(owner,name);
206  if(address && address != evData) {
207  std::stringstream msg;
208  msg<<"Object: "<<label.GetStringLabel()<<" is already present in the event";
209  msg<<": you may want to set the ExtraLabel or OverWriteEvent module options ";
210  throw QError(QERR_EVENT_GENERIC,__FILE__,__LINE__,msg.str());
211  }
212  (*evData)->SetIsRequired();
213  fObjectMap[label]= evData;
214  if(fDefaultMap.find(name) == fDefaultMap.end()) {
215  fDefaultMap[name] = owner;
216  }
217  /*
218  QObject* dc = *address;
219  // automatic alias for basetypes
220  if(dynamic_cast<QInt*>(dc) ||
221  dynamic_cast<QDouble*>(dc) ||
222  dynamic_cast<QFloat*>(dc) ||
223  dynamic_cast<QBool*>(dc) ) {
224  QAliases& aliases = GetAliases();
225  aliases.Add(label,"fValue",label);
226  }
227  */
228 
229 }
230 
#define Q_END_NAMESPACE
Definition: QDiana.hh:22
#define Q_BEGIN_NAMESPACE
Definition: QDiana.hh:20
@ QERR_EVENT_GENERIC
Definition: QError.hh:42
error class with error type and description
Definition: QError.hh:115
label for QObject in the QEvent
Definition: QEventLabel.hh:23
std::string GetStringLabel() const
get string in the format "owner@name"
Definition: QEventLabel.cc:48
std::string owner
owner of the QObject
Definition: QEventLabel.hh:60
std::string name
name of the QObject
Definition: QEventLabel.hh:62
diana event
Definition: QEvent.hh:46
std::map< std::string, std::string > fDefaultMap
Definition: QEvent.hh:384
QObject ** GetObject(const char *owner, const char *name) const
Definition: QEvent.cc:167
void Clear()
reset members to default values
Definition: QEvent.cc:74
bool fOverWrite
Definition: QEvent.hh:391
unsigned int fReadNumber
event number
Definition: QEvent.hh:389
QObject ** GetObjectByLabel(const QEventLabel &label) const
Definition: QEvent.cc:175
void Dump(std::ostream &o) const
Print the event and its member.
Definition: QEvent.cc:98
void Consolidate()
keep only Required and JustAdded QObjects
Definition: QEvent.cc:84
QEvent()
constructor
Definition: QEvent.cc:8
const std::string mutable * fModuleName
Definition: QEvent.hh:386
~QEvent()
destructor
Definition: QEvent.cc:17
const QEvent & operator=(const QEvent &ev)
operator= new QObject's are created if needed and are owned by this. This this method is protected,...
Definition: QEvent.cc:43
QObjectMap fObjectMap
Definition: QEvent.hh:383
void GetLabel(const char *owner, const char *name, QEventLabel &label) const
Definition: QEvent.cc:184
std::vector< QEventLabel > GetLabels() const
Get all object labels.
Definition: QEvent.cc:156
std::map< QEventLabel, QObject ** > QObjectMap
Definition: QEvent.hh:48
void Add(WriteHandle< Q > &handle)
Add a QObject to the event.
Definition: QEvent.hh:193
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
bool IsRequired() const
check wheter this object is required by some module
Definition: QObject.hh:123
virtual QObject * Duplicate() const =0
create object of same type, this function is automatically declared/implemented in derived classes by...
void FullyFill(QObject *evData) const
calls Fill(QObject*) if this QObject IsValid(). Copies Validity flag and bits
Definition: QObject.cc:39
std::string Resize(const std::string &s, size_t len)
resize a string to len, adding spaces if necessary