[gjs] arg: Use a template function to fill gjs array from C vector



commit 8e9ed469d966f7bc037e814c7427bcd5f4d7a5d9
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Fri May 8 17:20:38 2020 +0200

    arg: Use a template function to fill gjs array from C vector
    
    This allows to generate code statically checked by gcc with proper type
    checks

 gi/arg.cpp | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index e30b2fa4..056b50da 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -2442,6 +2442,20 @@ gjs_array_from_g_list (JSContext             *context,
     return true;
 }
 
+template <typename T, T GIArgument::*member>
+GJS_JSAPI_RETURN_CONVENTION static bool fill_vector_from_carray(
+    JSContext* cx, JS::RootedValueVector& elems,  // NOLINT(runtime/references)
+    GITypeInfo* param_info, GIArgument* arg, void* array, size_t length) {
+    for (size_t i = 0; i < length; i++) {
+        arg->*member = *(static_cast<T*>(array) + i);
+
+        if (!gjs_value_from_g_argument(cx, elems[i], param_info, arg, true))
+            return false;
+    }
+
+    return true;
+}
+
 GJS_JSAPI_RETURN_CONVENTION
 static bool
 gjs_array_from_carray_internal (JSContext             *context,
@@ -2488,13 +2502,10 @@ gjs_array_from_carray_internal (JSContext             *context,
         return false;
     }
 
-#define ITERATE(type) \
-    for (i = 0; i < length; i++) { \
-        arg.v_##type = *(((g##type*)array) + i);                         \
-        if (!gjs_value_from_g_argument(context, elems[i], param_info,    \
-                                       &arg, true))                      \
-            return false; \
-    }
+#define ITERATE(type)                                             \
+    if (!fill_vector_from_carray<g##type, &GIArgument::v_##type>( \
+            context, elems, param_info, &arg, array, length))     \
+        return false;
 
     switch (element_type) {
         /* Special cases handled above */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]