From: g0dil Date: Thu, 17 Jan 2008 14:45:35 +0000 (+0000) Subject: Packets: (Re-)add (consistent) 'nothrow' variants of packet traversal functions X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=03e6d8f7c983e22cdd021176a3190b7d34a2d8c5;p=senf.git Packets: (Re-)add (consistent) 'nothrow' variants of packet traversal functions git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@618 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Packets/DefaultBundle/EthernetPacket.cc b/Packets/DefaultBundle/EthernetPacket.cc index ede6558..9926efc 100644 --- a/Packets/DefaultBundle/EthernetPacket.cc +++ b/Packets/DefaultBundle/EthernetPacket.cc @@ -72,7 +72,7 @@ prefix_ senf::PacketInterpreterBase::factory_t senf::EthernetPacketType::nextPac prefix_ void senf::EthernetPacketType::finalize(packet p) { - optional_registry_key_t k = key(p.next()); + optional_registry_key_t k = key(p.next(nothrow)); if (k) p->type_length() << k; else @@ -94,7 +94,7 @@ prefix_ void senf::EthVLanPacketType::dump(packet p, std::ostream & os) prefix_ void senf::EthVLanPacketType::finalize(packet p) { - p->type() << key(p.next()); + p->type() << key(p.next(nothrow)); } diff --git a/Packets/DefaultBundle/IPv4Packet.cc b/Packets/DefaultBundle/IPv4Packet.cc index f534aa1..69ed501 100644 --- a/Packets/DefaultBundle/IPv4Packet.cc +++ b/Packets/DefaultBundle/IPv4Packet.cc @@ -88,7 +88,7 @@ prefix_ void senf::IPv4PacketType::dump(packet p, std::ostream & os) prefix_ void senf::IPv4PacketType::finalize(packet p) { p->length() << p.size(); - p->protocol() << key(p.next()); + p->protocol() << key(p.next(nothrow)); p->checksum() << p->calcChecksum(); } diff --git a/Packets/DefaultBundle/UDPPacket.cc b/Packets/DefaultBundle/UDPPacket.cc index 8d4a850..bc204b3 100644 --- a/Packets/DefaultBundle/UDPPacket.cc +++ b/Packets/DefaultBundle/UDPPacket.cc @@ -54,7 +54,7 @@ prefix_ boost::uint16_t senf::UDPPacketParser::calcChecksum() summer.feed( i()+checksum_offset+2, data().end() ); // Now on to the awkward part: the IP pseudo header - IPv4Packet ipv4 (packet().rfind()); + IPv4Packet ipv4 (packet().rfind(nothrow)); if (ipv4) { // Pseudo header defined in RFC768 summer.feed( ipv4->source().i(), @@ -70,7 +70,7 @@ prefix_ boost::uint16_t senf::UDPPacketParser::calcChecksum() } else { // Pseudo header defined in RFC2460 - IPv6Packet ipv6 (packet().rfind()); + IPv6Packet ipv6 (packet().rfind(nothrow)); if (ipv6) { summer.feed( ipv6->source().i(), ipv6->source().i() + IPv6Packet::Parser::source_t::fixed_bytes ); diff --git a/Packets/Packet.cc b/Packets/Packet.cc index bd3ce57..b770e93 100644 --- a/Packets/Packet.cc +++ b/Packets/Packet.cc @@ -50,10 +50,10 @@ prefix_ senf::Packet senf::Packet::checkLast() const { Packet p (*this); - Packet n (p.next()); + Packet n (p.next(nothrow)); while (n) { p = n; - n = p.next(); + n = p.next(nothrow); } return p; } diff --git a/Packets/Packet.cci b/Packets/Packet.cci index e29f050..ea4f549 100644 --- a/Packets/Packet.cci +++ b/Packets/Packet.cci @@ -47,6 +47,14 @@ prefix_ senf::Packet senf::Packet::clone() prefix_ senf::Packet senf::Packet::next() const { + Packet p (next(nothrow)); + if (!p) throw InvalidPacketChainException(); + return p; +} + +prefix_ senf::Packet senf::Packet::next(NoThrow_t) + const +{ PacketInterpreterBase::ptr p (ptr()->next()); return !p && ptr()->nextPacketRange() ? checkNext() : Packet(p); } @@ -54,6 +62,14 @@ prefix_ senf::Packet senf::Packet::next() prefix_ senf::Packet senf::Packet::prev() const { + Packet p (prev(nothrow)); + if (!p) throw InvalidPacketChainException(); + return p; +} + +prefix_ senf::Packet senf::Packet::prev(NoThrow_t) + const +{ return Packet(ptr()->prev()); } @@ -67,7 +83,7 @@ prefix_ senf::Packet senf::Packet::last() const { Packet p (ptr()->last()); - return p.next() ? checkLast() : p; + return p.next(nothrow) ? checkLast() : p; } prefix_ senf::Packet senf::Packet::parseNextAs(factory_t factory) diff --git a/Packets/Packet.ct b/Packets/Packet.ct index 48b83fc..7c556d7 100644 --- a/Packets/Packet.ct +++ b/Packets/Packet.ct @@ -34,12 +34,12 @@ // senf::Packet template -prefix_ OtherPacket senf::Packet::find() +prefix_ OtherPacket senf::Packet::find(NoThrow_t) const { Packet p (*this); while (p && ! p.is()) - p = p.next(); + p = p.next(nothrow); if (p) return p.as(); else @@ -47,12 +47,12 @@ prefix_ OtherPacket senf::Packet::find() } template -prefix_ OtherPacket senf::Packet::rfind() +prefix_ OtherPacket senf::Packet::rfind(NoThrow_t) const { Packet p (*this); while (p && ! p.is()) - p = p.prev(); + p = p.prev(nothrow); if (p) return p.as(); else diff --git a/Packets/Packet.cti b/Packets/Packet.cti index ed032da..5f67ccb 100644 --- a/Packets/Packet.cti +++ b/Packets/Packet.cti @@ -69,16 +69,48 @@ template prefix_ OtherPacket senf::Packet::next() const { - Packet p (next()) - return p ? p.as() : OtherPacket(); + return next().as(); +} + +template +prefix_ OtherPacket senf::Packet::next(NoThrow_t) + const +{ + Packet p (next(nothrow)); + return p && p.is() ? p.as() : OtherPacket(); +} + +template +prefix_ OtherPacket senf::Packet::find() + const +{ + OtherPacket p (find(nothrow)); + if (!p) throw InvalidPacketChainException(); + return p; } template prefix_ OtherPacket senf::Packet::prev() const { - Packet p (prev()); - return p ? p.as() : OtherPacket(); + return prev().as(); +} + +template +prefix_ OtherPacket senf::Packet::prev(NoThrow_t) + const +{ + Packet p (prev(nothrow)); + return p && p.is() ? p.as() : OtherPacket(); +} + +template +prefix_ OtherPacket senf::Packet::rfind() + const +{ + OtherPacket p (rfind(nothrow)); + if (!p) throw InvalidPacketChainException(); + return p; } template diff --git a/Packets/Packet.hh b/Packets/Packet.hh index 9855adb..4fc2e1e 100644 --- a/Packets/Packet.hh +++ b/Packets/Packet.hh @@ -188,33 +188,63 @@ namespace senf { ///< Get next packet in chain /**< \returns in - valid() packet, if no next packet exists */ + Packet next(NoThrow_t) const; + ///< Get next packet in chain + /**< \returns in - valid() packet, if no next packet + exists */ template OtherPacket next() const; ///< Get next packet in chain and cast to \a OtherPacket /**< \throws std::bad_cast if the next() packet is not of type \a OtherPacket \returns in - valid() packet, if no next packet exists */ + template OtherPacket next(NoThrow_t) const; + ///< Get next packet in chain and cast to \a OtherPacket + /**< \throws std::bad_cast if the next() packet is not of + type \a OtherPacket + \returns in - valid() packet, if no next packet + exists */ template OtherPacket find() const; ///< Search chain forward for packet of type \a OtherPacket /**< The search will start with the current packet. \returns in - valid() packet, if no packet of type \a OtherPacket can be found. */ + template OtherPacket find(NoThrow_t) const; + ///< Search chain forward for packet of type \a OtherPacket + /**< The search will start with the current packet. + \returns in - valid() packet, if no packet of type \a + OtherPacket can be found. */ Packet prev() const; ///< Get previous packet in chain /**< \returns in - valid() packet, if no previous packet exists */ + Packet prev(NoThrow_t) const; + ///< Get previous packet in chain + /**< \returns in - valid() packet, if no previous packet + exists */ template OtherPacket prev() const; ///< Get previous packet in chain and cast to \a OtherPacket /**< \throws std::bad_cast, if the previous packet is not of type \a OtherPacket \returns in - valid() packet, if no previous packet exists */ + template OtherPacket prev(NoThrow_t) const; + ///< Get previous packet in chain and cast to \a OtherPacket + /**< \throws std::bad_cast, if the previous packet is not of + type \a OtherPacket + \returns in - valid() packet, if no previous packet + exists */ template OtherPacket rfind() const; ///< Search chain backwards for packet of type \a OtherPacket /**< The search will start with the current packet. \returns in - valid() packet, if no packet of type \a OtherPacket can be found. */ + template OtherPacket rfind(NoThrow_t) const; + ///< Search chain backwards for packet of type \a OtherPacket + /**< The search will start with the current packet. + \returns in - valid() packet, if no packet of type \a + OtherPacket can be found. */ Packet first() const; diff --git a/Packets/Packet.test.cc b/Packets/Packet.test.cc index d738f60..e79f172 100644 --- a/Packets/Packet.test.cc +++ b/Packets/Packet.test.cc @@ -88,7 +88,7 @@ namespace { << "length: " << p->length() << "\n"; } static void finalize(packet p) { - if (p.next()) + if (p.next(senf::nothrow)) p->type() = senf::PacketRegistry::key(p.next()); else p->type() = -1; @@ -113,8 +113,8 @@ BOOST_AUTO_UNIT_TEST(packet) BOOST_REQUIRE( packet ); BOOST_CHECK( packet.next() ); - BOOST_CHECK( ! packet.next().next() ); - BOOST_CHECK( ! packet.prev() ); + BOOST_CHECK( ! packet.next().next(senf::nothrow) ); + BOOST_CHECK( ! packet.prev(senf::nothrow) ); BOOST_CHECK( packet.next().prev() == packet ); BOOST_CHECK( packet.next() != packet ); BOOST_CHECK_EQUAL( packet.size(), 12u ); @@ -170,7 +170,7 @@ BOOST_AUTO_UNIT_TEST(packet) BOOST_CHECK_EQUAL( packet.next().size(), 11u ); BOOST_REQUIRE( packet.next().next() ); BOOST_CHECK( packet.next().next().is() ); - BOOST_CHECK( ! packet.next().next().next() ); + BOOST_CHECK( ! packet.next().next().next(senf::nothrow) ); BOOST_CHECK_EQUAL( packet.next().next().data()[0], 0x81u ); BOOST_CHECK( packet.first().find() == packet );