Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-04 09:58:33

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_view>
0013 
0014 // Undefine macros from <sys/sysmacros.h> if included; possibly included
0015 // through <sys/types.h> for historical reasons
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  * Versions evaluate to \c false with the default constructor or when
0035  * constructed from a Celeritas package that is disabled.
0036  *
0037  * \par Constructors
0038  *
0039  * With Geant4 enabled, these should all produce the same Version object:
0040  * \code
0041  * Version::from_package("Geant4"); // [1]
0042  * Version::from_hex_xxyyzz(CELERITAS_GEANT4_VERSION); // [1, 2]
0043  * Version::from_dec_xyz(G4VERSION_NUMBER); // [2, 3]
0044  * \endcode
0045  *
0046  * but note that:
0047  * - [1] uses the built-in Celeritas CMake configuration (requires including
0048  *   \c corecel/Config.hh)
0049  * - [2] is constexpr
0050  * - [3] uses an external header file (must link against a Geant4 cmake target
0051  *   to access the include directories)
0052  *
0053  * \par Example
0054  *
0055  * \code
0056  * assert(Version(4) == Version(4.0) == Version(4.0.0));
0057  * assert(Version(3.1) > Version(3));
0058  * \endcode
0059  */
0060 class Version
0061 {
0062   public:
0063     //!@{
0064     //! \name Type aliases
0065     using size_type = unsigned int;
0066     using ArrayT = std::array<size_type, 3>;
0067     //!@}
0068 
0069   public:
0070     // Construct from a string "1.2.3"
0071     static Version from_string(std::string_view sv);
0072 
0073     // Construct from a Celeritas-configured package name "vecgeom"/"VecGeom"
0074     // False/zero if undefined, throw if unknown package
0075     static Version from_package(std::string_view p);
0076 
0077     // Construct from an 0xXXYYZZ integer
0078     static inline constexpr Version from_hex_xxyyzz(size_type value);
0079 
0080     // Construct from an 0xXXYZ integer
0081     static inline constexpr Version from_dec_xyz(size_type value);
0082 
0083     // Construct from x.y.z integers
0084     inline constexpr Version(
0085         size_type major, size_type minor = 0, size_type patch = 0);
0086 
0087     //! Default (false) version
0088     constexpr Version() : version_{0, 0, 0} {}
0089 
0090     //!@{
0091     //! \name Accessors
0092 
0093     //! Get version as an array
0094     constexpr ArrayT const& value() const { return version_; }
0095 
0096     //! Get major version
0097     constexpr size_type major() const { return version_[0]; }
0098 
0099     //! Get minor version
0100     constexpr size_type minor() const { return version_[1]; }
0101 
0102     //! Get patch version
0103     constexpr size_type patch() const { return version_[2]; }
0104 
0105     //! Whether the version is defined
0106     constexpr explicit operator bool() const
0107     {
0108         return version_[0] || version_[1] || version_[2];
0109     }
0110     //!@}
0111 
0112     //// INLINE FRIENDS ////
0113 
0114 #define CELER_DEFINE_VERSION_CMP(TOKEN) \
0115     inline friend bool operator TOKEN(Version const& lhs, Version const& rhs) \
0116     { \
0117         return lhs.value() TOKEN rhs.value(); \
0118     }
0119 
0120     CELER_DEFINE_VERSION_CMP(==)
0121     CELER_DEFINE_VERSION_CMP(!=)
0122     CELER_DEFINE_VERSION_CMP(<)
0123     CELER_DEFINE_VERSION_CMP(>)
0124     CELER_DEFINE_VERSION_CMP(<=)
0125     CELER_DEFINE_VERSION_CMP(>=)
0126 
0127 #undef CELER_DEFINE_VERSION_CMP
0128 
0129     // Write to stream
0130     friend std::ostream& operator<<(std::ostream&, Version const&);
0131 
0132   private:
0133     ArrayT version_;
0134 };
0135 
0136 //---------------------------------------------------------------------------//
0137 // INLINE DEFINITIONS
0138 //---------------------------------------------------------------------------//
0139 /*!
0140  * Construct from an 0xXXYYZZ integer.
0141  *
0142  * This version scheme is used by Celeritas. (The leading 0x prevents
0143  * version `01` from turning the expression into an octal.)
0144  */
0145 constexpr Version Version::from_hex_xxyyzz(size_type value)
0146 {
0147     return {(value >> 16 & 0xffu), (value >> 8 & 0xffu), (value & 0xffu)};
0148 }
0149 
0150 //---------------------------------------------------------------------------//
0151 /*!
0152  * Construct from a decimal with XYZ or XXYZ.
0153  *
0154  * This version scheme is used by Geant4.
0155  */
0156 constexpr Version Version::from_dec_xyz(size_type value)
0157 {
0158     return {(value / 100), (value / 10) % 10, value % 10};
0159 }
0160 
0161 //---------------------------------------------------------------------------//
0162 /*!
0163  * Construct from x.y.z integers.
0164  */
0165 constexpr Version::Version(size_type major, size_type minor, size_type patch)
0166     : version_{{major, minor, patch}}
0167 {
0168 }
0169 
0170 // Get the Celeritas version as an object
0171 Version celer_version();
0172 
0173 //---------------------------------------------------------------------------//
0174 }  // namespace celeritas