Packets: added Packet::is_shared() member
tho [Tue, 7 Sep 2010 08:32:42 +0000 (08:32 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1710 270642c3-0616-0410-b53a-bc976706d245

senf/Packets/Packet.cci
senf/Packets/Packet.hh
senf/Packets/Packet.test.cc

index e5f9f4d..cb9e4de 100644 (file)
@@ -191,6 +191,12 @@ prefix_ bool senf::Packet::valid()
     return *this;
 }
 
+prefix_ bool senf::Packet::is_shared()
+    const
+{
+    return ptr()->is_shared() || (ptr()->impl().refcount() > 1);
+}
+
 template <class PacketType, class Parser>
 prefix_ Parser senf::operator<<(Parser target, ConcretePacket<PacketType> const & packet)
 {
index 090cd66..6693d0d 100644 (file)
@@ -492,7 +492,11 @@ namespace senf {
 
         unsigned long id() const;       ///< Unique packet id
                                         /**< Get a unique packet id. If two packets have the same
-                                             id, they share the internal data representation.. */
+                                             id, they share the internal data representation. */
+
+        bool is_shared() const;         ///< check if this packet shares data with any another packet handle.
+                                        /**< This method returns true if there is any other packet
+                                             handle pointing to any header in the packet chain. */
 
         ///@}
 
index 8fe9771..557e7f8 100644 (file)
@@ -178,8 +178,18 @@ SENF_AUTO_UNIT_TEST(packet)
     BOOST_CHECK( packet.first() == packet );
     BOOST_CHECK( packet.last() == packet.next() );
 
+    BOOST_CHECK( ! packet.is_shared() );
+    {
+        senf::Packet p2 (packet);
+        BOOST_CHECK( packet.is_shared() );
+        BOOST_CHECK( p2.is_shared() );
+    }
+    BOOST_CHECK( ! packet.is_shared() );
+
     senf::Packet p2 (packet.next());
     BOOST_CHECK( p2 );
+    BOOST_CHECK( packet.is_shared() );
+    BOOST_CHECK( p2.is_shared() );
     packet.parseNextAs<FooPacket>();
     BOOST_CHECK_EQUAL( packet.size(), 12u );
     BOOST_CHECK_EQUAL( packet.next().size(), 8u );
@@ -188,6 +198,7 @@ SENF_AUTO_UNIT_TEST(packet)
     BOOST_CHECK( packet.next().as<FooPacket>() );
 
     p2 = packet.next().clone();
+    BOOST_CHECK( ! packet.is_shared() );
     BOOST_REQUIRE( p2 );
     packet.next().append( p2 );
     BOOST_REQUIRE( packet.next().next() );