[niepce] npc-fwk: Bind Date with cxx



commit 809c5f537702d9f4c24b0b426bb3a0b6dbde1e60
Author: Hubert Figuière <hub figuiere net>
Date:   Sat Oct 15 12:32:48 2022 -0400

    npc-fwk: Bind Date with cxx
    
    - temporary wrap it
    - remove some unused functions

 crates/npc-engine/src/db/libmetadata.rs  |  5 +--
 crates/npc-fwk/build.rs                  |  1 +
 crates/npc-fwk/src/base/date.rs          | 27 +++++++++------
 crates/npc-fwk/src/base/propertyvalue.rs |  6 ++--
 crates/npc-fwk/src/lib.rs                |  9 +++++
 src/fwk/base/Makefile.am                 |  1 -
 src/fwk/base/date.cpp                    | 57 --------------------------------
 src/fwk/base/date.hpp                    | 52 -----------------------------
 src/fwk/base/propertybag.cpp             |  5 ---
 src/fwk/base/propertybag.hpp             |  1 -
 src/fwk/toolkit/metadatawidget.cpp       | 10 ++----
 src/rust_bindings.hpp                    |  6 ++--
 12 files changed, 40 insertions(+), 140 deletions(-)
---
diff --git a/crates/npc-engine/src/db/libmetadata.rs b/crates/npc-engine/src/db/libmetadata.rs
index 0eec89e9..a61b4715 100644
--- a/crates/npc-engine/src/db/libmetadata.rs
+++ b/crates/npc-engine/src/db/libmetadata.rs
@@ -1,7 +1,7 @@
 /*
  * niepce - eng/db/libmetadata.rs
  *
- * Copyright (C) 2017-2021 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
@@ -24,6 +24,7 @@ use super::props;
 use super::NiepceProperties as Np;
 use super::{FromDb, LibraryId};
 use crate::{NiepcePropertyBag, NiepcePropertySet};
+use npc_fwk::base::date::Date;
 use npc_fwk::utils::exempi::{NS_DC, NS_XAP};
 use npc_fwk::{dbg_out, err_out};
 use npc_fwk::{xmp_date_from, PropertyBag, PropertySet, PropertyValue, XmpMeta};
@@ -207,7 +208,7 @@ impl LibMetadata {
                     }
                 }
                 Np::Index(NpExifDateTimeOriginalProp) => {
-                    if let Some(date) = self.xmp.creation_date() {
+                    if let Some(date) = self.xmp.creation_date().map(Date) {
                         props.set_value(*prop_id, PropertyValue::Date(date));
                     }
                 }
diff --git a/crates/npc-fwk/build.rs b/crates/npc-fwk/build.rs
index 2a7961ce..58c71e8f 100644
--- a/crates/npc-fwk/build.rs
+++ b/crates/npc-fwk/build.rs
@@ -21,6 +21,7 @@ fn main() {
             .with_parse_exclude(&[
                 "exempi", "chrono", "multimap", "glib", "clap", "winapi", "strum",
             ])
+            .exclude_item("Date")
             .exclude_item("CUSTOM_START")
             .exclude_item("INTERNAL_START")
             .exclude_item("GdkPixbuf")
diff --git a/crates/npc-fwk/src/base/date.rs b/crates/npc-fwk/src/base/date.rs
index 080e1423..7945790c 100644
--- a/crates/npc-fwk/src/base/date.rs
+++ b/crates/npc-fwk/src/base/date.rs
@@ -17,12 +17,26 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-use std::ffi::CString;
-
 use chrono::{Datelike, Timelike};
 
 pub type Time = i64;
-pub type Date = chrono::DateTime<chrono::Utc>;
+// XXX a tuple for the cxx bindings
+#[derive(Clone, Copy, Debug)]
+pub struct Date(pub chrono::DateTime<chrono::Utc>);
+
+impl std::string::ToString for Date {
+    fn to_string(&self) -> String {
+        self.0.to_string()
+    }
+}
+
+impl std::ops::Deref for Date {
+    type Target = chrono::DateTime<chrono::Utc>;
+
+    fn deref(&self) -> &chrono::DateTime<chrono::Utc> {
+        &self.0
+    }
+}
 
 pub fn xmp_date_from(d: &chrono::DateTime<chrono::Utc>) -> exempi::DateTime {
     let mut xmp_date = exempi::DateTime::new();
@@ -32,10 +46,3 @@ pub fn xmp_date_from(d: &chrono::DateTime<chrono::Utc>) -> exempi::DateTime {
 
     xmp_date
 }
-
-#[no_mangle]
-pub extern "C" fn fwk_date_to_string(date: &Date) -> *mut libc::c_char {
-    CString::new(date.to_string().as_bytes())
-        .unwrap()
-        .into_raw()
-}
diff --git a/crates/npc-fwk/src/base/propertyvalue.rs b/crates/npc-fwk/src/base/propertyvalue.rs
index 3b61455d..68eef699 100644
--- a/crates/npc-fwk/src/base/propertyvalue.rs
+++ b/crates/npc-fwk/src/base/propertyvalue.rs
@@ -1,7 +1,7 @@
 /*
  * niepce - fwk/base/propertyvalue.rs
  *
- * Copyright (C) 2017-2019 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
@@ -97,9 +97,9 @@ pub extern "C" fn fwk_property_value_is_date(v: &PropertyValue) -> bool {
 }
 
 #[no_mangle]
-pub extern "C" fn fwk_property_value_get_date(v: &PropertyValue) -> *const Date {
+pub extern "C" fn fwk_property_value_get_date(v: &PropertyValue) -> *mut Date {
     match *v {
-        PropertyValue::Date(ref d) => d,
+        PropertyValue::Date(ref d) => Box::into_raw(Box::new(*d)),
         _ => panic!("value is not Date"),
     }
 }
diff --git a/crates/npc-fwk/src/lib.rs b/crates/npc-fwk/src/lib.rs
index ac5057f9..006c5bda 100644
--- a/crates/npc-fwk/src/lib.rs
+++ b/crates/npc-fwk/src/lib.rs
@@ -44,6 +44,7 @@ pub fn init() {
     rexiv2::initialize().expect("Unable to initialize rexiv2");
 }
 
+use crate::base::date::Date;
 use crate::toolkit::Configuration;
 
 fn make_config_path(file: &str) -> String {
@@ -104,4 +105,12 @@ mod ffi {
         #[cxx_name = "fraction_to_decimal"]
         fn fraction_to_decimal_(value: &str) -> f64;
     }
+
+    extern "Rust" {
+        type Date;
+
+        fn to_string(&self) -> String;
+    }
+
+    impl Box<Date> {}
 }
diff --git a/src/fwk/base/Makefile.am b/src/fwk/base/Makefile.am
index e3b7a0b6..2ab6e932 100644
--- a/src/fwk/base/Makefile.am
+++ b/src/fwk/base/Makefile.am
@@ -37,7 +37,6 @@ testoption_LDADD = $(testing_ldadd)
 
 libfwkbase_a_SOURCES = colour.hpp colour.cpp \
        autoflag.hpp \
-       date.hpp date.cpp \
        debug.hpp debug.cpp \
        moniker.hpp moniker.cpp \
        geometry.hpp geometry.cpp \
diff --git a/src/fwk/base/propertybag.cpp b/src/fwk/base/propertybag.cpp
index 7b780873..88fb52e1 100644
--- a/src/fwk/base/propertybag.cpp
+++ b/src/fwk/base/propertybag.cpp
@@ -49,11 +49,6 @@ PropertyValuePtr property_value_new(int v)
     return property_value_wrap(ffi::fwk_property_value_new_int(v));
 }
 
-PropertyValuePtr property_value_new(const DatePtr& d)
-{
-    return property_value_wrap(ffi::fwk_property_value_new_date(d.get()));
-}
-
 PropertyValuePtr property_value_new(const std::vector<std::string>& sa)
 {
     PropertyValue* value = ffi::fwk_property_value_new_string_array();
diff --git a/src/fwk/base/propertybag.hpp b/src/fwk/base/propertybag.hpp
index 74e9f4e4..5d41c27d 100644
--- a/src/fwk/base/propertybag.hpp
+++ b/src/fwk/base/propertybag.hpp
@@ -25,7 +25,6 @@
 #include <set>
 #include <memory>
 
-#include "fwk/base/date.hpp"
 #include "fwk/base/option.hpp"
 
 #include "rust_bindings.hpp"
diff --git a/src/fwk/toolkit/metadatawidget.cpp b/src/fwk/toolkit/metadatawidget.cpp
index 785f9b85..9e17c313 100644
--- a/src/fwk/toolkit/metadatawidget.cpp
+++ b/src/fwk/toolkit/metadatawidget.cpp
@@ -397,14 +397,10 @@ bool MetaDataWidget::set_date_data(Gtk::Widget* w, const PropertyValuePtr& value
     }
     try {
         AutoFlag flag(m_update);
-        const fwk::Date* date = fwk_property_value_get_date(value.get());
-        if (date) {
-            static_cast<Gtk::Label*>(w)->set_text(fwk::date_to_string(date));
+        fwk::DatePtr date = fwk::DatePtr::from_raw(fwk_property_value_get_date(value.get()));
+        static_cast<Gtk::Label*>(w)->set_text(std::string(date->to_string()));
 
-            DBG_OUT("setting date data %s", fwk::date_to_string(date).c_str());
-        } else {
-            return false;
-        }
+        DBG_OUT("setting date data %s", date->to_string().c_str());
     }
     catch(...) {
         return false;
diff --git a/src/rust_bindings.hpp b/src/rust_bindings.hpp
index 86a72c72..27a3967b 100644
--- a/src/rust_bindings.hpp
+++ b/src/rust_bindings.hpp
@@ -23,11 +23,14 @@
 
 #include <gtk/gtk.h>
 
+#include "fwk/cxx_fwk_bindings.hpp"
+
 namespace ffi {
 class rust_str;
 struct Utc;
 template <class T>
 struct DateTime;
+typedef fwk::Date Date;
 typedef rust_str str;
 struct NiepcePropertyBag;
 struct NiepcePropertySet;
@@ -36,14 +39,13 @@ struct NiepcePropertySet;
 #include "target/fwk_bindings.h"
 #include "target/eng_bindings.h"
 #include "target/bindings.h"
-#include "fwk/cxx_fwk_bindings.hpp"
 
 namespace fwk {
 typedef std::shared_ptr<SharedConfiguration> ConfigurationPtr;
+typedef rust::Box<fwk::Date> DatePtr;
 typedef ffi::PropertyValue PropertyValue;
 typedef ffi::NiepcePropertyBag PropertyBag;
 typedef ffi::NiepcePropertySet PropertySet;
-typedef ffi::Date Date;
 typedef ffi::RgbColour RgbColour;
 typedef ffi::FileList FileList;
 }


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