Test.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Defines macros used for statically verifying the JoltC bindings against the
  2. // Jolt Physics C++ types.
  3. //
  4. // When the JoltC.h header is normally included, these macros are defined to do
  5. // nothing.
  6. #include <assert.h>
  7. #include <stddef.h>
  8. #include <type_traits>
  9. #include <Jolt/Jolt.h>
  10. #include <Jolt/Core/Factory.h>
  11. #include <Jolt/Core/JobSystemThreadPool.h>
  12. #include <Jolt/Core/TempAllocator.h>
  13. #include <Jolt/Physics/Body/BodyActivationListener.h>
  14. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  15. #include <Jolt/Physics/Body/BodyManager.h>
  16. #include <Jolt/Physics/Collision/ActiveEdgeMode.h>
  17. #include <Jolt/Physics/Collision/CollectFacesMode.h>
  18. #include <Jolt/Physics/Collision/CollideShape.h>
  19. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  20. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  21. #include <Jolt/Physics/PhysicsSettings.h>
  22. #include <Jolt/Physics/PhysicsSystem.h>
  23. #include <Jolt/RegisterTypes.h>
  24. template<typename E>
  25. constexpr auto to_integral(E e) -> typename std::underlying_type<E>::type
  26. {
  27. return static_cast<typename std::underlying_type<E>::type>(e);
  28. }
  29. #define ENSURE_TESTS
  30. // Ensures that a constant value defined in JoltC doesn't drift out of sync with
  31. // its Jolt Physics C++ counterpart.
  32. #define ENSURE_EQUAL(c_const, cpp_const) \
  33. static_assert(c_const == cpp_const, #c_const " did not match " #cpp_const); \
  34. static_assert(std::is_same_v<decltype(c_const), decltype(cpp_const)>, "type of " #c_const " did not match type of " #cpp_const);
  35. // Ensures that a C constant that represents an enum value matches the integral
  36. // value of a C++ enum.
  37. #define ENSURE_ENUM_EQ(c_const, cpp_enum) \
  38. static_assert(c_const == to_integral(cpp_enum), #c_const " did not match " #cpp_enum); \
  39. static_assert(sizeof(c_const) == sizeof(cpp_enum), #c_const " did not have same size as " #cpp_enum);
  40. // Ensures that a C struct we define is compatible in layout with its
  41. // corresponding C++ type, and that the C++ type is safe to represent in C.
  42. #define ENSURE_SIZE_ALIGN(c_type, cpp_type) \
  43. static_assert(sizeof(c_type) == sizeof(cpp_type), "size of " #c_type " did not match size of " #cpp_type); \
  44. static_assert(alignof(c_type) == alignof(cpp_type), "align of " #c_type " did not match align of " #cpp_type); \
  45. static_assert(!std::is_polymorphic_v<cpp_type>, #cpp_type " is polymorphic and cannot be mirrored");
  46. // Internal macro to get the type of a struct field by name.
  47. #define unsafe_fieldtype(st, m) decltype(((st *)(0))->m)
  48. // Ensures that the field offset, size, and alignment of two fields from two
  49. // different structs are identical.
  50. //
  51. // Comparing the offsets and alignments of each field helps provide some guarantee that
  52. #define ENSURE_FIELD(type0, field0, type1, field1) \
  53. static_assert(offsetof(type0, field0) == offsetof(type1, field1), \
  54. #type0 "." #field0 " did not have same offset as " #type1 "." #field1); \
  55. ENSURE_SIZE_ALIGN(unsafe_fieldtype(type0, field0), unsafe_fieldtype(type1, field1)) \
  56. // Just like ENSURE_FIELD, but follows the normal conventions of both JoltC and
  57. // Jolt Physics to reduce boilerplate.
  58. //
  59. // JoltC's types begin with JPC_ while Jolt's types are in the JPH namespace.
  60. // Jolt prefixes public members with 'm' but JoltC strips that name away.
  61. #define ENSURE_NORMAL_FIELD(basetype, basefield) \
  62. ENSURE_FIELD(JPC_ ## basetype, basefield, JPH:: ## basetype, m ## basefield);
  63. #include "JoltC/JoltC.h"