Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataHandle.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file DataHandle.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
14 
15 #include <iostream>
16 #include <stdexcept>
17 #include <typeinfo>
18 
19 namespace ActsExamples {
20 
22  protected:
23  virtual ~DataHandleBase() = default;
24 
26  : m_parent(parent), m_name(name) {}
27 
28  // We can't change addresses after construction
29  DataHandleBase(const DataHandleBase&) = delete;
30  DataHandleBase(DataHandleBase&&) = default;
31 
32  public:
33  const std::string& key() const { return m_key.value(); }
34 
35  virtual const std::type_info& typeInfo() const = 0;
36 
37  bool isInitialized() const { return m_key.has_value(); }
38 
39  const std::string& name() const { return m_name; }
40 
42  if (!key.empty()) {
43  m_key = key;
44  }
45  }
46 
47  virtual bool isCompatible(const DataHandleBase& other) const = 0;
48 
49  std::string fullName() const { return m_parent->name() + "." + name(); }
50 
51  protected:
54  std::optional<std::string> m_key{};
55 };
56 
57 template <typename T>
59 
60 template <typename T>
61 class WriteDataHandle final : public DataHandleBase {
62  public:
64  : DataHandleBase{parent, name} {
66  }
67 
68  void operator()(const AlgorithmContext& ctx, T&& value) const {
69  (*this)(ctx.eventStore, std::forward<T>(value));
70  }
71 
72  void operator()(WhiteBoard& wb, T&& value) const {
73  if (!isInitialized()) {
74  throw std::runtime_error{"WriteDataHandle '" + fullName() +
75  "' not initialized"};
76  }
77  wb.add(m_key.value(), std::move(value));
78  }
79 
80  void initialize(const std::string& key) {
81  if (key.empty()) {
82  throw std::invalid_argument{"Write handle '" + fullName() +
83  "' cannot receive empty key"};
84  }
85  m_key = key;
86  }
87 
88  bool isCompatible(const DataHandleBase& other) const override {
89  return dynamic_cast<const ReadDataHandle<T>*>(&other) != nullptr;
90  }
91 
92  const std::type_info& typeInfo() const override { return typeid(T); };
93 };
94 
95 template <typename T>
96 class ReadDataHandle final : public DataHandleBase {
97  public:
99  : DataHandleBase{parent, name} {
101  }
102 
103  void initialize(const std::string& key) {
104  if (key.empty()) {
105  throw std::invalid_argument{"Read handle '" + fullName() +
106  "' cannot receive empty key"};
107  }
108  m_key = key;
109  }
110 
111  const T& operator()(const AlgorithmContext& ctx) const {
112  return (*this)(ctx.eventStore);
113  }
114 
115  const T& operator()(const WhiteBoard& wb) const {
116  if (!isInitialized()) {
117  throw std::runtime_error{"ReadDataHandle '" + fullName() +
118  "' not initialized"};
119  }
120  return wb.get<T>(m_key.value());
121  }
122 
123  bool isCompatible(const DataHandleBase& other) const override {
124  return dynamic_cast<const WriteDataHandle<T>*>(&other) != nullptr;
125  }
126 
127  const std::type_info& typeInfo() const override { return typeid(T); };
128 };
129 
130 } // namespace ActsExamples