LCFIPlus  0.6.5
EventStore.h
Go to the documentation of this file.
1 #ifndef EventStore_h
2 #define EventStore_h 1
3 
4 #include <map>
5 #include <list>
6 #include <string>
7 #include <iostream>
8 #include "TClass.h"
9 
10 #define EVENTSTORE_VERSION 0
11 
12 using namespace std;
13 
14 namespace lcfiplus {
15 
16 // observer class
18  public:
20  virtual ~EventStoreObserver() {}
21 
22  virtual void GetCallback(const char* /*name*/, const char* /*classname*/) {}
23  virtual void RegisterCallback(const char* /*name*/, const char* /*classname*/, int /*flags*/) {}
24 
25  ClassDef(EventStoreObserver,1)
26 };
27 
42 class EventStore {
43 
44  public:
45  // singleton is tranfered to Event class
46 // static EventStore * Instance();
47 
48  // register flags
49  enum {
50  DO_NOT_DELETE = 0x0001, // do not delete pointed object at ClearObjects(): to be used in reference collections
51  PERSIST = 0x0002, // lcio: to be saved in the output
52  JET_WRITE_VERTEX = 0x1000
53  };
54 
55  // Observer registration
57  _observerList.push_back(observer);
58  }
60  _observerList.remove(observer);
61  }
62 
63  // Check collection
64  int Count(const char* name)const;
65  bool IsExist(const char* name)const {
66  return Count(name);
67  }
68  bool IsExist(const char* name, const char* classname)const;
69 
70  // Object retrieval
71  const char* GetClassName(const char* name, int idx = 0)const;
72  void* GetObject(const char* name, const char* classname = "")const; // CAUTION: no type check
73  template<typename T> bool Get(const char* name, const vector<const T*>*& buf)const; // for pointer-vector classes, add const
74  template<typename T> bool Get(const char* name, const vector<T*>*& buf)const; // non-const pointer vector prohibited: invoke error
75  template<typename T> bool Get(const char* name, const vector<T>*& buf)const; // for vector classes
76  template<typename T> bool Get(const char* name, const T*& buf)const; // for non-vector
77 
78  // Object registration
79  void* RegisterObject(const char* name, const char* classname, int flags = 0);
80  template<typename T> bool Register(const char* name, vector<T*>*& buf, int flags = 0); // for pointer-vector classes
81  template<typename T> bool Register(const char* name, vector<T>*& buf, int flags = 0); // for vector classes
82  template<typename T> bool Register(const char* name, T*& buf, int flags = 0); // for non-vector
83 
84  // print object map
85  void Print()const;
86 
87  // clear current objects: to switch event
88  void ClearObjects();
89 
90  // public dtor
91  virtual ~EventStore();
92 
93  // for persistency classes /////////////////////////////
94  // internal storing structure
95  struct StoredEntry {
96  string classname;
97  void* obj;
98  int flag;
99 
100  StoredEntry(string cn = string(""), void* ob = 0, int fl = 0) : classname(cn), obj(ob), flag(fl) {}
101  };
102 
103  // map accessor
104  const multimap<string, lcfiplus::EventStore::StoredEntry >& GetObjectMap()const {
105  return _objectMap;
106  }
107 
108  protected:
109  // reference retrieval: internal use
110  void* const& GetObjectRef(const char* name, const char* classname = "")const; // CAUTION: no type check
111  // constructor not public: use Event class
112  EventStore();
113 
114  private:
115  multimap<string, lcfiplus::EventStore::StoredEntry > _objectMap;
116  mutable multimap<string, lcfiplus::EventStore::StoredEntry >::const_iterator _itMap;
117 
118  list<EventStoreObserver*> _observerList;
119 };
120 
121 
122 // template implementations
123 template<typename T> bool EventStore::Get(const char* name, const T*& buf)const {
124  buf = static_cast<const T*>(GetObject(name, TClass::GetClass(typeid(T))->GetName()));
125  return buf;
126 }
127 // vector version
128 template<typename T> bool EventStore::Get(const char* name, const vector<T>*& buf)const {
129  const char* elemclasname = TClass::GetClass(typeid(T))->GetName();
130  string vectclasname = "vector<";
131  vectclasname += elemclasname;
132  vectclasname += ">";
133 
134  buf = static_cast<const vector<T>*>(GetObject(name, vectclasname.c_str()));
135  return buf;
136 }
137 
138 // non-const pointer-vector prohibited
139 template<typename T> bool EventStore::Get(const char* name, const vector<T*>*& buf)const {
140  T a = "abc"; // invoke compiler error
141  throw ("EventStore::Get: non-const pointer-vector prohibited");
142  return false;
143 }
144 
145 // pointer-vector version
146 template<typename T> bool EventStore::Get(const char* name, const vector<const T*>*& buf)const {
147  const char* elemclasname = TClass::GetClass(typeid(T))->GetName();
148  string vectclasname = "vector<";
149  vectclasname += elemclasname;
150  vectclasname += "*>";
151 
152  buf = static_cast<const vector<const T*>*>(GetObject(name, vectclasname.c_str()));
153  return buf;
154 }
155 
156 template<typename T> bool EventStore::Register(const char* name, T*& buf, int flags) {
157  string classname = TClass::GetClass(typeid(T))->GetName();
158  classname += "*";
159  buf = static_cast<T*>(RegisterObject(name, classname.c_str(), flags));
160  return buf;
161 }
162 template<typename T> bool EventStore::Register(const char* name, vector<T>*& buf, int flags) {
163  const char* elemclasname = TClass::GetClass(typeid(T))->GetName();
164  string vectclasname = "vector<";
165  vectclasname += elemclasname;
166  vectclasname += ">";
167  buf = static_cast<vector<T>*>(RegisterObject(name, vectclasname.c_str(), flags));
168  return buf;
169 }
170 template<typename T> bool EventStore::Register(const char* name, vector<T*>*& buf, int flags) {
171  const char* elemclasname = TClass::GetClass(typeid(T))->GetName();
172  string vectclasname = "vector<";
173  vectclasname += elemclasname;
174  vectclasname += "*>";
175 // cout << vectclasname << endl;
176  buf = static_cast<vector<T*>*>(RegisterObject(name, vectclasname.c_str(), flags));
177  return buf;
178 }
179 
180 }
181 
182 #endif
StoredEntry(string cn=string(""), void *ob=0, int fl=0)
Definition: EventStore.h:100
void UnregisterObserver(EventStoreObserver *observer)
Definition: EventStore.h:59
string classname
Definition: EventStore.h:96
Definition: EventStore.h:95
const multimap< string, lcfiplus::EventStore::StoredEntry > & GetObjectMap() const
Definition: EventStore.h:104
virtual void RegisterCallback(const char *, const char *, int)
Definition: EventStore.h:23
virtual void GetCallback(const char *, const char *)
Definition: EventStore.h:22
Definition: EventStore.h:17
virtual ~EventStoreObserver()
Definition: EventStore.h:20
A simple named storage for event data.
Definition: EventStore.h:42
void * obj
Definition: EventStore.h:97
bool IsExist(const char *name) const
Definition: EventStore.h:65
EventStoreObserver()
Definition: EventStore.h:19
void RegisterObserver(EventStoreObserver *observer)
Definition: EventStore.h:56
int flag
Definition: EventStore.h:98