summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarco Trevisan (TreviƱo) <mail@3v1n0.net>2022-06-29 22:09:11 +0200
committerPhilip Chimento <philip.chimento@gmail.com>2022-11-19 12:03:46 -0800
commit37c74fb3c07c9aca828575ce20e16e3eb7d21155 (patch)
tree99c06913db7ab50f8fadcc36c58a0ef0423da83a
parentb3ff9fb2458b505d5721060c5ee31a56cffb9ffa (diff)
downloadgjs-37c74fb3c07c9aca828575ce20e16e3eb7d21155.tar.gz
objectbox: Define Ptr as a GjsAutoPointer
We can avoid storing the destructor in this way, together with all the constexpr operations.
-rw-r--r--gjs/objectbox.cpp8
-rw-r--r--gjs/objectbox.h8
2 files changed, 11 insertions, 5 deletions
diff --git a/gjs/objectbox.cpp b/gjs/objectbox.cpp
index 8a3288f9..09f35658 100644
--- a/gjs/objectbox.cpp
+++ b/gjs/objectbox.cpp
@@ -83,8 +83,10 @@ struct ObjectBox::impl {
ObjectBox::ObjectBox(JSObject* obj)
: m_impl(std::make_unique<ObjectBox::impl>(this, obj)) {}
+void ObjectBox::destroy(ObjectBox* object) { object->m_impl->unref(); }
+
ObjectBox::Ptr ObjectBox::boxed(JSContext* cx, JSObject* obj) {
- ObjectBox* box = nullptr;
+ ObjectBox::Ptr box;
ObjectBox** found =
std::find_if(m_wrappers.begin(), m_wrappers.end(),
@@ -96,10 +98,10 @@ ObjectBox::Ptr ObjectBox::boxed(JSContext* cx, JSObject* obj) {
} else {
box = new ObjectBox(obj);
if (!box->m_impl->init(cx))
- return ObjectBox::Ptr(nullptr, [](ObjectBox*) {});
+ return nullptr;
}
- return ObjectBox::Ptr(box, [](ObjectBox* b) { b->m_impl->unref(); });
+ return box;
}
JSObject* ObjectBox::object_for_c_ptr(JSContext* cx, ObjectBox* box) {
diff --git a/gjs/objectbox.h b/gjs/objectbox.h
index b2d3f349..3d2ed76f 100644
--- a/gjs/objectbox.h
+++ b/gjs/objectbox.h
@@ -12,12 +12,16 @@
#include <js/TypeDecls.h>
+#include "gjs/jsapi-util.h"
#include "gjs/macros.h"
class JSTracer;
-struct ObjectBox {
- using Ptr = std::unique_ptr<ObjectBox, void (*)(ObjectBox*)>;
+class ObjectBox {
+ static void destroy(ObjectBox*);
+
+ public:
+ using Ptr = GjsAutoPointer<ObjectBox, ObjectBox, ObjectBox::destroy>;
[[nodiscard]] static GType gtype();