2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Globals.h
|
|
|
|
|
|
|
|
// This file gets included from every module in the project, so that global symbols may be introduced easily
|
|
|
|
// Also used for precompiled header generation in MSVC environments
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-05-16 15:59:10 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Compiler-dependent stuff:
|
|
|
|
#if defined(_MSC_VER)
|
2014-10-10 03:58:54 -04:00
|
|
|
// Use non-standard defines in <cmath>
|
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
|
2021-01-26 04:41:55 -05:00
|
|
|
#ifndef NDEBUG
|
2017-06-19 05:09:16 -04:00
|
|
|
// Override the "new" operator to include file and line specification for debugging memory leaks
|
|
|
|
// Ref.: https://social.msdn.microsoft.com/Forums/en-US/ebc7dd7a-f3c6-49f1-8a60-e381052f21b6/debugging-memory-leaks?forum=vcgeneral#53f0cc89-62fe-45e8-bbf0-56b89f2a1901
|
|
|
|
// This causes MSVC Debug runs to produce a report upon program exit, that contains memory-leaks
|
|
|
|
// together with the file:line information about where the memory was allocated.
|
|
|
|
// Note that this doesn't work with placement-new, which needs to temporarily #undef the macro
|
|
|
|
// (See AllocationPool.h for an example).
|
2021-01-26 04:41:55 -05:00
|
|
|
#define _CRTDBG_MAP_ALLOC
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#define DEBUG_CLIENTBLOCK new(_CLIENT_BLOCK, __FILE__, __LINE__)
|
|
|
|
#define new DEBUG_CLIENTBLOCK
|
|
|
|
// For some reason this works magically - each "new X" gets replaced as "new(_CLIENT_BLOCK, "file", line) X"
|
|
|
|
// The CRT has a definition for this operator new that stores the debugging info for leak-finding later.
|
2017-06-19 05:09:16 -04:00
|
|
|
#endif
|
|
|
|
|
2021-02-20 11:24:13 -05:00
|
|
|
#define UNREACHABLE_INTRINSIC __assume(false)
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
#elif defined(__GNUC__)
|
|
|
|
|
|
|
|
// TODO: Can GCC explicitly mark classes as abstract (no instances can be created)?
|
|
|
|
#define abstract
|
2013-10-19 12:37:47 -04:00
|
|
|
|
2021-02-20 11:24:13 -05:00
|
|
|
#define UNREACHABLE_INTRINSIC __builtin_unreachable()
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
#else
|
|
|
|
|
|
|
|
#error "You are using an unsupported compiler, you might need to #define some stuff here for your compiler"
|
2013-10-19 12:37:47 -04:00
|
|
|
|
2016-02-24 08:56:10 -05:00
|
|
|
#endif
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-03 05:57:05 -04:00
|
|
|
// A macro to disallow the copy constructor and operator = functions
|
2017-07-14 22:09:55 -04:00
|
|
|
// This should be used in the declarations for any class that shouldn't allow copying itself
|
2012-06-14 09:06:06 -04:00
|
|
|
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
2017-07-14 22:09:55 -04:00
|
|
|
TypeName(const TypeName &) = delete; \
|
|
|
|
TypeName & operator =(const TypeName &) = delete
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-09-26 16:53:11 -04:00
|
|
|
// A macro that is used to mark unused local variables, to avoid pedantic warnings in gcc / clang / MSVC
|
|
|
|
// Note that in MSVC it requires the full type of X to be known
|
|
|
|
#define UNUSED_VAR(X) (void)(X)
|
|
|
|
|
2012-08-03 07:53:11 -04:00
|
|
|
// A macro that is used to mark unused function parameters, to avoid pedantic warnings in gcc
|
2014-09-26 16:53:11 -04:00
|
|
|
// Written so that the full type of param needn't be known
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define UNUSED(X)
|
|
|
|
#else
|
|
|
|
#define UNUSED UNUSED_VAR
|
|
|
|
#endif
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-11 11:39:43 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// OS-dependent stuff:
|
|
|
|
#ifdef _WIN32
|
2021-03-28 08:34:57 -04:00
|
|
|
#define NOMINMAX // Windows SDK defines min and max macros, messing up with our std::min and std::max usage.
|
2014-11-25 18:03:33 -05:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2021-03-28 08:34:57 -04:00
|
|
|
#define _WIN32_WINNT 0x0501 // We want to target Windows XP with Service Pack 2 & Windows Server 2003 with Service Pack 1 and higher.
|
2013-10-19 12:37:47 -04:00
|
|
|
|
2021-03-28 08:34:57 -04:00
|
|
|
// Use CryptoAPI primitives when targeting a version that supports encrypting with AES-CFB8 smaller than a full block at a time.
|
|
|
|
#define PLATFORM_CRYPTOGRAPHY (_WIN32_WINNT >= 0x0602)
|
2015-02-08 11:35:10 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
#include <Windows.h>
|
|
|
|
#include <winsock2.h>
|
2013-03-05 04:53:29 -05:00
|
|
|
#include <Ws2tcpip.h> // IPv6 stuff
|
2013-10-19 12:37:47 -04:00
|
|
|
|
2021-03-28 08:34:57 -04:00
|
|
|
// Windows SDK defines GetFreeSpace as a constant, probably a Win16 API remnant:
|
2012-08-17 06:18:07 -04:00
|
|
|
#ifdef GetFreeSpace
|
|
|
|
#undef GetFreeSpace
|
|
|
|
#endif // GetFreeSpace
|
2012-06-14 09:06:06 -04:00
|
|
|
#else
|
2021-03-28 08:34:57 -04:00
|
|
|
#define PLATFORM_CRYPTOGRAPHY 0
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
#include <arpa/inet.h>
|
2015-06-04 09:13:07 -04:00
|
|
|
#include <unistd.h>
|
2012-08-15 17:24:11 -04:00
|
|
|
#endif
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CRT stuff:
|
2014-09-03 19:25:45 -04:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdarg>
|
2021-01-11 11:39:43 -05:00
|
|
|
#include <cstddef>
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// STL stuff:
|
2020-07-19 14:58:53 -04:00
|
|
|
#include <algorithm>
|
2015-06-30 10:50:15 -04:00
|
|
|
#include <array>
|
2020-07-19 14:58:53 -04:00
|
|
|
#include <atomic>
|
2014-10-19 09:10:18 -04:00
|
|
|
#include <chrono>
|
2020-07-19 14:58:53 -04:00
|
|
|
#include <condition_variable>
|
2012-06-14 09:06:06 -04:00
|
|
|
#include <deque>
|
2020-08-18 18:36:05 -04:00
|
|
|
#include <fstream>
|
2020-07-19 14:58:53 -04:00
|
|
|
#include <limits>
|
|
|
|
#include <list>
|
2012-06-14 09:06:06 -04:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2020-07-19 14:58:53 -04:00
|
|
|
#include <mutex>
|
2012-10-13 05:53:28 -04:00
|
|
|
#include <queue>
|
2017-07-28 12:54:40 -04:00
|
|
|
#include <random>
|
2020-07-19 14:58:53 -04:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2017-08-03 09:34:19 -04:00
|
|
|
#include <thread>
|
2020-07-19 14:58:53 -04:00
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <vector>
|
2021-01-11 11:39:43 -05:00
|
|
|
#include <variant>
|
|
|
|
|
2014-10-24 05:01:45 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-11 11:39:43 -05:00
|
|
|
// Integral types with predefined sizes:
|
|
|
|
typedef signed long long Int64;
|
|
|
|
typedef signed int Int32;
|
|
|
|
typedef signed short Int16;
|
|
|
|
typedef signed char Int8;
|
|
|
|
|
|
|
|
typedef unsigned long long UInt64;
|
|
|
|
typedef unsigned int UInt32;
|
|
|
|
typedef unsigned short UInt16;
|
|
|
|
typedef unsigned char UInt8;
|
|
|
|
|
|
|
|
typedef unsigned char Byte;
|
|
|
|
typedef Byte ColourID;
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T, size_t Size>
|
|
|
|
class SizeChecker
|
|
|
|
{
|
|
|
|
static_assert(sizeof(T) == Size, "Check the size of integral types");
|
|
|
|
};
|
|
|
|
|
|
|
|
template class SizeChecker<Int64, 8>;
|
|
|
|
template class SizeChecker<Int32, 4>;
|
|
|
|
template class SizeChecker<Int16, 2>;
|
|
|
|
template class SizeChecker<Int8, 1>;
|
|
|
|
|
|
|
|
template class SizeChecker<UInt64, 8>;
|
|
|
|
template class SizeChecker<UInt32, 4>;
|
|
|
|
template class SizeChecker<UInt16, 2>;
|
|
|
|
template class SizeChecker<UInt8, 1>;
|
|
|
|
|
2015-06-18 17:30:41 -04:00
|
|
|
// Common headers (part 1, without macros):
|
2020-05-05 17:52:14 -04:00
|
|
|
#include "fmt.h"
|
2015-06-18 17:30:41 -04:00
|
|
|
#include "StringUtils.h"
|
2020-05-14 22:35:43 -04:00
|
|
|
#include "LoggerSimple.h"
|
2015-06-18 17:30:41 -04:00
|
|
|
#include "OSSupport/CriticalSection.h"
|
|
|
|
#include "OSSupport/Event.h"
|
|
|
|
#include "OSSupport/File.h"
|
|
|
|
#include "OSSupport/StackTrace.h"
|
|
|
|
|
2020-05-14 22:35:43 -04:00
|
|
|
#ifdef TEST_GLOBALS
|
2015-08-30 17:57:43 -04:00
|
|
|
|
2020-05-14 22:35:43 -04:00
|
|
|
// Basic logging function implementations
|
|
|
|
namespace Logger
|
|
|
|
{
|
2014-09-17 13:40:10 -04:00
|
|
|
|
2020-05-14 22:35:43 -04:00
|
|
|
inline void LogFormat(
|
|
|
|
std::string_view a_Format, eLogLevel, fmt::format_args a_ArgList
|
|
|
|
)
|
2016-06-17 07:43:58 -04:00
|
|
|
{
|
2020-05-14 22:35:43 -04:00
|
|
|
fmt::vprint(a_Format, a_ArgList);
|
2016-06-17 07:43:58 -04:00
|
|
|
putchar('\n');
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2014-09-17 13:40:10 -04:00
|
|
|
|
2020-05-14 22:35:43 -04:00
|
|
|
inline void LogPrintf(
|
|
|
|
std::string_view a_Format, eLogLevel, fmt::printf_args a_ArgList
|
|
|
|
)
|
2016-06-17 07:43:58 -04:00
|
|
|
{
|
2020-05-14 22:35:43 -04:00
|
|
|
fmt::vprintf(a_Format, a_ArgList);
|
2016-06-17 07:43:58 -04:00
|
|
|
putchar('\n');
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2020-05-14 22:35:43 -04:00
|
|
|
inline void LogSimple(std::string_view a_Message, eLogLevel)
|
|
|
|
{
|
|
|
|
fmt::print("{}\n", a_Message);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Logger
|
2015-06-18 17:30:41 -04:00
|
|
|
|
2014-05-27 07:44:56 -04:00
|
|
|
#endif
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Common definitions:
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Evaluates to the number of elements in an array (compile-time!) */
|
2012-06-14 09:06:06 -04:00
|
|
|
#define ARRAYCOUNT(X) (sizeof(X) / sizeof(*(X)))
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Allows arithmetic expressions like "32 KiB" (but consider using parenthesis around it, "(32 KiB)") */
|
2012-06-14 09:06:06 -04:00
|
|
|
#define KiB * 1024
|
2013-02-27 05:01:20 -05:00
|
|
|
#define MiB * 1024 * 1024
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Faster than (int)floorf((float)x / (float)div) */
|
|
|
|
#define FAST_FLOOR_DIV(x, div) (((x) - (((x) < 0) ? ((div) - 1) : 0)) / (div))
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2019-08-26 15:38:34 -04:00
|
|
|
// Own version of ASSERT() that plays nicely with the testing framework
|
2014-05-27 07:44:56 -04:00
|
|
|
#ifdef TEST_GLOBALS
|
|
|
|
|
|
|
|
class cAssertFailure
|
|
|
|
{
|
2019-08-26 15:38:34 -04:00
|
|
|
AString mExpression;
|
|
|
|
AString mFileName;
|
|
|
|
int mLineNumber;
|
|
|
|
|
|
|
|
public:
|
|
|
|
cAssertFailure(const AString & aExpression, const AString & aFileName, int aLineNumber):
|
|
|
|
mExpression(aExpression),
|
|
|
|
mFileName(aFileName),
|
|
|
|
mLineNumber(aLineNumber)
|
|
|
|
{
|
2014-05-30 04:56:12 -04:00
|
|
|
}
|
2019-08-26 15:38:34 -04:00
|
|
|
|
|
|
|
const AString & expression() const { return mExpression; }
|
|
|
|
const AString & fileName() const { return mFileName; }
|
|
|
|
int lineNumber() const { return mLineNumber; }
|
|
|
|
};
|
2016-08-02 07:12:34 -04:00
|
|
|
|
2021-01-26 04:41:55 -05:00
|
|
|
#ifdef NDEBUG
|
|
|
|
#define ASSERT(x)
|
2016-08-02 07:12:34 -04:00
|
|
|
#else
|
2021-01-26 04:41:55 -05:00
|
|
|
#define ASSERT(x) do { if (!(x)) { throw cAssertFailure(#x, __FILE__, __LINE__);} } while (0)
|
2016-08-02 07:12:34 -04:00
|
|
|
#endif
|
2014-05-27 07:44:56 -04:00
|
|
|
|
2019-08-26 15:38:34 -04:00
|
|
|
// Pretty much the same as ASSERT() but stays in Release builds
|
2020-07-22 19:34:43 -04:00
|
|
|
#define VERIFY(x) (!!(x) || ( LOGERROR("Verification failed: %s, file %s, line %i", #x, __FILE__, __LINE__), std::abort(), 0))
|
2019-08-26 15:38:34 -04:00
|
|
|
|
|
|
|
#else // TEST_GLOBALS
|
|
|
|
|
2021-01-26 04:41:55 -05:00
|
|
|
#ifdef NDEBUG
|
2016-08-02 07:12:34 -04:00
|
|
|
#define ASSERT(x)
|
2021-01-26 04:41:55 -05:00
|
|
|
#else
|
|
|
|
#define ASSERT(x) ( !!(x) || ( LOGERROR("Assertion failed: %s, file %s, line %i", #x, __FILE__, __LINE__), std::abort(), 0))
|
2014-05-27 07:44:56 -04:00
|
|
|
#endif
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2019-08-26 15:38:34 -04:00
|
|
|
// Pretty much the same as ASSERT() but stays in Release builds
|
2020-07-22 19:34:43 -04:00
|
|
|
#define VERIFY(x) (!!(x) || ( LOGERROR("Verification failed: %s, file %s, line %i", #x, __FILE__, __LINE__), std::abort(), 0))
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2019-08-26 15:38:34 -04:00
|
|
|
#endif // else TEST_GLOBALS
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2021-02-20 11:24:13 -05:00
|
|
|
// Use to mark code that should be impossible to reach.
|
|
|
|
#ifdef NDEBUG
|
|
|
|
#define UNREACHABLE(x) UNREACHABLE_INTRINSIC
|
|
|
|
#else
|
|
|
|
#define UNREACHABLE(x) ( FLOGERROR("Hit unreachable code: {0}, file {1}, line {2}", #x, __FILE__, __LINE__), std::abort(), 0)
|
|
|
|
#endif
|
2018-02-04 18:07:12 -05:00
|
|
|
|
2014-03-28 16:35:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-11 11:39:43 -05:00
|
|
|
|
|
|
|
namespace cpp20
|
|
|
|
{
|
|
|
|
template <class T>
|
|
|
|
std::enable_if_t<std::is_array_v<T> && (std::extent_v<T> == 0), std::unique_ptr<T>> make_unique_for_overwrite(std::size_t a_Size)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<T>(new std::remove_extent_t<T>[a_Size]);
|
|
|
|
}
|
2021-03-05 08:03:55 -05:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
std::enable_if_t<!std::is_array_v<T>, std::unique_ptr<T>> make_unique_for_overwrite()
|
|
|
|
{
|
|
|
|
return std::unique_ptr<T>(new T);
|
|
|
|
}
|
2021-01-11 11:39:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-04 21:05:14 -05:00
|
|
|
/**
|
|
|
|
You can use this struct to use in std::visit
|
|
|
|
example:
|
|
|
|
std::visit(
|
|
|
|
OverloadedVariantAccess
|
|
|
|
{
|
|
|
|
[&] (cFirstType & a_FirstTypeObject) { // Your code to handle cFirstType },
|
|
|
|
[&] (cSecondType & a_SecondTypeObject) { // YourCode to handle cSecondType },
|
|
|
|
...
|
|
|
|
}
|
|
|
|
, YourVariant);
|
|
|
|
You can use constant references if you want to.
|
|
|
|
*/
|
|
|
|
template<class... Ts> struct OverloadedVariantAccess : Ts... { using Ts::operator()...; };
|
|
|
|
template<class... Ts> OverloadedVariantAccess(Ts...)->OverloadedVariantAccess<Ts...>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-20 09:38:37 -05:00
|
|
|
/** Clamp X to the specified range. */
|
2014-02-20 08:37:15 -05:00
|
|
|
template <typename T>
|
2014-02-20 09:38:37 -05:00
|
|
|
T Clamp(T a_Value, T a_Min, T a_Max)
|
2014-02-20 08:37:15 -05:00
|
|
|
{
|
2014-02-20 09:38:37 -05:00
|
|
|
return (a_Value < a_Min) ? a_Min : ((a_Value > a_Max) ? a_Max : a_Value);
|
2014-02-20 08:37:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-09-03 19:51:38 -04:00
|
|
|
/** Floors a value, then casts it to C (an int by default) */
|
|
|
|
template <typename C = int, typename T>
|
|
|
|
typename std::enable_if<std::is_arithmetic<T>::value, C>::type FloorC(T a_Value)
|
2014-09-03 19:25:45 -04:00
|
|
|
{
|
|
|
|
return static_cast<C>(std::floor(a_Value));
|
|
|
|
}
|
|
|
|
|
2014-09-03 19:51:38 -04:00
|
|
|
/** Ceils a value, then casts it to C (an int by default) */
|
|
|
|
template <typename C = int, typename T>
|
|
|
|
typename std::enable_if<std::is_arithmetic<T>::value, C>::type CeilC(T a_Value)
|
2014-09-03 19:25:45 -04:00
|
|
|
{
|
|
|
|
return static_cast<C>(std::ceil(a_Value));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-15 08:57:27 -04:00
|
|
|
|
2014-09-03 19:25:45 -04:00
|
|
|
|
2015-01-10 20:54:18 -05:00
|
|
|
// a tick is 50 ms
|
|
|
|
using cTickTime = std::chrono::duration<int, std::ratio_multiply<std::chrono::milliseconds::period, std::ratio<50>>>;
|
2015-01-11 16:12:26 -05:00
|
|
|
using cTickTimeLong = std::chrono::duration<Int64, cTickTime::period>;
|
2014-09-03 19:25:45 -04:00
|
|
|
|
2021-01-11 11:39:43 -05:00
|
|
|
using ContiguousByteBuffer = std::basic_string<std::byte>;
|
|
|
|
using ContiguousByteBufferView = std::basic_string_view<std::byte>;
|
|
|
|
|
2014-03-11 10:01:17 -04:00
|
|
|
#ifndef TOLUA_TEMPLATE_BIND
|
2014-04-24 15:34:45 -04:00
|
|
|
#define TOLUA_TEMPLATE_BIND(x)
|
2014-03-11 10:01:17 -04:00
|
|
|
#endif
|
|
|
|
|
2017-08-28 09:36:23 -04:00
|
|
|
#ifdef TOLUA_EXPOSITION
|
|
|
|
#error TOLUA_EXPOSITION should never actually be defined
|
|
|
|
#endif
|
|
|
|
|
2020-05-09 11:51:54 -04:00
|
|
|
template <typename T>
|
|
|
|
auto ToUnsigned(T a_Val)
|
|
|
|
{
|
|
|
|
ASSERT(a_Val >= 0);
|
|
|
|
return static_cast<std::make_unsigned_t<T>>(a_Val);
|
|
|
|
}
|
|
|
|
|
2014-03-11 10:01:17 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Common headers (part 2, with macros):
|
2017-08-03 09:34:19 -04:00
|
|
|
#include "Vector3.h"
|