# HG changeset patch # User David Demelier # Date 1414080938 -7200 # Node ID adcae2bde2f0a7d12d02f25c9f36a01502451bde # Parent b544a599e08ee3aa1845f97a617a07f052149659 Socket: listener now have initializer list constructor diff -r b544a599e08e -r adcae2bde2f0 C++/SocketListener.cpp --- a/C++/SocketListener.cpp Thu Oct 23 17:59:14 2014 +0200 +++ b/C++/SocketListener.cpp Thu Oct 23 18:15:38 2014 +0200 @@ -315,3 +315,10 @@ m_interface = std::make_unique(); } + +SocketListener::SocketListener(std::initializer_list> list, int method) + : SocketListener(method) +{ + for (const auto &p : list) + add(p.first, p.second); +} diff -r b544a599e08e -r adcae2bde2f0 C++/SocketListener.h --- a/C++/SocketListener.h Thu Oct 23 17:59:14 2014 +0200 +++ b/C++/SocketListener.h Thu Oct 23 18:15:38 2014 +0200 @@ -21,6 +21,8 @@ #include #include +#include +#include #include #include "Socket.h" @@ -175,7 +177,15 @@ * * @param method the preferred method */ - SocketListener(int method = Poll); + SocketListener(int method = PreferredMethod); + + /** + * Createa listener with some sockets. + * + * @param list the initializer list + * @param method the preferred method + */ + SocketListener(std::initializer_list> list, int method = PreferredMethod); /** * Add a socket to listen to. diff -r b544a599e08e -r adcae2bde2f0 C++/Tests/Sockets/main.cpp --- a/C++/Tests/Sockets/main.cpp Thu Oct 23 17:59:14 2014 +0200 +++ b/C++/Tests/Sockets/main.cpp Thu Oct 23 18:15:38 2014 +0200 @@ -55,6 +55,33 @@ s.close(); } +TEST(Misc, initializer) +{ + Socket s1, s2; + + try { + s1 = { AF_INET6, SOCK_STREAM, 0 }; + s2 = { AF_INET6, SOCK_STREAM, 0 }; + + SocketListener listener { + { s1, Read }, + { s2, Read }, + }; + + ASSERT_EQ(2UL, listener.size()); + + listener.list([&] (const auto &so, auto direction) { + ASSERT_TRUE(so == s1 || so == s2); + ASSERT_EQ(Read, direction); + }); + } catch (const std::exception &ex) { + std::cerr << "warning: " << ex.what() << std::endl; + } + + s1.close(); + s2.close(); +} + /* -------------------------------------------------------- * Select tests * -------------------------------------------------------- */