Back to home page

EIC code displayed by LXR

 
 

    


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

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