replace pointers with references
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Fri, 08 Sep 2006 18:45:48 +0200
changeset 90 d670ba9f726e
parent 89 661932f116ea
child 91 8066ef8d9274
replace pointers with references
samples/main-packet.cc
src/common/buffer.cc
src/common/buffer.h
src/common/packet.cc
src/common/packet.h
src/common/pcap-writer.cc
src/common/tags.cc
src/common/tags.h
src/core/system-file.h
src/core/unix-system-file.cc
src/core/win32-system-file.cc
--- a/samples/main-packet.cc	Fri Sep 08 09:20:17 2006 +0200
+++ b/samples/main-packet.cc	Fri Sep 08 18:45:48 2006 +0200
@@ -77,11 +77,11 @@
 receive (Packet p)
 {
     MyChunk my;
-    p.peek (&my);
-    p.remove (&my);
+    p.peek (my);
+    p.remove (my);
     std::cout << "received data=" << my.getData () << std::endl;
     struct MyTag myTag;
-    p.peekTag (&myTag);
+    p.peekTag (myTag);
 }
 
 
@@ -91,10 +91,10 @@
     MyChunk my;
     my.setData (2);
     std::cout << "send data=2" << std::endl;
-    p.add (&my);
+    p.add (my);
     struct MyTag myTag;
     myTag.m_streamId = 5;
-    p.addTag (&myTag);
+    p.addTag (myTag);
     receive (p);
     return 0;
 }
--- a/src/common/buffer.cc	Fri Sep 08 09:20:17 2006 +0200
+++ b/src/common/buffer.cc	Fri Sep 08 18:45:48 2006 +0200
@@ -332,7 +332,7 @@
     if (m_zeroAreaSize != 0 &&
         start + length > zeroStart &&
         start <= zeroEnd) {
-        transform_intoRealBuffer ();
+        transformIntoRealBuffer ();
     }
     Buffer tmp = *this;
     tmp.removeAtStart (start);
@@ -341,7 +341,7 @@
 }
 
 void
-Buffer::transform_intoRealBuffer (void) const
+Buffer::transformIntoRealBuffer (void) const
 {
     if (m_zeroAreaSize != 0) {
         assert (m_data->m_initialStart >= m_start);
@@ -362,10 +362,10 @@
 }
 
 
-uint8_t *
+uint8_t const*
 Buffer::peekData (void) const
 {
-    transform_intoRealBuffer ();
+    transformIntoRealBuffer ();
     return m_data->m_data + m_start;
 }
 
@@ -399,7 +399,7 @@
 {
     bool success = true;
     uint8_t *expected = array;
-    uint8_t *got;
+    uint8_t const*got;
     got = b.peekData ();
     for (uint32_t j = 0; j < n; j++) {
         if (got[j] != expected[j]) {
--- a/src/common/buffer.h	Fri Sep 08 09:20:17 2006 +0200
+++ b/src/common/buffer.h	Fri Sep 08 18:45:48 2006 +0200
@@ -261,7 +261,7 @@
      * Please, try to never ever use this method. It is really
      * evil and is present only for a few specific uses.
      */
-    uint8_t *peekData (void) const;
+    uint8_t const*peekData (void) const;
 
     /**
      * \param start size to reserve
@@ -337,7 +337,7 @@
     typedef std::vector<struct Buffer::BufferData*> BufferDataList;
 
     inline uint8_t *getStart (void) const;
-    void transform_intoRealBuffer (void) const;
+    void transformIntoRealBuffer (void) const;
     static void recycle (struct Buffer::BufferData *data);
     static struct Buffer::BufferData *create (void);
     static struct Buffer::BufferData *allocate (uint32_t size, uint32_t start);
--- a/src/common/packet.cc	Fri Sep 08 09:20:17 2006 +0200
+++ b/src/common/packet.cc	Fri Sep 08 18:45:48 2006 +0200
@@ -49,33 +49,23 @@
 }
 
 void 
-Packet::add (Chunk *chunk)
+Packet::add (Chunk const&chunk)
 {
-    chunk->add (&m_buffer);
-}
-
-void 
-Packet::peek (Chunk *chunk) const
-{
-    chunk->peek (&m_buffer);
+    chunk.add (&m_buffer);
 }
 
 void 
-Packet::remove (Chunk *chunk)
+Packet::peek (Chunk &chunk) const
 {
-    chunk->remove (&m_buffer);
+    chunk.peek (&m_buffer);
 }
 
-
 void 
-Packet::write (PacketReadWriteCallback callback) const
+Packet::remove (Chunk &chunk)
 {
-    uint8_t *data = m_buffer.peekData ();
-    uint32_t toWrite = getSize ();
-    callback (data, toWrite);
+    chunk.remove (&m_buffer);
 }
 
-
 void 
 Packet::addAtEnd (Packet packet)
 {
@@ -124,4 +114,10 @@
     m_tags.removeAll ();
 }
 
+uint8_t const *
+Packet::peekData (void) const
+{
+	return m_buffer.peekData ();
+}
+
 }; // namespace ns3
--- a/src/common/packet.h	Fri Sep 08 09:20:17 2006 +0200
+++ b/src/common/packet.h	Fri Sep 08 18:45:48 2006 +0200
@@ -86,7 +86,6 @@
  */
 class Packet {
 public:
-    typedef Callback<void,uint8_t *,uint32_t> PacketReadWriteCallback;
 	/**
 	 * Create an empty packet.
 	 */
@@ -122,7 +121,7 @@
 	 *
 	 * \param chunk a pointer to the chunk to add to this packet.
 	 */
-    void add (Chunk *chunk);
+    void add (Chunk const&chunk);
 	/**
 	 * Deserialize chunk from this packet. This method invokes the
 	 * ns3::Chunk::peekFrom method to request the chunk to deserialize
@@ -131,7 +130,7 @@
 	 *
 	 * \param chunk a pointer to the chunk to deserialize from the buffer
 	 */
-    void peek (Chunk *chunk) const;
+    void peek (Chunk &chunk) const;
 	/**
 	 * Remove a deserialized chunk from the internal buffer.
 	 * This method invokes ns3::Chunk::removeFrom to complete
@@ -139,7 +138,7 @@
 	 *
 	 * \param chunk a pointer to the chunk to remove from the internal buffer.
 	 */
-    void remove (Chunk *chunk);
+    void remove (Chunk &chunk);
 	/**
 	 * Attach a tag to this packet. The tag is fully copied
 	 * in a packet-specific internal buffer. This operation 
@@ -148,7 +147,7 @@
 	 * \param tag a pointer to the tag to attach to this packet.
 	 */
     template <typename T>
-    void addTag (T const *tag);
+    void addTag (T const &tag);
 	/**
 	 * Remove a tag from this packet. The data stored internally
 	 * for this tag is copied in the input tag if an instance
@@ -166,7 +165,7 @@
 	 *          in this packet, false otherwise.
 	 */
     template <typename T>
-    bool removeTag (T *tag);
+    bool removeTag (T &tag);
 	/**
 	 * Copy a tag stored internally to the input tag. If no instance
 	 * of this tag is present internally, the input tag is not modified.
@@ -176,13 +175,12 @@
 	 *          in this packet, false otherwise.
 	 */
     template <typename T>
-    bool peekTag (T *tag) const;
+    bool peekTag (T &tag) const;
 	/**
 	 * Remove all the tags stored in this packet. This operation is
 	 * much much faster than invoking removeTag n times.
 	 */
     void removeAllTags (void);
-    void write (PacketReadWriteCallback callback) const;
 	/**
 	 * Concatenate the input packet at the end of the current
 	 * packet.
@@ -216,7 +214,14 @@
 	 * \param size number of bytes from remove
 	 */
     void removeAtStart (uint32_t size);
-
+	
+	/**
+	 * If you try to change the content of the buffer
+	 * returned by this method, you will die.
+	 *
+	 * \returns a pointer to the internal buffer of the packet.
+	 */
+	uint8_t const *peekData (void) const;
 private:
     Packet (Buffer buffer, Tags tags);
     Buffer m_buffer;
@@ -234,17 +239,17 @@
 namespace ns3 {
 
 template <typename T>
-void Packet::addTag (T const*tag)
+void Packet::addTag (T const& tag)
 {
     m_tags.add (tag);
 }
 template <typename T>
-bool Packet::removeTag (T *tag)
+bool Packet::removeTag (T & tag)
 {
     return m_tags.remove (tag);
 }
 template <typename T>
-bool Packet::peekTag (T *tag) const
+bool Packet::peekTag (T & tag) const
 {
     return m_tags.peek (tag);
 }
--- a/src/common/pcap-writer.cc	Fri Sep 08 09:20:17 2006 +0200
+++ b/src/common/pcap-writer.cc	Fri Sep 08 18:45:48 2006 +0200
@@ -39,7 +39,6 @@
 PcapWriter::PcapWriter ()
 {
     m_writer = 0;
-    m_writeCallback = makeCallback (&PcapWriter::writeData, this);
 }
 PcapWriter::~PcapWriter ()
 {
@@ -76,7 +75,7 @@
         write_32 (us & 0xffffffff);
         write_32 (packet.getSize ());
         write_32 (packet.getSize ());
-        packet.write (m_writeCallback);
+		m_writer->write (packet.peekData (), packet.getSize ());
     }
 }
 
--- a/src/common/tags.cc	Fri Sep 08 09:20:17 2006 +0200
+++ b/src/common/tags.cc	Fri Sep 08 18:45:48 2006 +0200
@@ -225,18 +225,18 @@
     Tags tags;
     struct myTagA a;
     a.a = 10;
-    tags.add (&a);
+    tags.add (a);
     a.a = 0;
-    tags.peek (&a);
+    tags.peek (a);
     if (a.a != 10) {
         ok = false;
     }
     //tags.prettyPrint (std::cout);
     struct myTagB b;
     b.b = 0xff;
-    tags.add (&b);
+    tags.add (b);
     b.b = 0;
-    tags.peek (&b);
+    tags.peek (b);
     if (b.b != 0xff) {
         ok = false;
     }
@@ -248,29 +248,29 @@
     //tags.prettyPrint (std::cout);
     struct myTagA oA;
     oA.a = 0;
-    other.peek (&oA);
+    other.peek (oA);
     if (oA.a != 10) {
         ok = false;
     }
     struct myTagB oB;
-    other.peek (&oB);
+    other.peek (oB);
     if (oB.b != 0xff) {
         ok = false;
     }
     // remove data.
-    other.remove (&oA);
-    if (other.peek (&oA)) {
+    other.remove (oA);
+    if (other.peek (oA)) {
         ok = false;
     }
     //other.prettyPrint (std::cout);
-    if (!tags.peek (&oA)) {
+    if (!tags.peek (oA)) {
         ok = false;
     }
-    other.remove (&oB);
-    if (other.peek (&oB)) {
+    other.remove (oB);
+    if (other.peek (oB)) {
         ok = false;
     }
-    if (!tags.peek (&oB)) {
+    if (!tags.peek (oB)) {
         ok = false;
     }
 
@@ -278,13 +278,13 @@
     Tags another = other;
     struct myTagC c;
     c.c[0] = 0x66;
-    another.add (&c);
+    another.add (c);
     c.c[0] = 0;
-    another.peek (&c);
-    if (!another.peek (&c)) {
+    another.peek (c);
+    if (!another.peek (c)) {
         ok = false;
     }
-    if (tags.peek (&c)) {
+    if (tags.peek (c)) {
         ok = false;
     }
 
--- a/src/common/tags.h	Fri Sep 08 09:20:17 2006 +0200
+++ b/src/common/tags.h	Fri Sep 08 18:45:48 2006 +0200
@@ -38,13 +38,13 @@
     inline ~Tags ();
 
     template <typename T>
-    void add (T const*tag);
+    void add (T const&tag);
 
     template <typename T>
-    bool remove (T *tag);
+    bool remove (T &tag);
 
     template <typename T>
-    bool peek (T *tag) const;
+    bool peek (T &tag) const;
 
     void prettyPrint (std::ostream &os);
 
@@ -193,10 +193,10 @@
 
 template <typename T>
 void 
-Tags::add (T const*tag)
+Tags::add (T const&tag)
 {
     assert (sizeof (T) <= Tags::SIZE);
-    uint8_t const*buf = reinterpret_cast<uint8_t const*> (tag);
+    uint8_t const*buf = reinterpret_cast<uint8_t const*> (&tag);
     // ensure this id was not yet added
     for (struct TagData *cur = m_next; cur != 0; cur = cur->m_next) {
         assert (cur->m_id != TypeUid<T>::getUid ());
@@ -212,7 +212,7 @@
 
 template <typename T>
 bool
-Tags::remove (T *tag)
+Tags::remove (T &tag)
 {
     assert (sizeof (T) <= Tags::SIZE);
     return remove (TypeUid<T>::getUid ());
@@ -220,10 +220,10 @@
 
 template <typename T>
 bool
-Tags::peek (T *tag) const
+Tags::peek (T &tag) const
 {
     assert (sizeof (T) <= Tags::SIZE);
-    uint8_t *buf = reinterpret_cast<uint8_t *> (tag);
+    uint8_t *buf = reinterpret_cast<uint8_t *> (&tag);
     for (struct TagData *cur = m_next; cur != 0; cur = cur->m_next) {
         if (cur->m_id == TypeUid<T>::getUid ()) {
             /* found tag */
--- a/src/core/system-file.h	Fri Sep 08 09:20:17 2006 +0200
+++ b/src/core/system-file.h	Fri Sep 08 18:45:48 2006 +0200
@@ -67,7 +67,7 @@
 	 * To make sure the data is written to disk, destroy 
 	 * this object.
 	 */
-    void write (uint8_t *buffer, uint32_t size);
+    void write (uint8_t const*buffer, uint32_t size);
 private:
     SystemFilePrivate *m_priv;
 };
--- a/src/core/unix-system-file.cc	Fri Sep 08 09:20:17 2006 +0200
+++ b/src/core/unix-system-file.cc	Fri Sep 08 18:45:48 2006 +0200
@@ -50,7 +50,7 @@
     ~SystemFilePrivate ();
 
     void open (char const *filename);
-    void write (uint8_t *buffer, uint32_t size);
+    void write (uint8_t const*buffer, uint32_t size);
 private:
     uint8_t m_data[BUFFER_SIZE];
     uint32_t m_current;
@@ -79,7 +79,7 @@
 #endif /* min */
 
 void
-SystemFilePrivate::write (uint8_t *buffer, uint32_t size)
+SystemFilePrivate::write (uint8_t const*buffer, uint32_t size)
 {
     while (size > 0) {
         uint32_t toCopy = min (BUFFER_SIZE - m_current, size);
@@ -111,7 +111,7 @@
     m_priv->open (filename);
 }
 void 
-SystemFile::write (uint8_t *buffer, uint32_t size)
+SystemFile::write (uint8_t const*buffer, uint32_t size)
 {
     m_priv->write (buffer, size);
 }
--- a/src/core/win32-system-file.cc	Fri Sep 08 09:20:17 2006 +0200
+++ b/src/core/win32-system-file.cc	Fri Sep 08 18:45:48 2006 +0200
@@ -42,7 +42,7 @@
     ~SystemFilePrivate ();
 
     void open (char const *filename);
-    void write (uint8_t *buffer, uint32_t size);
+    void write (uint8_t const*buffer, uint32_t size);
 private:
 };
 
@@ -59,7 +59,7 @@
 }
 
 void
-SystemFilePrivate::write (uint8_t *buffer, uint32_t size)
+SystemFilePrivate::write (uint8_t const*buffer, uint32_t size)
 {
 }
 
@@ -78,7 +78,7 @@
     m_priv->open (filename);
 }
 void 
-SystemFile::write (uint8_t *buffer, uint32_t size)
+SystemFile::write (uint8_t const*buffer, uint32_t size)
 {
     m_priv->write (buffer, size);
 }