Commit 9b0554e2 authored by Murray Cumming's avatar Murray Cumming

C++11: Use auto in examples.

parent 19609dd5
......@@ -38,7 +38,7 @@ private:
void ChildWatch::run()
{
GPid pid = fork();
auto pid = fork();
if(pid==0)
{
......@@ -59,7 +59,7 @@ void ChildWatch::on_child_exited(GPid pid, int status)
int main()
{
Glib::RefPtr<Glib::MainLoop> mainLoop = Glib::MainLoop::create();
auto mainLoop = Glib::MainLoop::create();
ChildWatch cwatch(mainLoop);
cwatch.run();
......
......@@ -56,7 +56,7 @@ void on_dbus_proxy_available(Glib::RefPtr<Gio::AsyncResult>& result)
result.get_child(names_variant);
// Get the vector of strings.
std::vector<Glib::ustring> names = names_variant.get();
const auto names = names_variant.get();
std::cout << "The names on the message bus are:" << std::endl;
......@@ -81,7 +81,7 @@ int main(int, char**)
loop = Glib::MainLoop::create();
// Get the user session bus connection.
Glib::RefPtr<Gio::DBus::Connection> connection =
auto connection =
Gio::DBus::Connection::get_sync(Gio::DBus::BUS_TYPE_SESSION);
// Check for an unavailable connection.
......
......@@ -35,7 +35,7 @@ namespace
static Glib::RefPtr<Gio::DBus::NodeInfo> introspection_data;
static Glib::ustring introspection_xml =
static auto introspection_xml =
"<node>"
" <interface name='org.glibmm.DBus.Clock'>"
" <method name='GetTime'>"
......@@ -67,12 +67,12 @@ static void on_method_call(const Glib::RefPtr<Gio::DBus::Connection>& /* connect
Glib::TimeVal curr_time;
curr_time.assign_current_time();
const Glib::ustring time_str = curr_time.as_iso8601();
const Glib::Variant<Glib::ustring> time_var =
const auto time_str = curr_time.as_iso8601();
const auto time_var =
Glib::Variant<Glib::ustring>::create(time_str);
// Create the tuple.
Glib::VariantContainerBase response =
auto response =
Glib::VariantContainerBase::create_tuple(time_var);
// Return the tuple with the included time.
......@@ -89,7 +89,7 @@ static void on_method_call(const Glib::RefPtr<Gio::DBus::Connection>& /* connect
parameters.get_child(param);
// Get the time string.
const Glib::ustring time_str = param.get();
const auto time_str = param.get();
if(!curr_alarm.assign_from_iso8601(time_str))
{
......@@ -114,7 +114,7 @@ const Gio::DBus::InterfaceVTable interface_vtable(sigc::ptr_fun(&on_method_call)
bool on_server_new_connection(const Glib::RefPtr<Gio::DBus::Connection>& connection)
{
Glib::RefPtr<Gio::Credentials> credentials =
auto credentials =
connection->get_peer_credentials();
std::string credentials_str;
......@@ -179,7 +179,7 @@ int main(int, char**)
Glib::RefPtr<Gio::DBus::Server> server;
const Glib::ustring address = "unix:abstract=myadd";
const auto address = "unix:abstract=myadd";
try
{
server = Gio::DBus::Server::create_sync(address,
......@@ -200,7 +200,7 @@ int main(int, char**)
server->signal_new_connection().connect(sigc::ptr_fun(&on_server_new_connection));
//Keep the server running until the process is killed:
Glib::RefPtr<Glib::MainLoop> loop = Glib::MainLoop::create();
auto loop = Glib::MainLoop::create();
loop->run();
return EXIT_SUCCESS;
......
......@@ -35,7 +35,7 @@ namespace
static Glib::RefPtr<Gio::DBus::NodeInfo> introspection_data;
static Glib::ustring introspection_xml =
static auto introspection_xml =
"<node>"
" <interface name='org.glibmm.DBusExample.Clock'>"
" <method name='GetTime'>"
......@@ -65,12 +65,12 @@ static void on_method_call(const Glib::RefPtr<Gio::DBus::Connection>& /* connect
Glib::TimeVal curr_time;
curr_time.assign_current_time();
const Glib::ustring time_str = curr_time.as_iso8601();
const Glib::Variant<Glib::ustring> time_var =
const auto time_str = curr_time.as_iso8601();
const auto time_var =
Glib::Variant<Glib::ustring>::create(time_str);
// Create the tuple.
Glib::VariantContainerBase response =
auto response =
Glib::VariantContainerBase::create_tuple(time_var);
// Return the tuple with the included time.
......@@ -87,7 +87,7 @@ static void on_method_call(const Glib::RefPtr<Gio::DBus::Connection>& /* connect
parameters.get_child(param);
// Get the time string.
const Glib::ustring time_str = param.get();
const auto time_str = param.get();
if(!curr_alarm.assign_from_iso8601(time_str))
{
......@@ -163,7 +163,7 @@ int main(int, char**)
sigc::ptr_fun(&on_name_lost));
//Keep the service running until the process is killed:
Glib::RefPtr<Glib::MainLoop> loop = Glib::MainLoop::create();
auto loop = Glib::MainLoop::create();
loop->run();
Gio::DBus::unown_name(id);
......
......@@ -200,7 +200,7 @@ std::streamsize fdstreambuf::xsgetn(char* dest, std::streamsize num)
// available would normally be 0, but could be up to 2 if there
// have been putbacks or a peek and a putback
std::streamsize available = egptr() - gptr();
auto available = egptr() - gptr();
// if num is less than or equal to the characters already in the
// putback buffer, extract from buffer
......
......@@ -42,7 +42,7 @@ int main(int, char**)
// An exception will be thrown if the value is not in the file:
try
{
const Glib::ustring value = keyfile.get_value("somegroup", "somekey");
const auto value = keyfile.get_value("somegroup", "somekey");
std::cout << "somekey value=" << value << std::endl;
}
catch(const Glib::KeyFileError& ex)
......@@ -54,7 +54,7 @@ int main(int, char**)
// An exception will be thrown if the value is not in the file:
try
{
const Glib::ustring value = keyfile.get_value("First Group", "Welcome");
const auto value = keyfile.get_value("First Group", "Welcome");
std::cout << "Welcome value=" << value << std::endl;
}
catch(const Glib::KeyFileError& ex)
......@@ -66,9 +66,9 @@ int main(int, char**)
// An exception will be thrown if the value is not in the file:
try
{
const std::vector<int> values = keyfile.get_integer_list("Another Group", "Numbers");
const auto values = keyfile.get_integer_list("Another Group", "Numbers");
for(std::vector<int>::const_iterator p = values.begin(); p != values.end(); ++p)
for(auto p = values.begin(); p != values.end(); ++p)
std::cout << "Number list value: item=" << *p << std::endl;
}
catch(const Glib::KeyFileError& ex)
......
......@@ -32,13 +32,13 @@ void file_get_contents(const std::string& filename, Glib::ustring& contents)
Glib::ustring trim_whitespace(const Glib::ustring& text)
{
Glib::ustring::const_iterator pbegin (text.begin());
Glib::ustring::const_iterator pend (text.end());
auto pbegin (text.begin());
auto pend (text.end());
while(pbegin != pend && Glib::Unicode::isspace(*pbegin))
++pbegin;
Glib::ustring::const_iterator temp (pend);
auto temp (pend);
while(pbegin != temp && Glib::Unicode::isspace(*--temp))
pend = temp;
......@@ -84,7 +84,7 @@ void DumpParser::on_start_element(Glib::Markup::ParseContext&,
indent();
std::cout << '<' << element_name;
for(AttributeMap::const_iterator p = attributes.begin(); p != attributes.end(); ++p)
for(auto p = attributes.begin(); p != attributes.end(); ++p)
{
std::cout << ' ' << p->first << "=\"" << p->second << '"';
}
......@@ -104,7 +104,7 @@ void DumpParser::on_end_element(Glib::Markup::ParseContext&, const Glib::ustring
void DumpParser::on_text(Glib::Markup::ParseContext&, const Glib::ustring& text)
{
const Glib::ustring trimmed_text = trim_whitespace(text);
const auto trimmed_text = trim_whitespace(text);
if(!trimmed_text.empty())
{
......
......@@ -95,7 +95,7 @@ print_resolved_addresses (const Glib::ustring& name,
{
G_LOCK (response);
std::cout << Glib::ustring::compose ("Name: %1\n", name);
for (std::list<Glib::RefPtr<Gio::InetAddress> >::const_iterator iter = addresses.begin ();
for (auto iter = addresses.begin ();
iter != addresses.end (); ++iter)
{
std::cout << Glib::ustring::compose ("Address: %1\n", (*iter)->to_string ());
......@@ -112,7 +112,7 @@ print_resolved_service (const Glib::ustring& service,
{
G_LOCK (response);
std::cout << Glib::ustring::compose ("Service: %1\n", service);
for (std::list<Gio::SrvTarget>::const_iterator iter = targets.begin ();
for (auto iter = targets.begin ();
iter != targets.end (); ++iter)
{
std::cout <<
......@@ -132,14 +132,14 @@ static std::vector<Glib::ustring>
split_service_parts (const Glib::ustring& arg)
{
std::vector<Glib::ustring> parts;
std::size_t delim1 = 0;
std::size_t delim2 = 0;
delim1 = arg.find ('/', 0);
auto delim1 = arg.find ('/', 0);
if (delim1 == std::string::npos)
return parts;
delim2 = arg.find ('/', delim1 + 1);
auto delim2 = arg.find ('/', delim1 + 1);
if (delim2 == std::string::npos)
return parts;
parts.push_back (arg.substr (0, delim1));
parts.push_back (arg.substr (delim1 + 1, delim2 - delim1 - 1));
parts.push_back (arg.substr (delim2 + 1));
......@@ -154,7 +154,7 @@ lookup_one_sync (const Glib::ustring& arg)
{
std::list<Gio::SrvTarget> targets;
/* service/protocol/domain */
std::vector<Glib::ustring> parts = split_service_parts (arg);
auto parts = split_service_parts (arg);
if (parts.size () != 3) {
usage ();
return;
......@@ -173,10 +173,10 @@ lookup_one_sync (const Glib::ustring& arg)
}
else if (Gio::hostname_is_ip_address (arg))
{
Glib::RefPtr<Gio::InetAddress> addr = Gio::InetAddress::create (arg);
auto addr = Gio::InetAddress::create (arg);
try
{
Glib::ustring name = resolver->lookup_by_address (addr, cancellable);
const auto name = resolver->lookup_by_address (addr, cancellable);
print_resolved_name (arg, name);
}
catch (const Gio::ResolverError& err)
......@@ -186,11 +186,9 @@ lookup_one_sync (const Glib::ustring& arg)
}
else
{
std::list<Glib::RefPtr<Gio::InetAddress> > addresses;
try
{
addresses = resolver->lookup_by_name (arg, cancellable);
const auto addresses = resolver->lookup_by_name (arg, cancellable);
print_resolved_addresses (arg, addresses);
}
catch (const Gio::ResolverError& err)
......@@ -271,7 +269,7 @@ start_async_lookups (char **argv, int argc)
if (arg.find ('/') != std::string::npos)
{
/* service/protocol/domain */
std::vector<Glib::ustring> parts = split_service_parts (arg);
auto parts = split_service_parts (arg);
if (parts.size () != 3) {
usage ();
return;
......@@ -286,7 +284,7 @@ start_async_lookups (char **argv, int argc)
}
else if (Gio::hostname_is_ip_address (argv[i]))
{
Glib::RefPtr<Gio::InetAddress> addr = Gio::InetAddress::create (argv[i]);
auto addr = Gio::InetAddress::create (argv[i]);
resolver->lookup_by_address_async (addr,
sigc::bind (sigc::ptr_fun
......@@ -311,8 +309,7 @@ start_async_lookups (char **argv, int argc)
static void
print_connectable_sockaddr (Glib::RefPtr<Gio::SocketAddress> sockaddr)
{
Glib::ustring phys;
Glib::RefPtr<Gio::InetSocketAddress> isa =
auto isa =
Glib::RefPtr<Gio::InetSocketAddress>::cast_dynamic (sockaddr);
if (!isa)
......@@ -323,7 +320,7 @@ print_connectable_sockaddr (Glib::RefPtr<Gio::SocketAddress> sockaddr)
}
else
{
phys = isa->get_address ()->to_string ();
const auto phys = isa->get_address ()->to_string ();
std::cout << Glib::ustring::compose ("Address: %1%2%3:%4\n",
phys.find (':') != std::string::npos ? "[" : "",
phys,
......@@ -382,14 +379,12 @@ Glib::RefPtr<Gio::SocketConnectable> global_connectable;
static void
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)
{
/* service/protocol/domain */
parts = split_service_parts (arg);
auto parts = split_service_parts (arg);
if (parts.size () != 3) {
usage ();
return;
......@@ -421,7 +416,7 @@ do_connectable (const std::string& arg, gboolean synchronous)
connectable = Gio::NetworkAddress::create (arg, port);
}
enumerator = connectable->enumerate ();
auto enumerator = connectable->enumerate ();
if (synchronous)
do_sync_connectable (enumerator);
......@@ -435,9 +430,10 @@ static volatile int cancel_fd;
static void
interrupted (int /*sig*/)
{
const int save_errno = errno;
const auto save_errno = errno;
while (write(cancel_fd, "", 1) < 0 && errno == EINTR)
{}
errno = save_errno;
}
......
......@@ -28,18 +28,14 @@ static GOptionEntry cmd_entries[] = {
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 =
auto 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);
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;
}
......@@ -57,14 +53,12 @@ ensure_condition (const Glib::RefPtr<Gio::Socket>& socket,
const Glib::RefPtr<Gio::Cancellable>& cancellable,
Glib::IOCondition condition)
{
GSource *source;
if (!non_blocking)
return;
if (use_source)
{
source = g_socket_create_source (socket->gobj (),
auto source = g_socket_create_source (socket->gobj (),
(GIOCondition) condition,
cancellable->gobj ());
g_source_set_callback (source,
......@@ -103,14 +97,12 @@ main (int argc,
Glib::RefPtr<Gio::SocketAddress> address;
Gio::SocketType socket_type;
GError *error = NULL;
GOptionContext *context;
Glib::RefPtr<Gio::Cancellable> cancellable;
Glib::RefPtr<Gio::SocketAddressEnumerator> enumerator;
Glib::RefPtr<Gio::SocketConnectable> connectable;
Gio::init ();
context = g_option_context_new (" <hostname>[:port] - Test GSocket client stuff");
auto context = g_option_context_new (" <hostname>[:port] - Test GSocket client stuff");
g_option_context_add_main_entries (context, cmd_entries, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error))
{
......@@ -156,7 +148,7 @@ main (int argc,
return 1;
}
enumerator = connectable->enumerate ();
auto enumerator = connectable->enumerate ();
while (true)
{
try {
......
......@@ -62,14 +62,12 @@ ensure_condition (const Glib::RefPtr<Gio::Socket>& socket,
const Glib::RefPtr<Gio::Cancellable>& cancellable,
Glib::IOCondition condition)
{
GSource *source;
if (!non_blocking)
return;
if (use_source)
{
source = g_socket_create_source (socket->gobj (),
const auto source = g_socket_create_source (socket->gobj (),
(GIOCondition) condition,
cancellable->gobj ());
g_source_set_callback (source,
......@@ -104,16 +102,14 @@ 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;
GError *error = NULL;
GOptionContext *context;
Glib::RefPtr<Gio::Cancellable> cancellable;
GError *error = NULL;
Gio::init ();
context = g_option_context_new (" - Test GSocket server stuff");
auto context = g_option_context_new (" - Test GSocket server stuff");
g_option_context_add_main_entries (context, cmd_entries, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error))
{
......@@ -145,7 +141,7 @@ main (int argc,
if (non_blocking)
socket->set_blocking (false);
src_address = Gio::InetSocketAddress::create (Gio::InetAddress::create_any ((Gio::SocketFamily) G_SOCKET_FAMILY_IPV4), port);
auto src_address = Gio::InetSocketAddress::create (Gio::InetAddress::create_any ((Gio::SocketFamily) G_SOCKET_FAMILY_IPV4), port);
try {
socket->bind (src_address, !dont_reuse_address);
} catch (const Gio::Error& error) {
......@@ -181,7 +177,7 @@ main (int argc,
new_socket->set_blocking (false);
try {
address = new_socket->get_remote_address ();
address = new_socket->get_remote_address ();
} catch (const Gio::Error& error)
{
std::cerr << Glib::ustring::compose ("Error getting remote address: %1\n",
......
......@@ -222,7 +222,7 @@ int main(int argc, char** argv)
//This one shows the results of multiple instance of the same option, such as --list=1 --list=a --list=b
std::cout << " list = ";
for(Glib::OptionGroup::vecustrings::const_iterator iter = group.m_arg_list.begin(); iter != group.m_arg_list.end(); ++iter)
for(auto iter = group.m_arg_list.begin(); iter != group.m_arg_list.end(); ++iter)
{
std::cout << *iter << ", ";
}
......@@ -230,7 +230,7 @@ int main(int argc, char** argv)
//This one shows the remaining arguments on the command line, which had no name= form:
std::cout << " remaining = ";
for(Glib::OptionGroup::vecustrings::const_iterator iter = group.m_remaining_list.begin(); iter != group.m_remaining_list.end(); ++iter)
for(auto iter = group.m_remaining_list.begin(); iter != group.m_remaining_list.end(); ++iter)
{
std::cout << *iter << ", ";
}
......
......@@ -40,8 +40,10 @@ public:
// the 'changed' signal, etc.
Glib::PropertyProxy<Glib::ustring> property_firstname ()
{ return prop_firstname.get_proxy (); }
Glib::PropertyProxy<Glib::ustring> property_lastname ()
{ return prop_lastname.get_proxy (); }
Glib::PropertyProxy<int> property_age ()
{ return prop_age.get_proxy (); }
......
......@@ -24,7 +24,7 @@ int main(int, char**)
Glib::init();
/* Reusing one regex pattern: */
Glib::RefPtr<Glib::Regex> regex = Glib::Regex::create("(a)?(b)");
auto regex = Glib::Regex::create("(a)?(b)");
std::cout << "Pattern=" << regex->get_pattern()
<< ", with string=abcd, result="
<< std::boolalpha << regex->match("abcd")
......
......@@ -30,7 +30,7 @@ static void on_key_changed(const Glib::ustring& key, const Glib::RefPtr<Gio::Set
std::cout << Glib::ustring::compose("'%1' changed\n", key);
if (key == STRING_KEY)
{
Glib::ustring str = settings->get_string(key);
auto str = settings->get_string(key);
std::cout << Glib::ustring::compose("New value of '%1': '%2'\n",
key, str);
......@@ -63,7 +63,7 @@ int main(int, char**)
Glib::setenv("GSETTINGS_SCHEMA_DIR", ".", true);
Glib::setenv("GSETTINGS_BACKEND", "memory", true);
const Glib::RefPtr<Gio::Settings> settings =
const auto settings =
Gio::Settings::create("org.gtkmm.demo");
settings->signal_changed().connect(sigc::bind(sigc::ptr_fun(&on_key_changed), settings));
......
......@@ -152,7 +152,7 @@ Application::Application()
{
for (std::vector<ThreadProgress*>::size_type i = 0; i < progress_threads_.size(); ++i)
{
ThreadProgress *const progress = new ThreadProgress(i + 1);
const auto progress = new ThreadProgress(i + 1);
progress_threads_[i] = progress;
progress->signal_finished().connect(
......
......@@ -156,9 +156,10 @@ bool ThreadTimer::timeout_handler()
void ThreadTimer::thread_function()
{
// create a new Main Context
Glib::RefPtr<Glib::MainContext> context = Glib::MainContext::create();
auto context = Glib::MainContext::create();
// create a new Main Loop
Glib::RefPtr<Glib::MainLoop> mainloop = Glib::MainLoop::create(context, true);
auto mainloop = Glib::MainLoop::create(context, true);
// attach a timeout handler, that is called every second, to the
// newly created MainContext
......
......@@ -70,7 +70,7 @@ void MessageQueue::consumer()
while(queue_.empty())
cond_push_.wait(mutex_);
const int i = queue_.front();
const auto i = queue_.front();
queue_.pop();
std::cout << "\x08 \x08";
std::cout.flush();
......@@ -97,10 +97,10 @@ int main(int, char**)
MessageQueue queue;
Glib::Threads::Thread *const producer = Glib::Threads::Thread::create(
const auto producer = Glib::Threads::Thread::create(
sigc::mem_fun(queue, &MessageQueue::producer));
Glib::Threads::Thread *const consumer = Glib::Threads::Thread::create(
const auto consumer = Glib::Threads::Thread::create(
sigc::mem_fun(queue, &MessageQueue::consumer));
producer->join();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment