platformTools.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. //note this should be included last
  3. #define NOMINMAX
  4. #include "config.h"
  5. #include <string.h>
  6. #include <cstring>
  7. inline size_t constexpr KB(size_t x) { return x * 1024ull; }
  8. inline size_t constexpr MB(size_t x) { return KB(x) * 1024ull; }
  9. inline size_t constexpr GB(size_t x) { return MB(x) * 1024ull; }
  10. inline size_t constexpr TB(size_t x) { return GB(x) * 1024ull; }
  11. inline float constexpr BYTES_TO_KB(size_t x) { return x / 1024.f; }
  12. inline float constexpr BYTES_TO_MB(size_t x) { return BYTES_TO_KB(x) / 1024.f; }
  13. inline float constexpr BYTES_TO_GB(size_t x) { return BYTES_TO_MB(x) / 1024.f; }
  14. #define REMOVE_IMGUI 0
  15. void assertFuncProduction(
  16. const char *expression,
  17. const char *file_name,
  18. unsigned const line_number,
  19. const char *comment = "---");
  20. void assertFuncInternal(
  21. const char *expression,
  22. const char *file_name,
  23. unsigned const line_number,
  24. const char *comment = "---");
  25. #if INTERNAL_BUILD == 1
  26. #define permaAssert(expression) (void)( \
  27. (!!(expression)) || \
  28. (assertFuncInternal(#expression, __FILE__, (unsigned)(__LINE__)), 0) \
  29. )
  30. #define permaAssertComment(expression, comment) (void)( \
  31. (!!(expression)) || \
  32. (assertFuncInternal(#expression, __FILE__, (unsigned)(__LINE__), comment), 1)\
  33. )
  34. #else
  35. #define permaAssert(expression) (void)( \
  36. (!!(expression)) || \
  37. (assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__)), 0) \
  38. )
  39. #define permaAssertComment(expression, comment) (void)( \
  40. (!!(expression)) || \
  41. (assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__), comment), 1) \
  42. )
  43. #endif
  44. #include <functional>
  45. //raii stuff, it will basically call the function that you pass to it be called at scope end, usage: defer(func());
  46. struct DeferImpl
  47. {
  48. public:
  49. explicit DeferImpl(std::function<void()> func): func_(std::move(func)) {}
  50. ~DeferImpl() { func_(); }
  51. std::function<void()> func_;
  52. };
  53. #define CONCATENATE_DEFFER(x, y) x##y
  54. #define MAKE_UNIQUE_VAR_DEFFER(x, y) CONCATENATE_DEFFER(x, y)
  55. #define defer(func) DeferImpl MAKE_UNIQUE_VAR_DEFFER(_defer_, __COUNTER__)(func)
  56. #if INTERNAL_BUILD == 1
  57. #define permaAssertDevelopement permaAssert
  58. #define permaAssertCommentDevelopement permaAssertComment
  59. #else
  60. #define permaAssertDevelopement
  61. #define permaAssertCommentDevelopement
  62. #endif