Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:55

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2021 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <any>
0012 
0013 namespace Acts {
0014 
0015 /// Strong type wrapper around std::any. This has all the flexibility of
0016 /// std::any, but it does not convert-construct from anything. You have to call
0017 /// one of the explicit constructors manually to populate
0018 /// the internal std::any. You can then access and modify the any as desired.
0019 ///
0020 /// @note This is used for the context types, and should probably not be used
0021 /// outside of this use-case.
0022 class ContextType {
0023  public:
0024   /// Default constructor, does nothing
0025   ///
0026   ContextType() = default;
0027 
0028   /// Move construct a new Context Type object from anything. Must be explicit.
0029   ///
0030   /// @tparam T The type of the value to construct from
0031   /// @param value The value to construct from
0032   template <typename T>
0033   explicit ContextType(T&& value) : m_data{std::move(value)} {}
0034 
0035   /// Copy construct a new Context Type object from anything. Must be explicit.
0036   ///
0037   /// @tparam T The type of the value to construct from
0038   /// @param value The value to construct from
0039   template <typename T>
0040   explicit ContextType(const T& value) : m_data{value} {}
0041 
0042   /// Move assignment of anything to this object is allowed.
0043   ///
0044   /// @tparam T The type of the value to assign
0045   /// @param value The value to assign
0046   /// @return ContextType&
0047   template <typename T>
0048   ContextType& operator=(T&& value) {
0049     m_data = std::move(value);
0050     return *this;
0051   }
0052 
0053   /// Copy assignment of anything to this object is allowed.
0054   ///
0055   /// @tparam T The type of the value to assign
0056   /// @param value The value to assign
0057   /// @return ContextType&
0058   template <typename T>
0059   ContextType& operator=(const T& value) {
0060     m_data = value;
0061     return *this;
0062   }
0063 
0064   /// Retrieve a reference to the contained type
0065   ///
0066   /// @tparam T The type to attempt to retrieve the value as
0067   /// @return Reference to the contained value
0068   template <typename T>
0069   std::decay_t<T>& get() {
0070     return std::any_cast<std::decay_t<T>&>(m_data);
0071   }
0072 
0073   /// Retrieve a reference to the contained type
0074   ///
0075   /// @tparam T The type to attempt to retrieve the value as
0076   /// @return Reference to the contained value
0077   template <typename T>
0078   const std::decay_t<T>& get() const {
0079     return std::any_cast<const std::decay_t<T>&>(m_data);
0080   }
0081 
0082   /// Check if the contained type is initialized.
0083   /// @return Boolean indicating whether a type is present
0084   bool hasValue() const { return m_data.has_value(); }
0085 
0086  private:
0087   std::any m_data;
0088 };
0089 
0090 }  // namespace Acts