File indexing completed on 2025-12-16 10:36:43
0001
0002 #pragma once
0003
0004 #include <JANA/Components/JDatabundle.h>
0005 #include <JANA/JObject.h>
0006 #include <JANA/Utils/JTypeInfo.h>
0007 #include <typeindex>
0008
0009 #if JANA2_HAVE_ROOT
0010 #include <TObject.h>
0011 #endif
0012
0013 template <typename T>
0014 class JLightweightDatabundleT : public JDatabundle {
0015 private:
0016 std::vector<T*>* m_data = nullptr;
0017 bool m_owns_data = false;
0018 bool m_is_persistent = false;
0019 bool m_not_object_owner = false;
0020
0021 public:
0022 JLightweightDatabundleT(std::vector<T*>* external_data=nullptr);
0023 JLightweightDatabundleT(const JLightweightDatabundleT& other);
0024 ~JLightweightDatabundleT() override;
0025
0026 void ClearData() override;
0027
0028 size_t GetSize() const override { return m_data->size();}
0029 std::vector<T*>& GetData() { return *m_data; }
0030
0031 void SetPersistentFlag(bool persistent) { m_is_persistent = persistent; }
0032 void SetNotOwnerFlag(bool not_owner) { m_not_object_owner = not_owner; }
0033
0034 bool GetPersistentFlag() { return m_is_persistent; }
0035 bool GetNotOwnerFlag() { return m_not_object_owner; }
0036
0037
0038
0039
0040
0041 template <typename S> void EnableGetAs ();
0042
0043
0044
0045 template <typename S> void EnableGetAs(std::true_type) { EnableGetAs<S>(); }
0046 template <typename S> void EnableGetAs(std::false_type) {}
0047 };
0048
0049
0050
0051 template <typename T>
0052 JLightweightDatabundleT<T>::JLightweightDatabundleT(std::vector<T*>* external_data) {
0053 if (external_data != nullptr) {
0054 m_data = external_data;
0055 m_owns_data = false;
0056 }
0057 else {
0058 m_data = new std::vector<T*>;
0059 m_owns_data = true;
0060 }
0061
0062 SetTypeName(JTypeInfo::demangle<T>());
0063 SetTypeIndex(std::type_index(typeid(T)));
0064 EnableGetAs<T>();
0065 EnableGetAs<JObject>( std::is_convertible<T,JObject>() );
0066 #if JANA2_HAVE_ROOT
0067 EnableGetAs<TObject>( std::is_convertible<T,TObject>() );
0068 #endif
0069 }
0070
0071 template <typename T>
0072 JLightweightDatabundleT<T>::JLightweightDatabundleT(const JLightweightDatabundleT& other) : JDatabundle(other) {
0073
0074 m_data = new std::vector<T*>;
0075 m_owns_data = true;
0076
0077 m_is_persistent = other.m_is_persistent;
0078 m_not_object_owner = other.m_not_object_owner;
0079
0080
0081 EnableGetAs<T>();
0082 EnableGetAs<JObject>( std::is_convertible<T,JObject>() );
0083 #if JANA2_HAVE_ROOT
0084 EnableGetAs<TObject>( std::is_convertible<T,TObject>() );
0085 #endif
0086 }
0087
0088 template <typename T>
0089 JLightweightDatabundleT<T>::~JLightweightDatabundleT() {
0090 if (m_owns_data) {
0091 delete m_data;
0092 }
0093 }
0094
0095 template <typename T>
0096 void JLightweightDatabundleT<T>::ClearData() {
0097
0098
0099
0100 if (GetPersistentFlag()) {
0101 return;
0102 }
0103
0104
0105 if (!GetNotOwnerFlag()) {
0106 for (auto p : *m_data) delete p;
0107 }
0108 m_data->clear();
0109 SetStatus(Status::Empty);
0110 }
0111
0112 template<typename T>
0113 template<typename S>
0114 void JLightweightDatabundleT<T>::EnableGetAs() {
0115
0116 auto upcast_lambda = [this]() {
0117 std::vector<S*> results;
0118 for (auto t : *m_data) {
0119 results.push_back(static_cast<S*>(t));
0120 }
0121 return results;
0122 };
0123
0124 auto key = std::type_index(typeid(S));
0125 using upcast_fn_t = std::function<std::vector<S*>()>;
0126 (*m_upcast_fns)[key] = std::unique_ptr<JAny>(new JAnyT<upcast_fn_t>(std::move(upcast_lambda)));
0127 }
0128
0129
0130
0131
0132