gb.hpp - #ifndef for many macros

This commit is contained in:
gingerBill 2015-11-17 10:45:07 +00:00
parent 9be05386ac
commit a8f308e2cd
2 changed files with 218 additions and 184 deletions

View File

@ -6,7 +6,7 @@ library | latest version | category | languages | description
----------------|----------------|----------|-----------|------------- ----------------|----------------|----------|-----------|-------------
**gb_string.h** | 0.93 | strings | C, C++ | A better string library for C & C++ **gb_string.h** | 0.93 | strings | C, C++ | A better string library for C & C++
**gb_ini.h** | 0.91 | misc | C, C++ | A simple ini file loader library for C & C++ **gb_ini.h** | 0.91 | misc | C, C++ | A simple ini file loader library for C & C++
**gb.hpp** | 0.20 | misc | C++11 | (Experimental) A C++11 helper library without STL geared towards game development **gb.hpp** | 0.20a | misc | C++11 | (Experimental) A C++11 helper library without STL geared towards game development
## FAQ ## FAQ

82
gb.hpp
View File

@ -1,8 +1,9 @@
// gb.hpp - v0.20 - public domain C++11 helper library - no warranty implied; use at your own risk // gb.hpp - v0.20a - public domain C++11 helper library - no warranty implied; use at your own risk
// (Experimental) A C++11 helper library without STL geared towards game development // (Experimental) A C++11 helper library without STL geared towards game development
/* /*
Version History: Version History:
0.20a - #ifndef for many macros
0.20 - Angle 0.20 - Angle
0.19 - Cache friendly Transform and String fixes 0.19 - Cache friendly Transform and String fixes
0.18 - Hash_Table bug fixes 0.18 - Hash_Table bug fixes
@ -65,6 +66,7 @@ Context:
- Complex - Complex
- Quaternion - Quaternion
- Matrix(2,3,4) - Matrix(2,3,4)
- Angle
- Euler_Angles - Euler_Angles
- Transform - Transform
- Aabb - Aabb
@ -74,12 +76,6 @@ Context:
- Functions & Constants - Functions & Constants
- Type Functions - Type Functions
- Random - Random
- Generator_Type
- Geneartor Definition (Template/Concept)
- Mt19937_32
- Mt19937_64
- Random_Device
- Functions
*/ */
#ifndef GB_INCLUDE_GB_HPP #ifndef GB_INCLUDE_GB_HPP
@ -91,17 +87,20 @@ Context:
// NOTE(bill): Because static means three different things in C/C++ // NOTE(bill): Because static means three different things in C/C++
// Great Design(!) // Great Design(!)
#ifndef global
#define global static #define global static
#define internal static #define internal static
#define local_persist static #define local_persist static
#endif
#if defined(_MSC_VER) #if defined(_MSC_VER)
#define _ALLOW_KEYWORD_MACROS #define _ALLOW_KEYWORD_MACROS
#endif
#if !defined(alignof) // Needed for MSVC 2013 'cause Microsoft "loves" standards #ifndef alignof // Needed for MSVC 2013 'cause Microsoft "loves" standards
#define alignof(x) __alignof(x) #define alignof(x) __alignof(x)
#endif #endif
#endif
//////////////////////////////// ////////////////////////////////
/// /// /// ///
@ -109,16 +108,26 @@ Context:
/// /// /// ///
//////////////////////////////// ////////////////////////////////
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
#ifndef GB_SYSTEM_WINDOWS
#define GB_SYSTEM_WINDOWS 1 #define GB_SYSTEM_WINDOWS 1
#endif
#elif defined(__APPLE__) && defined(__MACH__) #elif defined(__APPLE__) && defined(__MACH__)
#ifndef GB_SYSTEM_OSX
#define GB_SYSTEM_OSX 1 #define GB_SYSTEM_OSX 1
#endif
#elif defined(__unix__) #elif defined(__unix__)
#ifndef GB_SYSTEM_UNIX
#define GB_SYSTEM_UNIX 1 #define GB_SYSTEM_UNIX 1
#endif
#if defined(__linux__) #if defined(__linux__)
#ifndef GB_SYSTEM_LINUX
#define GB_SYSTEM_LINUX 1 #define GB_SYSTEM_LINUX 1
#endif
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#ifndef GB_SYSTEM_FREEBSD
#define GB_SYSTEM_FREEBSD 1 #define GB_SYSTEM_FREEBSD 1
#endif
#else #else
#error This UNIX operating system is not supported by gb.hpp #error This UNIX operating system is not supported by gb.hpp
#endif #endif
@ -133,21 +142,30 @@ Context:
//////////////////////////////// ////////////////////////////////
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
#if defined(_WIN64) #if defined(_WIN64)
#ifndef GB_ARCH_64_BIT
#define GB_ARCH_64_BIT 1 #define GB_ARCH_64_BIT 1
#endif
#else #else
#ifndef GB_ARCH_32_BIT
#define GB_ARCH_32_BIT 1 #define GB_ARCH_32_BIT 1
#endif #endif
#endif #endif
#endif
// TODO(bill): Check if this KEPLER_ENVIRONMENT works on clang // TODO(bill): Check if this KEPLER_ENVIRONMENT works on clang
#if defined(__GNUC__) #if defined(__GNUC__)
#if defined(__x86_64__) || defined(__ppc64__) #if defined(__x86_64__) || defined(__ppc64__)
#ifndef GB_ARCH_64_BIT
#define GB_ARCH_64_BIT 1 #define GB_ARCH_64_BIT 1
#endif
#else #else
#ifndef GB_ARCH_32_BIT
#define GB_ARCH_32_BIT 1 #define GB_ARCH_32_BIT 1
#endif #endif
#endif #endif
#endif
// TODO(bill): Get this to work
// #if !defined(GB_LITTLE_EDIAN) && !defined(GB_BIG_EDIAN) // #if !defined(GB_LITTLE_EDIAN) && !defined(GB_BIG_EDIAN)
// // Source: http://sourceforge.net/p/predef/wiki/Endianness/ // // Source: http://sourceforge.net/p/predef/wiki/Endianness/
@ -225,20 +243,20 @@ Context:
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#if !defined(NDEBUG) #ifndef GB_ARRAY_BOUND_CHECKING
#define GB_ASSERT(x, ...) ((void)(::gb__assert_handler((x), #x, __FILE__, __LINE__, ##__VA_ARGS__)))
#else
#define GB_ASSERT(x, ...) ((void)sizeof(x))
#endif
#if !defined(GB_ARRAY_BOUND_CHECKING)
#define GB_ARRAY_BOUND_CHECKING 1 #define GB_ARRAY_BOUND_CHECKING 1
#endif #endif
#ifndef GB_DISABLE_COPY
#define GB_DISABLE_COPY(Type) \ #define GB_DISABLE_COPY(Type) \
Type(const Type&) = delete; \ Type(const Type&) = delete; \
Type& operator=(const Type&) = delete Type& operator=(const Type&) = delete
#endif
#if !defined(GB_ASSERT)
#if !defined(NDEBUG)
#define GB_ASSERT(x, ...) ((void)(::gb__assert_handler((x), #x, __FILE__, __LINE__, ##__VA_ARGS__)))
/// Helper function used as a better alternative to assert which allows for /// Helper function used as a better alternative to assert which allows for
/// optional printf style error messages /// optional printf style error messages
@ -265,13 +283,17 @@ gb__assert_handler(bool condition, const char* condition_str,
*(int*)0 = 0; *(int*)0 = 0;
} }
#else
#define GB_ASSERT(x, ...) ((void)sizeof(x))
#endif
#endif
//////////////////////////////// ////////////////////////////////
/// /// /// ///
/// snprintf_msvc /// /// snprintf_msvc ///
/// /// /// ///
//////////////////////////////// ////////////////////////////////
#if defined(_MSC_VER) #if defined(_MSC_VER)
extern "C" inline int extern "C" inline int
gb__vsnprintf_compatible(char* buffer, size_t size, const char* format, va_list args) gb__vsnprintf_compatible(char* buffer, size_t size, const char* format, va_list args)
{ {
@ -298,7 +320,6 @@ gb__snprintf_compatible(char* buffer, size_t size, const char* format, ...)
#define snprintf gb__snprintf_compatible #define snprintf gb__snprintf_compatible
#define vsnprintf gb__vsnprintf_compatible #define vsnprintf gb__vsnprintf_compatible
#endif // GB_DO_NOT_USE_MSVC_SPRINTF_FIX #endif // GB_DO_NOT_USE_MSVC_SPRINTF_FIX
#endif #endif
#if !defined(__GB_NAMESPACE_PREFIX) && !defined(GB_NO_GB_NAMESPACE) #if !defined(__GB_NAMESPACE_PREFIX) && !defined(GB_NO_GB_NAMESPACE)
@ -327,7 +348,8 @@ __GB_NAMESPACE_START
//////////////////////////////// ////////////////////////////////
#ifndef GB_BASIC_TYPES
#define GB_BASIC_TYPES
#if defined(_MSC_VER) #if defined(_MSC_VER)
using u8 = unsigned __int8; using u8 = unsigned __int8;
using s8 = signed __int8; using s8 = signed __int8;
@ -390,6 +412,9 @@ using uintptr = uintptr_t;
using ptrdiff = ptrdiff_t; using ptrdiff = ptrdiff_t;
#endif
#if !defined(GB_U8_MIN)
#define GB_U8_MIN 0u #define GB_U8_MIN 0u
#define GB_U8_MAX 0xffu #define GB_U8_MAX 0xffu
#define GB_S8_MIN (-0x7f - 1) #define GB_S8_MIN (-0x7f - 1)
@ -409,14 +434,15 @@ using ptrdiff = ptrdiff_t;
#define GB_U64_MAX 0xffffffffffffffffull #define GB_U64_MAX 0xffffffffffffffffull
#define GB_S64_MIN (-0x7fffffffffffffffll - 1) #define GB_S64_MIN (-0x7fffffffffffffffll - 1)
#define GB_S64_MAX 0x7fffffffffffffffll #define GB_S64_MAX 0x7fffffffffffffffll
#endif
#if defined(GB_ARCH_64_BIT) #if defined(GB_ARCH_64_BIT) && !defined(GB_USIZE_MIX)
#define GB_USIZE_MIX GB_U64_MIN #define GB_USIZE_MIX GB_U64_MIN
#define GB_USIZE_MAX GB_U64_MAX #define GB_USIZE_MAX GB_U64_MAX
#define GB_SSIZE_MIX GB_S64_MIN #define GB_SSIZE_MIX GB_S64_MIN
#define GB_SSIZE_MAX GB_S64_MAX #define GB_SSIZE_MAX GB_S64_MAX
#elif defined(GB_ARCH_32_BIT) #elif defined(GB_ARCH_32_BIT) && !defined(GB_USIZE_MIX)
#define GB_USIZE_MIX GB_U32_MIN #define GB_USIZE_MIX GB_U32_MIN
#define GB_USIZE_MAX GB_U32_MAX #define GB_USIZE_MAX GB_U32_MAX
@ -424,7 +450,7 @@ using ptrdiff = ptrdiff_t;
#define GB_SSIZE_MAX GB_S32_MAX #define GB_SSIZE_MAX GB_S32_MAX
#endif #endif
#if defined(GB_BASIC_WITHOUT_NAMESPACE) #if defined(GB_BASIC_WITHOUT_NAMESPACE) && !defined(U8_MIN)
#define U8_MIN 0u #define U8_MIN 0u
#define U8_MAX 0xffu #define U8_MAX 0xffu
#define S8_MIN (-0x7f - 1) #define S8_MIN (-0x7f - 1)
@ -445,13 +471,13 @@ using ptrdiff = ptrdiff_t;
#define S64_MIN (-0x7fffffffffffffffll - 1) #define S64_MIN (-0x7fffffffffffffffll - 1)
#define S64_MAX 0x7fffffffffffffffll #define S64_MAX 0x7fffffffffffffffll
#if defined(GB_ARCH_64_BIT) #if defined(GB_ARCH_64_BIT) && !defined(GB_USIZE_MIX)
#define USIZE_MIX U64_MIN #define USIZE_MIX U64_MIN
#define USIZE_MAX U64_MAX #define USIZE_MAX U64_MAX
#define SSIZE_MIX S64_MIN #define SSIZE_MIX S64_MIN
#define SSIZE_MAX S64_MAX #define SSIZE_MAX S64_MAX
#elif defined(GB_ARCH_32_BIT) #elif defined(GB_ARCH_32_BIT) && !defined(GB_USIZE_MIX)
#define USIZE_MIX U32_MIN #define USIZE_MIX U32_MIN
#define USIZE_MAX U32_MAX #define USIZE_MAX U32_MAX
@ -564,12 +590,16 @@ move(T&& t)
{ {
return static_cast<Remove_Reference<T>&&>(t); return static_cast<Remove_Reference<T>&&>(t);
} }
__GB_NAMESPACE_END
//////////////////////////////// ////////////////////////////////
/// /// /// ///
/// Defer /// /// Defer ///
/// /// /// ///
//////////////////////////////// ////////////////////////////////
#ifndef GB_DEFER
#define GB_DEFER
__GB_NAMESPACE_START
namespace impl namespace impl
{ {
template <typename Func> template <typename Func>
@ -592,11 +622,14 @@ __GB_NAMESPACE_END
#define GB_DEFER_2(x, y) GB_DEFER_1(x, y) #define GB_DEFER_2(x, y) GB_DEFER_1(x, y)
#define GB_DEFER_3(x) GB_DEFER_2(GB_DEFER_2(GB_DEFER_2(x, __COUNTER__), _), __LINE__) #define GB_DEFER_3(x) GB_DEFER_2(GB_DEFER_2(GB_DEFER_2(x, __COUNTER__), _), __LINE__)
#define defer(code) auto GB_DEFER_3(_defer_) = __GB_NAMESPACE_PREFIX::impl::defer_func([&](){code;}) #define defer(code) auto GB_DEFER_3(_defer_) = __GB_NAMESPACE_PREFIX::impl::defer_func([&](){code;})
#endif
#if !defined(GB_CASTS_WITHOUT_NAMESPACE) #if !defined(GB_CASTS_WITHOUT_NAMESPACE)
__GB_NAMESPACE_START __GB_NAMESPACE_START
#endif // GB_CASTS_WITHOUT_NAMESPACE #endif // GB_CASTS_WITHOUT_NAMESPACE
#ifndef GB_SPECIAL_CASTS
#define GB_SPECIAL_CASTS
// IMPORTANT NOTE(bill): Very similar to doing `*(T*)(&u)` but easier/clearer to write // IMPORTANT NOTE(bill): Very similar to doing `*(T*)(&u)` but easier/clearer to write
// however, it can be dangerous if sizeof(T) > sizeof(U) e.g. unintialized memory, undefined behavior // however, it can be dangerous if sizeof(T) > sizeof(U) e.g. unintialized memory, undefined behavior
// *(T*)(&u) ~~ pseudo_cast<T>(u) // *(T*)(&u) ~~ pseudo_cast<T>(u)
@ -618,6 +651,7 @@ bit_cast(const Source& source)
::memcpy(&dest, &source, sizeof(Dest)); ::memcpy(&dest, &source, sizeof(Dest));
return dest; return dest;
} }
#endif
// FORENOTE(bill): There used to be a magic_cast that was equivalent to // FORENOTE(bill): There used to be a magic_cast that was equivalent to
// a C-style cast but I removed it as I could not get it work as intented // a C-style cast but I removed it as I could not get it work as intented