Socket/Protocols/INet: added operator>> for INet6Network
tho [Tue, 4 May 2010 11:09:39 +0000 (11:09 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1616 270642c3-0616-0410-b53a-bc976706d245

senf/Socket/Protocols/INet/INet4Address.hh
senf/Socket/Protocols/INet/INet4Address.test.cc
senf/Socket/Protocols/INet/INet6Address.cc
senf/Socket/Protocols/INet/INet6Address.hh
senf/Socket/Protocols/INet/INet6Address.test.cc

index c0655c1..b214fc3 100644 (file)
@@ -277,6 +277,7 @@ namespace senf {
     std::ostream & operator<<(std::ostream & os, INet4Network const & addr);
 
     /** \brief Initialize INet4Address instance from a string representation
+
         sets std::ios::failbit on the stream if an error occurred
         \see INet4Address from_string()
         \related INet4Network
@@ -291,7 +292,7 @@ namespace senf {
 #include "INet4Address.cti"
 #endif
 
-\f
+
 // Local Variables:
 // mode: c++
 // fill-column: 100
index 05a682f..0dc95d9 100644 (file)
@@ -118,12 +118,28 @@ SENF_AUTO_UNIT_TEST(inet4Network)
 
     BOOST_CHECK_THROW( senf::INet4Network(""), senf::AddressSyntaxException );
     BOOST_CHECK_THROW( senf::INet4Network("192.0.2.0/24/beef"), senf::AddressSyntaxException );
+
+    {
+        std::stringstream str;
+        senf::INet4Network net;
+        str >> net;
+        BOOST_CHECK( str.fail());
+    }
+    {
+        std::stringstream str;
+        senf::INet4Network net ("128.129.130.131/128");
+        str << net;
+        BOOST_CHECK_EQUAL( str.str(), "128.129.130.131/128");
+        str >> net;
+        BOOST_CHECK( ! str.fail());
+        BOOST_CHECK_EQUAL(net, senf::INet4Network("128.129.130.131/128"));
+    }
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 
-\f
+
 // Local Variables:
 // mode: c++
 // fill-column: 100
index 760952b..eddc78b 100644 (file)
@@ -141,11 +141,25 @@ prefix_ senf::INet6Network::INet6Network(std::string const & s)
     detail::apply_mask(prefix_len_, address_.begin(), address_.end(), _1 &= _2);
 }
 
+prefix_ std::istream & senf::operator>>(std::istream & is, INet6Network & addr)
+{
+    std::string s;
+    if (!(is >> s))
+        return is;
+    try {
+        addr = INet6Network(s);
+    }
+    catch (AddressException &) {
+        is.setstate(std::ios::failbit);
+    }
+    return is;
+}
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 //#include "INet6Address.mpp"
 
-\f
+
 // Local Variables:
 // mode: c++
 // fill-column: 100
index f5f700a..cc26d96 100644 (file)
@@ -292,7 +292,9 @@ namespace senf {
         \related INet6Address
      */
     std::ostream & operator<<(std::ostream & os, INet6Address const & addr);
+
     /** \brief Try to initialize INet6Address instance from a string representation
+
         sets std::ios::failbit on the stream if an error occurred
         \see INet6Address from_string()
         \related INet6Address
@@ -394,6 +396,13 @@ namespace senf {
         \related INet6Network
      */
     std::ostream & operator<<(std::ostream & os, INet6Network const & addr);
+
+    /** \brief Try to initialize INet6Network instance from a string representation
+
+        sets std::ios::failbit on the stream if an error occurred
+        \related INet6Network
+     */
+    std::istream & operator>>(std::istream & is, INet6Network & addr);
 }
 
 ///////////////////////////////hh.e////////////////////////////////////////
@@ -402,7 +411,7 @@ namespace senf {
 #include "INet6Address.cti"
 #endif
 
-\f
+
 // Local Variables:
 // mode: c++
 // fill-column: 100
index 337feb1..e342b27 100644 (file)
@@ -196,12 +196,28 @@ SENF_AUTO_UNIT_TEST(inet6Network)
 
     BOOST_CHECK_THROW( INet6Network(""), AddressSyntaxException );
     BOOST_CHECK_THROW( INet6Network("2001:db8:1234::/beef"), AddressSyntaxException );
+
+    {
+        std::stringstream str;
+        INet6Network net;
+        str >> net;
+        BOOST_CHECK( str.fail());
+    }
+    {
+        std::stringstream str;
+        INet6Network net ("2001:db8:1230::/44");
+        str << net;
+        BOOST_CHECK_EQUAL( str.str(), "2001:db8:1230::/44");
+        str >> net;
+        BOOST_CHECK( ! str.fail());
+        BOOST_CHECK_EQUAL(net, INet6Network("2001:db8:1230::/44"));
+    }
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 
-\f
+
 // Local Variables:
 // mode: c++
 // fill-column: 100