aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/common/version.h
blob: e814de5870dea3c53cca9fbbedb212d9876a9f5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * version.h
 * ---------
 * Purpose: OpenMPT version handling.
 * Notes  : (currently none)
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */


#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; // e.g. 0x01170208

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; // e.g "1.17.02.08"

	// Returns numerical version value from given version string.
	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;
	}

	// Return a version without build number (the last number in the version).
	// The current versioning scheme uses this number only for test builds, and it should be 00 for official builds,
	// So sometimes it might be wanted to do comparisons without the build number.
	Version WithoutTestNumber() const noexcept;

	Version WithoutPatchOrTestNumbers() const noexcept;

public:

	// Return a OpenMPT version string suitable for file format tags 
	mpt::ustring GetOpenMPTVersionString() const; // e.g. "OpenMPT 1.17.02.08"

	// Returns true if a given version number is from a test build, false if it's a release build.
	bool IsTestVersion() const noexcept;

public:

	struct LiteralParser
	{
	
	public:

		// Work-around for GCC 5 which complains about instanciating non-literal type inside a constexpr function when using mpt::constexpr_throw(std::runtime_error("")).
		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)
		{
			// 0123456789
			// 1.23.45.67
			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);
}

// Create Version object from version string and check syntax, all at compile time.
// cppcheck false-positive
// cppcheck-suppress preprocessorErrorDirective
#define MPT_V(strver) MPT_FORCE_CONSTEXPR(Version{( strver ## _LiteralVersionImpl ).GetRawVersion()})



class SourceInfo
{
private:
	mpt::ustring m_Url; // svn repository url (or empty string)
	int m_Revision; // svn revision (or 0)
	bool m_IsDirty; // svn working copy is dirty (or false)
	bool m_HasMixedRevisions; // svn working copy has mixed revisions (or false)
	bool m_IsPackage; // source code originates from a packaged version of the source code
	mpt::ustring m_Date; // svn date (or empty string)
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; // i.e. "https://source.openmpt.org/svn/openmpt/trunk/OpenMPT@1234" or empty string
	mpt::ustring GetStateString() const; // i.e. "+dirty" or "clean"
};



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
{

	// Returns true if all conditions for an official release build are met
	bool IsReleasedBuild();

	// Return true if this is a debug build with no optimizations
	bool IsDebugBuild();

	// 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)
	mpt::ustring GetBuildDateString();

	// Return a string decribing some of the build features
	mpt::ustring GetBuildFeaturesString(); // e.g. " NO_VST NO_DSOUND"

	// Return a string describing the compiler version used for building.
	mpt::ustring GetBuildCompilerString(); // e.g. "Microsoft Compiler 15.00.20706.01"

	enum Strings
	{
		StringsNone         = 0,
		StringVersion       = 1<<0, // "1.23.35.45"
		StringRevision      = 1<<2, // "-r1234+"
		StringSourceInfo    = 1<<5, // "https://source.openmpt.org/svn/openmpt/trunk/OpenMPT@1234 (2016-01-02) +dirty"
		StringBuildFlags    = 1<<6, // "TEST DEBUG"
		StringBuildFeatures = 1<<7, // "NO_VST NO_DSOUND"
	};
	MPT_DECLARE_ENUM(Strings)

	// Returns a versions string with the fields selected via @strings.
	mpt::ustring GetVersionString(FlagSet<Build::Strings> strings);

	// Returns a pure version string
	mpt::ustring GetVersionStringPure(); // e.g. "1.17.02.08-r1234+"

	// Returns a simple version string
	mpt::ustring GetVersionStringSimple(); // e.g. "1.17.02.08-r1234+ TEST"

	// 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)
	mpt::ustring GetVersionStringExtended(); // e.g. "1.17.02.08-r1234+ DEBUG"

	enum class Url
	{
		Website,
		Download,
		Forum,
		Bugtracker,
		Updates,
		TopPicks,
	};
	// Returns a URL for the respective key.
	mpt::ustring GetURL(Build::Url key);

	// Returns a multi-line string containing the full credits for the code base
	mpt::ustring GetFullCreditsString();

	// Returns the OpenMPT license text
	mpt::ustring GetLicenseString();

} //namespace Build



OPENMPT_NAMESPACE_END