File indexing completed on 2025-09-15 08:28:30
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include "ActsExamples/Framework/SequenceElement.hpp"
0012 #include "ActsExamples/Framework/WhiteBoard.hpp"
0013
0014 #include <stdexcept>
0015 #include <typeinfo>
0016
0017 namespace ActsExamples {
0018
0019 class DataHandleBase {
0020 protected:
0021 virtual ~DataHandleBase() = default;
0022
0023 DataHandleBase(SequenceElement* parent, const std::string& name)
0024 : m_parent(parent), m_name(name) {}
0025
0026
0027 DataHandleBase(const DataHandleBase&) = delete;
0028 DataHandleBase(DataHandleBase&&) = default;
0029
0030 public:
0031 const std::string& key() const { return m_key.value(); }
0032
0033 virtual const std::type_info& typeInfo() const = 0;
0034
0035 bool isInitialized() const { return m_key.has_value(); }
0036
0037 const std::string& name() const { return m_name; }
0038
0039 void maybeInitialize(const std::string& key) {
0040 if (!key.empty()) {
0041 m_key = key;
0042 }
0043 }
0044
0045 virtual bool isCompatible(const DataHandleBase& other) const = 0;
0046
0047 std::string fullName() const { return m_parent->name() + "." + name(); }
0048
0049 protected:
0050 SequenceElement* m_parent{nullptr};
0051 std::string m_name;
0052 std::optional<std::string> m_key{};
0053 };
0054
0055 class WriteDataHandleBase : public DataHandleBase {
0056 protected:
0057 WriteDataHandleBase(SequenceElement* parent, const std::string& name)
0058 : DataHandleBase{parent, name} {}
0059
0060 public:
0061 void initialize(const std::string& key);
0062
0063 bool isCompatible(const DataHandleBase& other) const final;
0064 };
0065
0066 class ReadDataHandleBase : public DataHandleBase {
0067 protected:
0068 ReadDataHandleBase(SequenceElement* parent, const std::string& name)
0069 : DataHandleBase{parent, name} {}
0070
0071 public:
0072 void initialize(const std::string& key);
0073
0074 bool isCompatible(const DataHandleBase& other) const final;
0075 };
0076
0077 template <typename T>
0078 class WriteDataHandle final : public WriteDataHandleBase {
0079 public:
0080 WriteDataHandle(SequenceElement* parent, const std::string& name)
0081 : WriteDataHandleBase{parent, name} {
0082 m_parent->registerWriteHandle(*this);
0083 }
0084
0085 void operator()(const AlgorithmContext& ctx, T&& value) const {
0086 (*this)(ctx.eventStore, std::move(value));
0087 }
0088
0089 void operator()(WhiteBoard& wb, T&& value) const {
0090 if (!isInitialized()) {
0091 throw std::runtime_error{"WriteDataHandle '" + fullName() +
0092 "' not initialized"};
0093 }
0094 wb.add(m_key.value(), std::move(value));
0095 }
0096
0097 const std::type_info& typeInfo() const override { return typeid(T); };
0098 };
0099
0100 template <typename T>
0101 class ReadDataHandle final : public ReadDataHandleBase {
0102 public:
0103 ReadDataHandle(SequenceElement* parent, const std::string& name)
0104 : ReadDataHandleBase{parent, name} {
0105 m_parent->registerReadHandle(*this);
0106 }
0107
0108 const T& operator()(const AlgorithmContext& ctx) const {
0109 return (*this)(ctx.eventStore);
0110 }
0111
0112 const T& operator()(const WhiteBoard& wb) const {
0113 if (!isInitialized()) {
0114 throw std::runtime_error{"ReadDataHandle '" + fullName() +
0115 "' not initialized"};
0116 }
0117 return wb.get<T>(m_key.value());
0118 }
0119
0120 const std::type_info& typeInfo() const override { return typeid(T); };
0121 };
0122
0123 }