2
0

Test.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/EstimateCollisionResponse.h>
  20. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  21. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  22. #include <Jolt/Physics/Collision/ShapeCast.h>
  23. #include <Jolt/Physics/Constraints/ConstraintPart/SwingTwistConstraintPart.h>
  24. #include <Jolt/Physics/Constraints/SixDOFConstraint.h>
  25. #include <Jolt/Physics/PhysicsSettings.h>
  26. #include <Jolt/Physics/PhysicsSystem.h>
  27. #include <Jolt/RegisterTypes.h>
  28. template<typename E>
  29. constexpr auto to_integral(E e) -> typename std::underlying_type<E>::type
  30. {
  31. return static_cast<typename std::underlying_type<E>::type>(e);
  32. }
  33. #define ENSURE_TESTS
  34. // Ensures that a constant value defined in JoltC doesn't drift out of sync with
  35. // its Jolt Physics C++ counterpart.
  36. #define ENSURE_EQUAL(c_const, cpp_const) \
  37. static_assert(c_const == cpp_const, #c_const " did not match " #cpp_const); \
  38. static_assert(std::is_same_v<decltype(c_const), decltype(cpp_const)>, "type of " #c_const " did not match type of " #cpp_const);
  39. // Ensures that a C constant that represents an enum value matches the integral
  40. // value of a C++ enum.
  41. #define ENSURE_ENUM_EQ(c_const, cpp_enum) \
  42. static_assert(c_const == to_integral(cpp_enum), #c_const " did not match " #cpp_enum); \
  43. static_assert(sizeof(c_const) == sizeof(cpp_enum), #c_const " did not have same size as " #cpp_enum);
  44. // Ensures that a C struct we define is compatible in layout with its
  45. // corresponding C++ type, and that the C++ type is safe to represent in C.
  46. #define ENSURE_SIZE_ALIGN(c_type, cpp_type) \
  47. static_assert(sizeof(c_type) == sizeof(cpp_type), "size of " #c_type " did not match size of " #cpp_type); \
  48. static_assert(alignof(c_type) == alignof(cpp_type), "align of " #c_type " did not match align of " #cpp_type); \
  49. static_assert(!std::is_polymorphic_v<cpp_type>, #cpp_type " is polymorphic and cannot be mirrored");
  50. // Internal macro to get the type of a struct field by name.
  51. #define unsafe_fieldtype(st, m) decltype(((st *)(0))->m)
  52. // Ensures that the field offset, size, and alignment of two fields from two
  53. // different structs are identical.
  54. //
  55. // Comparing the offsets and alignments of each field helps provide some
  56. // guarantee that a manually-reflected C struct has the same layout as its C++
  57. // counterpart.
  58. #define ENSURE_FIELD(type0, field0, type1, field1) \
  59. static_assert(offsetof(type0, field0) == offsetof(type1, field1), \
  60. #type0 "." #field0 " did not have same offset as " #type1 "." #field1); \
  61. ENSURE_SIZE_ALIGN(unsafe_fieldtype(type0, field0), unsafe_fieldtype(type1, field1)) \
  62. // Just like ENSURE_FIELD, but follows the normal conventions of both JoltC and
  63. // Jolt Physics to reduce boilerplate.
  64. //
  65. // JoltC's types begin with JPC_ while Jolt's types are in the JPH namespace.
  66. // Jolt prefixes public members with 'm' but JoltC strips that name away.
  67. #define ENSURE_NORMAL_FIELD(basetype, basefield) \
  68. ENSURE_FIELD(JPC_ ## basetype, basefield, JPH::basetype, m ## basefield);
  69. #define const constexpr // 🫡
  70. #include "JoltC/Enums.h"
  71. #undef const
  72. #include "JoltC/Functions.h"