[gtkmm] Gdk::Event: Add operator bool(), make get_type() visible



commit ff47852df43ffd9bd804e2417847b7b6a98bfd55
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Tue May 30 10:48:55 2017 +0200

    Gdk::Event: Add operator bool(), make get_type() visible
    
    Event::get_type() is not skipped by doxygen. It's visible in other types of
    classes, e.g. _CLASS_BOXEDTYPE. It can be confusing with both get_type() and
    get_event_type(), even more so for someone who by mistake writes
    event.get_type() instead of event.get_event_type() and can't find get_type()
    in the documentation. Bug 135978

 gdk/src/event.ccg          |    6 +++++
 gdk/src/event.hg           |   13 ++++++++++-
 tools/m4/class_gdkevent.m4 |   50 ++++++++++++++++++++-----------------------
 3 files changed, 41 insertions(+), 28 deletions(-)
---
diff --git a/gdk/src/event.ccg b/gdk/src/event.ccg
index 1db98a5..42d3198 100644
--- a/gdk/src/event.ccg
+++ b/gdk/src/event.ccg
@@ -21,6 +21,12 @@
 
 namespace Gdk
 {
+
+Event::operator bool() const noexcept
+{
+  return gobj() != nullptr;
+}
+
 bool Event::is_send_event() const
 {
   return gobj()->any.send_event;
diff --git a/gdk/src/event.hg b/gdk/src/event.hg
index 1de5138..05c60e5 100644
--- a/gdk/src/event.hg
+++ b/gdk/src/event.hg
@@ -57,6 +57,17 @@ public:
     s#^3BUTTON_PRESS#TRIPLE_BUTTON_PRESS#
   )
 
+  /** Discover whether the event is valid.
+   * For instance,
+   * @code
+   * if (event)
+   *   do_something();
+   * @endcode
+   *
+   * @newin{3,92}
+   */
+  explicit operator bool() const noexcept;
+
   _WRAP_METHOD(Event::Type get_event_type() const, gdk_event_get_event_type)
 
   /**
@@ -96,7 +107,7 @@ public:
     gdk_event_get_root_coords, gdk_event_get_button, gdk_event_get_device,
     gdk_event_get_click_count, gdk_event_get_scroll_deltas,
     gdk_event_get_scroll_direction, gdk_event_get_event_sequence,
-    gdk_event_get_pointer_emulated, gdk_event_get_axis, shall_trigger_context_menu,
+    gdk_event_get_pointer_emulated, gdk_event_get_axis, gdk_event_triggers_context_menu,
     gdk_event_is_scroll_stop_event, gdk_event_request_motions)
 };
 
diff --git a/tools/m4/class_gdkevent.m4 b/tools/m4/class_gdkevent.m4
index bdd46b2..3bfc1c9 100644
--- a/tools/m4/class_gdkevent.m4
+++ b/tools/m4/class_gdkevent.m4
@@ -47,8 +47,7 @@ __NAMESPACE_BEGIN__
 inline void swap(__CPPNAME__& lhs, __CPPNAME__& rhs) noexcept
   { lhs.swap(rhs); }
 
-/**
- * Wraps a C event instance without making unnecessary copy when the ownership
+/** Wraps a C event instance without making unnecessary copy when the ownership
  * can not be acquired.
  *
  * The @a event pointer must not be deleted until last usage of the returned
@@ -71,12 +70,11 @@ namespace Glib
 {
 ifdef(`__BOOL_NO_WRAP_FUNCTION__',`',`dnl else
 
-/**
- * A Glib::wrap() method for this object.
+/** A Glib::wrap() method for this object.
  *
  * @param object The C instance.
- * @param take_copy False if the result should take ownership of the C instance.
- * True if it should take a new copy.
+ * @param take_copy @c false if the result should take ownership of the C instance,
+ *        @c true if it should take a new copy.
  * @result A C++ instance that wraps this C instance.
  *
  * @relates __NAMESPACE__::__CPPNAME__
@@ -133,9 +131,6 @@ ifelse(__CPPPARENT__,,`dnl base class
 
 __CPPNAME__::__CPPNAME__`'(__CNAME__* gobject, bool make_a_copy)
 :
-  // For Event wrappers, make_a_copy is false by default.  The static
-  // Event wrappers must not take a copy, thus make_a_copy = false
-  // ensures identical behaviour if the default argument is used.
 ifelse(__CPPPARENT__,,`dnl base class
   gobject_((make_a_copy && gobject) ? gdk_event_copy(gobject) : gobject)
 ',`dnl else i.e. subclass
@@ -198,6 +193,8 @@ ifelse(__CPPPARENT__,,`dnl base class
 ')dnl endif
 }
 
+// The reinterpret_cast works because __CPPNAME__ is a standard-layout class
+// whose only data member is a ifelse(__CPARENT__,,`__CNAME__',`__CPARENT__') pointer.
 const __CPPNAME__& wrap_event(const __CNAME__*& event)
 {
   return reinterpret_cast<const __CPPNAME__&>(event);
@@ -223,25 +220,24 @@ public:
 #ifndef DOXYGEN_SHOULD_SKIP_THIS
   using CppObjectType = __CPPNAME__;
   using BaseObjectType = __CNAME__;
-ifelse(__CPPPARENT__,,`dnl base class
+#endif /* DOXYGEN_SHOULD_SKIP_THIS */
 
+ifelse(__CPPPARENT__,,`dnl base class
+  /// Get the GType for this class, for use with the underlying GObject type system.
   static GType get_type() G_GNUC_CONST;
 ')dnl
-#endif /* DOXYGEN_SHOULD_SKIP_THIS */
 
-  /**
-   * Creates an invalid event object. Calling any member functions is undefined
-   * behavior.
+  /** Creates an invalid event object.
+   * Calling any member functions other than operator bool() is undefined behavior.
    */
   __CPPNAME__`'();
 
-  /**
-   * Wraps a C event instance. This constructor either acquires ownership of
-   * @a gobject, or copies it.
+  /** Wraps a C event instance.
+   * This constructor either acquires ownership of @a gobject, or copies it.
    *
    * @param gobject %Event to wrap.
    * @param make_a_copy If @c false, the ownership of @a gobject is acquired,
-   *        otherwise, a copy is made.
+   *        otherwise a copy is made.
    */
   explicit __CPPNAME__`'(__CNAME__* gobject, bool make_a_copy = false);
 ifelse(__CPPPARENT__,`',`dnl Only add for Gdk::Event
@@ -259,22 +255,22 @@ _IMPORT(SECTION_DTOR_DOCUMENTATION)
   void swap(__CPPNAME__& other) noexcept;
 
 ifelse(__CPPPARENT__,,`dnl
-  ///Provides access to the underlying C instance.
-  __CNAME__*       gobj()       { return gobject_; }
+  /// Provides access to the underlying C instance.
+  __CNAME__* gobj() noexcept { return gobject_; }
 
-  ///Provides access to the underlying C instance.
-  const __CNAME__* gobj() const { return gobject_; }
+  /// Provides access to the underlying C instance.
+  const __CNAME__* gobj() const noexcept { return gobject_; }
 ',`dnl else
   /// Provides access to the underlying C instance.
-  __CNAME__*       gobj()       { return reinterpret_cast<__CNAME__*>(__CPPPARENT__::gobj()); }
+  __CNAME__* gobj() noexcept { return reinterpret_cast<__CNAME__*>(__CPPPARENT__::gobj()); }
 
   /// Provides access to the underlying C instance.
-  const __CNAME__* gobj() const { return reinterpret_cast<const __CNAME__*>(__CPPPARENT__::gobj()); }
+  const __CNAME__* gobj() const noexcept { return reinterpret_cast<const __CNAME__*>(__CPPPARENT__::gobj()); 
}
 ')dnl endif
 
-  /**
-   * Provides access to the underlying C instance. The caller is responsible for
-   * freeing it with gdk_event_free(). Use when directly setting fields in structs.
+  /** Provides access to the underlying C instance.
+   * The caller is responsible for freeing it with gdk_event_free().
+   * Use when directly setting fields in structs.
    */
   __CNAME__* gobj_copy() const;
 


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