[gjs/arg-inlines: 6/11] arg: Add GIArgument setter overload for function pointers



commit 48b3a21b4c59e2b5efb3a7c96a03824c1b879881
Author: Philip Chimento <philip chimento gmail com>
Date:   Fri Jul 10 23:04:26 2020 -0700

    arg: Add GIArgument setter overload for function pointers
    
    Without this, we'd still need to reinterpret_cast function pointers to
    void*, because it's technically not legal to convert between data and
    function pointers. However, having a compiler that can do this is a
    requirement of GLib, so we do need to do it sometimes anyway.

 gi/arg-inl.h | 8 ++++++++
 1 file changed, 8 insertions(+)
---
diff --git a/gi/arg-inl.h b/gi/arg-inl.h
index 0430fc3a..a111451e 100644
--- a/gi/arg-inl.h
+++ b/gi/arg-inl.h
@@ -136,6 +136,14 @@ inline void gjs_g_argument_value_set(GIArgument* arg, T* v) {
     using NonconstT = std::remove_const_t<T>;
     gjs_g_argument_value<NonconstT*, TAG>(arg) = const_cast<NonconstT*>(v);
 }
+
+// Store function pointers as void*. It is a requirement of GLib that your
+// compiler can do this
+template <typename ReturnT, typename... Args>
+inline void gjs_g_argument_value_set(GIArgument* arg, ReturnT (*v)(Args...)) {
+    gjs_g_argument_value<void*>(arg) = reinterpret_cast<void*>(v);
+}
+
 template <>
 inline void gjs_g_argument_value_set<bool>(GIArgument* arg, bool v) {
     gjs_g_argument_value<bool>(arg) = !!v;


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