[niepce] eng: Bind LibFile with cxx



commit be38cfe92be7e71759b2f457649e0aeca72afa65
Author: Hubert Figuière <hub figuiere net>
Date:   Sun Oct 16 23:20:28 2022 -0400

    eng: Bind LibFile with cxx

 crates/npc-engine/src/db/libfile.rs   | 47 ++++++++---------------------------
 crates/npc-engine/src/lib.rs          |  6 ++++-
 src/niepce/ui/selectioncontroller.cpp |  6 ++---
 3 files changed, 18 insertions(+), 41 deletions(-)
---
diff --git a/crates/npc-engine/src/db/libfile.rs b/crates/npc-engine/src/db/libfile.rs
index 0b3dfa2b..ab764278 100644
--- a/crates/npc-engine/src/db/libfile.rs
+++ b/crates/npc-engine/src/db/libfile.rs
@@ -17,8 +17,6 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-use libc::c_char;
-use std::ffi::CStr;
 use std::ffi::CString;
 use std::path::{Path, PathBuf};
 
@@ -28,7 +26,6 @@ use super::fsfile::FsFile;
 use super::FromDb;
 use super::LibraryId;
 use super::NiepceProperties as Np;
-use super::NiepcePropertyIdx;
 
 pub use crate::ffi::FileType;
 
@@ -198,6 +195,11 @@ impl LibFile {
         }
     }
 
+    // cxx
+    pub fn property_int(&self, idx: u32) -> i32 {
+        self.property(Np::from(idx))
+    }
+
     pub fn set_property(&mut self, idx: Np, value: i32) {
         use super::NiepcePropertyIdx::*;
         match idx {
@@ -209,6 +211,11 @@ impl LibFile {
         };
     }
 
+    // cxx
+    pub fn set_property_int(&mut self, idx: u32, v: i32) {
+        self.set_property(Np::from(idx), v);
+    }
+
     /// return an URI of the real path as Glib want this, oftern
     pub fn uri(&self) -> String {
         let mut s = String::from("file://");
@@ -268,37 +275,3 @@ pub fn mimetype_to_filetype(mime: &npc_fwk::MimeType) -> FileType {
         FileType::Unknown
     }
 }
-
-/// # Safety
-/// Dereference raw pointer.
-#[no_mangle]
-pub unsafe extern "C" fn engine_db_libfile_new(
-    id: LibraryId,
-    folder_id: LibraryId,
-    fs_file_id: LibraryId,
-    path: *const c_char,
-    name: *const c_char,
-) -> *mut LibFile {
-    let lf = Box::new(LibFile::new(
-        id,
-        folder_id,
-        fs_file_id,
-        PathBuf::from(&*CStr::from_ptr(path).to_string_lossy()),
-        &*CStr::from_ptr(name).to_string_lossy(),
-    ));
-    Box::into_raw(lf)
-}
-
-#[no_mangle]
-pub extern "C" fn engine_db_libfile_property(obj: &LibFile, idx: NiepcePropertyIdx) -> i32 {
-    obj.property(Np::Index(idx))
-}
-
-#[no_mangle]
-pub extern "C" fn engine_db_libfile_set_property(
-    obj: &mut LibFile,
-    idx: NiepcePropertyIdx,
-    v: i32,
-) {
-    obj.set_property(Np::Index(idx), v);
-}
diff --git a/crates/npc-engine/src/lib.rs b/crates/npc-engine/src/lib.rs
index ba761750..c6ce84c4 100644
--- a/crates/npc-engine/src/lib.rs
+++ b/crates/npc-engine/src/lib.rs
@@ -41,9 +41,9 @@ impl std::ops::Deref for PropertySet {
     }
 }
 
-use npc_fwk::PropertyValue;
 use npc_fwk::base::PropertyIndex;
 use npc_fwk::toolkit::widgets::WrappedPropertyBag;
+use npc_fwk::PropertyValue;
 
 /// Delete the %WrappedPropertyBag object
 ///
@@ -201,6 +201,10 @@ mod ffi {
         fn orientation(&self) -> i32;
         // The type is `FileType`.
         fn file_type(&self) -> FileType;
+        #[cxx_name = "property"]
+        fn property_int(&self, idx: u32) -> i32;
+        #[cxx_name = "set_property"]
+        fn set_property_int(&mut self, idx: u32, v: i32);
     }
 
     impl Box<LibFile> {}
diff --git a/src/niepce/ui/selectioncontroller.cpp b/src/niepce/ui/selectioncontroller.cpp
index 18ea1f5f..d71436a3 100644
--- a/src/niepce/ui/selectioncontroller.cpp
+++ b/src/niepce/ui/selectioncontroller.cpp
@@ -270,8 +270,8 @@ void SelectionController::set_property(ffi::NiepcePropertyIdx idx, int value)
             return;
         }
         auto& file = f.value();
-        DBG_OUT("old property is %d", engine_db_libfile_property(&*file, idx));
-        int32_t old_value = engine_db_libfile_property(&*file, idx);
+        DBG_OUT("old property is %d", file->property((uint32_t)idx));
+        int32_t old_value = file->property((uint32_t)idx);
         const char *action = nullptr;
         switch(idx) {
         case ffi::NiepcePropertyIdx::NpNiepceFlagProp:
@@ -290,7 +290,7 @@ void SelectionController::set_property(ffi::NiepcePropertyIdx idx, int value)
         _set_metadata(action, selection, idx, old_value, value);
         // we need to set the property here so that undo/redo works
         // consistently.
-        engine_db_libfile_set_property(&*file, idx, value);
+        file->set_property((uint32_t)idx, value);
     }
 }
 


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