Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-27 07:24:14

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 // System include(s)
0012 #include <any>
0013 #include <cstddef>
0014 #include <memory>
0015 #include <ostream>
0016 #include <stdexcept>
0017 #include <string>
0018 #include <type_traits>
0019 #include <unordered_map>
0020 #include <utility>
0021 
0022 namespace detray::test {
0023 
0024 /// A container to store arbitrary objects with ownership transfer.
0025 ///
0026 /// This is an append-only container that takes ownership of the objects
0027 /// added to it. Once an object has been added, it can only be read but not
0028 /// be modified. Trying to replace an existing object is considered an error.
0029 /// Its lifetime is bound to the lifetime of the white board.
0030 /// @see
0031 /// https://github.com/acts-project/acts/blob/main/Examples/Framework/include/detray::test/Framework/WhiteBoard.hpp
0032 class whiteboard {
0033  public:
0034   whiteboard() = default;
0035 
0036   // A whiteboard holds unique elements and can not be copied
0037   whiteboard(const whiteboard& other) = delete;
0038   whiteboard& operator=(const whiteboard&) = delete;
0039 
0040   bool exists(const std::string& name) const;
0041 
0042   /// Store an object on the white board and transfer ownership.
0043   ///
0044   /// @param name Non-empty identifier to store it under
0045   /// @param object Movable reference to the transferable object
0046   /// @throws std::invalid_argument on empty or duplicate name
0047   template <typename T>
0048   void add(const std::string& name, T&& object);
0049 
0050   /// Get access to a stored object.
0051   ///
0052   /// @param[in] name Identifier for the object
0053   /// @return reference to the stored object
0054   /// @throws std::out_of_range if no object is stored under the requested
0055   /// name
0056   template <typename T>
0057   const T& get(const std::string& name) const;
0058 
0059   /// Get access to a stored object - non-const
0060   template <typename T>
0061   T& get(const std::string& name);
0062 
0063  private:
0064   /// Backend storage
0065   std::unordered_map<std::string, std::any> m_store{};
0066 };
0067 
0068 template <typename T>
0069 inline void detray::test::whiteboard::add(const std::string& name, T&& object) {
0070   if (name.empty()) {
0071     throw std::invalid_argument("Object cannot have an empty name");
0072   }
0073   if (m_store.contains(name)) {
0074     throw std::invalid_argument("Object '" + name + "' already exists");
0075   }
0076   m_store.emplace(name, std::forward<T>(object));
0077 }
0078 
0079 template <typename T>
0080 inline const T& detray::test::whiteboard::get(const std::string& name) const
0081     noexcept(false) {
0082   auto it = m_store.find(name);
0083   if (it == m_store.end()) {
0084     throw std::out_of_range("Object '" + name + "' does not exists");
0085   }
0086   // Try to retrieve the value as the requested type
0087   return std::any_cast<const T&>(it->second);
0088 }
0089 
0090 template <typename T>
0091 inline T& detray::test::whiteboard::get(const std::string& name) noexcept(
0092     false) {
0093   auto it = m_store.find(name);
0094   if (it == m_store.end()) {
0095     throw std::out_of_range("Object '" + name + "' does not exists");
0096   }
0097   // Try to retrieve the value as the requested type
0098   return std::any_cast<T&>(it->second);
0099 }
0100 
0101 inline bool detray::test::whiteboard::exists(const std::string& name) const {
0102   return m_store.contains(name);
0103 }
0104 
0105 }  // namespace detray::test