[gjs] arg-inl: Use more auto-generated code avoiding typed parameters



commit 72ac583301bdd8281dee297b8b860a5b35b33f87
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Wed May 12 12:25:01 2021 +0200

    arg-inl: Use more auto-generated code avoiding typed parameters
    
    Using c++17 auto support for template arguments we can use a static
    template member parameter to access to GIArgument member, in this way we
    create the code at compilation time without having to rely on defining
    further parameters and temporary variables.

 gi/arg-inl.h | 47 +++++++++++++++++++++++------------------------
 1 file changed, 23 insertions(+), 24 deletions(-)
---
diff --git a/gi/arg-inl.h b/gi/arg-inl.h
index e226b48b..583980fa 100644
--- a/gi/arg-inl.h
+++ b/gi/arg-inl.h
@@ -38,9 +38,8 @@
 // gjs_arg_steal<T>(GIArgument*) - sets the appropriate zero value in the
 //   appropriate union member for type T and returns the replaced value.
 
-template <typename T>
-[[nodiscard]] inline decltype(auto) gjs_arg_member(GIArgument* arg,
-                                                   T GIArgument::*member) {
+template <auto GIArgument::*member>
+[[nodiscard]] inline decltype(auto) gjs_arg_member(GIArgument* arg) {
     return (arg->*member);
 }
 
@@ -51,67 +50,67 @@ template <typename T, GITypeTag TAG = GI_TYPE_TAG_VOID>
 [[nodiscard]] inline decltype(auto) gjs_arg_member(GIArgument* arg) {
     if constexpr (TAG == GI_TYPE_TAG_VOID) {
         if constexpr (std::is_same_v<T, bool>)
-            return gjs_arg_member(arg, &GIArgument::v_boolean);
+            return gjs_arg_member<&GIArgument::v_boolean>(arg);
         if constexpr (std::is_same_v<T, int8_t>)
-            return gjs_arg_member(arg, &GIArgument::v_int8);
+            return gjs_arg_member<&GIArgument::v_int8>(arg);
         if constexpr (std::is_same_v<T, uint8_t>)
-            return gjs_arg_member(arg, &GIArgument::v_uint8);
+            return gjs_arg_member<&GIArgument::v_uint8>(arg);
         if constexpr (std::is_same_v<T, int16_t>)
-            return gjs_arg_member(arg, &GIArgument::v_int16);
+            return gjs_arg_member<&GIArgument::v_int16>(arg);
         if constexpr (std::is_same_v<T, uint16_t>)
-            return gjs_arg_member(arg, &GIArgument::v_uint16);
+            return gjs_arg_member<&GIArgument::v_uint16>(arg);
         if constexpr (std::is_same_v<T, int32_t>)
-            return gjs_arg_member(arg, &GIArgument::v_int32);
+            return gjs_arg_member<&GIArgument::v_int32>(arg);
         if constexpr (std::is_same_v<T, uint32_t>)
-            return gjs_arg_member(arg, &GIArgument::v_uint32);
+            return gjs_arg_member<&GIArgument::v_uint32>(arg);
         if constexpr (std::is_same_v<T, int64_t>)
-            return gjs_arg_member(arg, &GIArgument::v_int64);
+            return gjs_arg_member<&GIArgument::v_int64>(arg);
         if constexpr (std::is_same_v<T, uint64_t>)
-            return gjs_arg_member(arg, &GIArgument::v_uint64);
+            return gjs_arg_member<&GIArgument::v_uint64>(arg);
 
         // gunichar is stored in v_uint32
         if constexpr (std::is_same_v<T, char32_t>)
-            return gjs_arg_member(arg, &GIArgument::v_uint32);
+            return gjs_arg_member<&GIArgument::v_uint32>(arg);
 
         if constexpr (std::is_same_v<T, float>)
-            return gjs_arg_member(arg, &GIArgument::v_float);
+            return gjs_arg_member<&GIArgument::v_float>(arg);
 
         if constexpr (std::is_same_v<T, double>)
-            return gjs_arg_member(arg, &GIArgument::v_double);
+            return gjs_arg_member<&GIArgument::v_double>(arg);
 
         if constexpr (std::is_same_v<T, char*>)
-            return gjs_arg_member(arg, &GIArgument::v_string);
+            return gjs_arg_member<&GIArgument::v_string>(arg);
 
         if constexpr (std::is_same_v<T, void*>)
-            return gjs_arg_member(arg, &GIArgument::v_pointer);
+            return gjs_arg_member<&GIArgument::v_pointer>(arg);
 
         if constexpr (std::is_same_v<T, std::nullptr_t>)
-            return gjs_arg_member(arg, &GIArgument::v_pointer);
+            return gjs_arg_member<&GIArgument::v_pointer>(arg);
 
         if constexpr (std::is_pointer<T>()) {
             using NonconstPtrT = std::add_pointer_t<
                 std::remove_const_t<std::remove_pointer_t<T>>>;
             return reinterpret_cast<NonconstPtrT&>(
-                gjs_arg_member(arg, &GIArgument::v_pointer));
+                gjs_arg_member<&GIArgument::v_pointer>(arg));
         }
     }
 
     if constexpr (TAG == GI_TYPE_TAG_BOOLEAN && std::is_same_v<T, gboolean>)
-        return gjs_arg_member(arg, &GIArgument::v_boolean);
+        return gjs_arg_member<&GIArgument::v_boolean>(arg);
 
     if constexpr (TAG == GI_TYPE_TAG_GTYPE && std::is_same_v<T, GType>) {
         // GType is defined differently on 32-bit vs. 64-bit architectures.
         if constexpr (std::is_same_v<GType, gsize>)
-            return gjs_arg_member(arg, &GIArgument::v_size);
+            return gjs_arg_member<&GIArgument::v_size>(arg);
         else if constexpr (std::is_same_v<GType, gulong>)
-            return gjs_arg_member(arg, &GIArgument::v_ulong);
+            return gjs_arg_member<&GIArgument::v_ulong>(arg);
     }
 
     if constexpr (TAG == GI_TYPE_TAG_INTERFACE && std::is_integral_v<T>) {
         if constexpr (std::is_signed_v<T>)
-            return gjs_arg_member(arg, &GIArgument::v_int);
+            return gjs_arg_member<&GIArgument::v_int>(arg);
         else
-            return gjs_arg_member(arg, &GIArgument::v_uint);
+            return gjs_arg_member<&GIArgument::v_uint>(arg);
     }
 }
 


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