[niepce] fwk: added wrapper for Rust CString



commit 47adaf413b03c79dfba81531c4888574d208f930
Author: Hubert Figuière <hub figuiere net>
Date:   Fri Feb 7 11:31:23 2020 -0500

    fwk: added wrapper for Rust CString

 src/fwk/base/Makefile.am              |  1 +
 src/fwk/base/colour.cpp               | 11 ++++-----
 src/fwk/base/date.cpp                 |  9 ++++---
 src/fwk/base/propertybag.cpp          | 14 +++++------
 src/fwk/base/string.cpp               | 31 ++++++++++++++++++++++++
 src/fwk/base/string.hpp               | 44 +++++++++++++++++++++++++++++++++++
 src/niepce/ui/workspacecontroller.cpp |  8 +++----
 7 files changed, 95 insertions(+), 23 deletions(-)
---
diff --git a/src/fwk/base/Makefile.am b/src/fwk/base/Makefile.am
index 6952750..538837b 100644
--- a/src/fwk/base/Makefile.am
+++ b/src/fwk/base/Makefile.am
@@ -42,6 +42,7 @@ libfwkbase_a_SOURCES = colour.hpp colour.cpp \
        moniker.hpp moniker.cpp \
        geometry.hpp geometry.cpp \
        singleton.hpp \
+       string.hpp string.cpp \
        util.hpp \
        map.hpp \
        option.hpp \
diff --git a/src/fwk/base/colour.cpp b/src/fwk/base/colour.cpp
index 97fc51f..fd61083 100644
--- a/src/fwk/base/colour.cpp
+++ b/src/fwk/base/colour.cpp
@@ -1,7 +1,7 @@
 /*
- * niepce - fwk/base/colour.hpp
+ * niepce - fwk/base/colour.cpp
  *
- * Copyright (C) 2009 Hubert Figuiere
+ * Copyright (C) 2009-2020 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,6 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "fwk/base/string.hpp"
 #include "fwk/base/colour.hpp"
 
 #include "rust_bindings.hpp"
@@ -48,10 +49,8 @@ std::string rgbcolour_to_string(uint16_t r, uint16_t g, uint16_t b)
 
 std::string rgbcolour_to_string(const RgbColour* c)
 {
-  char* p = ffi::fwk_rgbcolour_to_string(c);
-  std::string s(p);
-  ffi::rust_cstring_delete(p);
-  return s;
+  auto p = fwk::RustFfiString(ffi::fwk_rgbcolour_to_string(c));
+  return p.str();
 }
 
 }
diff --git a/src/fwk/base/date.cpp b/src/fwk/base/date.cpp
index 31ac274..2a61bb4 100644
--- a/src/fwk/base/date.cpp
+++ b/src/fwk/base/date.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - fwk/base/date.cpp
  *
- * Copyright (C) 2012-2017 Hubert Figuiere
+ * Copyright (C) 2012-2020 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
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <time.h>
 
+#include "string.hpp"
 #include "date.hpp"
 #include "debug.hpp"
 
@@ -44,10 +45,8 @@ std::string date_to_string(const Date* d)
     if (!d) {
         return "";
     }
-    char* p = ffi::fwk_date_to_string(d);
-    std::string s(p);
-    ffi::rust_cstring_delete(p);
-    return s;
+    auto p = fwk::RustFfiString(ffi::fwk_date_to_string(d));
+    return p.str();
 }
 
 }
diff --git a/src/fwk/base/propertybag.cpp b/src/fwk/base/propertybag.cpp
index d24df2e..9287244 100644
--- a/src/fwk/base/propertybag.cpp
+++ b/src/fwk/base/propertybag.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - fwk/base/propertybag.cpp
  *
- * Copyright (C) 2011-2017 Hubert Figuiere
+ * Copyright (C) 2011-2020 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
@@ -18,6 +18,7 @@
  */
 
 
+#include "string.hpp"
 #include "debug.hpp"
 #include "propertybag.hpp"
 
@@ -64,10 +65,8 @@ PropertyValuePtr property_value_new(const std::vector<std::string>& sa)
 
 std::string property_value_get_string(const PropertyValue &value)
 {
-    auto s = ffi::fwk_property_value_get_string(&value);
-    std::string str(s);
-    ffi::rust_cstring_delete(s);
-    return str;
+    auto s = fwk::RustFfiString(ffi::fwk_property_value_get_string(&value));
+    return s.str();
 }
 
 std::vector<std::string> property_value_get_string_array(const PropertyValue &value)
@@ -75,9 +74,8 @@ std::vector<std::string> property_value_get_string_array(const PropertyValue &va
     std::vector<std::string> v;
     auto len = ffi::fwk_property_value_count_string_array(&value);
     for (size_t i = 0; i < len; i++) {
-        auto s = ffi::fwk_property_value_get_string_at(&value, i);
-        v.push_back(s);
-        ffi::rust_cstring_delete(s);
+        auto s = fwk::RustFfiString(ffi::fwk_property_value_get_string_at(&value, i));
+        v.push_back(s.str());
     }
     return v;
 }
diff --git a/src/fwk/base/string.cpp b/src/fwk/base/string.cpp
new file mode 100644
index 0000000..0869ac7
--- /dev/null
+++ b/src/fwk/base/string.cpp
@@ -0,0 +1,31 @@
+/*
+ * niepce - fwk/base/string.cpp
+ *
+ * Copyright (C) 2020 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/>.
+ */
+
+#include "string.hpp"
+
+#include "rust_bindings.hpp"
+
+namespace fwk {
+
+RustFfiString::RustFfiString(char* p)
+  : ptr(p, ffi::rust_cstring_delete)
+{
+}
+
+}
diff --git a/src/fwk/base/string.hpp b/src/fwk/base/string.hpp
new file mode 100644
index 0000000..9e614fb
--- /dev/null
+++ b/src/fwk/base/string.hpp
@@ -0,0 +1,44 @@
+/*
+ * niepce - fwk/base/string.hpp
+ *
+ * Copyright (C) 2020 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/>.
+ */
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+namespace fwk {
+
+/** A string coming from rust CString. */
+class RustFfiString {
+public:
+  RustFfiString(char* p);
+
+  const char* c_str() const {
+    return ptr.get();
+  }
+
+  std::string str() const {
+    return std::string(ptr.get());
+  }
+
+private:
+  std::shared_ptr<char> ptr;
+};
+
+}
diff --git a/src/niepce/ui/workspacecontroller.cpp b/src/niepce/ui/workspacecontroller.cpp
index ec0b433..ef3c609 100644
--- a/src/niepce/ui/workspacecontroller.cpp
+++ b/src/niepce/ui/workspacecontroller.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - ui/workspacecontroller.cpp
  *
- * Copyright (C) 2007-2019 Hubert Figuière
+ * Copyright (C) 2007-2020 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
@@ -25,6 +25,7 @@
 #include <gtkmm/image.h>
 
 #include "fwk/base/debug.hpp"
+#include "fwk/base/string.hpp"
 #include "fwk/toolkit/application.hpp"
 #include "fwk/toolkit/configuration.hpp"
 #include "fwk/toolkit/gtkutils.hpp"
@@ -320,11 +321,10 @@ void WorkspaceController::add_keyword_item(const eng::Keyword* k)
 {
     auto children = m_keywordsNode->children();
     bool was_empty = children.empty();
-    char* keyword = engine_db_keyword_keyword(k);
+    auto keyword = fwk::RustFfiString(engine_db_keyword_keyword(k));
     auto iter = add_item(m_treestore, children,
-                         m_icons[ICON_KEYWORD], keyword,
+                         m_icons[ICON_KEYWORD], keyword.c_str(),
                          engine_db_keyword_id(k), KEYWORD_ITEM);
-    ffi::rust_cstring_delete(keyword);
     ffi::libraryclient_count_keyword(getLibraryClient()->client(), engine_db_keyword_id(k));
     m_keywordsidmap[engine_db_keyword_id(k)] = iter;
     if(was_empty) {


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