yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Theresa FoleyCleanups related to RIFF support (#7041)4c76b2759

master
5.1 KiB170 linesraw
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{
13    typedef SemanticVersion ThisType;
14
15    typedef UInt64 RawValue;
16
17    SemanticVersion()
18        : m_major(0), m_minor(0), m_patch(0)
19    {
20    }
21
22    SemanticVersion(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
27    void reset()
28    {
29        m_major = 0;
30        m_minor = 0;
31        m_patch = 0;
32    }
33
34    /// All zeros means nothing is set
35    bool isSet() const { return m_major || m_minor || m_patch; }
36
37    RawValue getRawValue() const
38    {
39        return (RawValue(m_major) << 48) | (RawValue(m_minor) << 32) | m_patch;
40    }
41    void setRawValue(RawValue v)
42    {
43        set(int(v >> 48), int((v >> 32) & 0xffff), int(v & 0xffffffff));
44    }
45
46    static SemanticVersion fromRaw(RawValue rawValue)
47    {
48        SemanticVersion result;
49        result.setRawValue(rawValue);
50        return result;
51    }
52
53    void set(int major, int minor, int patch = 0)
54    {
55        SLANG_ASSERT(major >= 0 && minor >= 0 && patch >= 0);
56
57        m_major = uint16_t(major);
58        m_minor = uint16_t(minor);
59        m_patch = uint32_t(patch);
60    }
61
62    /// Get hash value
63    HashCode getHashCode() const { return Slang::getHashCode(getRawValue()); }
64
65    static SlangResult parse(const UnownedStringSlice& value, SemanticVersion& outVersion);
66    static SlangResult parse(
67        const UnownedStringSlice& value,
68        char separatorChar,
69        SemanticVersion& outVersion);
70
71    static ThisType getEarliest(const ThisType* versions, Count count);
72    static 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    ///
85    bool isBackwardsCompatibleWith(const ThisType& otherVersion) const;
86
87    void append(StringBuilder& buf) const;
88
89    bool operator>(const ThisType& rhs) const { return getRawValue() > rhs.getRawValue(); }
90    bool operator>=(const ThisType& rhs) const { return getRawValue() >= rhs.getRawValue(); }
91
92    bool operator<(const ThisType& rhs) const { return getRawValue() < rhs.getRawValue(); }
93    bool operator<=(const ThisType& rhs) const { return getRawValue() <= rhs.getRawValue(); }
94
95    bool operator==(const ThisType& rhs) const { return getRawValue() == rhs.getRawValue(); }
96    bool operator!=(const ThisType& rhs) const { return getRawValue() != rhs.getRawValue(); }
97
98    uint16_t m_major;
99    uint16_t m_minor;
100    uint32_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{
106    typedef MatchSemanticVersion ThisType;
107
108    enum class Kind
109    {
110        Unknown,         ///< Not known
111        Past,            ///< Some unknown past version
112        Future,          ///< Some future unknown version
113        Major,           ///< Major version is defined (minor is in effect undefined)
114        MajorMinor,      ///< Major and minor version are defined
115        MajorMinorPatch, ///< All elements of semantic version are defined
116    };
117
118    /// True if has a complete version
119    bool hasCompleteVersion() const { return m_kind == Kind::MajorMinorPatch; }
120    /// True if has some version information
121    bool hasVersion() const { return Index(m_kind) >= Index(Kind::Major); }
122
123    void set(Index major)
124    {
125        m_kind = Kind::Major;
126        m_version = SemanticVersion(int(major), 0, 0);
127    }
128    void set(Index major, Index minor)
129    {
130        m_kind = Kind::MajorMinor;
131        m_version = SemanticVersion(int(major), int(minor), 0);
132    }
133    void set(Index major, Index minor, Index patch)
134    {
135        m_kind = Kind::MajorMinorPatch;
136        m_version = SemanticVersion(int(major), int(minor), int(patch));
137    }
138
139    void append(StringBuilder& buf) const;
140
141    static MatchSemanticVersion makeFuture()
142    {
143        MatchSemanticVersion version;
144        version.m_kind = Kind::Future;
145        return 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
151    static SemanticVersion findAnyBest(
152        const SemanticVersion* versions,
153        Count count,
154        const ThisType& matchVersion);
155
156    MatchSemanticVersion()
157        : m_kind(Kind::Unknown)
158    {
159    }
160    MatchSemanticVersion(Kind kind, const SemanticVersion& version)
161        : m_kind(kind), m_version(version)
162    {
163    }
164
165    Kind m_kind;
166    SemanticVersion m_version;
167};
168
169} // namespace Slang
170#endif