[niepce: 7/29] engine+rust: Keyword is now implemented in Rust



commit ee1a07c0f59e6b3ec00a99e11d3b02503671b000
Author: Hubert Figuière <hub figuiere net>
Date:   Tue Jun 6 23:05:25 2017 -0400

    engine+rust: Keyword is now implemented in Rust

 src/engine/Makefile.am                    |    4 +-
 src/engine/db/keyword.cpp                 |   16 ++++----
 src/engine/db/keyword.hpp                 |   35 +++++------------
 src/engine/db/{storage.hpp => keyword.rs} |   58 ++++++++++++-----------------
 src/engine/db/libfile.hpp                 |    3 -
 src/engine/db/library.cpp                 |    6 +-
 src/engine/db/library.hpp                 |    2 +-
 src/engine/db/mod.rs                      |   49 ++++++++++++++++++++++++
 src/engine/db/test_library.cpp            |    2 +-
 src/engine/library/commands.cpp           |    2 +-
 src/engine/library/notification.hpp       |    4 +-
 src/engine/{db/storage.cpp => mod.rs}     |   14 +-----
 src/fwk/mod.rs                            |   27 +++++++++++++
 src/lib.rs                                |   26 +------------
 src/libraryclient/Makefile.am             |    5 ++-
 src/libraryclient/libraryclient.cpp       |    2 +
 src/libraryclient/libraryclient.hpp       |    4 --
 src/niepce/Makefile.am                    |    2 +-
 src/niepce/ui/workspacecontroller.cpp     |   11 +++--
 src/niepce/ui/workspacecontroller.hpp     |    4 +-
 20 files changed, 149 insertions(+), 127 deletions(-)
---
diff --git a/src/engine/Makefile.am b/src/engine/Makefile.am
index f71d219..09f94a6 100644
--- a/src/engine/Makefile.am
+++ b/src/engine/Makefile.am
@@ -11,9 +11,11 @@ TEST_LIBS =  \
        $(top_builddir)/src/fwk/utils/libniepceutils.a \
        $(top_builddir)/src/fwk/toolkit/libniepceframework.a \
        $(top_builddir)/src/fwk/base/libfwkbase.a \
+       $(top_builddir)/target/debug/libniece_rust.a \
        @BOOST_UNIT_TEST_FRAMEWORK_LIBS@ \
        @FRAMEWORK_LIBS@ \
        @OPENRAW_LIBS@ \
+       -ldl \
        $(NULL)
 
 
@@ -38,7 +40,7 @@ libniepceengine_a_SOURCES = \
        db/label.hpp \
        db/libmetadata.hpp db/libmetadata.cpp \
        db/keyword.hpp db/keyword.cpp \
-       db/storage.hpp db/storage.cpp \
+       db/storage.hpp \
        db/fsfile.hpp db/fsfile.cpp \
        db/filebundle.hpp db/filebundle.cpp \
        db/metadata.hpp \
diff --git a/src/engine/db/keyword.cpp b/src/engine/db/keyword.cpp
index 2848652..9875ff6 100644
--- a/src/engine/db/keyword.cpp
+++ b/src/engine/db/keyword.cpp
@@ -1,7 +1,7 @@
 /*
- * niepce - db/keyword.cpp
+ * niepce - engine/db/keyword.cpp
  *
- * Copyright (C) 2007 Hubert Figuiere
+ * Copyright (C) 2007-2017 Hubert Figuiere
  *
  * 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
@@ -18,16 +18,16 @@
  */
 
 
-
 #include "keyword.hpp"
 
+extern "C" eng::Keyword* engine_db_keyword_new(eng::library_id_t id, const char* keyword);
+extern "C" void engine_db_keyword_delete(eng::Keyword*);
 
 namespace eng {
 
-       Keyword::Keyword(library_id_t _id, const std::string & _keyword)
-               : m_id(_id), m_keyword(_keyword)
-       {
-       }
-
+KeywordPtr keyword_new(eng::library_id_t id, const char* keyword) {
+  return KeywordPtr(
+    engine_db_keyword_new(id, keyword), &engine_db_keyword_delete);
+}
 
 }
diff --git a/src/engine/db/keyword.hpp b/src/engine/db/keyword.hpp
index 67a5aba..b7ea91a 100644
--- a/src/engine/db/keyword.hpp
+++ b/src/engine/db/keyword.hpp
@@ -1,7 +1,7 @@
 /*
  * niepce - eng/db/keyword.hpp
  *
- * Copyright (C) 2007-2013 Hubert Figuiere
+ * Copyright (C) 2007-2017 Hubert Figuiere
  *
  * 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
@@ -18,36 +18,23 @@
  */
 
 
-#ifndef __NIEPCE_DB_KEYWORD_H__
-#define __NIEPCE_DB_KEYWORD_H__
+#pragma once
 
-#include <string>
 #include <vector>
 #include <memory>
 
 #include "engine/db/librarytypes.hpp"
 
+// just a rust interface.
 namespace eng {
+class Keyword;
+typedef std::shared_ptr<Keyword> KeywordPtr;
+typedef std::vector<KeywordPtr> KeywordList;
+typedef std::shared_ptr<KeywordList> KeywordListPtr;
 
-       class Keyword
-       {
-       public:
-               typedef std::shared_ptr<Keyword> Ptr;
-               typedef std::vector<Ptr> List;
-               typedef std::shared_ptr<List> ListPtr;
-               typedef std::vector<int> IdList;
-
-               Keyword(library_id_t id, const std::string & keyword);
-
-               library_id_t id() const
-                       { return m_id; }
-               const std::string & keyword()
-                       { return m_keyword; }
-       private:
-               library_id_t m_id;
-               std::string m_keyword;
-       };
-
+KeywordPtr keyword_new(eng::library_id_t id, const char* keyword);
 }
 
-#endif
+extern "C" eng::library_id_t engine_db_keyword_id(const eng::Keyword*);
+extern "C" const char* engine_db_keyword_keyword(const eng::Keyword*);
+
diff --git a/src/engine/db/storage.hpp b/src/engine/db/keyword.rs
similarity index 52%
rename from src/engine/db/storage.hpp
rename to src/engine/db/keyword.rs
index 4271052..ae4abed 100644
--- a/src/engine/db/storage.hpp
+++ b/src/engine/db/keyword.rs
@@ -1,7 +1,7 @@
 /*
- * niepce - eng/db/storage.hpp
+ * niepce - engine/db/keyword.rs
  *
- * Copyright (C) 2007-2013 Hubert Figuiere
+ * Copyright (C) 2017 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,38 +17,28 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+use super::LibraryId;
+use std::ffi::CString;
 
-#ifndef __NIEPCE_LIBRARY_STORAGE_H__
-#define __NIEPCE_LIBRARY_STORAGE_H__
-
-#include <memory>
-
-#include "engine/db/keyword.hpp"
-
-namespace eng {
-
-/** @brief the interface for a storage */
-class Storage
-{
-public:
-
-    virtual ~Storage();
-
-    virtual bool fetchKeywordsForFile(int file, Keyword::IdList &keywords) = 0;
-};
-
-typedef std::shared_ptr<Storage> StoragePtr;
-
+pub struct Keyword {
+    id: LibraryId,
+    keyword: String,
+    pub cstr: CString,
 }
 
-/*
-  Local Variables:
-  mode:c++
-  c-file-style:"stroustrup"
-  c-file-offsets:((innamespace . 0))
-  indent-tabs-mode:nil
-  fill-column:99
-  End:
-*/
-
-#endif
+impl Keyword {
+    pub fn new(id: LibraryId, keyword: &str) -> Keyword {
+        Keyword {
+            id: id, keyword: String::from(keyword),
+            cstr: CString::new("").unwrap()
+        }
+    }
+
+    pub fn id(&self) -> LibraryId {
+        self.id
+    }
+
+    pub fn keyword(&self) -> &String {
+        &self.keyword
+    }
+}
diff --git a/src/engine/db/libfile.hpp b/src/engine/db/libfile.hpp
index 64c0b72..26caa63 100644
--- a/src/engine/db/libfile.hpp
+++ b/src/engine/db/libfile.hpp
@@ -28,7 +28,6 @@
 #include "fwk/base/propertybag.hpp"
 #include "engine/db/librarytypes.hpp"
 #include "engine/db/keyword.hpp"
-#include "engine/db/storage.hpp"
 #include "engine/db/fsfile.hpp"
 
 namespace eng {
@@ -66,8 +65,6 @@ public:
     const std::string & path() const
         { return m_main_file.path(); }
 
-//             Storage::Ptr storage() const;
-
     void setOrientation(int32_t v);
     int32_t orientation() const
         { return m_orientation; }
diff --git a/src/engine/db/library.cpp b/src/engine/db/library.cpp
index fa22705..b55d8f2 100644
--- a/src/engine/db/library.cpp
+++ b/src/engine/db/library.cpp
@@ -552,7 +552,7 @@ int Library::countFolder(library_id_t folder_id)
     return count;
 }
 
-void Library::getAllKeywords(const Keyword::ListPtr & l)
+void Library::getAllKeywords(const KeywordListPtr & l)
 {
     SQLStatement sql("SELECT id,keyword FROM keywords ORDER BY keyword");
     try {
@@ -562,7 +562,7 @@ void Library::getAllKeywords(const Keyword::ListPtr & l)
                 std::string name;
                 m_dbdrv->get_column_content(0, id);
                 m_dbdrv->get_column_content(1, name);
-                l->push_back(Keyword::Ptr(new Keyword(id, name)));
+                l->push_back(keyword_new(id, name.c_str()));
             }
         }
     }
@@ -596,7 +596,7 @@ library_id_t Library::makeKeyword(const std::string & keyword)
         try {
             if(m_dbdrv->execute_statement(sql2)) {
                 keyword_id = m_dbdrv->last_row_id();
-                Keyword::Ptr kw(new Keyword(keyword_id, keyword));
+                KeywordPtr kw(keyword_new(keyword_id, keyword.c_str()));
                 notify(LibNotification::make<LibNotification::Type::ADDED_KEYWORD>({kw}));
             }
         }
diff --git a/src/engine/db/library.hpp b/src/engine/db/library.hpp
index 99cd5e0..05068de 100644
--- a/src/engine/db/library.hpp
+++ b/src/engine/db/library.hpp
@@ -146,7 +146,7 @@ public:
      */
     void getFolderContent(library_id_t folder_id, const LibFile::ListPtr & fl);
     int countFolder(library_id_t folder_id);
-    void getAllKeywords(const Keyword::ListPtr & l);
+    void getAllKeywords(const KeywordListPtr & l);
     void getKeywordContent(library_id_t keyword_id, const LibFile::ListPtr & fl);
     /** get the metadata block (XMP) */
     void getMetaData(library_id_t file_id, const LibMetadata::Ptr & );
diff --git a/src/engine/db/mod.rs b/src/engine/db/mod.rs
new file mode 100644
index 0000000..ada2df1
--- /dev/null
+++ b/src/engine/db/mod.rs
@@ -0,0 +1,49 @@
+/*
+ * niepce - eng/db/keyword.rs
+ *
+ * Copyright (C) 2017 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+mod keyword;
+
+pub type LibraryId = i64;
+
+use self::keyword::Keyword;
+use libc::c_char;
+use std::ffi::CStr;
+use std::ffi::CString;
+
+#[no_mangle]
+pub extern fn engine_db_keyword_new(id: i64, keyword: *const c_char) -> *mut Keyword {
+    let kw = Box::new(Keyword::new(id, &*unsafe { CStr::from_ptr(keyword) }.to_string_lossy()));
+    Box::into_raw(kw)
+}
+
+#[no_mangle]
+pub extern fn engine_db_keyword_id(this: &Keyword) -> i64 {
+    this.id() as i64
+}
+
+#[no_mangle]
+pub extern fn engine_db_keyword_keyword(this: &mut Keyword) -> *const c_char {
+    this.cstr = CString::new(this.keyword().clone()).unwrap();
+    this.cstr.as_ptr()
+}
+
+#[no_mangle]
+pub extern fn engine_db_keyword_delete(kw: *mut Keyword) {
+    unsafe { Box::from_raw(kw) };
+}
diff --git a/src/engine/db/test_library.cpp b/src/engine/db/test_library.cpp
index c1fe75f..7b7c1e8 100644
--- a/src/engine/db/test_library.cpp
+++ b/src/engine/db/test_library.cpp
@@ -89,7 +89,7 @@ int test_main(int, char *[])
     BOOST_CHECK(fl2->size() == 1);
     BOOST_CHECK(fl2->front()->id() == file_id);
 
-    eng::Keyword::ListPtr kl(new eng::Keyword::List);
+    eng::KeywordListPtr kl(new eng::KeywordList);
     lib.getAllKeywords(kl);
     BOOST_CHECK(kl->size() == 2);
 
diff --git a/src/engine/library/commands.cpp b/src/engine/library/commands.cpp
index 3a05d4e..eb533cb 100644
--- a/src/engine/library/commands.cpp
+++ b/src/engine/library/commands.cpp
@@ -39,7 +39,7 @@ namespace eng {
 
 void Commands::cmdListAllKeywords(const Library::Ptr & lib)
 {
-    Keyword::ListPtr l(new Keyword::List);
+    KeywordListPtr l(new KeywordList);
     lib->getAllKeywords(l);
     /////
     // notify folder added l
diff --git a/src/engine/library/notification.hpp b/src/engine/library/notification.hpp
index 0220b64..7241dee 100644
--- a/src/engine/library/notification.hpp
+++ b/src/engine/library/notification.hpp
@@ -61,11 +61,11 @@ public:
     };
 
     struct AddedKeyword {
-        Keyword::Ptr keyword;
+        KeywordPtr keyword;
     };
 
     struct AddedKeywords {
-        Keyword::ListPtr keywords;
+        KeywordListPtr keywords;
     };
 
     struct AddedLabels {
diff --git a/src/engine/db/storage.cpp b/src/engine/mod.rs
similarity index 82%
rename from src/engine/db/storage.cpp
rename to src/engine/mod.rs
index 452ba6b..8f653fc 100644
--- a/src/engine/db/storage.cpp
+++ b/src/engine/mod.rs
@@ -1,7 +1,7 @@
 /*
- * niepce - eng/db/storage.cpp
+ * niepce - engine/mod.rs
  *
- * Copyright (C) 2007 Hubert Figuiere
+ * Copyright (C) 2017 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,13 +17,5 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "storage.hpp"
+pub mod db;
 
-namespace eng {
-
-       Storage::~Storage()
-       {
-
-       }
-
-}
diff --git a/src/fwk/mod.rs b/src/fwk/mod.rs
index f5e3ee8..be8726c 100644
--- a/src/fwk/mod.rs
+++ b/src/fwk/mod.rs
@@ -30,3 +30,30 @@ pub use self::utils::exempi::{
 pub use self::base::fractions::{
     fraction_to_decimal
 };
+
+
+use std::f64;
+use std::ffi::CStr;
+use libc::c_char;
+
+#[no_mangle]
+pub extern fn fwk_gps_coord_from_xmp(cvalue: *const c_char) -> f64 {
+    let value = unsafe { CStr::from_ptr(cvalue) };
+    if let Ok(svalue) = value.to_str() {
+        if let Some(coord) = gps_coord_from_xmp(svalue) {
+            return coord;
+        }
+    }
+    f64::NAN
+}
+
+#[no_mangle]
+pub extern fn fwk_fraction_to_decimal(cvalue: *const c_char) -> f64 {
+    let value = unsafe { CStr::from_ptr(cvalue) };
+    if let Ok(svalue) = value.to_str() {
+        if let Some(dec) = fraction_to_decimal(svalue) {
+            return dec;
+        }
+    }
+    f64::NAN
+}
diff --git a/src/lib.rs b/src/lib.rs
index 144b951..5385367 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -21,29 +21,5 @@ extern crate exempi;
 extern crate libc;
 
 pub mod fwk;
+pub mod engine;
 
-use std::f64;
-use std::ffi::CStr;
-use libc::c_char;
-
-#[no_mangle]
-pub extern fn fwk_gps_coord_from_xmp(cvalue: *const c_char) -> f64 {
-    let value = unsafe { CStr::from_ptr(cvalue) };
-    if let Ok(svalue) = value.to_str() {
-        if let Some(coord) = fwk::gps_coord_from_xmp(svalue) {
-            return coord;
-        }
-    }
-    f64::NAN
-}
-
-#[no_mangle]
-pub extern fn fwk_fraction_to_decimal(cvalue: *const c_char) -> f64 {
-    let value = unsafe { CStr::from_ptr(cvalue) };
-    if let Ok(svalue) = value.to_str() {
-        if let Some(dec) = fwk::fraction_to_decimal(svalue) {
-            return dec;
-        }
-    }
-    f64::NAN
-}
diff --git a/src/libraryclient/Makefile.am b/src/libraryclient/Makefile.am
index dff284e..0b01907 100644
--- a/src/libraryclient/Makefile.am
+++ b/src/libraryclient/Makefile.am
@@ -12,9 +12,12 @@ TEST_LIBS =  \
         $(top_builddir)/src/fwk/utils/libniepceutils.a \
        $(top_builddir)/src/fwk/toolkit/libniepceframework.a \
        $(top_builddir)/src/fwk/base/libfwkbase.a \
+       $(top_builddir)/target/debug/libniece_rust.a \
         @BOOST_UNIT_TEST_FRAMEWORK_LIBS@ \
        @FRAMEWORK_LIBS@ \
-       @OPENRAW_LIBS@
+       @OPENRAW_LIBS@ \
+       -ldl \
+       $(NULL)
 
 test_worker_SOURCES = test_worker.cpp
 test_worker_LDADD = $(TEST_LIBS)
diff --git a/src/libraryclient/libraryclient.cpp b/src/libraryclient/libraryclient.cpp
index d05c74a..4f1325b 100644
--- a/src/libraryclient/libraryclient.cpp
+++ b/src/libraryclient/libraryclient.cpp
@@ -146,12 +146,14 @@ void LibraryClient::importFromDirectory(const std::string & dir, eng::Library::M
     m_pImpl->importFromDirectory(dir, manage);
 }
 
+#if 0
 bool LibraryClient::fetchKeywordsForFile(int /*file*/, 
                                          eng::Keyword::IdList & /*keywords*/)
 {
     // TODO
     return false;
 }
+#endif
 
 }
 /*
diff --git a/src/libraryclient/libraryclient.hpp b/src/libraryclient/libraryclient.hpp
index 77d89b6..27ff188 100644
--- a/src/libraryclient/libraryclient.hpp
+++ b/src/libraryclient/libraryclient.hpp
@@ -29,7 +29,6 @@
 #include "engine/library/thumbnailcache.hpp"
 #include "engine/db/library.hpp"
 #include "engine/db/librarytypes.hpp"
-#include "engine/db/storage.hpp"
 
 namespace fwk {
 class Moniker;
@@ -43,7 +42,6 @@ class UIDataProvider;
 class ClientImpl;
 
 class LibraryClient
-    : public eng::Storage
 {
 public:
     NON_COPYABLE(LibraryClient);
@@ -99,8 +97,6 @@ public:
     eng::ThumbnailCache & thumbnailCache()
         { return m_thumbnailCache; }
 
-    /* sync call */
-    virtual bool fetchKeywordsForFile(int file, eng::Keyword::IdList &keywords) override;
     const std::unique_ptr<UIDataProvider>& getDataProvider() const
         { return m_uidataprovider; }
 
diff --git a/src/niepce/Makefile.am b/src/niepce/Makefile.am
index de334a7..b9cebaf 100644
--- a/src/niepce/Makefile.am
+++ b/src/niepce/Makefile.am
@@ -21,7 +21,7 @@ niepce_LDADD = \
        $(top_builddir)/src/fwk/base/libfwkbase.a \
        $(top_builddir)/src/ncr/libncr.a \
        $(top_builddir)/src/ext/libview/libview.a \
-       $(top_builddir)/target/release/libniece_rust.a \
+       $(top_builddir)/target/debug/libniece_rust.a \
        @FRAMEWORK_LIBS@ \
        @GPHOTO_LIBS@ \
        @BABL_LIBS@ \
diff --git a/src/niepce/ui/workspacecontroller.cpp b/src/niepce/ui/workspacecontroller.cpp
index d07a5d1..5ab6874 100644
--- a/src/niepce/ui/workspacecontroller.cpp
+++ b/src/niepce/ui/workspacecontroller.cpp
@@ -106,7 +106,7 @@ void WorkspaceController::on_lib_notification(const eng::LibNotification &ln)
         auto l = ln.get<eng::LibNotification::Type::ADDED_KEYWORDS>().keywords;
         DBG_ASSERT(static_cast<bool>(l), "keyword list must not be NULL");
         for_each(l->cbegin(), l->cend(),
-                 [this] (const eng::Keyword::Ptr& k) {
+                 [this] (const eng::KeywordPtr& k) {
                      this->add_keyword_item(k);
                  });
         break;
@@ -209,15 +209,16 @@ void WorkspaceController::on_row_collapsed(const Gtk::TreeIter& iter,
 }
 
 
-void WorkspaceController::add_keyword_item(const eng::Keyword::Ptr & k)
+void WorkspaceController::add_keyword_item(const eng::KeywordPtr & k)
 {
     auto children = m_keywordsNode->children();
     bool was_empty = children.empty();
     auto iter = add_item(m_treestore, children,
-                         m_icons[ICON_KEYWORD], k->keyword(), k->id(),
-                         KEYWORD_ITEM);
+                         m_icons[ICON_KEYWORD],
+                         engine_db_keyword_keyword(k.get()),
+                         engine_db_keyword_id(k.get()), KEYWORD_ITEM);
 //             getLibraryClient()->countKeyword(f->id());
-    m_keywordsidmap[k->id()] = iter;
+    m_keywordsidmap[engine_db_keyword_id(k.get())] = 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 a37539c..6b2e033 100644
--- a/src/niepce/ui/workspacecontroller.hpp
+++ b/src/niepce/ui/workspacecontroller.hpp
@@ -100,8 +100,8 @@ private:
     /** add a folder item to the treeview */
     void add_folder_item(const eng::LibFolder::Ptr & f);
     /** add a keyword item to the treeview */
-    void add_keyword_item(const eng::Keyword::Ptr & k);
-    /** add a tree item in the treeview 
+    void add_keyword_item(const eng::KeywordPtr & k);
+    /** add a tree item in the treeview
      * @param treestore the treestore to add to
      * @param childrens the children subtree to add to
      * @param icon the icon for the item


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