From: tho Date: Tue, 4 May 2010 11:09:39 +0000 (+0000) Subject: Socket/Protocols/INet: added operator>> for INet6Network X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=afae50abf125df375ff1610b7a659f9b1da61bc0;hp=b8a2a9d30d2c0aa986c6c86f9cc9288ead67ebdb;p=senf.git Socket/Protocols/INet: added operator>> for INet6Network git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1616 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Socket/Protocols/INet/INet4Address.hh b/senf/Socket/Protocols/INet/INet4Address.hh index c0655c1..b214fc3 100644 --- a/senf/Socket/Protocols/INet/INet4Address.hh +++ b/senf/Socket/Protocols/INet/INet4Address.hh @@ -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 - + // Local Variables: // mode: c++ // fill-column: 100 diff --git a/senf/Socket/Protocols/INet/INet4Address.test.cc b/senf/Socket/Protocols/INet/INet4Address.test.cc index 05a682f..0dc95d9 100644 --- a/senf/Socket/Protocols/INet/INet4Address.test.cc +++ b/senf/Socket/Protocols/INet/INet4Address.test.cc @@ -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_ - + // Local Variables: // mode: c++ // fill-column: 100 diff --git a/senf/Socket/Protocols/INet/INet6Address.cc b/senf/Socket/Protocols/INet/INet6Address.cc index 760952b..eddc78b 100644 --- a/senf/Socket/Protocols/INet/INet6Address.cc +++ b/senf/Socket/Protocols/INet/INet6Address.cc @@ -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" - + // Local Variables: // mode: c++ // fill-column: 100 diff --git a/senf/Socket/Protocols/INet/INet6Address.hh b/senf/Socket/Protocols/INet/INet6Address.hh index f5f700a..cc26d96 100644 --- a/senf/Socket/Protocols/INet/INet6Address.hh +++ b/senf/Socket/Protocols/INet/INet6Address.hh @@ -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 - + // Local Variables: // mode: c++ // fill-column: 100 diff --git a/senf/Socket/Protocols/INet/INet6Address.test.cc b/senf/Socket/Protocols/INet/INet6Address.test.cc index 337feb1..e342b27 100644 --- a/senf/Socket/Protocols/INet/INet6Address.test.cc +++ b/senf/Socket/Protocols/INet/INet6Address.test.cc @@ -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_ - + // Local Variables: // mode: c++ // fill-column: 100