[gjs] arg: Add functions to get/set pointer values from/to integers
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs] arg: Add functions to get/set pointer values from/to integers
- Date: Sat, 8 Aug 2020 19:44:08 +0000 (UTC)
commit 39fe12c7011c8733abf2daa1c3797a86e2bc3584
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date: Tue May 12 21:15:13 2020 +0200
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
gi/arg-inl.h | 14 ++++++++++++++
gi/arg.cpp | 39 ++++++++++++++++++---------------------
2 files changed, 32 insertions(+), 21 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;
@@ -213,6 +221,12 @@ template <>
return !!gjs_arg_member<bool>(arg);
}
+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));
diff --git a/gi/arg.cpp b/gi/arg.cpp
index 2d4fa012..8d39f18c 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -276,33 +276,31 @@ static void _g_type_info_argument_from_hash_pointer(GITypeInfo* info,
switch (type_tag) {
case GI_TYPE_TAG_BOOLEAN:
- gjs_arg_set<gboolean, GI_TYPE_TAG_BOOLEAN>(
- arg, GPOINTER_TO_INT(hash_pointer));
+ gjs_arg_set<gboolean, GI_TYPE_TAG_BOOLEAN>(arg, hash_pointer);
break;
case GI_TYPE_TAG_INT8:
- gjs_arg_set<int8_t>(arg, GPOINTER_TO_INT(hash_pointer));
+ gjs_arg_set<int8_t>(arg, hash_pointer);
break;
case GI_TYPE_TAG_UINT8:
- gjs_arg_set<uint8_t>(arg, GPOINTER_TO_UINT(hash_pointer));
+ gjs_arg_set<uint8_t>(arg, hash_pointer);
break;
case GI_TYPE_TAG_INT16:
- gjs_arg_set<int16_t>(arg, GPOINTER_TO_INT(hash_pointer));
+ gjs_arg_set<int16_t>(arg, hash_pointer);
break;
case GI_TYPE_TAG_UINT16:
- gjs_arg_set<uint16_t>(arg, GPOINTER_TO_UINT(hash_pointer));
+ gjs_arg_set<uint16_t>(arg, hash_pointer);
break;
case GI_TYPE_TAG_INT32:
- gjs_arg_set<int32_t>(arg, GPOINTER_TO_INT(hash_pointer));
+ gjs_arg_set<int32_t>(arg, hash_pointer);
break;
case GI_TYPE_TAG_UINT32:
- gjs_arg_set<uint32_t>(arg, GPOINTER_TO_UINT(hash_pointer));
+ gjs_arg_set<uint32_t>(arg, hash_pointer);
break;
case GI_TYPE_TAG_UNICHAR:
- gjs_arg_set<char32_t>(arg, GPOINTER_TO_UINT(hash_pointer));
+ gjs_arg_set<char32_t>(arg, hash_pointer);
break;
case GI_TYPE_TAG_GTYPE:
- gjs_arg_set<GType, GI_TYPE_TAG_GTYPE>(
- arg, GPOINTER_TO_SIZE(hash_pointer));
+ gjs_arg_set<GType, GI_TYPE_TAG_GTYPE>(arg, hash_pointer);
break;
case GI_TYPE_TAG_UTF8:
case GI_TYPE_TAG_FILENAME:
@@ -332,24 +330,23 @@ static void _g_type_info_argument_from_hash_pointer(GITypeInfo* info,
switch (type_tag) {
case GI_TYPE_TAG_BOOLEAN:
- return GINT_TO_POINTER(gjs_arg_get<bool>(arg));
+ return gjs_arg_get_as_pointer<bool>(arg);
case GI_TYPE_TAG_INT8:
- return GINT_TO_POINTER(gjs_arg_get<int8_t>(arg));
+ return gjs_arg_get_as_pointer<int8_t>(arg);
case GI_TYPE_TAG_UINT8:
- return GUINT_TO_POINTER(gjs_arg_get<uint8_t>(arg));
+ return gjs_arg_get_as_pointer<uint8_t>(arg);
case GI_TYPE_TAG_INT16:
- return GINT_TO_POINTER(gjs_arg_get<int16_t>(arg));
+ return gjs_arg_get_as_pointer<int16_t>(arg);
case GI_TYPE_TAG_UINT16:
- return GUINT_TO_POINTER(gjs_arg_get<uint16_t>(arg));
+ return gjs_arg_get_as_pointer<uint16_t>(arg);
case GI_TYPE_TAG_INT32:
- return GINT_TO_POINTER(gjs_arg_get<int32_t>(arg));
+ return gjs_arg_get_as_pointer<int32_t>(arg);
case GI_TYPE_TAG_UINT32:
- return GUINT_TO_POINTER(gjs_arg_get<uint32_t>(arg));
+ return gjs_arg_get_as_pointer<uint32_t>(arg);
case GI_TYPE_TAG_UNICHAR:
- return GUINT_TO_POINTER(gjs_arg_get<char32_t>(arg));
+ return gjs_arg_get_as_pointer<char32_t>(arg);
case GI_TYPE_TAG_GTYPE:
- return GSIZE_TO_POINTER(
- (gjs_arg_get<GType, GI_TYPE_TAG_GTYPE>(arg)));
+ return gjs_arg_get_as_pointer<GType, GI_TYPE_TAG_GTYPE>(arg);
case GI_TYPE_TAG_UTF8:
case GI_TYPE_TAG_FILENAME:
case GI_TYPE_TAG_INTERFACE:
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]