summaryrefslogtreecommitdiff
path: root/gi/arg-inl.h
diff options
context:
space:
mode:
authorMarco Trevisan (TreviƱo) <mail@3v1n0.net>2020-05-12 21:15:13 +0200
committerPhilip Chimento <philip.chimento@gmail.com>2020-08-08 11:50:52 -0700
commit39fe12c7011c8733abf2daa1c3797a86e2bc3584 (patch)
tree3d52c8334805e6099f95583812a5ecd710e47153 /gi/arg-inl.h
parent25d5db5392bd1c80721d7edae4773cf96f85b9c0 (diff)
downloadgjs-39fe12c7011c8733abf2daa1c3797a86e2bc3584.tar.gz
arg: Add functions to get/set pointer values from/to integers
This allows to reduce some code duplication and again the need to manually pick the signed type we convert to, leaving this to the compiler
Diffstat (limited to 'gi/arg-inl.h')
-rw-r--r--gi/arg-inl.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/gi/arg-inl.h b/gi/arg-inl.h
index 4e9afb8a..e6492ee5 100644
--- a/gi/arg-inl.h
+++ b/gi/arg-inl.h
@@ -18,6 +18,8 @@
#include <glib-object.h> // for GType
#include <glib.h> // for gboolean
+#include "gi/utils-inl.h"
+
// GIArgument accessor templates
//
// These are intended to make access to the GIArgument union more type-safe and
@@ -186,6 +188,12 @@ inline void gjs_arg_set(GIArgument* arg, ReturnT (*v)(Args...)) {
gjs_arg_member<void*>(arg) = reinterpret_cast<void*>(v);
}
+template <typename T, GITypeTag TAG = GI_TYPE_TAG_VOID>
+inline std::enable_if_t<std::is_integral_v<T>> gjs_arg_set(GIArgument* arg,
+ void *v) {
+ 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;
@@ -214,6 +222,12 @@ template <>
}
template <typename T, GITypeTag TAG = GI_TYPE_TAG_VOID>
+[[nodiscard]] inline std::enable_if_t<std::is_integral_v<T>, void*>
+gjs_arg_get_as_pointer(GIArgument* arg) {
+ return gjs_int_to_pointer(gjs_arg_get<T, TAG>(arg));
+}
+
+template <typename T, GITypeTag TAG = GI_TYPE_TAG_VOID>
inline std::enable_if_t<!std::is_pointer_v<T>> gjs_arg_unset(GIArgument* arg) {
gjs_arg_set<T, TAG>(arg, static_cast<T>(0));
}