[gjs: 1/2] Ensure const-correctness in C++ objects



commit b0ad0c140033f7d607fda026d4ff258aec0be64f
Author: Onur Şahin <sahinonur2000 hotmail com>
Date:   Thu May 2 04:57:42 2019 +0000

    Ensure const-correctness in C++ objects

 gi/toggle.cpp  | 20 ++++++++++++++------
 gi/toggle.h    | 15 ++++++++++-----
 gjs/engine.cpp |  2 +-
 gjs/global.cpp |  6 +++---
 gjs/module.cpp |  2 +-
 5 files changed, 29 insertions(+), 16 deletions(-)
---
diff --git a/gi/toggle.cpp b/gi/toggle.cpp
index 9e402bed..8fae06d0 100644
--- a/gi/toggle.cpp
+++ b/gi/toggle.cpp
@@ -31,9 +31,17 @@
 #include "toggle.h"
 
 std::deque<ToggleQueue::Item>::iterator
-ToggleQueue::find_operation_locked(GObject               *gobj,
-                                   ToggleQueue::Direction direction)
-{
+ToggleQueue::find_operation_locked(const GObject               *gobj,
+                                   ToggleQueue::Direction direction) {
+    return std::find_if(q.begin(), q.end(),
+        [gobj, direction](const Item& item)->bool {
+            return item.gobj == gobj && item.direction == direction;
+        });
+}
+
+std::deque<ToggleQueue::Item>::const_iterator
+ToggleQueue::find_operation_locked(const GObject *gobj,
+                                   ToggleQueue::Direction direction) const {
     return std::find_if(q.begin(), q.end(),
         [gobj, direction](const Item& item)->bool {
             return item.gobj == gobj && item.direction == direction;
@@ -41,7 +49,7 @@ ToggleQueue::find_operation_locked(GObject               *gobj,
 }
 
 bool
-ToggleQueue::find_and_erase_operation_locked(GObject               *gobj,
+ToggleQueue::find_and_erase_operation_locked(const GObject               *gobj,
                                              ToggleQueue::Direction direction)
 {
     auto pos = find_operation_locked(gobj, direction);
@@ -71,7 +79,7 @@ ToggleQueue::idle_destroy_notify(void *data)
 }
 
 std::pair<bool, bool>
-ToggleQueue::is_queued(GObject *gobj)
+ToggleQueue::is_queued(GObject *gobj) const
 {
     std::lock_guard<std::mutex> hold(lock);
     bool has_toggle_down = find_operation_locked(gobj, DOWN) != q.end();
@@ -112,7 +120,7 @@ ToggleQueue::handle_toggle(Handler handler)
     debug("handle", item.gobj);
     if (item.needs_unref)
         g_object_unref(item.gobj);
-    
+
     return true;
 }
 
diff --git a/gi/toggle.h b/gi/toggle.h
index e4dd8f19..7699bf58 100644
--- a/gi/toggle.h
+++ b/gi/toggle.h
@@ -53,7 +53,7 @@ private:
         unsigned needs_unref : 1;
     };
 
-    std::mutex lock;
+    mutable std::mutex lock;
     std::deque<Item> q;
     std::atomic_bool m_shutdown = ATOMIC_VAR_INIT(false);
 
@@ -61,15 +61,20 @@ private:
     Handler m_toggle_handler;
 
     /* No-op unless GJS_VERBOSE_ENABLE_LIFECYCLE is defined to 1. */
-    inline void debug(const char *did, void *what) {
+    inline void debug(const char *did, const void *what) const {
         gjs_debug_lifecycle(GJS_DEBUG_GOBJECT, "ToggleQueue %s %p", did, what);
     }
 
     GJS_USE
-    std::deque<Item>::iterator find_operation_locked(GObject  *gobj,
+    std::deque<Item>::iterator find_operation_locked(const GObject  *gobj,
                                                      Direction direction);
+
+    GJS_USE
+    std::deque<Item>::const_iterator find_operation_locked(const GObject *gobj,
+                                                           Direction direction) const;
+
     GJS_USE
-    bool find_and_erase_operation_locked(GObject *gobj, Direction direction);
+    bool find_and_erase_operation_locked(const GObject *gobj, Direction direction);
 
     static gboolean idle_handle_toggle(void *data);
     static void idle_destroy_notify(void *data);
@@ -78,7 +83,7 @@ private:
     /* These two functions return a pair DOWN, UP signifying whether toggles
      * are / were queued. is_queued() just checks and does not modify. */
     GJS_USE
-    std::pair<bool, bool> is_queued(GObject *gobj);
+    std::pair<bool, bool> is_queued(GObject *gobj) const;
     /* Cancels pending toggles and returns whether any were queued. */
     std::pair<bool, bool> cancel(GObject *gobj);
 
diff --git a/gjs/engine.cpp b/gjs/engine.cpp
index 0db0942f..576c36cd 100644
--- a/gjs/engine.cpp
+++ b/gjs/engine.cpp
@@ -289,7 +289,7 @@ public:
         JS_ShutDown();
     }
 
-    operator bool() {
+    operator bool() const {
         return true;
     }
 };
diff --git a/gjs/global.cpp b/gjs/global.cpp
index d6c858dc..f9e6b869 100644
--- a/gjs/global.cpp
+++ b/gjs/global.cpp
@@ -130,9 +130,9 @@ gjs_log_error(JSContext *cx,
 
 GJS_JSAPI_RETURN_CONVENTION
 static bool
-gjs_print_parse_args(JSContext    *cx,
-                     JS::CallArgs& argv,
-                     GjsAutoChar  *buffer)
+gjs_print_parse_args(JSContext              *cx,
+                     const JS::CallArgs&    argv,
+                     GjsAutoChar            *buffer)
 {
     GString *str;
     guint n;
diff --git a/gjs/module.cpp b/gjs/module.cpp
index f6f6656a..ba42dbcd 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -74,7 +74,7 @@ class GjsModule {
     define_import(JSContext       *cx,
                   JS::HandleObject module,
                   JS::HandleObject importer,
-                  JS::HandleId     name)
+                  JS::HandleId     name) const
     {
         if (!JS_DefinePropertyById(cx, importer, name, module,
                                    GJS_MODULE_PROP_FLAGS & ~JSPROP_PERMANENT)) {


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