Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:31:28

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2022-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 celeritas/phys/AtomicNumber.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include "corecel/Assert.hh"
0011 
0012 namespace celeritas
0013 {
0014 //---------------------------------------------------------------------------//
0015 /*!
0016  * Type-safe atomic number identifier.
0017  *
0018  * Atomic numbers (Z and A) have constraints that integers do not (zero is not
0019  * an atomic number, dividing one atomic number by another is meaningless) so
0020  * this type improves the semantics and safety.
0021  *
0022  * Generally speaking, Z and A numbers are used during setup and when
0023  * evaluating expressions that use atomic charge. Z or A should not be used to
0024  * "index" data during runtime.
0025  *
0026  * Constructing with a nonpositive values gives the result in a `false` state
0027  * (\c get will throw a \c DebugError).
0028  */
0029 class AtomicNumber
0030 {
0031   public:
0032     //! Construct with an invalid/unassigned value of zero
0033     constexpr AtomicNumber() = default;
0034 
0035     //! Construct with the Z value
0036     explicit CELER_CONSTEXPR_FUNCTION AtomicNumber(int z_or_a_value)
0037         : value_(z_or_a_value)
0038     {
0039     }
0040 
0041     //! True if value is assigned/valid
0042     explicit CELER_CONSTEXPR_FUNCTION operator bool() const
0043     {
0044         return value_ > 0;
0045     }
0046 
0047     //! Get the Z or A value
0048     CELER_CONSTEXPR_FUNCTION int unchecked_get() const { return value_; }
0049 
0050     // Get the Z or A value
0051     inline CELER_FUNCTION int get() const;
0052 
0053   private:
0054     int value_{0};
0055 };
0056 
0057 //---------------------------------------------------------------------------//
0058 // INLINE DEFINITIONS
0059 //---------------------------------------------------------------------------//
0060 /*!
0061  * Get Z or A value.
0062  */
0063 inline CELER_FUNCTION int AtomicNumber::get() const
0064 {
0065     CELER_ENSURE(*this);
0066     return value_;
0067 }
0068 
0069 //---------------------------------------------------------------------------//
0070 // COMPARATORS
0071 //---------------------------------------------------------------------------//
0072 #define CELER_DEFINE_ATOMICNUMBER_CMP(TOKEN)                       \
0073     CELER_CONSTEXPR_FUNCTION bool operator TOKEN(AtomicNumber lhs, \
0074                                                  AtomicNumber rhs) \
0075     {                                                              \
0076         return lhs.unchecked_get() TOKEN rhs.unchecked_get();      \
0077     }
0078 
0079 CELER_DEFINE_ATOMICNUMBER_CMP(==)
0080 CELER_DEFINE_ATOMICNUMBER_CMP(!=)
0081 CELER_DEFINE_ATOMICNUMBER_CMP(<)
0082 CELER_DEFINE_ATOMICNUMBER_CMP(>)
0083 CELER_DEFINE_ATOMICNUMBER_CMP(<=)
0084 CELER_DEFINE_ATOMICNUMBER_CMP(>=)
0085 
0086 #undef CELER_DEFINE_ATOMICNUMBER_CMP
0087 
0088 //---------------------------------------------------------------------------//
0089 }  // namespace celeritas