TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_defines.hpp
1 
8 #ifndef TurtleBrains_Defines_hpp
9 #define TurtleBrains_Defines_hpp
10 
11 #include <limits>
12 #include <type_traits>
13 
14 #include <turtle_brains/core/tb_configuration.hpp>
15 #include <turtle_brains/core/tb_error.hpp>
16 
21 
25 #define tb_unused(parameter) ((void)parameter);
26 
27 namespace TurtleBrains
28 {
29  namespace Core
30  {
35  template <typename Type> void SafeDelete(Type*& objectPointer) { if (nullptr != objectPointer) { delete objectPointer; objectPointer = nullptr; } }
36 
41  template <typename Type> void SafeDeleteArray(Type*& arrayPointer) { if (nullptr != arrayPointer) { delete[] arrayPointer; arrayPointer = nullptr; } }
42 
43 
52  template <typename ReturnType, typename InitialType> ReturnType RangedCast(const InitialType& value)
53  {
54 #if defined(tb_visual_cpp)
55  #pragma warning(push)
56  //We are removing 2 warnings here:
57  // 4127: Constant Expression with first part of if, because it is constant ... but we only care about that at
58  // compile time, so it doesn't matter. The C++17 if constexpr (expression) could fix this (and the next).
59  // 4018: Signed/Unsigned Mismatch with the second part (inside the top if statement) because the compiler is
60  // still generating the code for the error checks, and the signs are mismatching. We just don't care since
61  // the code flow would jump to the bottom when those warnings would apply.
62  // Note: Both of these should be fixed in C++17 by using if constexpr () since that would let the compiler
63  // see the actual intent, and remove the error check completely when signs are mismatched.
64  #pragma warning(disable: 4127)
65  #pragma warning(disable: 4018)
66 #elif defined(__GNUC__)
67  #pragma GCC diagnostic push
68  #pragma GCC diagnostic ignored "-Wsign-compare"
69 #endif
70  if ((std::is_signed<ReturnType>::value == std::is_signed<InitialType>::value))
71  {
72  tb_error_if(value < std::numeric_limits<ReturnType>::min() || value > std::numeric_limits<ReturnType>::max(),
73  "tbCastingError: Value is outside the range of the following cast.");
74  }
75  else if (std::is_signed<InitialType>::value)
76  {
77  tb_error_if(value < 0, "tbCastingError: Signed value is less than 0 going to unsigned-value.");
78  }
79 #if defined(tb_visual_cpp)
80  #pragma warning(pop)
81 #elif defined(__GNUC__)
82  #pragma GCC diagnostic pop
83 #endif
84  return static_cast<ReturnType>(value);
85  }
86 
98  template <typename ReturnType, typename InitialType> ReturnType ClampedCast(const InitialType& value, const ReturnType& minimum, const ReturnType& maximum)
99  {
100  tb_error_if(minimum > maximum, "tbError: Minimum should never be larger than Maximum.");
101  ReturnType unclamped = RangedCast<ReturnType>(value);
102  if (unclamped < minimum)
103  {
104  return minimum;
105  }
106  if (unclamped > maximum)
107  {
108  return maximum;
109  }
110  return unclamped;
111  }
112 
113  }; /* namespace Core */
114 
115 }; /* namespace TurtleBrains */
116 
117 namespace tbCore = TurtleBrains::Core;
118 
122 
123 #endif /* TurtleBrains_Defines_hpp */
ReturnType RangedCast(const InitialType &value)
Definition: tb_defines.hpp:52
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:21
ReturnType ClampedCast(const InitialType &value, const ReturnType &minimum, const ReturnType &maximum)
Definition: tb_defines.hpp:98
void SafeDeleteArray(Type *&arrayPointer)
Definition: tb_defines.hpp:41
void SafeDelete(Type *&objectPointer)
Definition: tb_defines.hpp:35
Contains core functionality for each component of the API.
Definition: tb_debug_logger.hpp:88
#define tb_error_if(errorTest, message,...)
Definition: tb_error.hpp:42