[glibmm/c++11v3: 4/4] C++11: examples: more use of auto.



commit e3cf2a503132aaaddd69f2a149d3e60344b9df0a
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Jul 4 10:27:35 2013 +0200

    C++11: examples: more use of auto.

 examples/dbus/client_bus_listnames.cc |    4 +-
 examples/dbus/server_without_bus.cc   |    2 +-
 examples/dbus/session_bus_service.cc  |    2 +-
 examples/iochannel_stream/fdstream.cc |    3 +-
 examples/iochannel_stream/main.cc     |    2 +-
 examples/markup/parser.cc             |    2 +-
 examples/network/resolver.cc          |   35 ++++++++++++++------------------
 examples/network/socket-client.cc     |    2 +-
 examples/network/socket-server.cc     |   35 +++++++++++++-------------------
 examples/regex/main.cc                |    2 +-
 examples/thread/dispatcher.cc         |    2 +-
 examples/thread/thread.cc             |    2 +-
 examples/thread/threadpool.cc         |    4 +-
 13 files changed, 43 insertions(+), 54 deletions(-)
---
diff --git a/examples/dbus/client_bus_listnames.cc b/examples/dbus/client_bus_listnames.cc
index 3c51e12..4507aec 100644
--- a/examples/dbus/client_bus_listnames.cc
+++ b/examples/dbus/client_bus_listnames.cc
@@ -34,7 +34,7 @@ bool on_main_loop_idle()
 // method.
 void on_dbus_proxy_available(Glib::RefPtr<Gio::AsyncResult>& result)
 {
-  Glib::RefPtr<Gio::DBus::Proxy> proxy = Gio::DBus::Proxy::create_finish(result);
+  const auto proxy = Gio::DBus::Proxy::create_finish(result);
 
   if(!proxy)
   {
@@ -48,7 +48,7 @@ void on_dbus_proxy_available(Glib::RefPtr<Gio::AsyncResult>& result)
   {
     // The proxy's call method returns a tuple of the value(s) that the method
     // call produces so just get the tuple as a VariantContainerBase.
-    const Glib::VariantContainerBase result = proxy->call_sync("ListNames");
+    const auto result = proxy->call_sync("ListNames");
 
     // Now extract the single item in the variant container which is the
     // array of strings (the names).
diff --git a/examples/dbus/server_without_bus.cc b/examples/dbus/server_without_bus.cc
index d50632d..005dc55 100644
--- a/examples/dbus/server_without_bus.cc
+++ b/examples/dbus/server_without_bus.cc
@@ -166,7 +166,7 @@ int main(int, char**)
   std::locale::global(std::locale(""));
   Gio::init();
 
- try
+  try
   {
     introspection_data = Gio::DBus::NodeInfo::create_for_xml(introspection_xml);
   }
diff --git a/examples/dbus/session_bus_service.cc b/examples/dbus/session_bus_service.cc
index 0f0bd01..b5ec2a7 100644
--- a/examples/dbus/session_bus_service.cc
+++ b/examples/dbus/session_bus_service.cc
@@ -156,7 +156,7 @@ int main(int, char**)
     return 1;
   }
 
-  const guint id = Gio::DBus::own_name(Gio::DBus::BUS_TYPE_SESSION,
+  const auto id = Gio::DBus::own_name(Gio::DBus::BUS_TYPE_SESSION,
     "org.glibmm.DBusExample",
     sigc::ptr_fun(&on_bus_acquired),
     sigc::ptr_fun(&on_name_acquired),
diff --git a/examples/iochannel_stream/fdstream.cc b/examples/iochannel_stream/fdstream.cc
index 90df473..3764546 100644
--- a/examples/iochannel_stream/fdstream.cc
+++ b/examples/iochannel_stream/fdstream.cc
@@ -265,7 +265,8 @@ std::streamsize fdstreambuf::xsgetn(char* dest, std::streamsize num)
          *putback_buffer = *(gptr() - 1);
          putback_count = 2;
        }
-       else putback_count = 1;
+       else
+          putback_count = 1;
       }
 
       *(putback_buffer + 1) = *(dest + (chars_read - 1));
diff --git a/examples/iochannel_stream/main.cc b/examples/iochannel_stream/main.cc
index aaebbdd..e2e670f 100644
--- a/examples/iochannel_stream/main.cc
+++ b/examples/iochannel_stream/main.cc
@@ -81,7 +81,7 @@ int main( /* int argc, char *argv[] */)
     }
   }
  
-  int read_fd = open("testfifo", O_RDONLY);
+  const auto read_fd = open("testfifo", O_RDONLY);
   if(read_fd == -1)
   {
     std::cerr << "error opening fifo" << std::endl;
diff --git a/examples/markup/parser.cc b/examples/markup/parser.cc
index 54bac45..fafcc2f 100644
--- a/examples/markup/parser.cc
+++ b/examples/markup/parser.cc
@@ -26,7 +26,7 @@ namespace
 
 void file_get_contents(const std::string& filename, Glib::ustring& contents)
 {
-  const Glib::RefPtr<Glib::IOChannel> channel = Glib::IOChannel::create_from_file(filename, "r");
+  const auto channel = Glib::IOChannel::create_from_file(filename, "r");
   channel->read_to_end(contents);
 }
 
diff --git a/examples/network/resolver.cc b/examples/network/resolver.cc
index dee1ebf..49ad2a5 100644
--- a/examples/network/resolver.cc
+++ b/examples/network/resolver.cc
@@ -150,9 +150,8 @@ lookup_one_sync (const Glib::ustring& arg)
 {
     if (arg.find ('/') != std::string::npos)
     {
-        std::list<Gio::SrvTarget> targets;
         /* service/protocol/domain */
-        std::vector<Glib::ustring> parts = split_service_parts (arg);
+        const auto parts = split_service_parts (arg);
         if (parts.size () != 3) {
             usage ();
             return;
@@ -160,7 +159,7 @@ lookup_one_sync (const Glib::ustring& arg)
 
         try
         {
-            targets = resolver->lookup_service (parts[0], parts[1], parts[2],
+            const auto targets = resolver->lookup_service (parts[0], parts[1], parts[2],
                                                 cancellable);
             print_resolved_service (arg, targets);
         }
@@ -207,13 +206,11 @@ lookup_thread (const Glib::ustring& arg)
 static void
 start_threaded_lookups (char **argv, int argc)
 {
-    int i;
-
-    for (i = 0; i < argc; i++)
-    {
-        Glib::Threads::Thread::create (sigc::bind (sigc::ptr_fun (lookup_thread),
-                                          argv[i]));
-    }
+  for (auto i = 0; i < argc; i++)
+  {
+    Glib::Threads::Thread::create (sigc::bind (sigc::ptr_fun (lookup_thread),
+      argv[i]));
+   }
 }
 
 static void
@@ -263,7 +260,7 @@ lookup_service_callback (Glib::RefPtr<Gio::AsyncResult> result,
 static void
 start_async_lookups (char **argv, int argc)
 {
-    for (int i = 0; i < argc; i++)
+    for (auto i = 0; i < argc; i++)
     {
         Glib::ustring arg (argv[i]);
         if (arg.find ('/') != std::string::npos)
@@ -349,7 +346,7 @@ got_next_async (Glib::RefPtr<Gio::AsyncResult> result,
 {
     try
     {
-        Glib::RefPtr<Gio::SocketAddress> sockaddr = enumerator->next_finish (result);
+        const auto sockaddr = enumerator->next_finish (result);
         if (sockaddr)
         {
             print_connectable_sockaddr (sockaddr);
@@ -382,7 +379,6 @@ do_connectable (const std::string& arg, gboolean synchronous)
 {
     std::vector<Glib::ustring> parts;
     Glib::RefPtr<Gio::SocketConnectable> connectable;
-    Glib::RefPtr<Gio::SocketAddressEnumerator> enumerator;
 
     if (arg.find ('/') != std::string::npos)
     {
@@ -400,7 +396,7 @@ do_connectable (const std::string& arg, gboolean synchronous)
         std::string host, port_str;
         guint16 port;
 
-        std::size_t pos = arg.find (':');
+        const auto pos = arg.find (':');
         if (pos != std::string::npos)
         {
             host = arg.substr (0, pos);
@@ -412,15 +408,14 @@ do_connectable (const std::string& arg, gboolean synchronous)
 
         if (Gio::hostname_is_ip_address (host))
         {
-            Glib::RefPtr<Gio::InetAddress> addr = Gio::InetAddress::create (host);
+            const auto addr = Gio::InetAddress::create (host);
             connectable = Gio::InetSocketAddress::create (addr, port);
         }
         else
             connectable = Gio::NetworkAddress::create (arg, port);
     }
 
-    enumerator = connectable->enumerate ();
-
+    const auto enumerator = connectable->enumerate ();
     if (synchronous)
         do_sync_connectable (enumerator);
     else
@@ -450,8 +445,8 @@ async_cancel (Glib::IOCondition /*cond*/, Glib::RefPtr<Gio::Cancellable> cancell
 int
 main (int argc, char **argv)
 {
-    bool synchronous = false;
-    bool use_connectable = false;
+    auto synchronous = false;
+    auto use_connectable = false;
 #ifdef G_OS_UNIX
     Glib::RefPtr<Glib::IOChannel> chan;
     sigc::connection watch_conn;
@@ -495,7 +490,7 @@ main (int argc, char **argv)
     signal (SIGINT, interrupted);
 
     chan = Glib::IOChannel::create_from_fd (cancel_fds[0]);
-    Glib::RefPtr<Glib::IOSource> source = chan->create_watch (Glib::IO_IN);
+    const auto source = chan->create_watch (Glib::IO_IN);
     watch_conn = source->connect (sigc::bind (sigc::ptr_fun (async_cancel), cancellable));
 #endif
 
diff --git a/examples/network/socket-client.cc b/examples/network/socket-client.cc
index c7df51e..e6ecaba 100644
--- a/examples/network/socket-client.cc
+++ b/examples/network/socket-client.cc
@@ -154,7 +154,7 @@ main (int argc,
 
     if (argc != 2)
     {
-        const char* error_message = "Need to specify hostname";
+        const auto error_message = "Need to specify hostname";
         std::cerr << Glib::ustring::compose ("%1: %2\n", argv[0], error_message);
         return 1;
     }
diff --git a/examples/network/socket-server.cc b/examples/network/socket-server.cc
index b9e1a83..360c90b 100644
--- a/examples/network/socket-server.cc
+++ b/examples/network/socket-server.cc
@@ -68,19 +68,16 @@ public:
 Glib::ustring
 socket_address_to_string (const Glib::RefPtr<Gio::SocketAddress>& address)
 {
-    Glib::RefPtr<Gio::InetAddress> inet_address;
-    Glib::ustring str, res;
-    int port;
-
-    Glib::RefPtr<Gio::InetSocketAddress> isockaddr =
-        Glib::RefPtr<Gio::InetSocketAddress>::cast_dynamic (address);
-    if (!isockaddr)
-        return Glib::ustring ();
-    inet_address = isockaddr->get_address ();
-    str = inet_address->to_string ();
-    port = isockaddr->get_port ();
-    res = Glib::ustring::compose ("%1:%2", str, port);
-    return res;
+  auto isockaddr =
+      Glib::RefPtr<Gio::InetSocketAddress>::cast_dynamic (address);
+  if (!isockaddr)
+      return Glib::ustring ();
+
+  auto inet_address = isockaddr->get_address ();
+  auto str = inet_address->to_string ();
+  auto port = isockaddr->get_port ();
+  auto res = Glib::ustring::compose ("%1:%2", str, port);
+  return res;
 }
 
 static bool
@@ -132,10 +129,7 @@ main (int argc,
       char *argv[])
 {
     Glib::RefPtr<Gio::Socket> socket, new_socket, recv_socket;
-    Glib::RefPtr<Gio::SocketAddress> src_address;
     Glib::RefPtr<Gio::SocketAddress> address;
-    Gio::SocketType socket_type;
-    Gio::SocketFamily socket_family;
     Glib::RefPtr<Gio::Cancellable> cancellable;
 
     Gio::init ();
@@ -161,8 +155,8 @@ main (int argc,
 
     loop = Glib::MainLoop::create ();
 
-    socket_type = use_udp ? Gio::SOCKET_TYPE_DATAGRAM : Gio::SOCKET_TYPE_STREAM;
-    socket_family = use_ipv6 ? Gio::SOCKET_FAMILY_IPV6 : Gio::SOCKET_FAMILY_IPV4;
+    auto socket_type = use_udp ? Gio::SOCKET_TYPE_DATAGRAM : Gio::SOCKET_TYPE_STREAM;
+    auto socket_family = use_ipv6 ? Gio::SOCKET_FAMILY_IPV6 : Gio::SOCKET_FAMILY_IPV4;
 
     try {
         socket = Gio::Socket::create (socket_family, socket_type, Gio::SOCKET_PROTOCOL_DEFAULT);
@@ -175,7 +169,7 @@ main (int argc,
     if (non_blocking)
         socket->set_blocking (false);
 
-    src_address = Gio::InetSocketAddress::create (Gio::InetAddress::create_any (socket_family), port);
+    auto src_address = Gio::InetSocketAddress::create (Gio::InetAddress::create_any (socket_family), port);
     try {
         socket->bind (src_address, !dont_reuse_address);
     } catch (const Gio::Error& error) {
@@ -234,7 +228,6 @@ main (int argc,
     {
         gchar buffer[4096] = { };
         gssize size;
-        gsize to_send;
 
         ensure_condition (recv_socket, "receive", cancellable, Glib::IO_IN);
         try {
@@ -266,7 +259,7 @@ main (int argc,
                      "-------------------------\n",
                      (int)size, buffer);
 
-        to_send = size;
+        auto to_send = size;
 
         while (to_send > 0)
         {
diff --git a/examples/regex/main.cc b/examples/regex/main.cc
index cc128ab..ed6ac56 100644
--- a/examples/regex/main.cc
+++ b/examples/regex/main.cc
@@ -24,7 +24,7 @@ int main(int, char**)
   Glib::init();
 
   /* Reusing one regex pattern: */
-  Glib::RefPtr<Glib::Regex> regex = Glib::Regex::create("(a)?(b)");
+  const auto regex = Glib::Regex::create("(a)?(b)");
   std::cout << "Pattern=" << regex->get_pattern() 
      << ", with string=abcd, result=" 
      << std::boolalpha << regex->match("abcd")
diff --git a/examples/thread/dispatcher.cc b/examples/thread/dispatcher.cc
index 1c1eb66..79ff64b 100644
--- a/examples/thread/dispatcher.cc
+++ b/examples/thread/dispatcher.cc
@@ -134,7 +134,7 @@ void ThreadProgress::thread_function()
 {
   Glib::Rand rand;
 
-  for (int i = 0; i < ITERATIONS; ++i)
+  for (auto i = 0; i < ITERATIONS; ++i)
   {
     Glib::usleep(rand.get_int_range(2000, 20000));
 
diff --git a/examples/thread/thread.cc b/examples/thread/thread.cc
index ad7cfeb..6fb546c 100644
--- a/examples/thread/thread.cc
+++ b/examples/thread/thread.cc
@@ -36,7 +36,7 @@ void MessageQueue::producer()
 {
   Glib::Rand rand (1234);
 
-  for(int i = 0; i < 200; ++i)
+  for(auto i = 0; i < 200; ++i)
   {
     {
       Glib::Threads::Mutex::Lock lock (mutex_);
diff --git a/examples/thread/threadpool.cc b/examples/thread/threadpool.cc
index 632a77a..557997f 100644
--- a/examples/thread/threadpool.cc
+++ b/examples/thread/threadpool.cc
@@ -15,7 +15,7 @@ void print_char(char c)
 {
   Glib::Rand rand;
 
-  for(int i = 0; i < 100; ++i)
+  for(auto i = 0; i < 100; ++i)
   {
     {
       Glib::Threads::Mutex::Lock lock (mutex);
@@ -33,7 +33,7 @@ int main(int, char**)
 {
   Glib::ThreadPool pool (10);
 
-  for(char c = 'a'; c <= 'z'; ++c)
+  for(auto c = 'a'; c <= 'z'; ++c)
   {
     pool.push(sigc::bind<1>(sigc::ptr_fun(&print_char), c));
   }


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