Selaa lähdekoodia

More robust size-align checking, add VertexList

Lucien Greathouse 1 vuosi sitten
vanhempi
commit
9696fd593e
3 muutettua tiedostoa jossa 38 lisäystä ja 8 poistoa
  1. 21 5
      JoltC/Functions.h
  2. 11 0
      JoltC/JoltC.cpp
  3. 6 3
      JoltC/Test.cpp

+ 21 - 5
JoltC/Functions.h

@@ -26,6 +26,14 @@ JPC_API void JPC_UnregisterTypes();
 ////////////////////////////////////////////////////////////////////////////////
 // Primitive types
 
+typedef struct JPC_Float3 {
+	float x;
+	float y;
+	float z;
+} JPC_Float3;
+
+ENSURE_SIZE_ALIGN(JPC_Float3, JPH::Float3)
+
 typedef struct JPC_Vec3 {
 	alignas(16) float x;
 	float y;
@@ -33,7 +41,7 @@ typedef struct JPC_Vec3 {
 	float _w;
 } JPC_Vec3;
 
-ENSURE_SIZE_ALIGN(JPC_Vec3, JPH::Vec3);
+ENSURE_SIZE_ALIGN(JPC_Vec3, JPH::Vec3)
 
 typedef struct JPC_DVec3 {
 	alignas(32) double x;
@@ -42,7 +50,7 @@ typedef struct JPC_DVec3 {
 	double _w;
 } JPC_DVec3;
 
-ENSURE_SIZE_ALIGN(JPC_DVec3, JPH::DVec3);
+ENSURE_SIZE_ALIGN(JPC_DVec3, JPH::DVec3)
 
 typedef struct JPC_Quat {
 	alignas(16) float x;
@@ -51,7 +59,7 @@ typedef struct JPC_Quat {
 	float w;
 } JPC_Quat;
 
-ENSURE_SIZE_ALIGN(JPC_Quat, JPH::Quat);
+ENSURE_SIZE_ALIGN(JPC_Quat, JPH::Quat)
 
 #ifdef JPC_DOUBLE_PRECISION
 	typedef JPC_DVec3 JPC_RVec3;
@@ -61,10 +69,10 @@ ENSURE_SIZE_ALIGN(JPC_Quat, JPH::Quat);
 	typedef float Real;
 #endif
 
-ENSURE_SIZE_ALIGN(JPC_RVec3, JPH::RVec3);
+ENSURE_SIZE_ALIGN(JPC_RVec3, JPH::RVec3)
 
 typedef uint32_t JPC_BodyID;
-ENSURE_SIZE_ALIGN(JPC_BodyID, JPH::BodyID);
+ENSURE_SIZE_ALIGN(JPC_BodyID, JPH::BodyID)
 
 typedef uint8_t JPC_BroadPhaseLayer;
 ENSURE_SIZE_ALIGN(JPC_BroadPhaseLayer, JPH::BroadPhaseLayer)
@@ -83,6 +91,14 @@ ENSURE_SIZE_ALIGN(JPC_BroadPhaseLayer, JPH::BroadPhaseLayer)
 
 ENSURE_SIZE_ALIGN(JPC_ObjectLayer, JPH::ObjectLayer)
 
+////////////////////////////////////////////////////////////////////////////////
+// VertexList == Array<Float3> == std::vector<Float3>
+
+typedef struct JPC_VertexList JPC_VertexList;
+
+JPC_API JPC_VertexList* JPC_VertexList_new(const JPC_Float3* storage, size_t len);
+JPC_API void JPC_VertexList_delete(JPC_VertexList* object);
+
 ////////////////////////////////////////////////////////////////////////////////
 // TempAllocatorImpl
 

+ 11 - 0
JoltC/JoltC.cpp

@@ -54,6 +54,9 @@ OPAQUE_WRAPPER(JPC_ShapeSettings, JPH::ShapeSettings)
 OPAQUE_WRAPPER(JPC_Shape, JPH::Shape)
 OPAQUE_WRAPPER(JPC_Body, JPH::Body)
 
+OPAQUE_WRAPPER(JPC_VertexList, JPH::VertexList)
+DESTRUCTOR(JPC_VertexList)
+
 OPAQUE_WRAPPER(JPC_String, JPH::String)
 DESTRUCTOR(JPC_String)
 
@@ -115,6 +118,14 @@ JPC_API void JPC_UnregisterTypes() {
 	JPH::UnregisterTypes();
 }
 
+////////////////////////////////////////////////////////////////////////////////
+// VertexList == Array<Float3> == std::vector<Float3>
+
+JPC_API JPC_VertexList* JPC_VertexList_new(const JPC_Float3* storage, size_t len) {
+	const JPH::Float3* new_storage = (const JPH::Float3*)storage;
+	return to_jpc(new JPH::VertexList(new_storage, new_storage + len));
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // TempAllocatorImpl
 

+ 6 - 3
JoltC/Test.cpp

@@ -31,9 +31,12 @@ constexpr auto to_integral(E e) -> typename std::underlying_type<E>::type
     static_assert(c_const == to_integral(cpp_enum), #c_const " did not match " #cpp_enum); \
     static_assert(sizeof(c_const) == sizeof(cpp_enum), #c_const " did not have same size as " #cpp_enum);
 
-#define ENSURE_SIZE_ALIGN(type0, type1) \
-    static_assert(sizeof(type0) == sizeof(type1), "size of " #type0 " did not match size of " #type1); \
-    static_assert(alignof(type0) == alignof(type1), "align of " #type0 " did not match align of " #type1);
+// Ensures that a C struct we define is compatible in layout with its
+// corresponding C++ type, and that the C++ type is safe to represent in C.
+#define ENSURE_SIZE_ALIGN(c_type, cpp_type) \
+    static_assert(sizeof(c_type) == sizeof(cpp_type), "size of " #c_type " did not match size of " #cpp_type); \
+    static_assert(alignof(c_type) == alignof(cpp_type), "align of " #c_type " did not match align of " #cpp_type); \
+    static_assert(std::is_standard_layout<cpp_type>::value, #cpp_type " is not standard layout");
 
 #define unsafe_offsetof(st, m) ((size_t) ( (char *)&((st *)(0))->m - (char *)0 ))
 #define unsafe_fieldtype(st, m) decltype((st *)(0)->m)