[niepce] eng: Bind LibFolder with cxx



commit 9567123491bc5d8631845d9d229357fa5a448092
Author: Hubert Figuière <hub figuiere net>
Date:   Mon Oct 17 21:17:16 2022 -0400

    eng: Bind LibFolder with cxx

 crates/npc-engine/build.rs            |  2 ++
 crates/npc-engine/src/db/libfolder.rs | 61 +++++++----------------------------
 crates/npc-engine/src/db/library.rs   |  2 +-
 crates/npc-engine/src/lib.rs          | 18 ++++++++++-
 src/niepce/ui/workspacecontroller.cpp | 13 ++++----
 src/rust_bindings.hpp                 |  4 +--
 6 files changed, 40 insertions(+), 60 deletions(-)
---
diff --git a/crates/npc-engine/build.rs b/crates/npc-engine/build.rs
index 24690538..f4e5332f 100644
--- a/crates/npc-engine/build.rs
+++ b/crates/npc-engine/build.rs
@@ -33,7 +33,9 @@ fn main() {
             .exclude_item("Keyword")
             .exclude_item("Label")
             .exclude_item("LibFile")
+            .exclude_item("LibFolder")
             .exclude_item("LibMetadata")
+            .exclude_item("FolderVirtualType")
             // Ensure these are opaque as generics are still a problem.
             .exclude_item("NiepcePropertySet")
             .exclude_item("NiepcePropertyBag")
diff --git a/crates/npc-engine/src/db/libfolder.rs b/crates/npc-engine/src/db/libfolder.rs
index 22a7a8e1..e5830903 100644
--- a/crates/npc-engine/src/db/libfolder.rs
+++ b/crates/npc-engine/src/db/libfolder.rs
@@ -1,7 +1,7 @@
 /*
  * niepce - eng/db/libfolder.rs
  *
- * Copyright (C) 2017 Hubert Figuière
+ * Copyright (C) 2017-2022 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,18 +17,11 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-use libc::c_char;
-use std::ffi::CString;
-
 use super::FromDb;
 use super::LibraryId;
 
-#[repr(i32)]
-#[derive(Clone)]
-pub enum FolderVirtualType {
-    NONE = 0,
-    TRASH = 1,
-}
+// defined in the cxx bridge
+pub use crate::ffi::FolderVirtualType;
 
 impl From<i32> for FolderVirtualType {
     fn from(t: i32) -> Self {
@@ -40,6 +33,14 @@ impl From<i32> for FolderVirtualType {
     }
 }
 
+// this implementation is based on the cxx bridge implementation
+// of enums
+impl From<FolderVirtualType> for i32 {
+    fn from(t: FolderVirtualType) -> i32 {
+        t.repr
+    }
+}
+
 #[derive(Clone)]
 pub struct LibFolder {
     id: LibraryId,
@@ -51,7 +52,6 @@ pub struct LibFolder {
     expanded: bool,
     virt: FolderVirtualType,
     parent: LibraryId,
-    cstr: CString,
 }
 
 impl LibFolder {
@@ -64,7 +64,6 @@ impl LibFolder {
             expanded: false,
             virt: FolderVirtualType::NONE,
             parent: 0,
-            cstr: CString::new("").unwrap(),
         }
     }
 
@@ -72,7 +71,7 @@ impl LibFolder {
         self.id
     }
 
-    pub fn name(&self) -> &String {
+    pub fn name(&self) -> &str {
         &self.name
     }
 
@@ -140,39 +139,3 @@ impl FromDb for LibFolder {
         Ok(libfolder)
     }
 }
-
-#[no_mangle]
-pub extern "C" fn engine_db_libfolder_id(obj: &LibFolder) -> i64 {
-    obj.id() as i64
-}
-
-#[no_mangle]
-pub extern "C" fn engine_db_libfolder_name(obj: &mut LibFolder) -> *const c_char {
-    obj.cstr = CString::new(obj.name().clone()).unwrap();
-    obj.cstr.as_ptr()
-}
-
-#[no_mangle]
-pub extern "C" fn engine_db_libfolder_virtual_type(obj: &LibFolder) -> FolderVirtualType {
-    obj.virtual_type()
-}
-
-#[no_mangle]
-pub extern "C" fn engine_db_libfolder_expanded(obj: &LibFolder) -> bool {
-    obj.expanded
-}
-
-#[no_mangle]
-pub extern "C" fn engine_db_libfolder_set_locked(obj: &mut LibFolder, locked: bool) {
-    obj.set_locked(locked);
-}
-
-#[no_mangle]
-pub extern "C" fn engine_db_libfolder_set_expanded(obj: &mut LibFolder, expanded: bool) {
-    obj.set_expanded(expanded);
-}
-
-#[no_mangle]
-pub extern "C" fn engine_db_libfolder_set_virtual_type(obj: &mut LibFolder, t: FolderVirtualType) {
-    obj.set_virtual_type(t);
-}
diff --git a/crates/npc-engine/src/db/library.rs b/crates/npc-engine/src/db/library.rs
index 35ca52d0..7e243ce2 100644
--- a/crates/npc-engine/src/db/library.rs
+++ b/crates/npc-engine/src/db/library.rs
@@ -240,7 +240,7 @@ impl Library {
             )
             .unwrap();
             //
-            let trash_type = libfolder::FolderVirtualType::TRASH as i32;
+            let trash_type = i32::from(libfolder::FolderVirtualType::TRASH);
             conn.execute(
                 "insert into folders (name, locked, virtual, parent_id, path) \
                  values (?1, 1, ?2, 0, '')",
diff --git a/crates/npc-engine/src/lib.rs b/crates/npc-engine/src/lib.rs
index c6ce84c4..62fa8d50 100644
--- a/crates/npc-engine/src/lib.rs
+++ b/crates/npc-engine/src/lib.rs
@@ -145,7 +145,7 @@ pub fn property_set_new() -> Box<PropertySet> {
 pub type NiepcePropertySet = PropertySet;
 pub type NiepcePropertyBag = PropertyBag;
 
-use crate::db::{Keyword, Label, LibFile, LibMetadata};
+use crate::db::{Keyword, Label, LibFile, LibFolder, LibMetadata};
 
 #[cxx::bridge(namespace = "eng")]
 mod ffi {
@@ -209,6 +209,22 @@ mod ffi {
 
     impl Box<LibFile> {}
 
+    #[repr(i32)]
+    #[derive(Clone)]
+    pub enum FolderVirtualType {
+        NONE = 0,
+        TRASH = 1,
+    }
+
+    extern "Rust" {
+        type LibFolder;
+
+        fn id(&self) -> i64;
+        fn name(&self) -> &str;
+        fn expanded(&self) -> bool;
+        fn virtual_type(&self) -> FolderVirtualType;
+    }
+
     extern "Rust" {
         type LibMetadata;
 
diff --git a/src/niepce/ui/workspacecontroller.cpp b/src/niepce/ui/workspacecontroller.cpp
index 4925ae70..678aa2ba 100644
--- a/src/niepce/ui/workspacecontroller.cpp
+++ b/src/niepce/ui/workspacecontroller.cpp
@@ -351,21 +351,20 @@ void WorkspaceController::remove_folder_item(eng::library_id_t id)
 void WorkspaceController::add_folder_item(const eng::LibFolder* f)
 {
     int icon_idx = ICON_ROLL;
-    if(engine_db_libfolder_virtual_type(f) == eng::FolderVirtualType::TRASH) {
+    if(f->virtual_type() == eng::FolderVirtualType::TRASH) {
         icon_idx = ICON_TRASH;
-        ffi::libraryclient_set_trash_id(getLibraryClient()->client(), engine_db_libfolder_id(f));
+        ffi::libraryclient_set_trash_id(getLibraryClient()->client(), f->id());
     }
     auto children = m_folderNode->children();
     bool was_empty = children.empty();
     auto iter = add_item(m_treestore, children,
                          m_icons[icon_idx],
-                         engine_db_libfolder_name(const_cast<eng::LibFolder*>(f)),
-                         engine_db_libfolder_id(f), FOLDER_ITEM);
-    if(engine_db_libfolder_expanded(f)) {
+                         std::string(f->name()), f->id(), FOLDER_ITEM);
+    if (f->expanded()) {
         m_librarytree.expand_row(m_treestore->get_path(iter), false);
     }
-    ffi::libraryclient_count_folder(getLibraryClient()->client(), engine_db_libfolder_id(f));
-    m_folderidmap[engine_db_libfolder_id(f)] = iter;
+    ffi::libraryclient_count_folder(getLibraryClient()->client(), f->id());
+    m_folderidmap[f->id()] = iter;
     // expand if needed. Because Gtk doesn't expand empty
     if (was_empty) {
         expand_from_cfg("workspace_folders_expanded", m_folderNode);
diff --git a/src/rust_bindings.hpp b/src/rust_bindings.hpp
index b77b656b..fa762b63 100644
--- a/src/rust_bindings.hpp
+++ b/src/rust_bindings.hpp
@@ -40,10 +40,12 @@ typedef fwk::RgbColour RgbColour;
 typedef fwk::WrappedPropertyBag WrappedPropertyBag;
 typedef eng::Label Label;
 typedef eng::LibFile LibFile;
+typedef eng::LibFolder LibFolder;
 typedef eng::LibMetadata LibMetadata;
 typedef eng::Keyword Keyword;
 typedef fwk::PropertyBag NiepcePropertyBag;
 typedef fwk::PropertySet NiepcePropertySet;
+typedef eng::FolderVirtualType FolderVirtualType;
 }
 
 #include "target/eng_bindings.h"
@@ -70,11 +72,9 @@ typedef ffi::NiepcePropertyIdx Np;
 using NiepcePropertyIdx = ffi::NiepcePropertyIdx;
 typedef ffi::LibraryId library_id_t; // XXX change this to LibraryId
 typedef ffi::FileStatus FileStatus;
-typedef ffi::LibFolder LibFolder;
 typedef ffi::Managed Managed;
 typedef ffi::LibNotification LibNotification;
 typedef ffi::NotificationType NotificationType;
-typedef ffi::FolderVirtualType FolderVirtualType;
 }
 
 namespace ui {


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