Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-15 09:05:29

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/EnumArray.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <type_traits>
0010 
0011 #include "corecel/Assert.hh"
0012 #include "corecel/Macros.hh"
0013 #include "corecel/Types.hh"
0014 
0015 namespace celeritas
0016 {
0017 //---------------------------------------------------------------------------//
0018 /*!
0019  * Thin wrapper for an array of enums for accessing by enum instead of int.
0020  *
0021  * The enum \em must be a zero-indexed contiguous enumeration with a \c size_
0022  * enumeration as its last value.
0023  *
0024  * \todo The template parameters are reversed!!!
0025  */
0026 template<class E, class T>
0027 class EnumArray
0028 {
0029     static_assert(std::is_enum<E>::value, "Template parameter must be an enum");
0030 
0031   public:
0032     //!@{
0033     //! \name Type aliases
0034     using key_type = E;
0035     using value_type = T;
0036     using size_type = std::underlying_type_t<E>;
0037     using pointer = value_type*;
0038     using const_pointer = value_type const*;
0039     using reference = value_type&;
0040     using const_reference = value_type const&;
0041     using iterator = pointer;
0042     using const_iterator = const_pointer;
0043     //!@}
0044 
0045     enum
0046     {
0047         N = static_cast<size_type>(E::size_)
0048     };
0049 
0050     using CArrayConstRef = T const (&)[N];
0051 
0052   public:
0053     //! Default construction initializes to zero
0054     CELER_CEF EnumArray() : data_{T{}} {}
0055 
0056     //! Construct from an array for aggregate initialization of daughters
0057     CELER_CEF EnumArray(CArrayConstRef values)
0058     {
0059         for (size_type i = 0; i < N; ++i)
0060         {
0061             data_[i] = values[i];
0062         }
0063     }
0064 
0065     //! Construct with C-style aggregate initialization
0066     EnumArray(T first) : data_{{first}} {}
0067 
0068     //! Construct with the array's data
0069     template<class... Us>
0070     CELER_CEF EnumArray(T first, Us... rest)
0071         : data_{first, static_cast<T>(rest)...}
0072     {
0073         // Protect against leaving off an entry, e.g. EnumArray<Foo, int>(1)
0074         static_assert(sizeof...(rest) + 1 == N,
0075                       "All array entries must be explicitly specified");
0076     }
0077 
0078     //! Get an element
0079     CELER_CEF reference operator[](key_type const& k)
0080     {
0081         return data_[static_cast<size_type>(k)];
0082     }
0083 
0084     //! Get an element (const)
0085     CELER_CEF const_reference operator[](key_type const& k) const
0086     {
0087         return data_[static_cast<size_type>(k)];
0088     }
0089 
0090     //!@{
0091     //! Element access
0092     CELER_CEF const_reference front() const { return data_[0]; }
0093     CELER_CEF reference front() { return data_[0]; }
0094     CELER_CEF const_reference back() const { return data_[N - 1]; }
0095     CELER_CEF reference back() { return data_[N - 1]; }
0096     CELER_CEF const_pointer data() const { return data_; }
0097     CELER_CEF pointer data() { return data_; }
0098     //!@}
0099 
0100     //!@{
0101     //! Iterator access
0102     CELER_CEF iterator begin() { return data_; }
0103     CELER_CEF iterator end() { return data_ + N; }
0104     CELER_CEF const_iterator begin() const { return data_; }
0105     CELER_CEF const_iterator end() const { return data_ + N; }
0106     CELER_CEF const_iterator cbegin() const { return data_; }
0107     CELER_CEF const_iterator cend() const { return data_ + N; }
0108     //!@}
0109 
0110     //!@{
0111     //! Capacity
0112     CELER_CEF bool empty() const { return N == 0; }
0113     CELER_CEF size_type size() const { return N; }
0114     //!@}
0115 
0116   private:
0117     T data_[N];  //!< Storage
0118 };
0119 
0120 //---------------------------------------------------------------------------//
0121 }  // namespace celeritas