123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- #pragma once
- #include "openmpt/all/BuildSettings.hpp"
- #include "mptString.h"
- #include "openmpt/base/FlagSet.hpp"
- #include <stdexcept>
- OPENMPT_NAMESPACE_BEGIN
- class Version
- {
- private:
- uint32 m_Version;
- public:
- enum class Field
- {
- Major,
- Minor,
- Patch,
- Test,
- };
- public:
- static Version Current() noexcept;
- public:
- MPT_CONSTEXPRINLINE Version() noexcept
- : m_Version(0)
- {}
- explicit MPT_CONSTEXPRINLINE Version(uint32 version) noexcept
- : m_Version(version)
- {}
- explicit MPT_CONSTEXPRINLINE Version(uint8 v1, uint8 v2, uint8 v3, uint8 v4) noexcept
- : m_Version((static_cast<uint32>(v1) << 24) | (static_cast<uint32>(v2) << 16) | (static_cast<uint32>(v3) << 8) | (static_cast<uint32>(v4) << 0))
- {}
- public:
- mpt::ustring ToUString() const;
-
- static Version Parse(const mpt::ustring &s);
- public:
- explicit MPT_CONSTEXPRINLINE operator bool () const noexcept
- {
- return m_Version != 0;
- }
- MPT_CONSTEXPRINLINE bool operator ! () const noexcept
- {
- return m_Version == 0;
- }
- MPT_CONSTEXPRINLINE uint32 GetRawVersion() const noexcept
- {
- return m_Version;
- }
- MPT_FORCEINLINE Version Masked(uint32 mask) const noexcept
- {
- return Version(m_Version & mask);
- }
- MPT_CONSTEXPRINLINE uint8 GetField(Field field) const noexcept
- {
- return
- (field == Field::Major) ? static_cast<uint8>((m_Version >> 24) & 0xffu) :
- (field == Field::Minor) ? static_cast<uint8>((m_Version >> 16) & 0xffu) :
- (field == Field::Patch) ? static_cast<uint8>((m_Version >> 8) & 0xffu) :
- (field == Field::Test ) ? static_cast<uint8>((m_Version >> 0) & 0xffu) :
- 0u;
- }
-
-
-
- Version WithoutTestNumber() const noexcept;
- Version WithoutPatchOrTestNumbers() const noexcept;
- public:
-
- mpt::ustring GetOpenMPTVersionString() const;
-
- bool IsTestVersion() const noexcept;
- public:
- struct LiteralParser
- {
-
- public:
-
- struct ParseException {};
- private:
- static MPT_CONSTEXPRINLINE uint8 NibbleFromChar(char x)
- {
- return
- ('0' <= x && x <= '9') ? static_cast<uint8>(x - '0' + 0) :
- ('a' <= x && x <= 'z') ? static_cast<uint8>(x - 'a' + 10) :
- ('A' <= x && x <= 'Z') ? static_cast<uint8>(x - 'A' + 10) :
- mpt::constexpr_throw<uint8>(std::domain_error(""));
- }
- public:
- static MPT_CONSTEXPRINLINE Version Parse(const char * str, std::size_t len)
- {
-
-
- uint8 v[4] = {0, 0, 0, 0};
- std::size_t field = 0;
- std::size_t fieldlen = 0;
- for(std::size_t i = 0; i < len; ++i)
- {
- char c = str[i];
- if(c == '.')
- {
- if(field >= 3)
- {
- mpt::constexpr_throw(ParseException());
- }
- if(fieldlen == 0)
- {
- mpt::constexpr_throw(ParseException());
- }
- field++;
- fieldlen = 0;
- } else if(('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
- {
- fieldlen++;
- if(fieldlen > 2)
- {
- mpt::constexpr_throw(ParseException());
- }
- v[field] <<= 4;
- v[field] |= NibbleFromChar(c);
- } else
- {
- mpt::constexpr_throw(ParseException());
- }
- }
- if(fieldlen == 0)
- {
- mpt::constexpr_throw(ParseException());
- }
- return Version(v[0], v[1], v[2], v[3]);
- }
- };
- };
- MPT_CONSTEXPRINLINE bool operator == (const Version &a, const Version &b) noexcept
- {
- return a.GetRawVersion() == b.GetRawVersion();
- }
- MPT_CONSTEXPRINLINE bool operator != (const Version &a, const Version &b) noexcept
- {
- return a.GetRawVersion() != b.GetRawVersion();
- }
- MPT_CONSTEXPRINLINE bool operator <= (const Version &a, const Version &b) noexcept
- {
- return a.GetRawVersion() <= b.GetRawVersion();
- }
- MPT_CONSTEXPRINLINE bool operator >= (const Version &a, const Version &b) noexcept
- {
- return a.GetRawVersion() >= b.GetRawVersion();
- }
- MPT_CONSTEXPRINLINE bool operator < (const Version &a, const Version &b) noexcept
- {
- return a.GetRawVersion() < b.GetRawVersion();
- }
- MPT_CONSTEXPRINLINE bool operator > (const Version &a, const Version &b) noexcept
- {
- return a.GetRawVersion() > b.GetRawVersion();
- }
- MPT_CONSTEXPRINLINE Version operator "" _LiteralVersionImpl (const char * str, std::size_t len)
- {
- return Version::LiteralParser::Parse(str, len);
- }
- #define MPT_V(strver) MPT_FORCE_CONSTEXPR(Version{( strver ## _LiteralVersionImpl ).GetRawVersion()})
- class SourceInfo
- {
- private:
- mpt::ustring m_Url;
- int m_Revision;
- bool m_IsDirty;
- bool m_HasMixedRevisions;
- bool m_IsPackage;
- mpt::ustring m_Date;
- private:
- SourceInfo();
- public:
- static SourceInfo Current();
- public:
- const mpt::ustring & Url() const { return m_Url; }
- int Revision() const { return m_Revision; }
- bool IsDirty() const { return m_IsDirty; }
- bool HasMixedRevisions() const { return m_HasMixedRevisions; }
- bool IsPackage() const { return m_IsPackage; }
- const mpt::ustring & Date() const { return m_Date; }
- public:
- mpt::ustring GetUrlWithRevision() const;
- mpt::ustring GetStateString() const;
- };
- struct VersionWithRevision
- {
- Version version;
- uint64 revision;
- static VersionWithRevision Current();
- static VersionWithRevision Parse(const mpt::ustring &s);
- mpt::ustring ToUString() const;
- constexpr bool HasRevision() const noexcept
- {
- return revision != 0;
- }
- constexpr bool IsEqualTo(VersionWithRevision other) const noexcept
- {
- return version == other.version && revision == other.revision;
- }
- constexpr bool IsEquivalentTo(VersionWithRevision other) const noexcept
- {
- if(version == other.version && revision == other.revision)
- {
- return true;
- }
- if(HasRevision() && other.HasRevision())
- {
- return false;
- }
- return version == other.version;
- }
- constexpr bool IsNewerThan(VersionWithRevision other) const noexcept
- {
- if(version < other.version)
- {
- return false;
- }
- if(version > other.version)
- {
- return true;
- }
- if(!HasRevision() && !other.HasRevision())
- {
- return false;
- }
- if(HasRevision() && other.HasRevision())
- {
- if(revision < other.revision)
- {
- return false;
- }
- if(revision > other.revision)
- {
- return true;
- }
- return false;
- }
- return false;
- }
- constexpr bool IsOlderThan(VersionWithRevision other) const noexcept
- {
- if(version < other.version)
- {
- return true;
- }
- if(version > other.version)
- {
- return false;
- }
- if(!HasRevision() && !other.HasRevision())
- {
- return false;
- }
- if(HasRevision() && other.HasRevision())
- {
- if(revision < other.revision)
- {
- return true;
- }
- if(revision > other.revision)
- {
- return false;
- }
- return false;
- }
- return false;
- }
- };
- namespace Build
- {
-
- bool IsReleasedBuild();
-
- bool IsDebugBuild();
-
- mpt::ustring GetBuildDateString();
-
- mpt::ustring GetBuildFeaturesString();
-
- mpt::ustring GetBuildCompilerString();
- enum Strings
- {
- StringsNone = 0,
- StringVersion = 1<<0,
- StringRevision = 1<<2,
- StringSourceInfo = 1<<5,
- StringBuildFlags = 1<<6,
- StringBuildFeatures = 1<<7,
- };
- MPT_DECLARE_ENUM(Strings)
-
- mpt::ustring GetVersionString(FlagSet<Build::Strings> strings);
-
- mpt::ustring GetVersionStringPure();
-
- mpt::ustring GetVersionStringSimple();
-
- mpt::ustring GetVersionStringExtended();
- enum class Url
- {
- Website,
- Download,
- Forum,
- Bugtracker,
- Updates,
- TopPicks,
- };
-
- mpt::ustring GetURL(Build::Url key);
-
- mpt::ustring GetFullCreditsString();
-
- mpt::ustring GetLicenseString();
- }
- OPENMPT_NAMESPACE_END
|