[gjs: 5/8] jsapi-util: Add autoptr template for GI info structs



commit 47a40b377ab3034d34b7c1e79b786ff8f32ddef2
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat May 26 10:00:55 2018 -0700

    jsapi-util: Add autoptr template for GI info structs
    
    We have a lot of lookups and frees of GIBaseInfo and its subtypes. I've
    been meaning to add this for a long time and refactor a lot of code, but
    here I'm simply adding it because we'll need it in a following commit to
    use in a GCHashMap. The refactoring can come later.
    
    See: #160

 gjs/jsapi-util.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
---
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 2474f3b8..5b12a8ee 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -66,6 +66,20 @@ public:
     }
 };
 
+template<typename T = GIBaseInfo>
+class GjsAutoInfo : public std::unique_ptr<T, decltype(&g_base_info_unref)> {
+public:
+    GjsAutoInfo(T *ptr = nullptr) : GjsAutoInfo::unique_ptr(ptr, g_base_info_unref) {}
+
+    operator T *() { return GjsAutoInfo::unique_ptr::get(); }
+
+    const char *name(void) { return g_base_info_get_name(this); }
+};
+
+/* For use of GjsAutoInfo<T> in GC hash maps */
+template<typename T>
+struct JS::GCPolicy<GjsAutoInfo<T>> : public JS::IgnoreGCPolicy<GjsAutoInfo<T>> {};
+
 struct GjsJSFreeArgs {
     void operator() (char *str) {
         JS_free(nullptr, str);


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