[glibmm] Rewrite Gio::Tls[Client,Server]ConnectionImpl



commit 2da4503d039da6a12cbd61cbbfea0af4ba572229
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Wed Aug 12 12:52:31 2020 +0200

    Rewrite Gio::Tls[Client,Server]ConnectionImpl
    
    TlsConnection_Class::wrap_new() can wrap a C object in a TlsConnection,
    TlsClientConnectionImpl or TlsServerConnectionImpl depending on which
    interface, if any, the C object implements.
    
    No need for special wrap*() functions in Tls[Client,Server]Connection
    or SocketClient::signal_event().
    
    This is similar to Gdk::DeviceWithPad.

 gio/giomm/tlsclientconnectionimpl.cc | 32 ++++++++++++------------------
 gio/giomm/tlsclientconnectionimpl.h  | 38 +++++++++++++++---------------------
 gio/giomm/tlsserverconnectionimpl.cc | 33 ++++++++++++-------------------
 gio/giomm/tlsserverconnectionimpl.h  | 38 +++++++++++++++---------------------
 gio/src/socketclient.ccg             | 19 ------------------
 gio/src/socketclient.hg              |  2 +-
 gio/src/tlsclientconnection.hg       |  2 +-
 gio/src/tlsconnection.ccg            | 19 ++++++++++++++++++
 gio/src/tlsconnection.hg             |  1 +
 gio/src/tlsserverconnection.hg       |  2 +-
 10 files changed, 80 insertions(+), 106 deletions(-)
---
diff --git a/gio/giomm/tlsclientconnectionimpl.cc b/gio/giomm/tlsclientconnectionimpl.cc
index 003993c5..685a504c 100644
--- a/gio/giomm/tlsclientconnectionimpl.cc
+++ b/gio/giomm/tlsclientconnectionimpl.cc
@@ -15,34 +15,26 @@
  */
 
 #include <giomm/tlsclientconnectionimpl.h>
+#include <utility> // std::move()
 
 namespace Gio
 {
 TlsClientConnectionImpl::TlsClientConnectionImpl(GTlsConnection* castitem)
 : Glib::ObjectBase(nullptr), TlsConnection(castitem)
 {}
-} /* namespace Gio */
 
-namespace Glib
-{
+TlsClientConnectionImpl::TlsClientConnectionImpl(TlsClientConnectionImpl&& src) noexcept
+: TlsClientConnection(std::move(src)),
+  TlsConnection(std::move(src))
+{}
 
-Glib::RefPtr<Gio::TlsClientConnectionImpl> wrap_tls_client_connection_impl(
-  GTlsConnection* object, bool take_copy)
+TlsClientConnectionImpl& TlsClientConnectionImpl::operator=(TlsClientConnectionImpl&& src) noexcept
 {
-  using IfaceImpl = Gio::TlsClientConnectionImpl;
-
-  ObjectBase* pCppObject = nullptr;
-  if (object)
-  {
-    pCppObject = ObjectBase::_get_current_wrapper((GObject*)object);
-
-    if (!pCppObject)
-      pCppObject = new IfaceImpl(object);
-
-    if (take_copy)
-      pCppObject->reference();
-  }
-  return Glib::make_refptr_for_instance<IfaceImpl>(dynamic_cast<IfaceImpl*>(pCppObject));
+  TlsClientConnection::operator=(std::move(src));
+  TlsConnection::operator=(std::move(src));
+  return *this;
 }
 
-} /* namespace Glib */
+TlsClientConnectionImpl::~TlsClientConnectionImpl() noexcept
+{}
+} // namespace Gio
diff --git a/gio/giomm/tlsclientconnectionimpl.h b/gio/giomm/tlsclientconnectionimpl.h
index f77e82d1..adf015cd 100644
--- a/gio/giomm/tlsclientconnectionimpl.h
+++ b/gio/giomm/tlsclientconnectionimpl.h
@@ -24,7 +24,8 @@
 namespace Gio
 {
 
-/** %Gio::TlsClientConnectionImpl implements the Gio::TlsClientConnection interface.
+/** %Gio::TlsClientConnectionImpl is a Gio::TlsConnection that implements
+ * the Gio::TlsClientConnection interface.
  *
  * The GTlsClientConnection interface can be implemented by C classes that
  * derive from GTlsConnection. No public GLib class implements GTlsClientConnection.
@@ -40,30 +41,23 @@ namespace Gio
  */
 class GIOMM_API TlsClientConnectionImpl : public TlsClientConnection, public TlsConnection
 {
-public:
+private:
+  // noncopyable
+  TlsClientConnectionImpl(const TlsClientConnectionImpl&) = delete;
+  TlsClientConnectionImpl& operator=(const TlsClientConnectionImpl&) = delete;
+
+  friend class TlsConnection_Class;
+
+protected:
   explicit TlsClientConnectionImpl(GTlsConnection* castitem);
-};
 
-} // namespace Gio
+public:
+  TlsClientConnectionImpl(TlsClientConnectionImpl&& src) noexcept;
+  TlsClientConnectionImpl& operator=(TlsClientConnectionImpl&& src) noexcept;
 
-namespace Glib
-{
-  /** A %Glib::wrap() method for this object.
-   *
-   * It's not called %wrap() because it wraps a C object which is derived from
-   * GTlsConnection and implements the GTlsClientConnection interface.
-   *
-   * @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 ref.
-   * @result A C++ instance that wraps this C instance.
-   *
-   * @relates Gio::TlsClientConnectionImpl
-   */
-  GIOMM_API
-  Glib::RefPtr<Gio::TlsClientConnectionImpl> wrap_tls_client_connection_impl(
-    GTlsConnection* object, bool take_copy = false);
+  ~TlsClientConnectionImpl() noexcept override;
+};
 
-} // namespace Glib
+} // namespace Gio
 
 #endif /* _GIOMM_TLSCLIENTCONNECTIONIMPL_H */
diff --git a/gio/giomm/tlsserverconnectionimpl.cc b/gio/giomm/tlsserverconnectionimpl.cc
index 42bac7f3..7efd8882 100644
--- a/gio/giomm/tlsserverconnectionimpl.cc
+++ b/gio/giomm/tlsserverconnectionimpl.cc
@@ -15,34 +15,27 @@
  */
 
 #include <giomm/tlsserverconnectionimpl.h>
+#include <utility> // std::move()
 
 namespace Gio
 {
 TlsServerConnectionImpl::TlsServerConnectionImpl(GTlsConnection* castitem)
 : Glib::ObjectBase(nullptr), TlsConnection(castitem)
 {}
-} /* namespace Gio */
 
-namespace Glib
-{
+TlsServerConnectionImpl::TlsServerConnectionImpl(TlsServerConnectionImpl&& src) noexcept
+: TlsServerConnection(std::move(src)),
+  TlsConnection(std::move(src))
+{}
 
-Glib::RefPtr<Gio::TlsServerConnectionImpl> wrap_tls_server_connection_impl(
-  GTlsConnection* object, bool take_copy)
+TlsServerConnectionImpl& TlsServerConnectionImpl::operator=(TlsServerConnectionImpl&& src) noexcept
 {
-  using IfaceImpl = Gio::TlsServerConnectionImpl;
-
-  ObjectBase* pCppObject = nullptr;
-  if (object)
-  {
-    pCppObject = ObjectBase::_get_current_wrapper((GObject*)object);
-
-    if (!pCppObject)
-      pCppObject = new IfaceImpl(object);
-
-    if (take_copy)
-      pCppObject->reference();
-  }
-  return Glib::make_refptr_for_instance<IfaceImpl>(dynamic_cast<IfaceImpl*>(pCppObject));
+  TlsServerConnection::operator=(std::move(src));
+  TlsConnection::operator=(std::move(src));
+  return *this;
 }
 
-} /* namespace Glib */
+TlsServerConnectionImpl::~TlsServerConnectionImpl() noexcept
+{}
+
+} // namespace Gio
diff --git a/gio/giomm/tlsserverconnectionimpl.h b/gio/giomm/tlsserverconnectionimpl.h
index ff25eb40..bbd2ebc2 100644
--- a/gio/giomm/tlsserverconnectionimpl.h
+++ b/gio/giomm/tlsserverconnectionimpl.h
@@ -24,7 +24,8 @@
 namespace Gio
 {
 
-/** %Gio::TlsServerConnectionImpl implements the Gio::TlsServerConnection interface.
+/** %Gio::TlsServerConnectionImpl is a Gio::TlsConnection that implements
+ * the Gio::TlsServerConnection interface.
  *
  * The GTlsServerConnection interface can be implemented by C classes that
  * derive from GTlsConnection. No public GLib class implements GTlsServerConnection.
@@ -40,30 +41,23 @@ namespace Gio
  */
 class GIOMM_API TlsServerConnectionImpl : public TlsServerConnection, public TlsConnection
 {
-public:
+private:
+  // noncopyable
+  TlsServerConnectionImpl(const TlsServerConnectionImpl&) = delete;
+  TlsServerConnectionImpl& operator=(const TlsServerConnectionImpl&) = delete;
+
+  friend class TlsConnection_Class;
+
+protected:
   explicit TlsServerConnectionImpl(GTlsConnection* castitem);
-};
 
-} // namespace Gio
+public:
+  TlsServerConnectionImpl(TlsServerConnectionImpl&& src) noexcept;
+  TlsServerConnectionImpl& operator=(TlsServerConnectionImpl&& src) noexcept;
 
-namespace Glib
-{
-  /** A %Glib::wrap() method for this object.
-   *
-   * It's not called %wrap() because it wraps a C object which is derived from
-   * GTlsConnection and implements the GTlsServerConnection interface.
-   *
-   * @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 ref.
-   * @result A C++ instance that wraps this C instance.
-   *
-   * @relates Gio::TlsServerConnectionImpl
-   */
-  GIOMM_API
-  Glib::RefPtr<Gio::TlsServerConnectionImpl> wrap_tls_server_connection_impl(
-    GTlsConnection* object, bool take_copy = false);
+  ~TlsServerConnectionImpl() noexcept override;
+};
 
-} // namespace Glib
+} // namespace Gio
 
 #endif /* _GIOMM_TLSSERVERCONNECTIONIMPL_H */
diff --git a/gio/src/socketclient.ccg b/gio/src/socketclient.ccg
index 52658d62..8339f12d 100644
--- a/gio/src/socketclient.ccg
+++ b/gio/src/socketclient.ccg
@@ -15,28 +15,9 @@
  */
 
 #include <gio/gio.h>
-#include <giomm/tlsclientconnectionimpl.h>
 #include <giomm/asyncresult.h>
 #include "slot_async.h"
 
-namespace
-{
-// Wrap the GIOStream* connection in the 'event' signal.
-// The GIOStream pointer, if not a nullptr, can point to either a GSocketConnection
-// or to a subclass of GTlsConnection that implements the GTlsClientConnection interface.
-Glib::RefPtr<Gio::IOStream> SocketClient_signal_event_wrap_connection(GIOStream* object, bool take_copy)
-{
-  if (!Glib::ObjectBase::_get_current_wrapper((GObject*)object) &&
-      G_IS_TLS_CLIENT_CONNECTION(object))
-    // An instance of a class that implements GTlsClientConnection.
-    // It doesn't have a wrapper. Wrap it in a Gio::TlsClientConnectionImpl.
-    return Glib::wrap_tls_client_connection_impl(G_TLS_CONNECTION(object), take_copy);
-
-  return Glib::wrap(object, take_copy);
-}
-
-} // anonymous namespace
-
 namespace Gio
 {
 
diff --git a/gio/src/socketclient.hg b/gio/src/socketclient.hg
index b0a5d898..ab1ba680 100644
--- a/gio/src/socketclient.hg
+++ b/gio/src/socketclient.hg
@@ -167,7 +167,7 @@ public:
   _WRAP_PROPERTY("proxy-resolver", Glib::RefPtr<ProxyResolver>)
 
 #m4 _CONVERSION(`GSocketConnectable*',`const Glib::RefPtr<SocketConnectable>&',`Glib::wrap($3, true)')
-#m4 _CONVERSION(`GIOStream*',`const Glib::RefPtr<IOStream>&',`SocketClient_signal_event_wrap_connection($3, 
true)')
+#m4 _CONVERSION(`GIOStream*',`const Glib::RefPtr<IOStream>&',`Glib::wrap($3, true)')
   _WRAP_SIGNAL(void event(SocketClientEvent event, const Glib::RefPtr<SocketConnectable>& connectable,
     const Glib::RefPtr<IOStream>& connection), event)
 };
diff --git a/gio/src/tlsclientconnection.hg b/gio/src/tlsclientconnection.hg
index 94465477..d413f346 100644
--- a/gio/src/tlsclientconnection.hg
+++ b/gio/src/tlsclientconnection.hg
@@ -52,7 +52,7 @@ class GIOMM_API TlsClientConnection : public Glib::Interface
 public:
   // It's not possible to use _WRAP_CTOR/_WRAP_CREATE to wrap the new
   // function because this is an interface.
-#m4 
_CONVERSION(`GIOStream*',`Glib::RefPtr<TlsClientConnectionImpl>',`Glib::wrap_tls_client_connection_impl(G_TLS_CONNECTION($3))')
+#m4 
_CONVERSION(`GIOStream*',`Glib::RefPtr<TlsClientConnectionImpl>',`std::dynamic_pointer_cast<TlsClientConnectionImpl>(Glib::wrap(G_TLS_CONNECTION($3)))')
   _WRAP_METHOD(static Glib::RefPtr<TlsClientConnectionImpl> create(const Glib::RefPtr<IOStream>& 
base_io_stream,
     const Glib::RefPtr<const SocketConnectable>& server_identity{?}), g_tls_client_connection_new, errthrow)
 
diff --git a/gio/src/tlsconnection.ccg b/gio/src/tlsconnection.ccg
index 4ec8d7e4..7697ed9f 100644
--- a/gio/src/tlsconnection.ccg
+++ b/gio/src/tlsconnection.ccg
@@ -18,4 +18,23 @@
 #include <giomm/cancellable.h>
 #include <giomm/tlsdatabase.h>
 #include <giomm/tlsinteraction.h>
+#include <giomm/tlsclientconnectionimpl.h>
+#include <giomm/tlsserverconnectionimpl.h>
 #include "slot_async.h"
+
+namespace Gio
+{
+
+// Custom wrap_new() because we want to create
+// a TlsClientConnectionImpl if the underlying C class implements the GTlsClientConnection interface,
+// a TlsServerConnectionImpl if the underlying C class implements the GTlsServerConnection interface.
+Glib::ObjectBase* TlsConnection_Class::wrap_new(GObject* object)
+{
+  if (G_IS_TLS_CLIENT_CONNECTION(object))
+     return new TlsClientConnectionImpl((GTlsConnection*)object);
+  if (G_IS_TLS_SERVER_CONNECTION(object))
+     return new TlsServerConnectionImpl((GTlsConnection*)object);
+  return new TlsConnection((GTlsConnection*)object);
+}
+
+} // namespace Gio
diff --git a/gio/src/tlsconnection.hg b/gio/src/tlsconnection.hg
index 12132a80..c7bcc832 100644
--- a/gio/src/tlsconnection.hg
+++ b/gio/src/tlsconnection.hg
@@ -40,6 +40,7 @@ class GIOMM_API TlsInteraction;
 class GIOMM_API TlsConnection : public IOStream
 {
   _CLASS_GOBJECT(TlsConnection, GTlsConnection, G_TLS_CONNECTION, IOStream, GIOStream, , , GIOMM_API)
+  _CUSTOM_WRAP_NEW
 
 protected:
   _CTOR_DEFAULT
diff --git a/gio/src/tlsserverconnection.hg b/gio/src/tlsserverconnection.hg
index 94a729a5..e3193b00 100644
--- a/gio/src/tlsserverconnection.hg
+++ b/gio/src/tlsserverconnection.hg
@@ -47,7 +47,7 @@ class GIOMM_API TlsServerConnection : public Glib::Interface
 public:
   // It's not possible to use _WRAP_CTOR/_WRAP_CREATE to wrap the new
   // function because this is an interface.
-#m4 
_CONVERSION(`GIOStream*',`Glib::RefPtr<TlsServerConnectionImpl>',`Glib::wrap_tls_server_connection_impl(G_TLS_CONNECTION($3))')
+#m4 
_CONVERSION(`GIOStream*',`Glib::RefPtr<TlsServerConnectionImpl>',`std::dynamic_pointer_cast<TlsServerConnectionImpl>(Glib::wrap(G_TLS_CONNECTION($3)))')
   _WRAP_METHOD(static Glib::RefPtr<TlsServerConnectionImpl> create(const Glib::RefPtr<IOStream>& 
base_io_stream,
     const Glib::RefPtr<TlsCertificate>& certificate), g_tls_server_connection_new, errthrow)
 


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