[gtkmm] Gdk::ContentFormatsBuilder: Add operator bool()



commit a737f1a9401f17c197803c4e72a6a4414ea5085d
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Sat Nov 25 10:48:49 2017 +0100

    Gdk::ContentFormatsBuilder: Add operator bool()
    
    and let the move operators and make_content_formats() invalidate the
    content formats builder.

 gdk/gdkmm.h                       |    1 +
 gdk/src/contentformatsbuilder.ccg |   19 +++++++++++++++----
 gdk/src/contentformatsbuilder.hg  |   34 +++++++++++++++++++++++++---------
 3 files changed, 41 insertions(+), 13 deletions(-)
---
diff --git a/gdk/gdkmm.h b/gdk/gdkmm.h
index ba0c9bf..db3ea2a 100644
--- a/gdk/gdkmm.h
+++ b/gdk/gdkmm.h
@@ -40,5 +40,6 @@
 #include <gdkmm/texture.h>
 #include <gdkmm/monitor.h>
 #include <gdkmm/general.h>
+#include <gdkmm/contentformatsbuilder.h>
 
 #endif /* _GDKMM_GDKMM_H_ */
diff --git a/gdk/src/contentformatsbuilder.ccg b/gdk/src/contentformatsbuilder.ccg
index 051d936..97c16f5 100644
--- a/gdk/src/contentformatsbuilder.ccg
+++ b/gdk/src/contentformatsbuilder.ccg
@@ -21,10 +21,16 @@ ContentFormatsBuilder::ContentFormatsBuilder()
 {
 }
 
+ContentFormatsBuilder::ContentFormatsBuilder(GdkContentFormatsBuilder* castitem)
+: gobject_(castitem)
+{
+  // Always takes ownership - never takes copy.
+}
+
 ContentFormatsBuilder::ContentFormatsBuilder(ContentFormatsBuilder&& other) noexcept
 : gobject_(std::move(other.gobject_))
 {
-  other.gobject_ = gdk_content_formats_builder_new();
+  other.gobject_ = nullptr;
 }
 
 ContentFormatsBuilder& ContentFormatsBuilder::operator=(ContentFormatsBuilder&& other) noexcept
@@ -38,14 +44,19 @@ ContentFormatsBuilder::~ContentFormatsBuilder()
 {
   // The only way to destroy a GdkContentFormatsBuilder instance is to
   // make a GdkContentFormats instance, and unref it.
-  gdk_content_formats_unref(gdk_content_formats_builder_free(gobj()));
+  if (gobj())
+    gdk_content_formats_unref(gdk_content_formats_builder_free(gobj()));
+}
+
+ContentFormatsBuilder::operator bool() const noexcept
+{
+  return gobj() != nullptr;
 }
 
 Glib::RefPtr<ContentFormats> ContentFormatsBuilder::make_content_formats()
 {
   GdkContentFormats* formats = gdk_content_formats_builder_free(gobj());
-  // Make sure gobject_ is a valid pointer.
-  gobject_ = gdk_content_formats_builder_new();
+  gobject_ = nullptr;
   return Glib::wrap(formats);
 }
 
diff --git a/gdk/src/contentformatsbuilder.hg b/gdk/src/contentformatsbuilder.hg
index 91c445e..918d52c 100644
--- a/gdk/src/contentformatsbuilder.hg
+++ b/gdk/src/contentformatsbuilder.hg
@@ -25,7 +25,7 @@ namespace Gdk
  *
  * @newin{3,94}
  */
-class ContentFormatsBuilder final
+class ContentFormatsBuilder
 {
   _CLASS_GENERIC(ContentFormatsBuilder, GdkContentFormatsBuilder)
 public:
@@ -37,15 +37,30 @@ public:
   ContentFormatsBuilder();
   _IGNORE(gdk_content_formats_builder_new)
 
-  // Can't be copied.
+  /** This always takes ownership of the underlying GdkContentFormatsBuilder,
+   * so it is only useful with C functions that return a newly-allocated GdkContentFormatsBuilder.
+   */
+  explicit ContentFormatsBuilder(GdkContentFormatsBuilder* castitem);
+
+  // noncopyable
   ContentFormatsBuilder(const ContentFormatsBuilder& other) = delete;
   ContentFormatsBuilder& operator=(const ContentFormatsBuilder& other) = delete;
 
+  // movable
   ContentFormatsBuilder(ContentFormatsBuilder&& other) noexcept;
   ContentFormatsBuilder& operator=(ContentFormatsBuilder&& other) noexcept;
 
   ~ContentFormatsBuilder();
-  
+
+  /** Checks whether the content formats builder is valid.
+   * For instance,
+   * @code
+   * if (builder)
+   *   do_something();
+   * @endcode
+   */
+  explicit operator bool() const noexcept;
+
   _WRAP_METHOD(void add_formats(const Glib::RefPtr<const ContentFormats>& formats),
     gdk_content_formats_builder_add_formats)
   _WRAP_METHOD(void add_gtype(GType gtype), gdk_content_formats_builder_add_gtype)
@@ -54,19 +69,20 @@ public:
 
   /** Makes a new Gdk::ContentFormats from the builder.
    *
-   * The builder is reset to an empty builder, as if it had been newly constructed.
+   * The builder becomes invalid. The only safe operations after a call
+   * to %make_content_formats() are operator bool() and the destructor.
    *
    * @return A new Gdk::ContentFormats with all the formats added to the builder.
    */
   Glib::RefPtr<ContentFormats> make_content_formats();
   _IGNORE(gdk_content_formats_builder_free)
 
-  ///Provides access to the underlying C object.
-  GdkContentFormatsBuilder* gobj() { return gobject_; }
-  ///Provides access to the underlying C object.
-  const GdkContentFormatsBuilder* gobj() const { return gobject_; }
+  /// Provides access to the underlying C object.
+  GdkContentFormatsBuilder* gobj() noexcept { return gobject_; }
+  /// Provides access to the underlying C object.
+  const GdkContentFormatsBuilder* gobj() const noexcept { return gobject_; }
 
-private:
+protected:
   GdkContentFormatsBuilder* gobject_;
 };
 


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