Skip to content

Commit

Permalink
pauls patch
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminhuth committed Jan 16, 2025
1 parent 29560cc commit af06548
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 38 deletions.
57 changes: 24 additions & 33 deletions Examples/Framework/include/ActsExamples/Framework/WhiteBoard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,6 @@ class WhiteBoard {
void copyFrom(const WhiteBoard& other);

private:
/// Store an object on the white board and transfer ownership.
///
/// @param name Non-empty identifier to store it under
/// @param object Movable reference to the transferable object
/// @throws std::invalid_argument on empty or duplicate name
template <typename T>
void add(const std::string& name, T&& object);

/// Get access to a stored object.
///
/// @param[in] name Identifier for the object
/// @return reference to the stored object
/// @throws std::out_of_range if no object is stored under the requested name
template <typename T>
const T& get(const std::string& name) const;

private:
/// Find similar names for suggestions with levenshtein-distance
std::vector<std::string_view> similarNames(const std::string_view& name,
Expand All @@ -88,6 +72,30 @@ class WhiteBoard {
const std::type_info& type() const override { return typeid(T); }
};

/// Store an holder on the white board and transfer ownership.
///
/// @param name Non-empty identifier to store it under
/// @param holder The holder to store
/// @throws std::invalid_argument on empty or duplicate name
void addHolder(const std::string& name, std::shared_ptr<IHolder> holder);

/// Store an object on the white board and transfer ownership.
///
/// @param name Non-empty identifier to store it under
/// @param object Movable reference to the transferable object
template <typename T>
void add(const std::string& name, T&& object) {
addHolder(name, std::make_shared<HolderT<T>>(std::forward<T>(object)));
}

/// Get access to a stored object.
///
/// @param[in] name Identifier for the object
/// @return reference to the stored object
/// @throws std::out_of_range if no object is stored under the requested name
template <typename T>
const T& get(const std::string& name) const;

std::unique_ptr<const Acts::Logger> m_logger;
std::unordered_map<std::string, std::shared_ptr<IHolder>> m_store;
std::unordered_map<std::string, std::string> m_objectAliases;
Expand All @@ -111,23 +119,6 @@ inline ActsExamples::WhiteBoard::WhiteBoard(
std::unordered_map<std::string, std::string> objectAliases)
: m_logger(std::move(logger)), m_objectAliases(std::move(objectAliases)) {}

template <typename T>
inline void ActsExamples::WhiteBoard::add(const std::string& name, T&& object) {
if (name.empty()) {
throw std::invalid_argument("Object can not have an empty name");
}
if (m_store.contains(name)) {
throw std::invalid_argument("Object '" + name + "' already exists");
}
auto holder = std::make_shared<HolderT<T>>(std::forward<T>(object));
m_store.emplace(name, holder);
ACTS_VERBOSE("Added object '" << name << "' of type " << typeid(T).name());
if (auto it = m_objectAliases.find(name); it != m_objectAliases.end()) {
m_store[it->second] = holder;
ACTS_VERBOSE("Added alias object '" << it->second << "'");
}
}

template <typename T>
inline const T& ActsExamples::WhiteBoard::get(const std::string& name) const {
ACTS_VERBOSE("Attempt to get object '" << name << "' of type "
Expand Down
36 changes: 31 additions & 5 deletions Examples/Framework/src/Framework/WhiteBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ std::vector<std::string_view> ActsExamples::WhiteBoard::similarNames(
names.push_back({d, n});
}
}
for (const auto &[from, to] : m_objectAliases) {
if (const auto d = levenshteinDistance(from, name); d < distThreshold) {
names.push_back({d, from});
}
}

std::ranges::sort(names, {}, [](const auto &n) { return n.first; });

Expand All @@ -87,12 +92,33 @@ std::string ActsExamples::WhiteBoard::typeMismatchMessage(

void ActsExamples::WhiteBoard::copyFrom(const WhiteBoard &other) {
for (auto &[key, val] : other.m_store) {
auto [it, success] = m_store.insert({key, val});
addHolder(key, val);
ACTS_VERBOSE("Copied key '" << key << "' to whiteboard");
}
}

void ActsExamples::WhiteBoard::addHolder(const std::string &name,
std::shared_ptr<IHolder> holder) {
if (name.empty()) {
throw std::invalid_argument("Object can not have an empty name");
}

if (!success) {
throw std::runtime_error("Cannot insert key '" + key +
"', is already present");
if (holder == nullptr) {
throw std::invalid_argument("Object '" + name + "' is nullptr");
}

auto [storeIt, success] = m_store.insert({name, std::move(holder)});

if (!success) {
throw std::invalid_argument("Object '" + name + "' already exists");
}
ACTS_VERBOSE("Added object '" << name << "' of type "
<< storeIt->second->type().name());

if (success) {
if (auto it = m_objectAliases.find(name); it != m_objectAliases.end()) {
m_store[it->second] = storeIt->second;
ACTS_VERBOSE("Added alias object '" << it->second << "'");
}
ACTS_VERBOSE("Copied key '" << key << "' to whiteboard");
}
}

0 comments on commit af06548

Please sign in to comment.