Mercurial > code
changeset 278:adcae2bde2f0
Socket: listener now have initializer list constructor
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 23 Oct 2014 18:15:38 +0200 |
parents | b544a599e08e |
children | af630354610f |
files | C++/SocketListener.cpp C++/SocketListener.h C++/Tests/Sockets/main.cpp |
diffstat | 3 files changed, 45 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- 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<SelectMethod>(); } + +SocketListener::SocketListener(std::initializer_list<std::pair<Socket, int>> list, int method) + : SocketListener(method) +{ + for (const auto &p : list) + add(p.first, p.second); +}
--- 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 <chrono> #include <functional> +#include <initializer_list> +#include <utility> #include <vector> #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<std::pair<Socket, int>> list, int method = PreferredMethod); /** * Add a socket to listen to.
--- 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 * -------------------------------------------------------- */