[glibmm] Gio::MemoryInputStream: Add add_data() taking a sigc::slot



commit 8160f03e706d50458b08ed6ab9ee03feb3df149f
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Wed Feb 12 15:10:46 2014 +0100

    Gio::MemoryInputStream: Add add_data() taking a sigc::slot
    
    * gio/src/memoryinputstream.[h|cc]: Add add_data(
    const void*, gssize, const SlotDestroyData&). Deprecate
    add_data(const std::string&) and add_data(const void*, gssize).
    Bug #609946

 gio/src/memoryinputstream.ccg |   49 ++++++++++++++++++++++++++++++++++++++--
 gio/src/memoryinputstream.hg  |   26 +++++++++++++++++----
 2 files changed, 67 insertions(+), 8 deletions(-)
---
diff --git a/gio/src/memoryinputstream.ccg b/gio/src/memoryinputstream.ccg
index 0801e02..0dfd768 100644
--- a/gio/src/memoryinputstream.ccg
+++ b/gio/src/memoryinputstream.ccg
@@ -19,16 +19,55 @@
 
 #include <gio/gio.h>
 
+namespace
+{
+
+class SlotWithData
+{
+public:
+  SlotWithData(const Gio::MemoryInputStream::SlotDestroyData& slot, void* data)
+  :
+  m_slot(new Gio::MemoryInputStream::SlotDestroyData(slot)), m_data(data)
+  { }
+
+  ~SlotWithData() { delete m_slot; }
+
+  void operator()() { (*m_slot)(m_data); }
+
+private:
+  Gio::MemoryInputStream::SlotDestroyData* m_slot;
+  void* m_data;
+};
+
+void destroy_data_callback(void* user_data)
+{
+  SlotWithData* slot_with_data = static_cast<SlotWithData*>(user_data);
+  g_return_if_fail(slot_with_data != 0);
+
+  try
+  {
+    (*slot_with_data)(); // Invoke callback
+  }
+  catch (...)
+  {
+    Glib::exception_handlers_invoke();
+  }
+
+  delete slot_with_data;
+}
+
+} // anonymous namespace
+
 namespace Gio
 {
 
+_DEPRECATE_IFDEF_START
 void MemoryInputStream::add_data(const std::string& data)
 {
   char *data_copy = g_strdup (data.c_str ());
   g_memory_input_stream_add_data(gobj(), data_copy, -1, g_free);
 }
 
-
 void MemoryInputStream::add_data(const void* data, gssize len)
 {
   char *data_copy = 0;
@@ -41,10 +80,14 @@ void MemoryInputStream::add_data(const void* data, gssize len)
 
   g_memory_input_stream_add_data(gobj(), data_copy, len, g_free);
 }
+_DEPRECATE_IFDEF_END
 
-void MemoryInputStream::add_data(const void* data, gssize len, GDestroyNotify destroy)
+void MemoryInputStream::add_data(const void* data, gssize len, const SlotDestroyData& destroy_slot)
 {
-  g_memory_input_stream_add_data(gobj(), data, len, destroy);
+  SlotWithData* slot_with_data = new SlotWithData(destroy_slot, const_cast<void*>(data));
+  GBytes* bytes = g_bytes_new_with_free_func(data, len, &destroy_data_callback, slot_with_data);
+  g_memory_input_stream_add_bytes(gobj(), bytes);
+  g_bytes_unref(bytes); // g_memory_input_stream_add_bytes() takes a reference
 }
 
 } // namespace Gio
diff --git a/gio/src/memoryinputstream.hg b/gio/src/memoryinputstream.hg
index 925303a..eee7161 100644
--- a/gio/src/memoryinputstream.hg
+++ b/gio/src/memoryinputstream.hg
@@ -46,31 +46,47 @@ protected:
 public:
   _WRAP_CREATE()
 
+_DEPRECATE_IFDEF_START
   /** Appends to data that can be read from the input stream.
    *
    * @param data Input data.
+   *
+   * @deprecated Use add_data() with SlotDestroyData or GDestroyNotify instead.
    */
   void add_data(const std::string& data);
 
   /** Appends to data that can be read from the input stream.
    *
-   * Note that the data will copied internally and freed when no longer needed.
+   * Note that the data will be copied internally and freed when no longer needed.
    *
    * @param data Input data.
    * @param len Length of the data, may be -1 if data is a null-terminated string.
    *
-   * @deprecated Use version with destroy notification
+   * @deprecated Use add_data() with SlotDestroyData or GDestroyNotify instead.
    */
   void add_data(const void* data, gssize len);
+_DEPRECATE_IFDEF_END
+
+  _WRAP_METHOD(void add_data(const void* data, gssize len, GDestroyNotify destroy), 
g_memory_input_stream_add_data)
+
+  /** For example,
+   * void on_destroy_data(void* data);
+   *
+   * @param data The data to free.
+   *
+   * @newin{2,40}
+   */
+  typedef sigc::slot<void, void*> SlotDestroyData;
 
   /** Appends to data that can be read from the input stream.
    *
    * @param data Input data.
    * @param len Length of the data, may be -1 if data is a null-terminated string.
-   * @param destroy A function to be called to free the data when it is no
-   * longer needed
+   * @param destroy_slot A slot to be called to free the data when it is no longer needed.
+   *
+   * @newin{2,40}
    */
-  void add_data(const void* data, gssize len, GDestroyNotify destroy);
+  void add_data(const void* data, gssize len, const SlotDestroyData& destroy_slot);
 };
 
 } // namespace Gio


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