yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
4c76b2759
master
1// slang-semantic-version.h 2#ifndef SLANG_SEMANTIC_VERSION_H 3#define SLANG_SEMANTIC_VERSION_H 4 5#include "../core/slang-basic.h" 6#include "../core/slang-hash.h" 7 8namespace Slang 9{ 10 11struct SemanticVersion 12{ 13typedef SemanticVersion ThisType ; 14 15typedef UInt64 RawValue ; 16 17SemanticVersion () 18: m_major ( 0 ), m_minor( 0 ), m_patch( 0 ) 19{ 20} 21 22SemanticVersion( int inMajor , int inMinor = 0 , int inPatch = 0 ) 23: m_major ( uint16_t ( inMajor )), m_minor( uint16_t ( inMinor )), m_patch( uint32_t ( inPatch )) 24{ 25} 26 27void reset() 28{ 29m_major = 0 ; 30m_minor = 0 ; 31m_patch = 0 ; 32} 33 34/// All zeros means nothing is set 35bool isSet () const { return m_major || m_minor || m_patch ; } 36 37RawValue getRawValue () const 38{ 39return ( RawValue ( m_major ) << 48 ) | ( RawValue ( m_minor ) << 32 ) | m_patch ; 40} 41void setRawValue ( RawValue v ) 42{ 43set ( int ( v >> 48 ), int (( v >> 32 ) & 0xffff ), int ( v & 0xffffffff )); 44} 45 46static SemanticVersion fromRaw ( RawValue rawValue ) 47{ 48SemanticVersion result ; 49result . setRawValue ( rawValue ); 50return result ; 51} 52 53void set ( int major , int minor , int patch = 0 ) 54{ 55SLANG_ASSERT ( major >= 0 && minor >= 0 && patch >= 0 ); 56 57m_major = uint16_t ( major ); 58m_minor = uint16_t ( minor ); 59m_patch = uint32_t ( patch ); 60} 61 62/// Get hash value 63HashCode getHashCode () const { return Slang :: getHashCode ( getRawValue ()); } 64 65static SlangResult parse ( const UnownedStringSlice & value , SemanticVersion & outVersion ); 66static SlangResult parse ( 67const UnownedStringSlice & value , 68char separatorChar , 69SemanticVersion & outVersion ); 70 71static ThisType getEarliest ( const ThisType * versions , Count count ); 72static ThisType getLatest ( const ThisType * versions , Count count ); 73 74/// Determine if this version is backwards-compatible with `otherVersion`. 75/// 76/// Examples of when to apply this: 77/// 78/// * Code for this version is asked to load data produced by `otherVersion`. 79/// 80/// * Code for this version is asked if it provides the API of `otherVersion`. 81/// 82/// This uses the rules of semantic versioning, so it should only 83/// be applied to versions that obey those rules. 84/// 85bool isBackwardsCompatibleWith ( const ThisType & otherVersion ) const ; 86 87void append ( StringBuilder & buf ) const ; 88 89bool operator > ( const ThisType & rhs ) const { return getRawValue () > rhs . getRawValue (); } 90bool operator >=( const ThisType & rhs ) const { return getRawValue () >= rhs . getRawValue (); } 91 92bool operator < ( const ThisType & rhs ) const { return getRawValue () < rhs . getRawValue (); } 93bool operator <=( const ThisType & rhs ) const { return getRawValue () <= rhs . getRawValue (); } 94 95bool operator == ( const ThisType & rhs ) const { return getRawValue () == rhs . getRawValue (); } 96bool operator != ( const ThisType & rhs ) const { return getRawValue () != rhs . getRawValue (); } 97 98uint16_t m_major ; 99uint16_t m_minor ; 100uint32_t m_patch ; ///< Patch number. Can actually be quite large for some code bases. 101}; 102 103/* Adds to the semantic versioning information for an incomplete version that can be matched */ 104struct MatchSemanticVersion 105{ 106typedef MatchSemanticVersion ThisType ; 107 108enum class Kind 109{ 110Unknown, ///< Not known 111Past, ///< Some unknown past version 112Future, ///< Some future unknown version 113Major, ///< Major version is defined (minor is in effect undefined) 114MajorMinor, ///< Major and minor version are defined 115MajorMinorPatch, ///< All elements of semantic version are defined 116}; 117 118/// True if has a complete version 119bool hasCompleteVersion() const { return m_kind == Kind :: MajorMinorPatch ; } 120/// True if has some version information 121bool hasVersion () const { return Index ( m_kind ) >= Index ( Kind :: Major ); } 122 123void set ( Index major ) 124{ 125m_kind = Kind :: Major ; 126m_version = SemanticVersion ( int ( major ), 0 , 0 ); 127} 128void set ( Index major , Index minor ) 129{ 130m_kind = Kind :: MajorMinor ; 131m_version = SemanticVersion ( int ( major ), int ( minor ), 0 ); 132} 133void set ( Index major , Index minor , Index patch ) 134{ 135m_kind = Kind :: MajorMinorPatch ; 136m_version = SemanticVersion ( int ( major ), int ( minor ), int ( patch )); 137} 138 139void append ( StringBuilder & buf ) const ; 140 141static MatchSemanticVersion makeFuture () 142{ 143MatchSemanticVersion version ; 144version .m_kind = Kind :: Future ; 145return version ; 146} 147 148/// Finds the 'best' version based on the versions passed. 149/// Doesn't follow strict semantic rules as will attempt to return the closest 'any' in past or 150/// future If none can be found, returns an empty semantic version 151static SemanticVersion findAnyBest ( 152const SemanticVersion * versions , 153Count count , 154const ThisType & matchVersion ); 155 156MatchSemanticVersion () 157: m_kind ( Kind :: Unknown ) 158{ 159} 160MatchSemanticVersion ( Kind kind , const SemanticVersion & version ) 161: m_kind ( kind ), m_version ( version ) 162{ 163} 164 165Kind m_kind ; 166SemanticVersion m_version ; 167}; 168 169} // namespace Slang 170#endif