Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:55:03

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file corecel/cont/detail/VariantUtilsImpl.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <type_traits>
0010 #include <utility>
0011 #include <variant>
0012 
0013 namespace celeritas
0014 {
0015 namespace detail
0016 {
0017 //---------------------------------------------------------------------------//
0018 /*!
0019  * Implementation of \c return_as.
0020  */
0021 template<class T, class F>
0022 struct ReturnAsImpl
0023 {
0024     F apply;
0025 
0026     template<class U>
0027     T operator()(U&& other)
0028     {
0029         return this->apply(std::forward<U>(other));
0030     }
0031 };
0032 
0033 //---------------------------------------------------------------------------//
0034 /*!
0035  * Implementation for a variant that uses traits based on an enum.
0036  */
0037 template<class E, template<E> class ETraits>
0038 struct EnumVariantImpl
0039 {
0040     //! Underlying integer for enum
0041     using EInt = std::underlying_type_t<E>;
0042 
0043     static_assert(static_cast<EInt>(E::size_) > 0, "ill-defined enumeration");
0044 
0045     //! Compile-time range of all allowable enum integer values
0046     using EIntRange
0047         = std::make_integer_sequence<EInt, static_cast<EInt>(E::size_)>;
0048 
0049     //! Map enum integer value to class
0050     template<EInt I>
0051     struct EIntTraits
0052     {
0053         using type = typename ETraits<static_cast<E>(I)>::type;
0054     };
0055 
0056     //!@{
0057     //! Expand an integer sequence into a variant
0058     template<class Seq>
0059     struct VariantImpl;
0060     template<EInt... Is>
0061     struct VariantImpl<std::integer_sequence<EInt, Is...>>
0062     {
0063         using type = std::variant<typename EIntTraits<Is>::type...>;
0064     };
0065     //!@}
0066 
0067     //! Get a variant with all the surface types
0068     using type = typename VariantImpl<EIntRange>::type;
0069 };
0070 
0071 //---------------------------------------------------------------------------//
0072 }  // namespace detail
0073 }  // namespace celeritas