[niepce] npc-engine: bind Keyword with cxx



commit ae3bd963ba5255754891c1aa2983f03aafe18cc9
Author: Hubert Figuière <hub figuiere net>
Date:   Sat Oct 15 21:35:10 2022 -0400

    npc-engine: bind Keyword with cxx
    
    - Remove the RustFfiString class

 crates/npc-engine/build.rs                    |  1 +
 crates/npc-engine/src/db/keyword.rs           | 32 -----------------
 crates/npc-engine/src/lib.rs                  | 11 +++++-
 crates/npc-engine/src/library/notification.rs |  4 +--
 crates/npc-fwk/src/capi.rs                    | 30 ----------------
 crates/npc-fwk/src/lib.rs                     |  1 -
 src/Makefile.am                               |  1 -
 src/engine/importer/directoryimporter.cpp     |  1 -
 src/fwk/base/Makefile.am                      |  1 -
 src/fwk/base/propertybag.cpp                  |  2 --
 src/fwk/base/string.cpp                       | 31 -----------------
 src/fwk/base/string.hpp                       | 50 ---------------------------
 src/fwk/utils/modulemanager.cpp               |  1 -
 src/niepce/ui/workspacecontroller.cpp         | 18 +++++-----
 src/niepce/ui/workspacecontroller.hpp         |  2 +-
 src/rust_bindings.hpp                         |  3 +-
 16 files changed, 24 insertions(+), 165 deletions(-)
---
diff --git a/crates/npc-engine/build.rs b/crates/npc-engine/build.rs
index b4627639..257fdd72 100644
--- a/crates/npc-engine/build.rs
+++ b/crates/npc-engine/build.rs
@@ -30,6 +30,7 @@ fn main() {
             .exclude_item("GFileInfo")
             .exclude_item("RgbColour")
             .exclude_item("PropertyValue")
+            .exclude_item("Keyword")
             .exclude_item("Label")
             // Ensure these are opaque as generics are still a problem.
             .exclude_item("NiepcePropertySet")
diff --git a/crates/npc-engine/src/db/keyword.rs b/crates/npc-engine/src/db/keyword.rs
index 7153ae29..0628afb0 100644
--- a/crates/npc-engine/src/db/keyword.rs
+++ b/crates/npc-engine/src/db/keyword.rs
@@ -19,9 +19,6 @@
 
 use super::FromDb;
 use super::LibraryId;
-use libc::c_char;
-use std::ffi::CStr;
-use std::ffi::CString;
 
 #[derive(Clone)]
 pub struct Keyword {
@@ -64,32 +61,3 @@ impl FromDb for Keyword {
         Ok(Keyword::new(row.get(0)?, &kw))
     }
 }
-
-/// # Safety
-/// Dereference raw pointer.
-#[no_mangle]
-pub unsafe extern "C" fn engine_db_keyword_new(id: i64, keyword: *const c_char) -> *mut Keyword {
-    let kw = Box::new(Keyword::new(
-        id,
-        &*CStr::from_ptr(keyword).to_string_lossy(),
-    ));
-    Box::into_raw(kw)
-}
-
-#[no_mangle]
-pub extern "C" fn engine_db_keyword_id(obj: &Keyword) -> i64 {
-    obj.id() as i64
-}
-
-#[no_mangle]
-pub extern "C" fn engine_db_keyword_keyword(obj: &Keyword) -> *mut c_char {
-    let cstr = CString::new(obj.keyword()).unwrap();
-    cstr.into_raw()
-}
-
-/// # Safety
-/// Dereference raw pointer.
-#[no_mangle]
-pub unsafe extern "C" fn engine_db_keyword_delete(kw: *mut Keyword) {
-    drop(Box::from_raw(kw));
-}
diff --git a/crates/npc-engine/src/lib.rs b/crates/npc-engine/src/lib.rs
index a84e705f..28bf1d26 100644
--- a/crates/npc-engine/src/lib.rs
+++ b/crates/npc-engine/src/lib.rs
@@ -104,7 +104,7 @@ pub extern "C" fn eng_property_bag_set_value(
     b.set_value(key.into(), v.clone())
 }
 
-use crate::db::Label;
+use crate::db::{Keyword, Label};
 
 #[cxx::bridge(namespace = "eng")]
 mod ffi {
@@ -115,6 +115,15 @@ mod ffi {
         type RgbColour = npc_fwk::base::rgbcolour::RgbColour;
     }
 
+    extern "Rust" {
+        type Keyword;
+
+        fn id(&self) -> i64;
+        fn keyword(&self) -> &str;
+    }
+
+    impl Box<Keyword> {}
+
     extern "Rust" {
         type Label;
 
diff --git a/crates/npc-engine/src/library/notification.rs b/crates/npc-engine/src/library/notification.rs
index b5da0837..a7433f43 100644
--- a/crates/npc-engine/src/library/notification.rs
+++ b/crates/npc-engine/src/library/notification.rs
@@ -276,9 +276,9 @@ pub unsafe extern "C" fn engine_library_notification_get_libfolder(
 #[no_mangle]
 pub unsafe extern "C" fn engine_library_notification_get_keyword(
     n: *const LibNotification,
-) -> *const Keyword {
+) -> *mut Keyword {
     match n.as_ref() {
-        Some(&LibNotification::AddedKeyword(ref f)) => f,
+        Some(&LibNotification::AddedKeyword(ref f)) => Box::into_raw(Box::new(f.clone())),
         _ => unreachable!(),
     }
 }
diff --git a/crates/npc-fwk/src/lib.rs b/crates/npc-fwk/src/lib.rs
index 8d045bd6..179de4ef 100644
--- a/crates/npc-fwk/src/lib.rs
+++ b/crates/npc-fwk/src/lib.rs
@@ -19,7 +19,6 @@
 
 #[macro_use]
 pub mod base;
-pub mod capi;
 pub mod toolkit;
 pub mod utils;
 
diff --git a/src/Makefile.am b/src/Makefile.am
index 32259138..d0eb0c6b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -28,7 +28,6 @@ RUST_SOURCES = \
        @top_srcdir@/crates/npc-engine/src/lib.rs \
        @top_srcdir@/crates/npc-fwk/Cargo.toml \
        @top_srcdir@/crates/npc-fwk/build.rs \
-       @top_srcdir@/crates/npc-fwk/src/capi.rs \
        @top_srcdir@/crates/npc-fwk/src/base.rs \
        @top_srcdir@/crates/npc-fwk/src/base/date.rs \
        @top_srcdir@/crates/npc-fwk/src/base/debug.rs \
diff --git a/src/engine/importer/directoryimporter.cpp b/src/engine/importer/directoryimporter.cpp
index 0c881ce1..31c645b5 100644
--- a/src/engine/importer/directoryimporter.cpp
+++ b/src/engine/importer/directoryimporter.cpp
@@ -22,7 +22,6 @@
 #include <glibmm/miscutils.h>
 
 #include "fwk/base/debug.hpp"
-#include "fwk/base/string.hpp"
 #include "fwk/utils/pathutils.hpp"
 #include "engine/importer/directoryimporter.hpp"
 #include "engine/importer/importedfile.hpp"
diff --git a/src/fwk/base/Makefile.am b/src/fwk/base/Makefile.am
index 0a270d9e..90722524 100644
--- a/src/fwk/base/Makefile.am
+++ b/src/fwk/base/Makefile.am
@@ -41,7 +41,6 @@ libfwkbase_a_SOURCES = \
        moniker.hpp moniker.cpp \
        geometry.hpp geometry.cpp \
        singleton.hpp \
-       string.hpp string.cpp \
        util.hpp \
        map.hpp \
        option.hpp \
diff --git a/src/fwk/base/propertybag.cpp b/src/fwk/base/propertybag.cpp
index 03f05735..aa6ec429 100644
--- a/src/fwk/base/propertybag.cpp
+++ b/src/fwk/base/propertybag.cpp
@@ -17,8 +17,6 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "string.hpp"
-#include "debug.hpp"
 #include "propertybag.hpp"
 
 namespace fwk {
diff --git a/src/fwk/utils/modulemanager.cpp b/src/fwk/utils/modulemanager.cpp
index 3f782c2b..25eed6f1 100644
--- a/src/fwk/utils/modulemanager.cpp
+++ b/src/fwk/utils/modulemanager.cpp
@@ -35,7 +35,6 @@
 #include "fwk/utils/pathutils.hpp"
 #include "fwk/base/debug.hpp"
 #include "fwk/base/map.hpp"
-#include "fwk/base/string.hpp"
 #include "dynamicmodule.hpp"
 #include "modulemanager.hpp"
 
diff --git a/src/niepce/ui/workspacecontroller.cpp b/src/niepce/ui/workspacecontroller.cpp
index 6aaf52ce..4925ae70 100644
--- a/src/niepce/ui/workspacecontroller.cpp
+++ b/src/niepce/ui/workspacecontroller.cpp
@@ -27,7 +27,6 @@
 #include <gtkmm/messagedialog.h>
 
 #include "fwk/base/debug.hpp"
-#include "fwk/base/string.hpp"
 #include "fwk/toolkit/application.hpp"
 #include "fwk/toolkit/gtkutils.hpp"
 #include "engine/importer/iimporter.hpp"
@@ -178,9 +177,8 @@ void WorkspaceController::on_lib_notification(const eng::LibNotification &ln)
     }
     case eng::NotificationType::ADDED_KEYWORD:
     {
-        auto k = engine_library_notification_get_keyword(&ln);
-        DBG_ASSERT(k, "keyword must not be NULL");
-        add_keyword_item(k);
+        auto k = eng::KeywordPtr::from_raw(engine_library_notification_get_keyword(&ln));
+        add_keyword_item(*k);
         break;
     }
     case eng::NotificationType::FOLDER_COUNTED:
@@ -325,16 +323,16 @@ void WorkspaceController::on_row_collapsed(const Gtk::TreeModel::iterator& iter,
     on_row_expanded_collapsed(iter, path, false);
 }
 
-void WorkspaceController::add_keyword_item(const eng::Keyword* k)
+void WorkspaceController::add_keyword_item(const eng::Keyword& k)
 {
     auto children = m_keywordsNode->children();
     bool was_empty = children.empty();
-    auto keyword = fwk::RustFfiString(engine_db_keyword_keyword(k));
+    auto keyword = k.keyword();
     auto iter = add_item(m_treestore, children,
-                         m_icons[ICON_KEYWORD], keyword.c_str(),
-                         engine_db_keyword_id(k), KEYWORD_ITEM);
-    ffi::libraryclient_count_keyword(getLibraryClient()->client(), engine_db_keyword_id(k));
-    m_keywordsidmap[engine_db_keyword_id(k)] = iter;
+                         m_icons[ICON_KEYWORD], std::string(keyword),
+                         k.id(), KEYWORD_ITEM);
+    ffi::libraryclient_count_keyword(getLibraryClient()->client(), k.id());
+    m_keywordsidmap[k.id()] = iter;
     if(was_empty) {
         expand_from_cfg("workspace_keywords_expanded", m_keywordsNode);
     }
diff --git a/src/niepce/ui/workspacecontroller.hpp b/src/niepce/ui/workspacecontroller.hpp
index 6bf622c5..301ae9d9 100644
--- a/src/niepce/ui/workspacecontroller.hpp
+++ b/src/niepce/ui/workspacecontroller.hpp
@@ -112,7 +112,7 @@ private:
     /** Remove a folder from the treeview */
     void remove_folder_item(eng::library_id_t id);
     /** add a keyword item to the treeview */
-    void add_keyword_item(const eng::Keyword* k);
+    void add_keyword_item(const eng::Keyword& k);
     /** add a tree item in the treeview
      * @param treestore the treestore to add to
      * @param childrens the children subtree to add to
diff --git a/src/rust_bindings.hpp b/src/rust_bindings.hpp
index 821cb9ef..f7d6f09d 100644
--- a/src/rust_bindings.hpp
+++ b/src/rust_bindings.hpp
@@ -37,6 +37,7 @@ typedef fwk::FileList FileList;
 typedef fwk::PropertyValue PropertyValue;
 typedef fwk::RgbColour RgbColour;
 typedef eng::Label Label;
+typedef eng::Keyword Keyword;
 struct NiepcePropertyBag;
 struct NiepcePropertySet;
 }
@@ -59,13 +60,13 @@ typedef ffi::NiepcePropertySet PropertySet;
 
 namespace eng {
 typedef rust::Box<Label> LabelPtr;
+typedef rust::Box<Keyword> KeywordPtr;
 
 typedef ffi::NiepcePropertyIdx Np;
 using NiepcePropertyIdx = ffi::NiepcePropertyIdx;
 typedef ffi::LibraryId library_id_t; // XXX change this to LibraryId
 typedef ffi::FileType FileType;
 typedef ffi::FileStatus FileStatus;
-typedef ffi::Keyword Keyword;
 typedef ffi::LibFile LibFile;
 typedef ffi::LibFolder LibFolder;
 typedef ffi::LibMetadata LibMetadata;


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