Debian Codegraph @

common/version.h

raw
1 /*
2 * version.h
3 * ---------
4 * Purpose: OpenMPT version handling.
5 * Notes : (currently none)
6 * Authors: OpenMPT Devs
7 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
8 */
9
10
11 #pragma once
12
13 #include "openmpt/all/BuildSettings.hpp"
14
15 #include "mptString.h"
16 #include "openmpt/base/FlagSet.hpp"
17
18 #include <stdexcept>
19
20
21 OPENMPT_NAMESPACE_BEGIN
22
23
24 class Version
25 {
26
27 private:
28
29 uint32 m_Version; // e.g. 0x01170208
30
31 public:
32
33 enum class Field
34 {
35 Major,
36 Minor,
37 Patch,
38 Test,
39 };
40
41 public:
42
43 static Version Current() noexcept;
44
45 public:
46
47 MPT_CONSTEXPRINLINE Version() noexcept
48 : m_Version(0)
49 {}
50
51 explicit MPT_CONSTEXPRINLINE Version(uint32 version) noexcept
52 : m_Version(version)
53 {}
54
55 explicit MPT_CONSTEXPRINLINE Version(uint8 v1, uint8 v2, uint8 v3, uint8 v4) noexcept
56 : m_Version((static_cast<uint32>(v1) << 24) | (static_cast<uint32>(v2) << 16) | (static_cast<uint32>(v3) << 8) | (static_cast<uint32>(v4) << 0))
57 {}
58
59 public:
60
61 mpt::ustring ToUString() const; // e.g "1.17.02.08"
62
63 // Returns numerical version value from given version string.
64 static Version Parse(const mpt::ustring &s);
65
66 public:
67
68 explicit MPT_CONSTEXPRINLINE operator bool () const noexcept
69 {
70 return m_Version != 0;
71 }
72 MPT_CONSTEXPRINLINE bool operator ! () const noexcept
73 {
74 return m_Version == 0;
75 }
76
77 MPT_CONSTEXPRINLINE uint32 GetRawVersion() const noexcept
78 {
79 return m_Version;
80 }
81
82 MPT_CONSTEXPRINLINE Version Masked(uint32 mask) const noexcept
83 {
84 return Version(m_Version & mask);
85 }
86
87 MPT_CONSTEXPRINLINE uint8 GetField(Field field) const noexcept
88 {
89 return
90 (field == Field::Major) ? static_cast<uint8>((m_Version >> 24) & 0xffu) :
91 (field == Field::Minor) ? static_cast<uint8>((m_Version >> 16) & 0xffu) :
92 (field == Field::Patch) ? static_cast<uint8>((m_Version >> 8) & 0xffu) :
93 (field == Field::Test ) ? static_cast<uint8>((m_Version >> 0) & 0xffu) :
94 0u;
95 }
96
97 // Return a version without build number (the last number in the version).
98 // The current versioning scheme uses this number only for test builds, and it should be 00 for official builds,
99 // So sometimes it might be wanted to do comparisons without the build number.
100 Version WithoutTestNumber() const noexcept;
101
102 Version WithoutPatchOrTestNumbers() const noexcept;
103
104 public:
105
106 // Return a OpenMPT version string suitable for file format tags
107 mpt::ustring GetOpenMPTVersionString() const; // e.g. "OpenMPT 1.17.02.08"
108
109 // Returns true if a given version number is from a test build, false if it's a release build.
110 bool IsTestVersion() const noexcept;
111
112 public:
113
114 struct LiteralParser
115 {
116
117 public:
118
119 // Work-around for GCC 5 which complains about instanciating non-literal type inside a constexpr function when using mpt::constexpr_throw(std::runtime_error("")).
120 struct ParseException {};
121
122 private:
123
124 static MPT_CONSTEXPRINLINE uint8 NibbleFromChar(char x)
125 {
126 return
127 ('0' <= x && x <= '9') ? static_cast<uint8>(x - '0' + 0) :
128 ('a' <= x && x <= 'z') ? static_cast<uint8>(x - 'a' + 10) :
129 ('A' <= x && x <= 'Z') ? static_cast<uint8>(x - 'A' + 10) :
130 mpt::constexpr_throw<uint8>(std::domain_error(""));
131 }
132
133 public:
134
135 static MPT_CONSTEXPRINLINE Version Parse(const char * str, std::size_t len)
136 {
137 // 0123456789
138 // 1.23.45.67
139 uint8 v[4] = {0, 0, 0, 0};
140 std::size_t field = 0;
141 std::size_t fieldlen = 0;
142 for(std::size_t i = 0; i < len; ++i)
143 {
144 char c = str[i];
145 if(c == '.')
146 {
147 if(field >= 3)
148 {
149 mpt::constexpr_throw(ParseException());
150 }
151 if(fieldlen == 0)
152 {
153 mpt::constexpr_throw(ParseException());
154 }
155 field++;
156 fieldlen = 0;
157 } else if(('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
158 {
159 fieldlen++;
160 if(fieldlen > 2)
161 {
162 mpt::constexpr_throw(ParseException());
163 }
164 v[field] <<= 4;
165 v[field] |= NibbleFromChar(c);
166 } else
167 {
168 mpt::constexpr_throw(ParseException());
169 }
170 }
171 if(fieldlen == 0)
172 {
173 mpt::constexpr_throw(ParseException());
174 }
175 return Version(v[0], v[1], v[2], v[3]);
176 }
177
178 };
179
180 };
181
182 MPT_CONSTEXPRINLINE bool operator == (const Version &a, const Version &b) noexcept
183 {
184 return a.GetRawVersion() == b.GetRawVersion();
185 }
186 MPT_CONSTEXPRINLINE bool operator != (const Version &a, const Version &b) noexcept
187 {
188 return a.GetRawVersion() != b.GetRawVersion();
189 }
190 MPT_CONSTEXPRINLINE bool operator <= (const Version &a, const Version &b) noexcept
191 {
192 return a.GetRawVersion() <= b.GetRawVersion();
193 }
194 MPT_CONSTEXPRINLINE bool operator >= (const Version &a, const Version &b) noexcept
195 {
196 return a.GetRawVersion() >= b.GetRawVersion();
197 }
198 MPT_CONSTEXPRINLINE bool operator < (const Version &a, const Version &b) noexcept
199 {
200 return a.GetRawVersion() < b.GetRawVersion();
201 }
202 MPT_CONSTEXPRINLINE bool operator > (const Version &a, const Version &b) noexcept
203 {
204 return a.GetRawVersion() > b.GetRawVersion();
205 }
206
207
208 MPT_CONSTEVAL Version operator ""_LiteralVersionImpl (const char * str, std::size_t len)
209 {
210 return Version::LiteralParser::Parse(str, len);
211 }
212
213 // Create Version object from version string and check syntax, all at compile time.
214 // cppcheck false-positive
215 // cppcheck-suppress preprocessorErrorDirective
216 #define MPT_V(strver) MPT_FORCE_CONSTEVAL_VALUE(Version{( strver ## _LiteralVersionImpl ).GetRawVersion()})
217
218
219
220 class SourceInfo
221 {
222 private:
223 mpt::ustring m_Url; // svn repository url (or empty string)
224 int m_Revision; // svn revision (or 0)
225 bool m_IsDirty; // svn working copy is dirty (or false)
226 bool m_HasMixedRevisions; // svn working copy has mixed revisions (or false)
227 bool m_IsPackage; // source code originates from a packaged version of the source code
228 mpt::ustring m_Date; // svn date (or empty string)
229 private:
230 SourceInfo();
231 public:
232 static SourceInfo Current();
233 public:
234 const mpt::ustring & Url() const { return m_Url; }
235 int Revision() const { return m_Revision; }
236 bool IsDirty() const { return m_IsDirty; }
237 bool HasMixedRevisions() const { return m_HasMixedRevisions; }
238 bool IsPackage() const { return m_IsPackage; }
239 const mpt::ustring & Date() const { return m_Date; }
240 public:
241 mpt::ustring GetUrlWithRevision() const; // i.e. "https://source.openmpt.org/svn/openmpt/trunk/OpenMPT@1234" or empty string
242 mpt::ustring GetStateString() const; // i.e. "+dirty" or "clean"
243 };
244
245
246
247 struct VersionWithRevision
248 {
249 Version version;
250 uint64 revision;
251 static VersionWithRevision Current();
252 static VersionWithRevision Parse(const mpt::ustring &s);
253 mpt::ustring ToUString() const;
254 constexpr bool HasRevision() const noexcept
255 {
256 return revision != 0;
257 }
258 constexpr bool IsEqualTo(VersionWithRevision other) const noexcept
259 {
260 return version == other.version && revision == other.revision;
261 }
262 constexpr bool IsEquivalentTo(VersionWithRevision other) const noexcept
263 {
264 if(version == other.version && revision == other.revision)
265 {
266 return true;
267 }
268 if(HasRevision() && other.HasRevision())
269 {
270 return false;
271 }
272 return version == other.version;
273 }
274 constexpr bool IsNewerThan(VersionWithRevision other) const noexcept
275 {
276 if(version < other.version)
277 {
278 return false;
279 }
280 if(version > other.version)
281 {
282 return true;
283 }
284 if(!HasRevision() && !other.HasRevision())
285 {
286 return false;
287 }
288 if(HasRevision() && other.HasRevision())
289 {
290 if(revision < other.revision)
291 {
292 return false;
293 }
294 if(revision > other.revision)
295 {
296 return true;
297 }
298 return false;
299 }
300 return false;
301 }
302 constexpr bool IsOlderThan(VersionWithRevision other) const noexcept
303 {
304 if(version < other.version)
305 {
306 return true;
307 }
308 if(version > other.version)
309 {
310 return false;
311 }
312 if(!HasRevision() && !other.HasRevision())
313 {
314 return false;
315 }
316 if(HasRevision() && other.HasRevision())
317 {
318 if(revision < other.revision)
319 {
320 return true;
321 }
322 if(revision > other.revision)
323 {
324 return false;
325 }
326 return false;
327 }
328 return false;
329 }
330 };
331
332
333
334 namespace Build
335 {
336
337 // Returns true if all conditions for an official release build are met
338 bool IsReleasedBuild();
339
340 // Return true if this is a debug build with no optimizations
341 bool IsDebugBuild();
342
343 // Return a string decribing the time of the build process (if built from a svn working copy and tsvn was available during build, otherwise it returns the time version.cpp was last rebuild which could be unreliable as it does not get rebuild every time without tsvn)
344 mpt::ustring GetBuildDateString();
345
346 // Return a string decribing some of the build features
347 mpt::ustring GetBuildFeaturesString(); // e.g. " NO_VST NO_DSOUND"
348
349 // Return a string describing the compiler version used for building.
350 mpt::ustring GetBuildCompilerString(); // e.g. "Microsoft Compiler 15.00.20706.01"
351
352 enum Strings
353 {
354 StringsNone = 0,
355 StringVersion = 1<<0, // "1.23.35.45"
356 StringRevision = 1<<2, // "-r1234+"
357 StringSourceInfo = 1<<5, // "https://source.openmpt.org/svn/openmpt/trunk/OpenMPT@1234 (2016-01-02) +dirty"
358 StringBuildFlags = 1<<6, // "TEST DEBUG"
359 StringBuildFeatures = 1<<7, // "NO_VST NO_DSOUND"
360 };
361 MPT_DECLARE_ENUM(Strings)
362
363 // Returns a versions string with the fields selected via @strings.
364 mpt::ustring GetVersionString(FlagSet<Build::Strings> strings);
365
366 // Returns a pure version string
367 mpt::ustring GetVersionStringPure(); // e.g. "1.17.02.08-r1234+"
368
369 // Returns a simple version string
370 mpt::ustring GetVersionStringSimple(); // e.g. "1.17.02.08-r1234+ TEST"
371
372 // Returns Version::CurrentAsString() if the build is a clean release build straight from the repository or an extended string otherwise (if built from a svn working copy and tsvn was available during build)
373 mpt::ustring GetVersionStringExtended(); // e.g. "1.17.02.08-r1234+ DEBUG"
374
375 enum class Url
376 {
377 Website,
378 Download,
379 Forum,
380 Bugtracker,
381 Updates,
382 TopPicks,
383 };
384 // Returns a URL for the respective key.
385 mpt::ustring GetURL(Build::Url key);
386
387 // Returns a multi-line string containing the full credits for the code base
388 mpt::ustring GetFullCreditsString();
389
390 // Returns the OpenMPT license text
391 mpt::ustring GetLicenseString();
392
393 } //namespace Build
394
395
396
397 OPENMPT_NAMESPACE_END