From: Mukund Sivaraman Date: Tue, 7 Jan 2014 05:03:24 +0000 (+0530) Subject: [1283] Define operator<< for IOAddress X-Git-Tag: bind10-1.2.0beta1-release~132^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=df980e49b85b8f04aefbd2bdf3ad1b6da85c3ad9;p=thirdparty%2Fkea.git [1283] Define operator<< for IOAddress --- diff --git a/src/lib/asiolink/io_address.cc b/src/lib/asiolink/io_address.cc index 90bdf57d29..798672503c 100644 --- a/src/lib/asiolink/io_address.cc +++ b/src/lib/asiolink/io_address.cc @@ -114,5 +114,11 @@ IOAddress::operator uint32_t() const { } } +std::ostream& +operator<<(std::ostream& os, const IOAddress& address) { + os << address.toText(); + return (os); +} + } // namespace asiolink } // namespace isc diff --git a/src/lib/asiolink/io_address.h b/src/lib/asiolink/io_address.h index 5b11b8784c..8b18bf65d3 100644 --- a/src/lib/asiolink/io_address.h +++ b/src/lib/asiolink/io_address.h @@ -232,6 +232,22 @@ private: asio::ip::address asio_address_; }; +/// \brief Insert the IOAddress as a string into stream. +/// +/// This method converts the \c address into a string and inserts it +/// into the output stream \c os. +/// +/// This function overloads the global operator<< to behave as described +/// in ostream::operator<< but applied to \c IOAddress objects. +/// +/// \param os A \c std::ostream object on which the insertion operation is +/// performed. +/// \param address The \c IOAddress object output by the operation. +/// \return A reference to the same \c std::ostream object referenced by +/// parameter \c os after the insertion operation. +std::ostream& +operator<<(std::ostream& os, const IOAddress& address); + } // namespace asiolink } // namespace isc #endif // IO_ADDRESS_H diff --git a/src/lib/asiolink/tests/io_address_unittest.cc b/src/lib/asiolink/tests/io_address_unittest.cc index 4bd7626df3..eea10e9ef0 100644 --- a/src/lib/asiolink/tests/io_address_unittest.cc +++ b/src/lib/asiolink/tests/io_address_unittest.cc @@ -21,6 +21,7 @@ #include #include #include +#include using namespace isc::asiolink; @@ -172,3 +173,12 @@ TEST(IOAddressTest, lessThanEqual) { EXPECT_TRUE(addr6 <= addr7); } + +// test operator<<. We simply confirm it appends the result of toText(). +TEST(IOAddressTest, LeftShiftOperator) { + const IOAddress addr("192.0.2.5"); + + std::ostringstream oss; + oss << addr; + EXPECT_EQ(addr.toText(), oss.str()); +}