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