Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:52:44

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/sys/Version.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <array>
0010 #include <cstdlib>  // IWYU pragma: keep
0011 #include <iosfwd>
0012 #include <string>
0013 #include <string_view>
0014 
0015 // Undefine macros from sys/sysmacros.h
0016 #ifdef major
0017 #    undef major
0018 #endif
0019 #ifdef minor
0020 #    undef minor
0021 #endif
0022 
0023 namespace celeritas
0024 {
0025 //---------------------------------------------------------------------------//
0026 /*!
0027  * Simple version comparison.
0028  *
0029  * This is intended to be constructed with version numbers from \c
0030  * celeritas_cmake_strings.hh and used in unit tests. It can be used in `if
0031  * constexpr` expressions with preprocessor macros. In the constructor
0032  * documentation, x/y/z correspond to major/minor/patch.
0033  *
0034  * \code
0035  * Version(4) == Version(4.0) == Version(4.0.0)
0036  * Version(3.1) > Version(3)
0037  * \endcode
0038  */
0039 class Version
0040 {
0041   public:
0042     //!@{
0043     //! \name Type aliases
0044     using size_type = unsigned int;
0045     using ArrayT = std::array<size_type, 3>;
0046     //!@}
0047 
0048   public:
0049     // Construct from a string "1.2.3"
0050     static Version from_string(std::string_view sv);
0051 
0052     // Construct from an 0xXXYYZZ integer
0053     static inline constexpr Version from_hex_xxyyzz(size_type value);
0054 
0055     // Construct from an 0xXXYZ integer
0056     static inline constexpr Version from_dec_xyz(size_type value);
0057 
0058     // Construct from x.y.z integers
0059     inline constexpr Version(size_type major,
0060                              size_type minor = 0,
0061                              size_type patch = 0);
0062 
0063     //!@{
0064     //! \name Accessors
0065 
0066     //! Get version as an array
0067     constexpr ArrayT const& value() const { return version_; }
0068 
0069     //! Get major version
0070     constexpr size_type major() const { return version_[0]; }
0071 
0072     //! Get minor version
0073     constexpr size_type minor() const { return version_[1]; }
0074 
0075     //! Get patch version
0076     constexpr size_type patch() const { return version_[2]; }
0077 
0078     //!@}
0079 
0080   private:
0081     ArrayT version_;
0082 };
0083 
0084 //---------------------------------------------------------------------------//
0085 // INLINE DEFINITIONS
0086 //---------------------------------------------------------------------------//
0087 /*!
0088  * Construct from an 0xXXYYZZ integer.
0089  *
0090  * This version scheme is used by Celeritas. (The leading 0x prevents
0091  * version `01` from turning the expression into an octal.)
0092  */
0093 constexpr Version Version::from_hex_xxyyzz(size_type value)
0094 {
0095     return {(value >> 16 & 0xffu), (value >> 8 & 0xffu), (value & 0xffu)};
0096 }
0097 
0098 //---------------------------------------------------------------------------//
0099 /*!
0100  * Construct from a decimal with XYZ or XXYZ.
0101  *
0102  * This version scheme is used by Geant4.
0103  */
0104 constexpr Version Version::from_dec_xyz(size_type value)
0105 {
0106     return {(value / 100), (value / 10) % 10, value % 10};
0107 }
0108 
0109 //---------------------------------------------------------------------------//
0110 /*!
0111  * Construct from x.y.z integers.
0112  */
0113 constexpr Version::Version(size_type major, size_type minor, size_type patch)
0114     : version_{{major, minor, patch}}
0115 {
0116 }
0117 
0118 //---------------------------------------------------------------------------//
0119 // FREE FUNCTIONS
0120 //---------------------------------------------------------------------------//
0121 
0122 #define CELER_DEFINE_VERSION_CMP(TOKEN)                                \
0123     inline bool operator TOKEN(Version const& lhs, Version const& rhs) \
0124     {                                                                  \
0125         return lhs.value() TOKEN rhs.value();                          \
0126     }
0127 
0128 CELER_DEFINE_VERSION_CMP(==)
0129 CELER_DEFINE_VERSION_CMP(!=)
0130 CELER_DEFINE_VERSION_CMP(<)
0131 CELER_DEFINE_VERSION_CMP(>)
0132 CELER_DEFINE_VERSION_CMP(<=)
0133 CELER_DEFINE_VERSION_CMP(>=)
0134 
0135 #undef CELER_DEFINE_VERSION_CMP
0136 
0137 // Write to stream
0138 std::ostream& operator<<(std::ostream&, Version const&);
0139 
0140 // Save as string
0141 std::string to_string(Version const&);
0142 
0143 // Get the Celeritas version as an object
0144 Version celer_version();
0145 
0146 //---------------------------------------------------------------------------//
0147 }  // namespace celeritas