[gjs] arg: Use if constexpr to simplify boolean specializations on set



commit 7b24910103995d14cfc37fa6b041e3e2eecb5dc4
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Sun Aug 9 02:30:50 2020 +0200

    arg: Use if constexpr to simplify boolean specializations on set
    
    When setting and getting booleans we have some template specializations
    that we can now simplify using constexpr checks

 gi/arg-inl.h | 30 ++++++++----------------------
 1 file changed, 8 insertions(+), 22 deletions(-)
---
diff --git a/gi/arg-inl.h b/gi/arg-inl.h
index 588bb814..952260c5 100644
--- a/gi/arg-inl.h
+++ b/gi/arg-inl.h
@@ -120,6 +120,10 @@ inline void gjs_arg_set(GIArgument* arg, T v) {
             std::add_pointer_t<std::remove_const_t<std::remove_pointer_t<T>>>;
         gjs_arg_member<NonconstPtrT, TAG>(arg) = const_cast<NonconstPtrT>(v);
     } else {
+        if constexpr (std::is_same_v<T, bool> || (std::is_same_v<T, gboolean> &&
+                                                  TAG == GI_TYPE_TAG_BOOLEAN))
+            v = !!v;
+
         gjs_arg_member<T, TAG>(arg) = v;
     }
 }
@@ -137,31 +141,13 @@ inline std::enable_if_t<std::is_integral_v<T>> gjs_arg_set(GIArgument* arg,
     gjs_arg_set<T, TAG>(arg, gjs_pointer_to_int<T>(v));
 }
 
-template <>
-inline void gjs_arg_set<bool>(GIArgument* arg, bool v) {
-    gjs_arg_member<bool>(arg) = !!v;
-}
-
-template <>
-inline void gjs_arg_set<gboolean, GI_TYPE_TAG_BOOLEAN>(GIArgument* arg,
-                                                       gboolean v) {
-    gjs_arg_member<bool>(arg) = !!v;
-}
-
 template <typename T, GITypeTag TAG = GI_TYPE_TAG_VOID>
 [[nodiscard]] inline T gjs_arg_get(GIArgument* arg) {
-    return gjs_arg_member<T, TAG>(arg);
-}
+    if constexpr (std::is_same_v<T, bool> ||
+                  (std::is_same_v<T, gboolean> && TAG == GI_TYPE_TAG_BOOLEAN))
+        return !!gjs_arg_member<T, TAG>(arg);
 
-template <>
-[[nodiscard]] inline bool gjs_arg_get<bool>(GIArgument* arg) {
-    return !!gjs_arg_member<bool>(arg);
-}
-
-template <>
-[[nodiscard]] inline gboolean gjs_arg_get<gboolean, GI_TYPE_TAG_BOOLEAN>(
-    GIArgument* arg) {
-    return !!gjs_arg_member<bool>(arg);
+    return gjs_arg_member<T, TAG>(arg);
 }
 
 template <typename T, GITypeTag TAG = GI_TYPE_TAG_VOID>


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