[glibmm] Glib::KeyFile: Make it a _CLASS_OPAQUE_REFCOUNTED



commit 0cd517399a4fafa6578b63ba45cee8968b317058
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Sat Feb 2 10:08:42 2019 +0100

    Glib::KeyFile: Make it a _CLASS_OPAQUE_REFCOUNTED
    
    * examples/keyfile/main.cc:
    * gio/src/desktopappinfo.hg: Store KeyFile in a RefPtr.
    * glib/src/keyfile.[ccg|hg]: Replace _CLASS_GENERIC by _CLASS_OPAQUE_REFCOUNTED.
    Remove handcoded methods that are now generated by gmmproc.
    * tools/m4/convert_glib.m4: Update conversion for KeyFile.
    
    GKeyFile is refcounted since 2009.

 examples/keyfile/main.cc  | 10 +++++-----
 gio/src/desktopappinfo.hg |  2 +-
 glib/src/keyfile.ccg      | 42 ------------------------------------------
 glib/src/keyfile.hg       | 43 +++++--------------------------------------
 tools/m4/convert_glib.m4  |  3 ++-
 5 files changed, 13 insertions(+), 87 deletions(-)
---
diff --git a/examples/keyfile/main.cc b/examples/keyfile/main.cc
index c75719d0..a1b61556 100644
--- a/examples/keyfile/main.cc
+++ b/examples/keyfile/main.cc
@@ -24,12 +24,12 @@ main(int, char**)
 
   const std::string filepath = "./example.ini";
 
-  Glib::KeyFile keyfile;
+  auto keyfile = Glib::KeyFile::create();
 
   // An exception will be thrown if the file is not there, or if the file is incorrectly formatted:
   try
   {
-    keyfile.load_from_file(filepath);
+    keyfile->load_from_file(filepath);
   }
   catch (const Glib::Error& ex)
   {
@@ -41,7 +41,7 @@ main(int, char**)
   // An exception will be thrown if the value is not in the file:
   try
   {
-    const Glib::ustring value = keyfile.get_value("somegroup", "somekey");
+    const Glib::ustring value = keyfile->get_value("somegroup", "somekey");
     std::cout << "somekey value=" << value << std::endl;
   }
   catch (const Glib::KeyFileError& ex)
@@ -53,7 +53,7 @@ main(int, char**)
   // An exception will be thrown if the value is not in the file:
   try
   {
-    const Glib::ustring value = keyfile.get_value("First Group", "Welcome");
+    const Glib::ustring value = keyfile->get_value("First Group", "Welcome");
     std::cout << "Welcome value=" << value << std::endl;
   }
   catch (const Glib::KeyFileError& ex)
@@ -65,7 +65,7 @@ main(int, char**)
   // An exception will be thrown if the value is not in the file:
   try
   {
-    const auto values = keyfile.get_integer_list("Another Group", "Numbers");
+    const auto values = keyfile->get_integer_list("Another Group", "Numbers");
 
     for (const auto& p : values)
       std::cout << "Number list value: item=" << p << std::endl;
diff --git a/gio/src/desktopappinfo.hg b/gio/src/desktopappinfo.hg
index f169594a..40605931 100644
--- a/gio/src/desktopappinfo.hg
+++ b/gio/src/desktopappinfo.hg
@@ -51,7 +51,7 @@ public:
   // TODO: should use _WRAP_CREATE(), but these functions do more than just call
   // g_object_new() because there's quite a bit of error-handling to do.
   _WRAP_METHOD(static Glib::RefPtr<DesktopAppInfo> create(const std::string& desktop_id), 
g_desktop_app_info_new)
-  _WRAP_METHOD(static Glib::RefPtr<DesktopAppInfo> create_from_keyfile(Glib::KeyFile& key_file), 
g_desktop_app_info_new_from_keyfile)
+  _WRAP_METHOD(static Glib::RefPtr<DesktopAppInfo> create_from_keyfile(const Glib::RefPtr<Glib::KeyFile>& 
key_file), g_desktop_app_info_new_from_keyfile)
   _WRAP_METHOD(static Glib::RefPtr<DesktopAppInfo> create_from_filename(const std::string& filename), 
g_desktop_app_info_new_from_filename)
 
   _WRAP_METHOD(std::string get_filename() const, g_desktop_app_info_get_filename)
diff --git a/glib/src/keyfile.ccg b/glib/src/keyfile.ccg
index 2f86b552..05637fc6 100644
--- a/glib/src/keyfile.ccg
+++ b/glib/src/keyfile.ccg
@@ -17,48 +17,6 @@
 namespace Glib
 {
 
-/**** Glib::KeyFile ********************************************************/
-
-KeyFile::KeyFile()
-{
-  gobject_ = g_key_file_new();
-  owns_gobject_ = true;
-}
-
-KeyFile::KeyFile(GKeyFile* castitem, bool takes_ownership)
-{
-  gobject_ = castitem;
-  owns_gobject_ = takes_ownership;
-}
-
-KeyFile::KeyFile(KeyFile&& other) noexcept : gobject_(std::move(other.gobject_)),
-                                             owns_gobject_(std::move(other.owns_gobject_))
-{
-  other.gobject_ = nullptr;
-  other.owns_gobject_ = false;
-}
-
-KeyFile&
-KeyFile::operator=(KeyFile&& other) noexcept
-{
-  if (owns_gobject_)
-    g_key_file_free(gobject_);
-
-  gobject_ = std::move(other.gobject_);
-  owns_gobject_ = std::move(other.owns_gobject_);
-
-  other.gobject_ = nullptr;
-  other.owns_gobject_ = false;
-
-  return *this;
-}
-
-KeyFile::~KeyFile()
-{
-  if (owns_gobject_)
-    g_key_file_free(gobject_);
-}
-
 bool
 KeyFile::load_from_data(const Glib::ustring& data, Flags flags)
 {
diff --git a/glib/src/keyfile.hg b/glib/src/keyfile.hg
index 584b9d33..a57878e5 100644
--- a/glib/src/keyfile.hg
+++ b/glib/src/keyfile.hg
@@ -20,6 +20,7 @@ _DEFS(glibmm,glib)
 #include <glibmm/ustring.h>
 #include <glibmm/error.h>
 #include <glibmm/utility.h>
+#include <glibmm/refptr.h>
 #include <glib.h>
 #include <vector>
 
@@ -89,44 +90,18 @@ _WRAP_GERROR(KeyFileError, GKeyFileError, G_KEY_FILE_ERROR, NO_GTYPE)
  *
  * @newin{2,14}
  */
-class KeyFile
+class KeyFile final
 {
-  _CLASS_GENERIC(KeyFile, GKeyFile)
+  _CLASS_OPAQUE_REFCOUNTED(KeyFile, GKeyFile, g_key_file_new, g_key_file_ref, g_key_file_unref)
+  _IGNORE(g_key_file_new, g_key_file_ref, g_key_file_unref, g_key_file_free)
+
 public:
 
   _WRAP_ENUM(Flags, GKeyFileFlags, NO_GTYPE)
 
-  //TODO: GKeyFile now seems to be a reference-counted type.
-
   //TODO: Maybe replace all the get_*/set_* methods with some generic get/set
   //methods when it is possible.
 
-  /** Creates a new, empty KeyFile object.
-   */
-  KeyFile();
-
-  KeyFile(const KeyFile&) = delete;
-  KeyFile& operator=(const KeyFile&) = delete;
-
-  KeyFile(KeyFile&& other) noexcept;
-  KeyFile& operator=(KeyFile&& other) noexcept;
-
-  /** Destructor
-   */
-  ~KeyFile();
-  _IGNORE(g_key_file_free)
-
-  /** Creates a glibmm KeyFile wrapper for a GKeyFile object.
-   * Note, when using this that when the wrapper is deleted,
-   * it will not automatically delete the GKeyFile unless you
-   * set the @a takes_ownership boolean to <tt>true</tt>.
-   * @param castitem The C instance to wrap.
-   * @param takes_ownership If the C instance should be deleted when
-   * the wrapper is deleted.
-   */
-  KeyFile(GKeyFile* castitem, bool takes_ownership = false);
-
-public:
   _WRAP_METHOD(bool load_from_file(const std::string& file, Flags flags = Flags::NONE), 
g_key_file_load_from_file, errthrow)
 
   /** Loads a KeyFile from memory
@@ -421,14 +396,6 @@ public:
   _WRAP_METHOD(void remove_comment(const Glib::ustring& group_name, const Glib::ustring& key), 
g_key_file_remove_comment, errthrow)
   _WRAP_METHOD(void remove_key(const Glib::ustring& group_name, const Glib::ustring& key), 
g_key_file_remove_key, errthrow)
   _WRAP_METHOD(void remove_group(const Glib::ustring& group_name), g_key_file_remove_group, errthrow)
-
-  GKeyFile*       gobj()       { return gobject_; }
-  const GKeyFile* gobj() const { return gobject_; }
-
-protected:
-  GKeyFile* gobject_;
-  bool owns_gobject_;
 };
 
 } // namespace Glib
-
diff --git a/tools/m4/convert_glib.m4 b/tools/m4/convert_glib.m4
index 87197e49..f8bc1364 100644
--- a/tools/m4/convert_glib.m4
+++ b/tools/m4/convert_glib.m4
@@ -109,7 +109,8 @@ _CONVERSION(`GDateTime*',`Glib::DateTime',`Glib::wrap($3)')
 _CONVERSION(`const DateTime&',`GDateTime*',`const_cast<$2>($3.gobj())')
 
 dnl KeyFile
-_CONVERSION(`Glib::KeyFile&',`GKeyFile*',`($3).gobj()')
+_CONVERSION(`const Glib::RefPtr<Glib::KeyFile>&',`GKeyFile*',__CONVERT_REFPTR_TO_P)
+_CONVERSION(`const Glib::RefPtr<const Glib::KeyFile>&',`GKeyFile*',__CONVERT_CONST_REFPTR_TO_P)
 
 dnl Object
 _CONVERSION(`const Glib::RefPtr<Glib::Object>&',`GObject*',__CONVERT_REFPTR_TO_P)


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