00001
00002
00003
00004 #ifndef _JFACTORY_H_
00005 #define _JFACTORY_H_
00006
00007
00008
00009 #include "JObject.h"
00010
00011
00012 #include <vector>
00013 #include <string>
00014 using std::vector;
00015 using std::string;
00016
00017 #include "JEventLoop.h"
00018 #include "JFactory_base.h"
00019 #include "JEvent.h"
00020
00021
00022 #ifdef __CINT__
00023 class pthread_mutex_t;
00024 typedef unsigned long pthread_t;
00025 typedef unsigned long oid_t;
00026 #endif
00027
00028
00029 namespace jana{
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 template<class T>
00063 class JFactory:public JFactory_base{
00064
00065 public:
00066 JFactory(const char *tag="");
00067 ~JFactory();
00068
00069 enum data_origin_t{
00070 ORIGIN_NONE,
00071 ORIGIN_FACTORY,
00072 ORIGIN_EXTERNAL
00073 };
00074
00075 vector<void*>& Get(void);
00076 jerror_t Get(vector<const T*> &d);
00077 int GetNrows(void);
00078 data_origin_t GetDataOrigin(void){return data_origin;}
00079 inline const char* className(void){return T::static_className();}
00080 inline const char* GetDataClassName(void){return className();}
00081 inline void toStrings(vector<vector<pair<string,string> > > &items, bool append_types=false) const;
00082 virtual const char* Tag(void){return tag_str;}
00083 inline int GetDataClassSize(void){return sizeof(T);}
00084 inline int GetEventCalled(void){return evnt_called;}
00085 inline int GetCheckSourceFirst(void){return !use_factory;}
00086 jerror_t CopyFrom(vector<const T*> &data);
00087 jerror_t CopyTo(vector<T*> &data);
00088 const T* GetByIDT(JObject::oid_t id);
00089 const JObject* GetByID(JObject::oid_t id){return dynamic_cast<const JObject*>(GetByIDT(id));}
00090
00091
00092 protected:
00093 vector<T*> _data;
00094 vector<void*>_vdata;
00095 int use_factory;
00096 const char* tag_str;
00097
00098 jerror_t Reset(void);
00099 jerror_t HardReset(void);
00100
00101 data_origin_t data_origin;
00102 };
00103
00104
00105
00106 #ifndef __CINT__
00107
00108
00109
00110
00111 template<class T>
00112 JFactory<T>::JFactory(const char *tag)
00113 {
00114
00115
00116
00117
00118
00119 _data.clear();
00120
00121
00122 flags = WRITE_TO_OUTPUT;
00123 use_factory = 0;
00124 busy = 0;
00125 tag_str = tag;
00126 Ncalls_to_Get = 0;
00127 Ncalls_to_evnt = 0;
00128
00129
00130 debug_level = 0;
00131 string envar = string() + "DEBUG_" + GetDataClassName();
00132 char *ptr = getenv(envar.c_str());
00133 if(ptr)debug_level = atoi(ptr);
00134 }
00135
00136
00137
00138
00139 template<class T>
00140 JFactory<T>::~JFactory()
00141 {
00142
00143 HardReset();
00144 }
00145
00146
00147
00148
00149 template<class T>
00150 vector<void*>& JFactory<T>::Get(void)
00151 {
00152
00153
00154
00155
00156
00157 vector<const T*> d;
00158 Get(d);
00159
00160
00161 _vdata.clear();
00162 for(unsigned int i=0;i<_data.size();i++)_vdata.push_back((void*)_data[i]);
00163
00164 return _vdata;
00165 }
00166
00167
00168
00169
00170 template<class T>
00171 jerror_t JFactory<T>::Get(vector<const T*> &d)
00172 {
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 if(evnt_called)return CopyFrom(d);
00196
00197
00198 if(busy)throw(INFINITE_RECURSION);
00199 busy++;
00200 if(busy!=1)throw(INFINITE_RECURSION);
00201
00202
00203 int event_number = eventLoop->GetJEvent().GetEventNumber();
00204 int run_number = eventLoop->GetJEvent().GetRunNumber();
00205
00206
00207 if(!init_called){
00208 init();
00209 init_called = 1;
00210 }
00211
00212
00213 if(run_number!=brun_runnumber){
00214 if(brun_called && !erun_called){
00215 erun();
00216 erun_called = 1;
00217 }
00218 brun_called = 0;
00219 }
00220 if(!brun_called){
00221 brun(eventLoop, run_number);
00222 brun_called = 1;
00223 erun_called = 0;
00224 brun_runnumber = run_number;
00225 }
00226
00227
00228 try{
00229 Ncalls_to_evnt++;
00230 evnt(eventLoop, event_number);
00231 CopyFrom(d);
00232 }catch(JException *exception){
00233 JEventLoop::error_call_stack_t cs;
00234 cs.factory_name = GetDataClassName();
00235 cs.tag = Tag();
00236 cs.filename = __FILE__;
00237 cs.line = __LINE__;
00238 eventLoop->AddToErrorCallStack(cs);
00239 throw exception;
00240 }
00241 evnt_called = 1;
00242 busy=0;
00243
00244 return NOERROR;
00245 }
00246
00247
00248
00249
00250 template<class T>
00251 int JFactory<T>::GetNrows(void)
00252 {
00253
00254
00255
00256
00257 if(!evnt_called){
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 vector<const T*> d;
00269 eventLoop->Get(d,Tag());
00270 }
00271
00272 return _data.size();
00273 }
00274
00275
00276
00277
00278 template<class T>
00279 jerror_t JFactory<T>::Reset(void)
00280 {
00281
00282
00283 if(flags & (unsigned int)PERSISTANT)return NOERROR;
00284
00285
00286
00287
00288
00289 return HardReset();
00290 }
00291
00292
00293
00294
00295 template<class T>
00296 jerror_t JFactory<T>::HardReset(void)
00297 {
00298
00299
00300 if(!TestFactoryFlag(NOT_OBJECT_OWNER)){
00301 for(unsigned int i=0;i<_data.size();i++){
00302 delete _data[i];
00303 }
00304 }
00305 _data.clear();
00306
00307 evnt_called = 0;
00308
00309 return NOERROR;
00310 }
00311
00312
00313
00314
00315 template<class T>
00316 jerror_t JFactory<T>::CopyTo(vector<T*> &data)
00317 {
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 evnt_called = 1;
00333
00334
00335
00336 _data.clear();
00337 for(unsigned int i=0;i<data.size();i++){
00338 _data.push_back(data[i]);
00339 }
00340
00341 return NOERROR;
00342 }
00343
00344
00345
00346
00347 template<class T>
00348 jerror_t JFactory<T>::CopyFrom(vector<const T*> &data)
00349 {
00350
00351
00352
00353
00354
00355 for(unsigned int i=0;i<_data.size(); i++)data.push_back(_data[i]);
00356 Ncalls_to_Get++;
00357
00358 return NOERROR;
00359 }
00360
00361
00362
00363
00364 template<class T>
00365 const T* JFactory<T>::GetByIDT(JObject::oid_t id)
00366 {
00367 for(unsigned int i=0;i<_data.size();i++)
00368 if(_data[i]->id == id)return (const T*)_data[i];
00369 return NULL;
00370 }
00371
00372
00373
00374
00375 template<class T>
00376 void JFactory<T>::toStrings(vector<vector<pair<string,string> > > &items, bool append_types) const
00377 {
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393 items.clear();
00394
00395 for(unsigned int i=0;i<_data.size();i++){
00396 vector<pair<string, string> > myitems;
00397 bool save_append_types = _data[i]->GetAppendTypes();
00398 _data[i]->SetAppendTypes(append_types);
00399 _data[i]->toStrings(myitems);
00400 _data[i]->SetAppendTypes(save_append_types);
00401 if(myitems.size()>0)items.push_back(myitems);
00402 }
00403 }
00404
00405 #endif //__CINT__
00406
00407
00408 }
00409
00410
00411
00412 #endif // _JFACTORY_H_