forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDataHandle.cpp
More file actions
226 lines (192 loc) · 7.83 KB
/
DataHandle.cpp
File metadata and controls
226 lines (192 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#include "ActsExamples/Framework/DataHandle.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/Framework/Sequencer.hpp"
#include <regex>
#include <typeindex>
#include <typeinfo>
#include <boost/algorithm/string.hpp>
#include <boost/core/demangle.hpp>
namespace ActsExamples {
namespace {
/// Shorten some common but lengthy C++ constructs
std::string demangleAndShorten(std::string name) {
name = boost::core::demangle(name.c_str());
// Normalize libc++ std::__1 namespace to std::
boost::algorithm::replace_all(name, "std::__1::", "std::");
// Remove default deleter from unique_ptr
const static std::regex unique_ptr_pattern(
R"??(std::unique_ptr<(.*),\s*std::default_delete<(\1\s*)>\s*>)??");
name = std::regex_replace(name, unique_ptr_pattern, "std::unique_ptr<$1>");
// Remove std::allocator from vector
const static std::regex vector_pattern(
R"??(std::vector<(.*), std::allocator<(\1\s*)>\s*>)??");
name = std::regex_replace(name, vector_pattern, "std::vector<$1>");
// Shorten Acts::BoundVariantMeasurement
const static std::regex variant_pattern(
R"??(std::variant<(Acts::Measurement<Acts::BoundIndices, [0-9]ul>(,|)\s+)+>)??");
name = std::regex_replace(name, variant_pattern,
"Acts::BoundVariantMeasurement");
// strip namespaces
boost::algorithm::replace_all(name, "std::", "");
boost::algorithm::replace_all(name, "boost::container::", "");
boost::algorithm::replace_all(name, "Acts::", "");
boost::algorithm::replace_all(name, "ActsExamples::", "");
boost::algorithm::replace_all(name, "ActsFatras::", "");
return name;
}
std::string symbol(const char* in) {
std::stringstream ss;
std::string s = demangleAndShorten(in);
std::size_t pos = 0;
while (pos + 80 < s.size()) {
ss << " " + s.substr(pos, 80);
pos += 80;
}
ss << s.substr(pos);
return ss.str();
};
} // namespace
void WriteDataHandleBase::initialize(std::string_view key) {
if (key.empty()) {
throw std::invalid_argument{"Write handle '" + fullName() +
"' cannot receive empty key"};
}
m_key = key;
}
void DataHandleBase::maybeInitialize(std::optional<std::string_view> key) {
if (key.has_value() && !key.value().empty()) {
m_key = key.value();
}
}
bool WriteDataHandleBase::isCompatible(const DataHandleBase& other) const {
return typeHash() == other.typeHash();
}
void WriteDataHandleBase::emulate(StateMapType& state,
WhiteBoard::AliasMapType& aliases,
const Acts::Logger& logger) const {
if (!isInitialized()) {
return;
}
ACTS_INFO("-> " << name() << " '" << key() << "':");
ACTS_INFO(" " << symbol(typeInfo().name()));
if (auto it = state.find(key()); it != state.end()) {
const auto& source = *it->second;
ACTS_ERROR("White board will already contain key '"
<< key() << "'. Source: '" << source.fullName()
<< "' (cannot overwrite)");
throw SequenceConfigurationException{
"Incompatible data handle type for key '" + key() +
"': " + source.fullName()};
}
state.try_emplace(key(), this);
if (auto it = aliases.find(key()); it != aliases.end()) {
ACTS_DEBUG("Key '" << key() << "' aliased to '" << it->second << "'");
state.try_emplace(it->second, this);
}
}
void ReadDataHandleBase::initialize(std::string_view key) {
if (key.empty()) {
throw std::invalid_argument{"Read handle '" + fullName() +
"' cannot receive empty key"};
}
m_key = key;
}
bool ReadDataHandleBase::isCompatible(const DataHandleBase& other) const {
return typeHash() == other.typeHash();
}
void ReadDataHandleBase::emulate(StateMapType& state,
WhiteBoard::AliasMapType& /*aliases*/,
const Acts::Logger& logger) const {
if (!isInitialized()) {
return;
}
ACTS_INFO("<- " << name() << " '" << key() << "':");
ACTS_INFO(" " << symbol(typeInfo().name()));
if (auto it = state.find(key()); it != state.end()) {
const auto& source = *it->second;
if (!source.isCompatible(*this)) {
ACTS_ERROR(
"Adding " << m_parent->typeName() << " " << m_parent->name() << ":"
<< "\n-> white board will contain key '" << key() << "'"
<< "\nat this point in the sequence (source: "
<< source.fullName() << "),"
<< "\nbut the type will be\n"
<< "'" << demangleAndShorten(source.typeInfo().name())
<< "'"
<< "\nand not\n"
<< "'" << demangleAndShorten(typeInfo().name()) << "'");
throw SequenceConfigurationException{
"Incompatible data handle type for key '" + key() +
"': " + source.fullName()};
}
} else {
ACTS_ERROR(
"Adding " << m_parent->typeName() << " " << m_parent->name() << ":"
<< "\n-> white board will not contain key"
<< " '" << key() << "' at this point in the sequence."
<< "\n Needed for read data handle '" << name() << "'");
throw SequenceConfigurationException{"Missing data handle for key '" +
key() + "'"};
}
}
void ConsumeDataHandleBase::emulate(StateMapType& state,
WhiteBoard::AliasMapType& aliases,
const Acts::Logger& logger) const {
if (!isInitialized()) {
return;
}
ACTS_INFO("<< " << name() << " '" << key() << "':");
ACTS_INFO(" " << symbol(typeInfo().name()));
if (auto it = state.find(key()); it != state.end()) {
if (const auto& source = *it->second; !source.isCompatible(*this)) {
ACTS_ERROR(
"Adding " << m_parent->typeName() << " " << m_parent->name() << ":"
<< "\n-> white board will contain key '" << key() << "'"
<< "\nat this point in the sequence (source: "
<< source.fullName() << "),"
<< "\nbut the type will be\n"
<< "'" << demangleAndShorten(source.typeInfo().name())
<< "'"
<< "\nand not\n"
<< "'" << demangleAndShorten(typeInfo().name()) << "'");
throw SequenceConfigurationException{
"Incompatible data handle type for key '" + key() +
"': " + source.fullName()};
}
// Remove the key from state since it will be consumed
ACTS_VERBOSE("Removing key '" << key() << "' from state");
state.erase(it);
for (const auto& [source, target] : aliases) {
if (target == key()) {
ACTS_VERBOSE("Removing alias target '" << target << "' from state");
state.erase(source);
}
if (source == key()) {
ACTS_VERBOSE("Removing alias source '" << source << "' from state");
state.erase(target);
}
}
} else {
ACTS_ERROR(
"Adding " << m_parent->typeName() << " " << m_parent->name() << ":"
<< "\n-> white board will not contain key"
<< " '" << key() << "' at this point in the sequence."
<< "\n Needed for consume data handle '" << name() << "'");
throw SequenceConfigurationException{"Missing data handle for key '" +
key() + "'"};
}
}
void DataHandleBase::registerAsWriteHandle() {
m_parent->registerWriteHandle(*this);
}
void DataHandleBase::registerAsReadHandle() {
m_parent->registerReadHandle(*this);
}
} // namespace ActsExamples