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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Compiler-dependent stuff:
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
// Disable some warnings that we don't care about:
|
2014-07-17 13:13:23 -04:00
|
|
|
#pragma warning(disable:4100) // Unreferenced formal parameter
|
2014-07-17 10:33:09 -04:00
|
|
|
|
|
|
|
// Useful warnings from warning level 4:
|
2014-07-17 13:13:23 -04:00
|
|
|
#pragma warning(3 : 4189) // Local variable is initialized but not referenced
|
2015-05-09 03:25:09 -04:00
|
|
|
#pragma warning(3 : 4245) // Conversion from 'type1' to 'type2', signed / unsigned mismatch
|
2014-07-17 13:13:23 -04:00
|
|
|
#pragma warning(3 : 4310) // Cast truncates constant value
|
2015-05-09 03:25:09 -04:00
|
|
|
#pragma warning(3 : 4389) // Signed / unsigned mismatch
|
2014-07-17 13:13:23 -04:00
|
|
|
#pragma warning(3 : 4505) // Unreferenced local function has been removed
|
|
|
|
#pragma warning(3 : 4701) // Potentially unitialized local variable used
|
|
|
|
#pragma warning(3 : 4702) // Unreachable code
|
|
|
|
#pragma warning(3 : 4706) // Assignment within conditional expression
|
2014-10-23 04:53:18 -04:00
|
|
|
|
|
|
|
// 2014-10-23 xoft: Disabled this because the new C++11 headers in MSVC produce tons of these warnings uselessly
|
|
|
|
// #pragma warning(3 : 4127) // Conditional expression is constant
|
|
|
|
|
2014-01-07 11:15:08 -05:00
|
|
|
// Disabling this warning, because we know what we're doing when we're doing this:
|
|
|
|
#pragma warning(disable: 4355) // 'this' used in initializer list
|
2014-04-03 03:27:17 -04:00
|
|
|
|
|
|
|
// Disabled because it's useless:
|
|
|
|
#pragma warning(disable: 4512) // 'class': assignment operator could not be generated - reported for each class that has a reference-type member
|
2015-11-05 05:09:37 -05:00
|
|
|
#pragma warning(disable: 4351) // new behavior: elements of array 'member' will be default initialized
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-01-06 16:23:03 -05:00
|
|
|
// 2014_01_06 xoft: Disabled this warning because MSVC is stupid and reports it in obviously wrong places
|
2014-07-17 13:13:23 -04:00
|
|
|
// #pragma warning(3 : 4244) // Conversion from 'type1' to 'type2', possible loss of data
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#define OBSOLETE __declspec(deprecated)
|
2013-10-19 12:37:47 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// No alignment needed in MSVC
|
|
|
|
#define ALIGN_8
|
|
|
|
#define ALIGN_16
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-03-12 14:56:24 -04:00
|
|
|
#define FORMATSTRING(formatIndex, va_argsIndex)
|
2013-10-19 12:37:47 -04:00
|
|
|
|
2014-03-12 13:09:08 -04:00
|
|
|
// MSVC has its own custom version of zu format
|
|
|
|
#define SIZE_T_FMT "%Iu"
|
2014-03-12 13:34:50 -04:00
|
|
|
#define SIZE_T_FMT_PRECISION(x) "%" #x "Iu"
|
|
|
|
#define SIZE_T_FMT_HEX "%Ix"
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-03-14 10:59:25 -04:00
|
|
|
#define NORETURN __declspec(noreturn)
|
2013-10-19 12:37:47 -04:00
|
|
|
|
2014-10-10 03:58:54 -04:00
|
|
|
// Use non-standard defines in <cmath>
|
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
|
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
|
|
|
|
2014-01-25 18:02:31 -05:00
|
|
|
// override is part of c++11
|
|
|
|
#if __cplusplus < 201103L
|
2014-07-17 10:33:09 -04:00
|
|
|
#define override
|
|
|
|
#endif
|
2013-10-19 12:37:47 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
#define OBSOLETE __attribute__((deprecated))
|
|
|
|
|
|
|
|
#define ALIGN_8 __attribute__((aligned(8)))
|
|
|
|
#define ALIGN_16 __attribute__((aligned(16)))
|
|
|
|
|
|
|
|
// Some portability macros :)
|
|
|
|
#define stricmp strcasecmp
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-03-12 14:56:24 -04:00
|
|
|
#define FORMATSTRING(formatIndex, va_argsIndex) __attribute__((format (printf, formatIndex, va_argsIndex)))
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-06-30 15:58:23 -04:00
|
|
|
#if defined(_WIN32)
|
|
|
|
// We're compiling on MinGW, which uses an old MSVCRT library that has no support for size_t printfing.
|
|
|
|
// We need direct size formats:
|
|
|
|
#if defined(_WIN64)
|
|
|
|
#define SIZE_T_FMT "%I64u"
|
|
|
|
#define SIZE_T_FMT_PRECISION(x) "%" #x "I64u"
|
|
|
|
#define SIZE_T_FMT_HEX "%I64x"
|
|
|
|
#else
|
|
|
|
#define SIZE_T_FMT "%u"
|
|
|
|
#define SIZE_T_FMT_PRECISION(x) "%" #x "u"
|
|
|
|
#define SIZE_T_FMT_HEX "%x"
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
// We're compiling on Linux, so we can use libc's size_t printf format:
|
|
|
|
#define SIZE_T_FMT "%zu"
|
|
|
|
#define SIZE_T_FMT_PRECISION(x) "%" #x "zu"
|
|
|
|
#define SIZE_T_FMT_HEX "%zx"
|
|
|
|
#endif
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-03-14 10:59:25 -04:00
|
|
|
#define NORETURN __attribute((__noreturn__))
|
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-03-14 10:59:25 -04:00
|
|
|
#ifdef _DEBUG
|
|
|
|
#define NORETURNDEBUG NORETURN
|
|
|
|
#else
|
|
|
|
#define NORETURNDEBUG
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-03-09 14:47:22 -04:00
|
|
|
#include <stddef.h>
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
// Integral types with predefined sizes:
|
2015-03-21 08:00:20 -04:00
|
|
|
typedef signed long long Int64;
|
|
|
|
typedef signed int Int32;
|
|
|
|
typedef signed short Int16;
|
|
|
|
typedef signed char Int8;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-10-06 12:58:31 -04:00
|
|
|
typedef unsigned long long UInt64;
|
|
|
|
typedef unsigned int UInt32;
|
|
|
|
typedef unsigned short UInt16;
|
2015-03-21 08:00:20 -04:00
|
|
|
typedef unsigned char UInt8;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-01-23 17:35:23 -05:00
|
|
|
typedef unsigned char Byte;
|
2015-06-30 10:50:15 -04:00
|
|
|
typedef Byte ColourID;
|
2014-01-23 17:35:23 -05:00
|
|
|
|
|
|
|
|
2014-03-09 14:47:22 -04:00
|
|
|
// If you get an error about specialization check the size of integral types
|
|
|
|
template <typename T, size_t Size, bool x = sizeof(T) == Size>
|
|
|
|
class SizeChecker;
|
|
|
|
|
|
|
|
template <typename T, size_t Size>
|
2014-03-14 09:11:49 -04:00
|
|
|
class SizeChecker<T, Size, true>
|
|
|
|
{
|
2014-07-17 10:33:09 -04:00
|
|
|
T v;
|
2014-03-09 14:47:22 -04:00
|
|
|
};
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-03-09 14:47:22 -04:00
|
|
|
template class SizeChecker<Int64, 8>;
|
|
|
|
template class SizeChecker<Int32, 4>;
|
|
|
|
template class SizeChecker<Int16, 2>;
|
2015-03-21 08:00:20 -04:00
|
|
|
template class SizeChecker<Int8, 1>;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-03-09 14:47:22 -04:00
|
|
|
template class SizeChecker<UInt64, 8>;
|
|
|
|
template class SizeChecker<UInt32, 4>;
|
|
|
|
template class SizeChecker<UInt16, 2>;
|
2015-03-21 08:00:20 -04:00
|
|
|
template class SizeChecker<UInt8, 1>;
|
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
|
2012-06-14 09:06:06 -04:00
|
|
|
// This should be used in the private: declarations for any class that shouldn't allow copying itself
|
|
|
|
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
|
|
|
TypeName(const TypeName &); \
|
2014-08-03 05:57:05 -04:00
|
|
|
void operator =(const TypeName &)
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// OS-dependent stuff:
|
|
|
|
#ifdef _WIN32
|
2013-10-19 12:37:47 -04:00
|
|
|
|
2014-11-25 18:03:33 -05:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#define _WIN32_WINNT _WIN32_WINNT_WS03 // 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
|
|
|
|
2015-02-08 11:35:10 -05:00
|
|
|
// Windows SDK defines min and max macros, messing up with our std::min and std::max usage
|
|
|
|
#define NOMINMAX
|
|
|
|
|
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
|
|
|
|
2012-08-17 06:18:07 -04:00
|
|
|
// Windows SDK defines GetFreeSpace as a constant, probably a Win16 API remnant
|
|
|
|
#ifdef GetFreeSpace
|
|
|
|
#undef GetFreeSpace
|
|
|
|
#endif // GetFreeSpace
|
2012-06-14 09:06:06 -04:00
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2015-06-04 09:13:07 -04:00
|
|
|
#include <unistd.h>
|
2012-08-15 17:24:11 -04:00
|
|
|
#endif
|
|
|
|
|
2012-08-16 16:28:14 -04:00
|
|
|
#if defined(ANDROID_NDK)
|
2015-09-25 04:14:17 -04:00
|
|
|
#define FILE_IO_PREFIX "/sdcard/Cuberite/"
|
2012-08-16 16:28:14 -04:00
|
|
|
#else
|
|
|
|
#define FILE_IO_PREFIX ""
|
|
|
|
#endif
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CRT stuff:
|
2013-10-09 03:38:47 -04:00
|
|
|
#include <sys/stat.h>
|
2014-09-03 19:25:45 -04:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdarg>
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// STL stuff:
|
2015-06-30 10:50:15 -04:00
|
|
|
#include <array>
|
2014-10-19 09:10:18 -04:00
|
|
|
#include <chrono>
|
2012-06-14 09:06:06 -04:00
|
|
|
#include <vector>
|
|
|
|
#include <list>
|
|
|
|
#include <deque>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
2012-10-13 05:53:28 -04:00
|
|
|
#include <set>
|
|
|
|
#include <queue>
|
2014-03-09 14:47:22 -04:00
|
|
|
#include <limits>
|
2014-10-24 05:01:45 -04:00
|
|
|
#include <chrono>
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-18 17:30:41 -04:00
|
|
|
// Common headers (part 1, without macros):
|
|
|
|
#include "StringUtils.h"
|
|
|
|
#include "OSSupport/CriticalSection.h"
|
|
|
|
#include "OSSupport/Event.h"
|
|
|
|
#include "OSSupport/File.h"
|
|
|
|
#include "OSSupport/StackTrace.h"
|
|
|
|
|
2014-05-27 07:44:56 -04:00
|
|
|
#ifndef TEST_GLOBALS
|
2015-08-30 17:57:43 -04:00
|
|
|
|
2016-06-17 07:43:58 -04:00
|
|
|
// These functions are defined in Logger.cpp, but are declared here to avoid including all of logger.h
|
|
|
|
extern void LOG (const char * a_Format, ...) FORMATSTRING(1, 2);
|
|
|
|
extern void LOGINFO (const char * a_Format, ...) FORMATSTRING(1, 2);
|
|
|
|
extern void LOGWARNING(const char * a_Format, ...) FORMATSTRING(1, 2);
|
|
|
|
extern void LOGERROR (const char * a_Format, ...) FORMATSTRING(1, 2);
|
|
|
|
|
|
|
|
// In debug builds, translate LOGD to LOG, otherwise leave it out altogether:
|
|
|
|
#ifdef _DEBUG
|
|
|
|
#define LOGD LOG
|
|
|
|
#else
|
|
|
|
#define LOGD(...)
|
|
|
|
#endif // _DEBUG
|
2015-08-30 17:57:43 -04:00
|
|
|
|
2016-06-17 07:43:58 -04:00
|
|
|
#define LOGWARN LOGWARNING
|
2015-08-30 17:57:43 -04:00
|
|
|
|
2014-05-27 07:44:56 -04:00
|
|
|
#else
|
|
|
|
// Logging functions
|
2016-06-17 07:43:58 -04:00
|
|
|
void inline LOGERROR(const char * a_Format, ...) FORMATSTRING(1, 2);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2016-06-17 07:43:58 -04:00
|
|
|
void inline LOGERROR(const char * a_Format, ...)
|
|
|
|
{
|
|
|
|
va_list argList;
|
|
|
|
va_start(argList, a_Format);
|
|
|
|
vprintf(a_Format, argList);
|
|
|
|
putchar('\n');
|
|
|
|
fflush(stdout);
|
|
|
|
va_end(argList);
|
|
|
|
}
|
2014-09-17 13:40:10 -04:00
|
|
|
|
2016-06-17 07:43:58 -04:00
|
|
|
void inline LOGWARNING(const char * a_Format, ...) FORMATSTRING(1, 2);
|
2014-09-17 13:40:10 -04:00
|
|
|
|
2016-06-17 07:43:58 -04:00
|
|
|
void inline LOGWARNING(const char * a_Format, ...)
|
|
|
|
{
|
|
|
|
va_list argList;
|
|
|
|
va_start(argList, a_Format);
|
|
|
|
vprintf(a_Format, argList);
|
|
|
|
putchar('\n');
|
|
|
|
fflush(stdout);
|
|
|
|
va_end(argList);
|
|
|
|
}
|
2014-09-17 13:40:10 -04:00
|
|
|
|
2016-06-17 07:43:58 -04:00
|
|
|
void inline LOGD(const char * a_Format, ...) FORMATSTRING(1, 2);
|
2014-09-17 13:40:10 -04:00
|
|
|
|
2016-06-17 07:43:58 -04:00
|
|
|
void inline LOGD(const char * a_Format, ...)
|
|
|
|
{
|
|
|
|
va_list argList;
|
|
|
|
va_start(argList, a_Format);
|
|
|
|
vprintf(a_Format, argList);
|
|
|
|
putchar('\n');
|
|
|
|
fflush(stdout);
|
|
|
|
va_end(argList);
|
|
|
|
}
|
2014-09-17 13:40:10 -04:00
|
|
|
|
2016-06-17 07:43:58 -04:00
|
|
|
void inline LOG(const char * a_Format, ...) FORMATSTRING(1, 2);
|
2015-01-09 09:07:59 -05:00
|
|
|
|
2016-06-17 07:43:58 -04:00
|
|
|
void inline LOG(const char * a_Format, ...)
|
|
|
|
{
|
|
|
|
va_list argList;
|
|
|
|
va_start(argList, a_Format);
|
|
|
|
vprintf(a_Format, argList);
|
|
|
|
putchar('\n');
|
|
|
|
fflush(stdout);
|
|
|
|
va_end(argList);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LOGINFO LOG
|
|
|
|
#define LOGWARN LOGWARNING
|
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
|
|
|
|
|
|
|
// Own version of assert() that writes failed assertions to the log for review
|
2014-05-27 07:44:56 -04:00
|
|
|
#ifdef TEST_GLOBALS
|
|
|
|
|
|
|
|
class cAssertFailure
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2014-05-30 04:56:12 -04:00
|
|
|
#ifdef _WIN32
|
2014-05-30 05:01:13 -04:00
|
|
|
#if (defined(_MSC_VER) && defined(_DEBUG))
|
|
|
|
#define DBG_BREAK _CrtDbgBreak()
|
|
|
|
#else
|
|
|
|
#define DBG_BREAK
|
|
|
|
#endif
|
2014-05-30 04:56:12 -04:00
|
|
|
#define REPORT_ERROR(FMT, ...) \
|
|
|
|
{ \
|
|
|
|
AString msg = Printf(FMT, __VA_ARGS__); \
|
|
|
|
puts(msg.c_str()); \
|
|
|
|
fflush(stdout); \
|
|
|
|
OutputDebugStringA(msg.c_str()); \
|
2014-05-30 05:01:13 -04:00
|
|
|
DBG_BREAK; \
|
2014-05-30 04:56:12 -04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define REPORT_ERROR(FMT, ...) \
|
|
|
|
{ \
|
|
|
|
AString msg = Printf(FMT, __VA_ARGS__); \
|
|
|
|
puts(msg.c_str()); \
|
|
|
|
fflush(stdout); \
|
|
|
|
}
|
|
|
|
#endif
|
2016-08-02 07:12:34 -04:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
#define ASSERT(x) do { if (!(x)) { throw cAssertFailure();} } while (0)
|
|
|
|
#define testassert(x) do { if (!(x)) { REPORT_ERROR("Test failure: %s, file %s, line %d\n", #x, __FILE__, __LINE__); exit(1); } } while (0)
|
|
|
|
#define CheckAsserts(x) do { try {x} catch (cAssertFailure) { break; } REPORT_ERROR("Test failure: assert didn't fire for %s, file %s, line %d\n", #x, __FILE__, __LINE__); exit(1); } while (0)
|
|
|
|
#else
|
|
|
|
#define ASSERT(...)
|
|
|
|
#define testassert(...)
|
|
|
|
#define CheckAsserts(...) LOG("Assert checking is disabled in Release-mode executables (file %s, line %d)", __FILE__, __LINE__)
|
|
|
|
#endif
|
2014-05-27 07:44:56 -04:00
|
|
|
|
2012-09-04 11:29:45 -04:00
|
|
|
#else
|
2016-08-02 07:12:34 -04:00
|
|
|
#ifdef _DEBUG
|
|
|
|
#define ASSERT(x) ( !!(x) || ( LOGERROR("Assertion failed: %s, file %s, line %i", #x, __FILE__, __LINE__), PrintStackTrace(), assert(0), 0))
|
2014-05-27 07:44:56 -04:00
|
|
|
#else
|
2016-08-02 07:12:34 -04:00
|
|
|
#define ASSERT(x)
|
2014-05-27 07:44:56 -04:00
|
|
|
#endif
|
2012-06-14 09:06:06 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Pretty much the same as ASSERT() but stays in Release builds
|
2014-11-29 17:06:10 -05:00
|
|
|
#define VERIFY( x) ( !!(x) || ( LOGERROR("Verification failed: %s, file %s, line %i", #x, __FILE__, __LINE__), PrintStackTrace(), exit(1), 0))
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-03-10 16:12:43 -04:00
|
|
|
// Same as assert but in all Self test builds
|
|
|
|
#ifdef SELF_TEST
|
2014-04-24 15:34:45 -04:00
|
|
|
#define assert_test(x) ( !!(x) || (assert(!#x), exit(1), 0))
|
2014-03-10 16:12:43 -04:00
|
|
|
#endif
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2015-02-12 14:05:55 -05:00
|
|
|
// Unified ptr types from before C++11. Also no silly undercores.
|
2014-10-19 12:52:24 -04:00
|
|
|
#define SharedPtr std::shared_ptr
|
2015-01-30 15:24:02 -05:00
|
|
|
#define WeakPtr std::weak_ptr
|
2015-02-12 14:05:55 -05:00
|
|
|
#define UniquePtr std::unique_ptr
|
2014-04-24 15:34:45 -04:00
|
|
|
|
2014-03-28 16:35:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** A generic interface used mainly in ForEach() functions */
|
2012-06-14 09:06:06 -04:00
|
|
|
template <typename Type> class cItemCallback
|
|
|
|
{
|
|
|
|
public:
|
2014-03-28 16:35:45 -04:00
|
|
|
virtual ~cItemCallback() {}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-03-28 16:35:45 -04:00
|
|
|
/** Called for each item in the internal list; return true to stop the loop, or false to continue enumerating */
|
2012-06-14 09:06:06 -04:00
|
|
|
virtual bool Item(Type * a_Type) = 0;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-10 10:33:19 -04:00
|
|
|
//temporary replacement for std::make_unique until we get c++14
|
2015-05-15 08:57:27 -04:00
|
|
|
|
|
|
|
namespace cpp14
|
2014-10-10 10:33:19 -04:00
|
|
|
{
|
2015-05-15 08:57:27 -04:00
|
|
|
template <class T, class... Args>
|
|
|
|
std::unique_ptr<T> make_unique(Args&&... args)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
|
|
|
}
|
2014-10-10 10:33:19 -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
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Common headers (part 2, with macros):
|
|
|
|
#include "ChunkDef.h"
|
2014-01-09 16:21:46 -05:00
|
|
|
#include "BiomeDef.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
#include "BlockID.h"
|
2014-03-01 14:34:19 -05:00
|
|
|
#include "BlockInfo.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
2014-10-10 10:33:19 -04:00
|
|
|
|
2015-01-10 20:54:18 -05:00
|
|
|
|