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



commit e6d6f039698b043b244e01996e36efa7a10d5a6e
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          |   33 +++++++++++++++------------------
 examples/network/socket-client.cc     |    2 +-
 examples/regex/main.cc                |    2 +-
 examples/thread/dispatcher.cc         |    2 +-
 examples/thread/thread.cc             |    2 +-
 examples/thread/threadpool.cc         |    4 ++--
 12 files changed, 29 insertions(+), 31 deletions(-)
---
diff --git a/examples/dbus/client_bus_listnames.cc b/examples/dbus/client_bus_listnames.cc
index 148c4cb..7e01c02 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 efdda93..a6298c9 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 dd735f6..46c98e0 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 4ce1fce..3580216 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 d3538be..27c7cc1 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 6a5b51f..1331af1 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 */
-        auto 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);
         }
@@ -205,13 +204,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
@@ -261,7 +258,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)
@@ -346,7 +343,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);
@@ -395,7 +392,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);
@@ -407,14 +404,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);
     }
 
-    auto enumerator = connectable->enumerate ();
+    const auto enumerator = connectable->enumerate ();
 
     if (synchronous)
         do_sync_connectable (enumerator);
@@ -446,8 +443,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;
@@ -491,7 +488,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 505df6b..b3a9be7 100644
--- a/examples/network/socket-client.cc
+++ b/examples/network/socket-client.cc
@@ -112,7 +112,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/regex/main.cc b/examples/regex/main.cc
index 624b328..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: */
-  auto 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 5f69440..5f83c44 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 5601e8c..068a972 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]