Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:46

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