[glibmm] Avoid unnecessary conversions between std::string and Glib::ustring



commit d3f7d6109d396adb449a150fc2b9feb3045634d0
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Mon Nov 18 09:25:37 2019 +0100

    Avoid unnecessary conversions between std::string and Glib::ustring
    
    A few implicit conversions in tests/ have been kept. They are probably
    deliberate, to test implicit conversion. Inspired by issue #65

 examples/dbus/server_without_bus.cc   |  4 ++--
 examples/network/resolver.cc          | 18 ++++++++----------
 examples/options/main.cc              |  2 +-
 glib/src/checksum.ccg                 |  2 +-
 tests/giomm_memoryinputstream/main.cc |  2 +-
 5 files changed, 13 insertions(+), 15 deletions(-)
---
diff --git a/examples/dbus/server_without_bus.cc b/examples/dbus/server_without_bus.cc
index d97eed3d..15c558c4 100644
--- a/examples/dbus/server_without_bus.cc
+++ b/examples/dbus/server_without_bus.cc
@@ -113,7 +113,7 @@ on_server_new_connection(const Glib::RefPtr<Gio::DBus::Connection>& connection)
 {
   auto credentials = connection->get_peer_credentials();
 
-  std::string credentials_str;
+  Glib::ustring credentials_str;
 
   if (!credentials)
     credentials_str = "(no credentials received)";
@@ -177,7 +177,7 @@ main(int, char**)
 
   Glib::RefPtr<Gio::DBus::Server> server;
 
-  const Glib::ustring address = "unix:abstract=myadd";
+  const std::string address = "unix:abstract=myadd";
   try
   {
     server = Gio::DBus::Server::create_sync(address, Gio::DBus::generate_guid());
diff --git a/examples/network/resolver.cc b/examples/network/resolver.cc
index a651a1a7..949a0d04 100644
--- a/examples/network/resolver.cc
+++ b/examples/network/resolver.cc
@@ -347,12 +347,12 @@ do_async_connectable(Glib::RefPtr<Gio::SocketAddressEnumerator> enumerator)
 Glib::RefPtr<Gio::SocketConnectable> global_connectable;
 
 static void
-do_connectable(const std::string& arg, gboolean synchronous)
+do_connectable(const Glib::ustring& arg, gboolean synchronous)
 {
   std::vector<Glib::ustring> parts;
   Glib::RefPtr<Gio::SocketConnectable> connectable;
 
-  if (arg.find('/') != std::string::npos)
+  if (arg.find('/') != Glib::ustring::npos)
   {
     /* service/protocol/domain */
     parts = split_service_parts(arg);
@@ -366,18 +366,16 @@ do_connectable(const std::string& arg, gboolean synchronous)
   }
   else
   {
-    std::string host, port_str;
-    guint16 port;
+    Glib::ustring host;
+    guint16 port = 0;
 
     const auto pos = arg.find(':');
-    if (pos != std::string::npos)
+    if (pos != Glib::ustring::npos)
     {
       host = arg.substr(0, pos);
-      port_str = arg.substr(pos);
-      port = std::stoul(port_str);
+      auto port_str = arg.substr(pos);
+      port = std::stoul(port_str.raw());
     }
-    else
-      port = 0;
 
     if (Gio::hostname_is_ip_address(host))
     {
@@ -385,7 +383,7 @@ do_connectable(const std::string& arg, gboolean synchronous)
       connectable = Gio::InetSocketAddress::create(addr, port);
     }
     else
-      connectable = Gio::NetworkAddress::create(arg, port);
+      connectable = Gio::NetworkAddress::create(arg.raw(), port);
   }
 
   const auto enumerator = connectable->enumerate();
diff --git a/examples/options/main.cc b/examples/options/main.cc
index cf6796fc..fc673cb1 100644
--- a/examples/options/main.cc
+++ b/examples/options/main.cc
@@ -43,7 +43,7 @@ public:
   Glib::OptionGroup::vecustrings m_arg_list;
   Glib::OptionGroup::vecustrings m_remaining_list;
   Glib::ustring m_arg_x_string;
-  std::string m_arg_x_filename;
+  Glib::ustring m_arg_x_filename;
 };
 
 ExampleOptionGroup::ExampleOptionGroup()
diff --git a/glib/src/checksum.ccg b/glib/src/checksum.ccg
index c6120490..a40d2f6a 100644
--- a/glib/src/checksum.ccg
+++ b/glib/src/checksum.ccg
@@ -38,7 +38,7 @@ Checksum::get_length(Type checksum_type)
 std::string
 Checksum::compute_checksum(Type checksum_type, const std::string& data)
 {
-  return Glib::convert_return_gchar_ptr_to_ustring(
+  return Glib::convert_return_gchar_ptr_to_stdstring(
     g_compute_checksum_for_string(((GChecksumType)checksum_type), data.c_str(), data.size()));
 }
 
diff --git a/tests/giomm_memoryinputstream/main.cc b/tests/giomm_memoryinputstream/main.cc
index 2c35d1a1..ce1b54ec 100644
--- a/tests/giomm_memoryinputstream/main.cc
+++ b/tests/giomm_memoryinputstream/main.cc
@@ -27,7 +27,7 @@ destroy_func1(void* data)
 }
 
 void
-destroy_func2(void* data, const Glib::ustring& intro)
+destroy_func2(void* data, const std::string& intro)
 {
   char* cdata = static_cast<char*>(data);
   func2_output += intro + cdata;


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