From: tho Date: Wed, 15 Sep 2010 13:44:15 +0000 (+0000) Subject: Packets: Packet::clone() clones now also annotations X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=80550046686fc160f5d7ff076f1b167a13538466;p=senf.git Packets: Packet::clone() clones now also annotations git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1715 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Packets/Packet.hh b/senf/Packets/Packet.hh index cc9c9f2..8b116e6 100644 --- a/senf/Packets/Packet.hh +++ b/senf/Packets/Packet.hh @@ -164,9 +164,9 @@ namespace senf { validity. */ Packet clone() const; ///< Create copy packet /**< clone() will create a complete copy of \c this - packet. The returned packet will have the same data and - packet chain. It does however not share any data with - the original packet. */ + packet. The returned packet will have the same data, + annotations and packet chain. It does however not + share any data with the original packet. */ // conversion constructors diff --git a/senf/Packets/Packet.test.cc b/senf/Packets/Packet.test.cc index 1684a88..62b22c4 100644 --- a/senf/Packets/Packet.test.cc +++ b/senf/Packets/Packet.test.cc @@ -349,11 +349,17 @@ SENF_AUTO_UNIT_TEST(packetAnnotation) BOOST_CHECK_EQUAL( p2.annotation().s, "dead beef" ); BOOST_CHECK_EQUAL( p2.annotation().i, 0x12345678 ); + senf::Packet pClone (packet.clone()); + p2.clearAnnotations(); BOOST_CHECK_EQUAL( p2.annotation().s, "empty" ); BOOST_CHECK_EQUAL( p2.annotation().i, -1 ); BOOST_CHECK_EQUAL( p2.annotation().value, 0 ); + BOOST_CHECK_EQUAL( pClone.annotation().value, 0xDEADBEEFu ); + BOOST_CHECK_EQUAL( pClone.annotation().s, "dead beef" ); + BOOST_CHECK_EQUAL( pClone.annotation().i, 0x12345678 ); + BOOST_CHECK( Reg::lookup() >= 0 ); BOOST_CHECK( Reg::lookup() < 0 ); BOOST_CHECK( Reg::lookup() < 0 ); diff --git a/senf/Packets/PacketImpl.cc b/senf/Packets/PacketImpl.cc index c374e6d..c958d44 100644 --- a/senf/Packets/PacketImpl.cc +++ b/senf/Packets/PacketImpl.cc @@ -162,6 +162,14 @@ prefix_ void senf::detail::PacketImpl::clearAnnotations() complexAnnotations_.clear(); } +prefix_ void senf::detail::PacketImpl::assignAnnotations(PacketImpl const & other) +{ + std::copy(&other.simpleAnnotations_[0], &other.simpleAnnotations_[0] + + sizeof(simpleAnnotations_)/sizeof(simpleAnnotations_[0]), simpleAnnotations_); + complexAnnotations_.assign( + other.complexAnnotations_.begin(), other.complexAnnotations_.end()); +} + prefix_ void * senf::detail::PacketImpl::complexAnnotation(AnnotationRegistry::key_type key) { SENF_ASSERT( key < 0, "complexAnnotation called with invalid key"); diff --git a/senf/Packets/PacketImpl.hh b/senf/Packets/PacketImpl.hh index 32b2acf..b41fe23 100644 --- a/senf/Packets/PacketImpl.hh +++ b/senf/Packets/PacketImpl.hh @@ -172,6 +172,7 @@ namespace detail { Annotation & annotation(); void clearAnnotations(); + void assignAnnotations(PacketImpl const & other); void dumpAnnotations(std::ostream & os); /** \brief Internal: Keep PacketImpl instance alive diff --git a/senf/Packets/PacketImpl.ih b/senf/Packets/PacketImpl.ih index e6b7bfe..c04e136 100644 --- a/senf/Packets/PacketImpl.ih +++ b/senf/Packets/PacketImpl.ih @@ -154,8 +154,16 @@ namespace detail { virtual ~EntryBase() {} virtual void * get() = 0; + + typedef EntryBase * ptr; + virtual ptr clone() const = 0; }; + inline AnnotationRegistry::EntryBase::ptr new_clone( AnnotationRegistry::EntryBase const & entry) + { + return entry.clone(); + } + template class AnnotationRegistry::Entry : public AnnotationRegistry::EntryBase @@ -168,6 +176,7 @@ namespace detail { { senf::IGNORE(&proxy_); return key_; } virtual void * get() { return & annotation_; } + virtual EntryBase::ptr clone() const { return new Entry( *this); } private: Annotation annotation_; diff --git a/senf/Packets/PacketInterpreter.cc b/senf/Packets/PacketInterpreter.cc index 3b03742..be6c63d 100644 --- a/senf/Packets/PacketInterpreter.cc +++ b/senf/Packets/PacketInterpreter.cc @@ -46,6 +46,7 @@ prefix_ senf::PacketInterpreterBase::ptr senf::PacketInterpreterBase::clone() ptr pi (appendClone(p.p,begin(),p.p->begin())); for (ptr i (next()); i; i = i->next()) i->appendClone(p.p,begin(),p.p->begin()); + pi->impl().assignAnnotations( impl()); return pi; }