Skip to content

Commit e6f109e

Browse files
committed
JEvent getters rely less on JFactoryT
1 parent 9adfb56 commit e6f109e

File tree

1 file changed

+78
-70
lines changed

1 file changed

+78
-70
lines changed

src/libraries/JANA/JEvent.h

Lines changed: 78 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
#include <typeindex>
2727
#include <vector>
28-
#include <cstddef>
2928
#include <memory>
3029
#include <atomic>
3130

@@ -220,14 +219,14 @@ template<class T>
220219
inline std::vector<JFactoryT<T>*> JEvent::GetFactoryAll(bool throw_on_missing) const {
221220
std::vector<JFactoryT<T>*> factories;
222221
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);
225225
}
226226
}
227227
if (factories.size() == 0) {
228228
if (throw_on_missing) {
229229
JException ex("Could not find any JFactoryT<" + JTypeInfo::demangle<T>() + "> (from any tag)");
230-
ex.show_stacktrace = false;
231230
throw ex;
232231
}
233232
};
@@ -238,8 +237,7 @@ inline std::vector<JFactoryT<T>*> JEvent::GetFactoryAll(bool throw_on_missing) c
238237
/// C-style getters
239238

240239
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 {
243241
/// This is a convenience method that can be used to get a pointer to the single
244242
/// object of type T from the specified factory. It simply calls the Get(vector<...>) method
245243
/// 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_
252250
/// exception_if_not_one to false. In that case, you will have to check if t==NULL to
253251
/// know if the call succeeded.
254252

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());
263258
}
264259
}
265-
t = v[0];
266-
return fac;
260+
else {
261+
t = databundle->GetData().at(0);
262+
}
263+
return dynamic_cast<JFactoryT<T>*>(databundle->GetFactory());
267264
}
268265

269266
/// 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_
274271
/// - If the factory contains exactly one item, GetSingle updates the `destination` to point to that item.
275272
/// - If the factory contains more than one item, GetSingle updates the `destination` to point to the first time.
276273
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) {
283277
*destination = nullptr;
284278
}
285279
else {
286-
*destination = *iterators.first;
280+
*destination = databundle->GetData().at(0);
287281
}
288-
return factory;
282+
return dynamic_cast<JFactoryT<T>*>(databundle->GetFactory());
289283
}
290284

291285

292286
template<class T>
293287
JFactoryT<T>* JEvent::Get(std::vector<const T*>& destination, const std::string& tag, bool strict) const
294288
{
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());
303296
}
304297

305298

@@ -375,26 +368,31 @@ std::vector<const T*> JEvent::Get(const std::string& tag, bool strict) const {
375368

376369
template<class T>
377370
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());
382375
}
383376

384377
/// GetAll returns all JObjects of (child) type T, regardless of tag.
385378
template<class T>
386379
std::vector<const T*> JEvent::GetAll() const {
387-
std::vector<const T*> vec;
388-
auto factories = GetFactoryAll<T>(true);
389380

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+
}
395393
}
396394
}
397-
return vec; // Assumes RVO
395+
return results;
398396
}
399397

400398
// 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
424422
template <class T>
425423
inline JFactoryT<T>* JEvent::Insert(T* item, const std::string& tag) const {
426424

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>;
435429
factory->SetTag(tag);
436430
factory->SetLevel(mFactorySet.GetLevel());
437431
mFactorySet.Add(factory);
432+
databundle = GetLightweightDatabundle<T>(tag, false, false);
438433
}
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
442447
}
443448

444449
template <class T>
445450
inline JFactoryT<T>* JEvent::Insert(const std::vector<T*>& items, const std::string& tag) const {
446451

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>;
455456
factory->SetTag(tag);
456457
factory->SetLevel(mFactorySet.GetLevel());
457458
mFactorySet.Add(factory);
459+
databundle = GetLightweightDatabundle<T>(tag, false, false);
458460
}
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
466474
}
467475

468476

0 commit comments

Comments
 (0)