25
25
26
26
#include < typeindex>
27
27
#include < vector>
28
- #include < cstddef>
29
28
#include < memory>
30
29
#include < atomic>
31
30
@@ -220,14 +219,14 @@ template<class T>
220
219
inline std::vector<JFactoryT<T>*> JEvent::GetFactoryAll (bool throw_on_missing) const {
221
220
std::vector<JFactoryT<T>*> factories;
222
221
for (auto * factory : mFactorySet .GetAllFactories ()) {
223
- if (dynamic_cast <JFactoryT<T>*>(factory) != nullptr ) {
224
- factories.push_back (factory);
222
+ auto typed_factory = dynamic_cast <JFactoryT<T>*>(factory);
223
+ if (typed_factory != nullptr ) {
224
+ factories.push_back (typed_factory);
225
225
}
226
226
}
227
227
if (factories.size () == 0 ) {
228
228
if (throw_on_missing) {
229
229
JException ex (" Could not find any JFactoryT<" + JTypeInfo::demangle<T>() + " > (from any tag)" );
230
- ex.show_stacktrace = false ;
231
230
throw ex;
232
231
}
233
232
};
@@ -238,8 +237,7 @@ inline std::vector<JFactoryT<T>*> JEvent::GetFactoryAll(bool throw_on_missing) c
238
237
// / C-style getters
239
238
240
239
template <class T >
241
- JFactoryT<T>* JEvent::GetSingle (const T* &t, const char *tag, bool exception_if_not_one) const
242
- {
240
+ JFactoryT<T>* JEvent::GetSingle (const T* &t, const char *tag, bool exception_if_not_one) const {
243
241
// / This is a convenience method that can be used to get a pointer to the single
244
242
// / object of type T from the specified factory. It simply calls the Get(vector<...>) method
245
243
// / and copies the first pointer into "t" (or NULL if something other than 1 object is returned).
@@ -252,18 +250,17 @@ JFactoryT<T>* JEvent::GetSingle(const T* &t, const char *tag, bool exception_if_
252
250
// / exception_if_not_one to false. In that case, you will have to check if t==NULL to
253
251
// / know if the call succeeded.
254
252
255
- std::vector<const T*> v;
256
- auto fac = GetFactory<T>(tag, true ); // throw exception if factory not found
257
- JCallGraphEntryMaker cg_entry (mCallGraph , fac); // times execution until this goes out of scope
258
- Get (v, tag);
259
- if (v.size ()!=1 ){
260
- t = NULL ;
261
- if (exception_if_not_one) {
262
- throw JException (" GetSingle<%s>: Factory has wrong number of items: 1 expected, %d found." , JTypeInfo::demangle<T>().c_str (), v.size ());
253
+ auto * databundle = GetLightweightDatabundle<T>(tag, true , true ); // throw exception if databundle not found
254
+ if (databundle->GetSize () != 1 ) {
255
+ t = nullptr ;
256
+ if (exception_if_not_one) {
257
+ throw JException (" GetSingle<%s>: Databundle has wrong number of items: 1 expected, %d found." , JTypeInfo::demangle<T>().c_str (), databundle->GetSize ());
263
258
}
264
259
}
265
- t = v[0 ];
266
- return fac;
260
+ else {
261
+ t = databundle->GetData ().at (0 );
262
+ }
263
+ return dynamic_cast <JFactoryT<T>*>(databundle->GetFactory ());
267
264
}
268
265
269
266
// / Get conveniently returns one item from inside the JFactory. This should be used when the data in question
@@ -274,32 +271,28 @@ JFactoryT<T>* JEvent::GetSingle(const T* &t, const char *tag, bool exception_if_
274
271
// / - If the factory contains exactly one item, GetSingle updates the `destination` to point to that item.
275
272
// / - If the factory contains more than one item, GetSingle updates the `destination` to point to the first time.
276
273
template <class T >
277
- JFactoryT<T>* JEvent::Get (const T** destination, const std::string& tag) const
278
- {
279
- auto factory = GetFactory<T>(tag, true );
280
- JCallGraphEntryMaker cg_entry (mCallGraph , factory); // times execution until this goes out of scope
281
- auto iterators = factory->CreateAndGetData (*this );
282
- if (std::distance (iterators.first , iterators.second ) == 0 ) {
274
+ JFactoryT<T>* JEvent::Get (const T** destination, const std::string& tag) const {
275
+ auto * databundle = GetLightweightDatabundle<T>(tag, true , true ); // throw exception if databundle not found
276
+ if (databundle->GetSize () == 0 ) {
283
277
*destination = nullptr ;
284
278
}
285
279
else {
286
- *destination = *iterators. first ;
280
+ *destination = databundle-> GetData (). at ( 0 ) ;
287
281
}
288
- return factory ;
282
+ return dynamic_cast <JFactoryT<T>*>(databundle-> GetFactory ()) ;
289
283
}
290
284
291
285
292
286
template <class T >
293
287
JFactoryT<T>* JEvent::Get (std::vector<const T*>& destination, const std::string& tag, bool strict) const
294
288
{
295
- auto factory = GetFactory<T>(tag, strict);
296
- if (factory == nullptr ) return nullptr ; // Will have thrown already if strict==true
297
- JCallGraphEntryMaker cg_entry (mCallGraph , factory); // times execution until this goes out of scope
298
- auto iterators = factory->CreateAndGetData (*this );
299
- for (auto it=iterators.first ; it!=iterators.second ; it++) {
300
- destination.push_back (*it);
301
- }
302
- return factory;
289
+ auto * databundle = GetLightweightDatabundle<T>(tag, strict, true ); // throw exception if databundle not found
290
+ if (databundle != nullptr ) {
291
+ for (auto * item : databundle->GetData ()) {
292
+ destination.push_back (item);
293
+ }
294
+ }
295
+ return dynamic_cast <JFactoryT<T>*>(databundle->GetFactory ());
303
296
}
304
297
305
298
@@ -375,26 +368,31 @@ std::vector<const T*> JEvent::Get(const std::string& tag, bool strict) const {
375
368
376
369
template <class T >
377
370
typename JFactoryT<T>::PairType JEvent::GetIterators (const std::string& tag) const {
378
- auto factory = GetFactory<T>(tag, true );
379
- JCallGraphEntryMaker cg_entry ( mCallGraph , factory); // times execution until this goes out of scope
380
- auto iters = factory-> CreateAndGetData (* this );
381
- return iters ;
371
+
372
+ auto databundle = GetLightweightDatabundle<T>(tag, true , true );
373
+ auto & data = databundle-> GetData ( );
374
+ return std::make_pair (data. cbegin (), data. cend ()) ;
382
375
}
383
376
384
377
// / GetAll returns all JObjects of (child) type T, regardless of tag.
385
378
template <class T >
386
379
std::vector<const T*> JEvent::GetAll () const {
387
- std::vector<const T*> vec;
388
- auto factories = GetFactoryAll<T>(true );
389
380
390
- for (auto factory : factories) {
391
- auto iters = factory->CreateAndGetData (*this );
392
- std::vector<const T*> vec;
393
- for (auto it = iters.first ; it != iters.second ; ++it) {
394
- vec.push_back (*it);
381
+ std::vector<const T*> results;
382
+
383
+ for (auto databundle : mFactorySet .GetDatabundles (std::type_index (typeid (T)))) {
384
+ auto fac = databundle->GetFactory ();
385
+ if (fac != nullptr ) {
386
+ fac->Create (*this );
387
+ }
388
+ auto typed_databundle = dynamic_cast <JLightweightDatabundleT<T>*>(databundle);
389
+ if (typed_databundle != nullptr ) {
390
+ for (auto * item : typed_databundle->GetData ()) {
391
+ results.push_back (item);
392
+ }
395
393
}
396
394
}
397
- return vec; // Assumes RVO
395
+ return results;
398
396
}
399
397
400
398
// GetAllChildren will furnish a map { (type_name,tag_name) : [BaseClass*] } containing all JFactoryT<T> data where
@@ -424,45 +422,55 @@ std::map<std::pair<std::string, std::string>, std::vector<S*>> JEvent::GetAllChi
424
422
template <class T >
425
423
inline JFactoryT<T>* JEvent::Insert (T* item, const std::string& tag) const {
426
424
427
- std::string resolved_tag = tag;
428
- if (mUseDefaultTags && tag.empty ()) {
429
- auto defaultTag = mDefaultTags .find (JTypeInfo::demangle<T>());
430
- if (defaultTag != mDefaultTags .end ()) resolved_tag = defaultTag->second ;
431
- }
432
- auto factory = GetFactory<T>(resolved_tag);
433
- if (factory == nullptr ) {
434
- factory = new JFactoryT<T>;
425
+ auto * databundle = GetLightweightDatabundle<T>(tag, false , false );
426
+
427
+ if (databundle == nullptr ) {
428
+ auto * factory = new JFactoryT<T>;
435
429
factory->SetTag (tag);
436
430
factory->SetLevel (mFactorySet .GetLevel ());
437
431
mFactorySet .Add (factory);
432
+ databundle = GetLightweightDatabundle<T>(tag, false , false );
438
433
}
439
- factory->Insert (item);
440
- factory->SetInsertOrigin ( mCallGraph .GetInsertDataOrigin () ); // (see note at top of JCallGraphRecorder.h)
441
- return factory;
434
+
435
+ databundle->SetStatus (JDatabundle::Status::Inserted);
436
+ databundle->GetData ().push_back (item);
437
+
438
+ auto * factory = databundle->GetFactory ();
439
+ if (factory != nullptr ) {
440
+ factory->SetStatus (JFactory::Status::Inserted); // for when items is empty
441
+ factory->SetCreationStatus (JFactory::CreationStatus::Inserted); // for when items is empty
442
+ factory->SetInsertOrigin ( mCallGraph .GetInsertDataOrigin () ); // (see note at top of JCallGraphRecorder.h)
443
+ return dynamic_cast <JFactoryT<T>*>(factory);
444
+ }
445
+ throw JException (" Attempted to call JEvent::Insert without an underlying JFactoryT. Hint: Did you previously use Output<T>?" );
446
+ // TODO: Have Insert() return the databundle instead of the factory
442
447
}
443
448
444
449
template <class T >
445
450
inline JFactoryT<T>* JEvent::Insert (const std::vector<T*>& items, const std::string& tag) const {
446
451
447
- std::string resolved_tag = tag;
448
- if (mUseDefaultTags && tag.empty ()) {
449
- auto defaultTag = mDefaultTags .find (JTypeInfo::demangle<T>());
450
- if (defaultTag != mDefaultTags .end ()) resolved_tag = defaultTag->second ;
451
- }
452
- auto factory = GetFactory<T>(resolved_tag);
453
- if (factory == nullptr ) {
454
- factory = new JFactoryT<T>;
452
+ auto * databundle = GetLightweightDatabundle<T>(tag, false , false );
453
+
454
+ if (databundle == nullptr ) {
455
+ auto * factory = new JFactoryT<T>;
455
456
factory->SetTag (tag);
456
457
factory->SetLevel (mFactorySet .GetLevel ());
457
458
mFactorySet .Add (factory);
459
+ databundle = GetLightweightDatabundle<T>(tag, false , false );
458
460
}
459
- for (T* item : items) {
460
- factory->Insert (item);
461
- }
462
- factory->SetStatus (JFactory::Status::Inserted); // for when items is empty
463
- factory->SetCreationStatus (JFactory::CreationStatus::Inserted); // for when items is empty
464
- factory->SetInsertOrigin ( mCallGraph .GetInsertDataOrigin () ); // (see note at top of JCallGraphRecorder.h)
465
- return factory;
461
+
462
+ databundle->SetStatus (JDatabundle::Status::Inserted);
463
+ databundle->GetData () = items;
464
+
465
+ auto * factory = databundle->GetFactory ();
466
+ if (factory != nullptr ) {
467
+ factory->SetStatus (JFactory::Status::Inserted); // for when items is empty
468
+ factory->SetCreationStatus (JFactory::CreationStatus::Inserted); // for when items is empty
469
+ factory->SetInsertOrigin ( mCallGraph .GetInsertDataOrigin () ); // (see note at top of JCallGraphRecorder.h)
470
+ return dynamic_cast <JFactoryT<T>*>(factory);
471
+ }
472
+ throw JException (" Attempted to call JEvent::Insert without an underlying JFactoryT. Hint: Did you previously use Output<T>?" );
473
+ // TODO: Have Insert() return the databundle instead of the factory
466
474
}
467
475
468
476
0 commit comments