[niepce] eng: Bind ThumbnailCache with cxx



commit e06ad4590fb232dfbbdbd1a12634f13346b7e81c
Author: Hubert Figuière <hub figuiere net>
Date:   Mon Oct 17 22:17:07 2022 -0400

    eng: Bind ThumbnailCache with cxx

 crates/npc-engine/build.rs                       |  1 +
 crates/npc-engine/src/lib.rs                     | 15 ++++++++++
 crates/npc-engine/src/library/thumbnail_cache.rs | 35 +++++++-----------------
 niepce-main/build.rs                             |  1 +
 src/fwk/cxx_prelude.hpp                          |  4 +++
 src/libraryclient/libraryclient.cpp              |  9 +++---
 src/libraryclient/libraryclient.hpp              |  8 +++---
 src/rust_bindings.hpp                            |  1 +
 8 files changed, 40 insertions(+), 34 deletions(-)
---
diff --git a/crates/npc-engine/build.rs b/crates/npc-engine/build.rs
index f4e5332f..dd3bd0ec 100644
--- a/crates/npc-engine/build.rs
+++ b/crates/npc-engine/build.rs
@@ -36,6 +36,7 @@ fn main() {
             .exclude_item("LibFolder")
             .exclude_item("LibMetadata")
             .exclude_item("FolderVirtualType")
+            .exclude_item("ThumbnailCache")
             // Ensure these are opaque as generics are still a problem.
             .exclude_item("NiepcePropertySet")
             .exclude_item("NiepcePropertyBag")
diff --git a/crates/npc-engine/src/lib.rs b/crates/npc-engine/src/lib.rs
index 62fa8d50..9138a7b6 100644
--- a/crates/npc-engine/src/lib.rs
+++ b/crates/npc-engine/src/lib.rs
@@ -146,6 +146,7 @@ pub type NiepcePropertySet = PropertySet;
 pub type NiepcePropertyBag = PropertyBag;
 
 use crate::db::{Keyword, Label, LibFile, LibFolder, LibMetadata};
+use crate::library::thumbnail_cache::{thumbnail_cache_new, ThumbnailCache};
 
 #[cxx::bridge(namespace = "eng")]
 mod ffi {
@@ -252,4 +253,18 @@ mod ffi {
         fn property_set_new() -> Box<PropertySet>;
         fn add(&mut self, v: u32);
     }
+
+    extern "C++" {
+        type LcChannel;
+    }
+
+    extern "Rust" {
+        type ThumbnailCache;
+
+        #[cxx_name = "ThumbnailCache_new"]
+        pub unsafe fn thumbnail_cache_new(
+            dir: &str,
+            channel: *const LcChannel,
+        ) -> Box<ThumbnailCache>;
+    }
 }
diff --git a/crates/npc-engine/src/library/thumbnail_cache.rs 
b/crates/npc-engine/src/library/thumbnail_cache.rs
index 1a58a8b6..0605c1a0 100644
--- a/crates/npc-engine/src/library/thumbnail_cache.rs
+++ b/crates/npc-engine/src/library/thumbnail_cache.rs
@@ -17,10 +17,8 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-use libc::c_char;
 use std::cmp;
 use std::collections::VecDeque;
-use std::ffi::CStr;
 use std::fs;
 use std::path::{Path, PathBuf};
 use std::sync;
@@ -32,7 +30,6 @@ use crate::db::LibraryId;
 use crate::library::notification;
 use crate::library::notification::LibNotification::{FileStatusChanged, ThumbnailLoaded};
 use crate::library::notification::{FileStatusChange, LcChannel, LibNotification};
-use crate::library::queriedcontent::QueriedContent;
 use npc_fwk::toolkit;
 use npc_fwk::toolkit::thumbnail::Thumbnail;
 use npc_fwk::{dbg_out, err_out};
@@ -222,28 +219,16 @@ impl ThumbnailCache {
     }
 }
 
+// cxx
 /// # Safety
 /// Dereference raw pointer.
-#[no_mangle]
-pub unsafe extern "C" fn engine_library_thumbnail_cache_new(
-    dir: *const c_char,
-    channel: *const LcChannel,
-) -> *mut ThumbnailCache {
-    let path = PathBuf::from(&*CStr::from_ptr(dir).to_string_lossy());
-    Box::into_raw(Box::new(ThumbnailCache::new(&path, (*channel).0.clone())))
-}
-
-/// # Safety
-/// Dereference raw pointer.
-#[no_mangle]
-pub unsafe extern "C" fn engine_library_thumbnail_cache_delete(obj: *mut ThumbnailCache) {
-    drop(Box::from_raw(obj));
-}
-
-#[no_mangle]
-pub extern "C" fn engine_library_thumbnail_cache_request(
-    self_: &mut ThumbnailCache,
-    content: &QueriedContent,
-) {
-    self_.request(content.get_content())
+pub unsafe fn thumbnail_cache_new(
+    dir: &str,
+    channel: *const crate::ffi::LcChannel,
+) -> Box<ThumbnailCache> {
+    let channel = channel as *const LcChannel;
+    Box::new(ThumbnailCache::new(
+        &PathBuf::from(&dir),
+        (*channel).0.clone(),
+    ))
 }
diff --git a/niepce-main/build.rs b/niepce-main/build.rs
index 216b9413..a45365b8 100644
--- a/niepce-main/build.rs
+++ b/niepce-main/build.rs
@@ -47,6 +47,7 @@ fn main() {
             .exclude_item("GFileInfo")
             .exclude_item("PortableChannel")
             .exclude_item("Option")
+            .exclude_item("ThumbnailCache")
             .with_crate(&crate_dir)
             .generate()
             .expect("Couldn't generate bindings")
diff --git a/src/fwk/cxx_prelude.hpp b/src/fwk/cxx_prelude.hpp
index afd0fb2a..b8d8593a 100644
--- a/src/fwk/cxx_prelude.hpp
+++ b/src/fwk/cxx_prelude.hpp
@@ -11,3 +11,7 @@ namespace fwk {
 class WrappedPropertyBag;
 class PropertyValue;
 }
+
+namespace eng {
+class LcChannel;
+}
diff --git a/src/libraryclient/libraryclient.cpp b/src/libraryclient/libraryclient.cpp
index 67018c2b..8cf0b1de 100644
--- a/src/libraryclient/libraryclient.cpp
+++ b/src/libraryclient/libraryclient.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - libraryclient/libraryclient.cpp
  *
- * Copyright (C) 2007-2020 Hubert Figuière
+ * Copyright (C) 2007-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
@@ -36,16 +36,15 @@ LibraryClient::LibraryClient(const fwk::Moniker & moniker,
     : m_client(
         ffi::libraryclient_new(moniker.path().c_str(), channel.get()),
         ffi::libraryclient_delete)
-    , m_thumbnailCache(ffi::engine_library_thumbnail_cache_new(
-                           std::string(moniker.path() + "/" + s_thumbcacheDirname).c_str(),
-                           channel.get()))
+    , m_thumbnailCache(eng::ThumbnailCache_new(
+                           moniker.path() + "/" + s_thumbcacheDirname,
+                           (const eng::LcChannel*)channel.get()))
     , m_uidataprovider(new UIDataProvider())
 {
 }
 
 LibraryClient::~LibraryClient()
 {
-    ffi::engine_library_thumbnail_cache_delete(m_thumbnailCache);
 }
 
 }
diff --git a/src/libraryclient/libraryclient.hpp b/src/libraryclient/libraryclient.hpp
index a169530a..a7c94d5f 100644
--- a/src/libraryclient/libraryclient.hpp
+++ b/src/libraryclient/libraryclient.hpp
@@ -1,7 +1,7 @@
 /*
  * niepce - libraryclient/libraryclient.hpp
  *
- * Copyright (C) 2007-2020 Hubert Figuière
+ * Copyright (C) 2007-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
@@ -41,8 +41,8 @@ public:
     LibraryClient(const fwk::Moniker & moniker, const std::shared_ptr<ffi::LcChannel>& channel);
     virtual ~LibraryClient();
 
-    ffi::ThumbnailCache* thumbnailCache()
-        { return m_thumbnailCache; }
+    eng::ThumbnailCache* thumbnailCache()
+        { return &*m_thumbnailCache; }
 
     const UIDataProviderPtr& getDataProvider() const
         { return m_uidataprovider; }
@@ -53,7 +53,7 @@ public:
 private:
     std::shared_ptr<ffi::LibraryClientWrapper> m_client;
 
-    ffi::ThumbnailCache* m_thumbnailCache;
+    rust::Box<eng::ThumbnailCache> m_thumbnailCache;
     UIDataProviderPtr m_uidataprovider;
 };
 
diff --git a/src/rust_bindings.hpp b/src/rust_bindings.hpp
index fa762b63..e49e1117 100644
--- a/src/rust_bindings.hpp
+++ b/src/rust_bindings.hpp
@@ -46,6 +46,7 @@ typedef eng::Keyword Keyword;
 typedef fwk::PropertyBag NiepcePropertyBag;
 typedef fwk::PropertySet NiepcePropertySet;
 typedef eng::FolderVirtualType FolderVirtualType;
+typedef eng::ThumbnailCache ThumbnailCache;
 }
 
 #include "target/eng_bindings.h"


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