File indexing completed on 2025-07-01 08:58:09
0001
0002 #pragma once
0003
0004 #include <JANA/Components/JDatabundle.h>
0005 #include <JANA/JObject.h>
0006 #include <JANA/Utils/JTypeInfo.h>
0007
0008 #if JANA2_HAVE_ROOT
0009 #include <TObject.h>
0010 #endif
0011
0012 template <typename T>
0013 class JLightweightDatabundleT : public JDatabundle {
0014 private:
0015 std::vector<T*>* m_data = nullptr;
0016 bool m_is_owned = false;
0017 bool m_is_persistent = false;
0018 bool m_not_object_owner = false;
0019
0020 public:
0021 JLightweightDatabundleT();
0022 void AttachData(std::vector<T*>* data) { m_data = data; }
0023 void ClearData() override;
0024
0025 size_t GetSize() const override { return m_data->size();}
0026 std::vector<T*>& GetData() { return *m_data; }
0027
0028 void SetPersistentFlag(bool persistent) { m_is_persistent = persistent; }
0029 void SetNotOwnerFlag(bool not_owner) { m_not_object_owner = not_owner; }
0030
0031 bool GetPersistentFlag() { return m_is_persistent; }
0032 bool GetNotOwnerFlag() { return m_not_object_owner; }
0033
0034
0035
0036
0037
0038 template <typename S> void EnableGetAs ();
0039
0040
0041
0042 template <typename S> void EnableGetAs(std::true_type) { EnableGetAs<S>(); }
0043 template <typename S> void EnableGetAs(std::false_type) {}
0044 };
0045
0046
0047
0048 template <typename T>
0049 JLightweightDatabundleT<T>::JLightweightDatabundleT() {
0050 SetTypeName(JTypeInfo::demangle<T>());
0051 EnableGetAs<T>();
0052 EnableGetAs<JObject>( std::is_convertible<T,JObject>() );
0053 #if JANA2_HAVE_ROOT
0054 EnableGetAs<TObject>( std::is_convertible<T,TObject>() );
0055 #endif
0056 }
0057
0058 template <typename T>
0059 void JLightweightDatabundleT<T>::ClearData() {
0060
0061
0062
0063 if (GetPersistentFlag()) {
0064 return;
0065 }
0066
0067
0068 if (!GetNotOwnerFlag()) {
0069 for (auto p : *m_data) delete p;
0070 }
0071 m_data->clear();
0072 SetStatus(Status::Empty);
0073 }
0074
0075 template<typename T>
0076 template<typename S>
0077 void JLightweightDatabundleT<T>::EnableGetAs() {
0078
0079 auto upcast_lambda = [this]() {
0080 std::vector<S*> results;
0081 for (auto t : *m_data) {
0082 results.push_back(static_cast<S*>(t));
0083 }
0084 return results;
0085 };
0086
0087 auto key = std::type_index(typeid(S));
0088 using upcast_fn_t = std::function<std::vector<S*>()>;
0089 mUpcastVTable[key] = std::unique_ptr<JAny>(new JAnyT<upcast_fn_t>(std::move(upcast_lambda)));
0090 }
0091
0092
0093
0094
0095