Browse Source

[1283] Define operator<< for IOAddress

Mukund Sivaraman 11 years ago
parent
commit
df980e49b8

+ 6 - 0
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

+ 16 - 0
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

+ 10 - 0
src/lib/asiolink/tests/io_address_unittest.cc

@@ -21,6 +21,7 @@
 #include <algorithm>
 #include <cstring>
 #include <vector>
+#include <sstream>
 
 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());
+}