From: Leandro Lucarella Date: Mon, 10 Nov 2008 16:03:04 +0000 (-0200) Subject: Add socket addresses ostream formatting X-Git-Url: https://git.llucax.com/software/posixx.git/commitdiff_plain/3e0a7ecf57252241e9d5a87720ed6359818f8d28?ds=inline Add socket addresses ostream formatting --- diff --git a/src/linux/tipc/print.hpp b/src/linux/tipc/print.hpp new file mode 100644 index 0000000..3b3d642 --- /dev/null +++ b/src/linux/tipc/print.hpp @@ -0,0 +1,35 @@ +#ifndef POSIXX_LINUX_TIPC_PRINT_HPP_ +#define POSIXX_LINUX_TIPC_PRINT_HPP_ + +#include "../tipc.hpp" // posixx::linux::tipc::sockaddr +#include // std::ostream + +inline +std::ostream& operator << (std::ostream& os, + const posixx::linux::tipc::sockaddr& sa) throw() +{ + using posixx::linux::tipc::sockaddr; + os << "tipc::sockaddr(scope=" << unsigned(sa.scope) << ", "; + switch (sa.type()) + { + case sockaddr::ID: + os << "id(ref=" << sa.addr.id.ref + << ", node=" << sa.addr.id.node << ")"; + break; + case sockaddr::NAME: + os << "name(type=" << sa.addr.name.name.type + << ", instance=" << sa.addr.name.name.instance + << ", domain=" << sa.addr.name.domain << ")"; + break; + case sockaddr::NAMESEQ: + os << "nameseq(type=" << sa.addr.nameseq.type + << ", lower=" << sa.addr.nameseq.lower + << ", upper=" << sa.addr.nameseq.upper << ")"; + break; + default: + os << "UNKNOWN!"; + } + return os << ")"; +} + +#endif // POSIXX_LINUX_TIPC_PRINT_HPP_ diff --git a/src/socket/inet/print.hpp b/src/socket/inet/print.hpp new file mode 100644 index 0000000..7c61e26 --- /dev/null +++ b/src/socket/inet/print.hpp @@ -0,0 +1,15 @@ +#ifndef POSIXX_SOCKET_INET_PRINT_HPP_ +#define POSIXX_SOCKET_INET_PRINT_HPP_ + +#include "../inet.hpp" // posixx::socket::inet::sockaddr +#include // std::ostream + +inline +std::ostream& operator << (std::ostream& os, + const posixx::socket::inet::sockaddr& sa) throw() +{ + return os << "inet::sockaddr(port=" << ntohs(sa.sin_port) + << ", addr=" << inet_ntoa(sa.sin_addr) << ")"; +} + +#endif // POSIXX_SOCKET_INET_PRINT_HPP_ diff --git a/src/socket/unix/print.hpp b/src/socket/unix/print.hpp new file mode 100644 index 0000000..3a540f2 --- /dev/null +++ b/src/socket/unix/print.hpp @@ -0,0 +1,14 @@ +#ifndef POSIXX_SOCKET_UNIX_PRINT_HPP_ +#define POSIXX_SOCKET_UNIX_PRINT_HPP_ + +#include "../inet.hpp" // posixx::socket::inet::sockaddr +#include // std::ostream + +inline +std::ostream& operator << (std::ostream& os, + const posixx::socket::unix::sockaddr& sa) throw() +{ + return os << "unix::sockaddr(path=" << sa.sun_path << ")"; +} + +#endif // POSIXX_SOCKET_UNIX_PRINT_HPP_ diff --git a/test/linux/tipc/common.hpp b/test/linux/tipc/common.hpp index 81cf51e..2619663 100644 --- a/test/linux/tipc/common.hpp +++ b/test/linux/tipc/common.hpp @@ -2,40 +2,12 @@ #define TEST_LINUX_TIPC_COMMON_HPP_ #include // posixx::linux::tipc -#include // std::ostream +#include // address output formatting #define PTYPE 10000 #define INST1 10001 #define INST2 10002 -static inline -std::ostream& operator << (std::ostream& os, - const posixx::linux::tipc::sockaddr& sa) throw() -{ - using posixx::linux::tipc::sockaddr; - os << "tipc::sockaddr(scope=" << unsigned(sa.scope) << ", "; - switch (sa.type()) - { - case sockaddr::ID: - os << "id(ref=" << sa.addr.id.ref - << ", node=" << sa.addr.id.node << ")"; - break; - case sockaddr::NAME: - os << "name(type=" << sa.addr.name.name.type - << ", instance=" << sa.addr.name.name.instance - << ", domain=" << sa.addr.name.domain << ")"; - break; - case sockaddr::NAMESEQ: - os << "nameseq(type=" << sa.addr.nameseq.type - << ", lower=" << sa.addr.nameseq.lower - << ", upper=" << sa.addr.nameseq.upper << ")"; - break; - default: - os << "UNKNOWN!"; - } - return os << ")"; -} - // no need to clean addresses #define clean_test_address(socket, addr) diff --git a/test/socket/inet/common.hpp b/test/socket/inet/common.hpp index 5dd8612..9884c7e 100644 --- a/test/socket/inet/common.hpp +++ b/test/socket/inet/common.hpp @@ -2,6 +2,7 @@ #define TEST_SOCKET_IP_COMMON_HPP_ #include // posixx::socket::inet +#include // address ostream formatting #include // posixx::socket::opt::REUSEADDR #include // std::ostream @@ -9,15 +10,6 @@ #define PORT1 10001 #define PORT2 10002 -static inline -std::ostream& operator << (std::ostream& os, - const posixx::socket::inet::sockaddr& sa) throw() -{ - return os << "inet::sockaddr(family=" << sa.sin_family - << ", port=" << ntohs(sa.sin_port) - << ", addr=" << inet_ntoa(sa.sin_addr) << ")"; -} - static inline void clean_test_address(posixx::socket::inet::socket& socket, const posixx::socket::inet::sockaddr& addr) diff --git a/test/socket/unix/common.hpp b/test/socket/unix/common.hpp index e80f1a6..4686974 100644 --- a/test/socket/unix/common.hpp +++ b/test/socket/unix/common.hpp @@ -2,6 +2,7 @@ #define TEST_SOCKET_UNIX_COMMON_HPP_ #include // posixx::socket::unix +#include // address ostream formatting #include // unit testing stuff #include // std::ostream #include // unlink @@ -11,14 +12,6 @@ #define PATH1 PATH "1" #define PATH2 PATH "2" -static inline -std::ostream& operator << (std::ostream& os, - const posixx::socket::unix::sockaddr& sa) throw() -{ - return os << "unix::sockaddr(family=" << sa.sun_family - << ", path=" << sa.sun_path << ")"; -} - static inline void clean_test_address(posixx::socket::unix::socket& socket, const posixx::socket::unix::sockaddr& addr)