From 1a510b4a2641774db7bcfcaf7ce8c7bacbc60323 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 8 Sep 2006 18:45:48 +0200 Subject: [PATCH] replace pointers with references --- samples/main-packet.cc | 10 +++++----- src/common/buffer.cc | 10 +++++----- src/common/buffer.h | 4 ++-- src/common/packet.cc | 28 ++++++++++++---------------- src/common/packet.h | 29 +++++++++++++++++------------ src/common/pcap-writer.cc | 3 +-- src/common/tags.cc | 32 ++++++++++++++++---------------- src/common/tags.h | 16 ++++++++-------- src/core/system-file.h | 2 +- src/core/unix-system-file.cc | 6 +++--- src/core/win32-system-file.cc | 6 +++--- 11 files changed, 73 insertions(+), 73 deletions(-) diff --git a/samples/main-packet.cc b/samples/main-packet.cc index d17696fbf..6cabdb233 100644 --- a/samples/main-packet.cc +++ b/samples/main-packet.cc @@ -77,11 +77,11 @@ static void 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 @@ int main (int argc, char *argv[]) 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; } diff --git a/src/common/buffer.cc b/src/common/buffer.cc index 55938cc59..a7d085d88 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -332,7 +332,7 @@ Buffer::createFragment (uint32_t start, uint32_t length) const if (m_zeroAreaSize != 0 && start + length > zeroStart && start <= zeroEnd) { - transform_intoRealBuffer (); + transformIntoRealBuffer (); } Buffer tmp = *this; tmp.removeAtStart (start); @@ -341,7 +341,7 @@ Buffer::createFragment (uint32_t start, uint32_t length) const } 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 @@ Buffer::transform_intoRealBuffer (void) const } -uint8_t * +uint8_t const* Buffer::peekData (void) const { - transform_intoRealBuffer (); + transformIntoRealBuffer (); return m_data->m_data + m_start; } @@ -399,7 +399,7 @@ BufferTest::ensureWrittenBytes (Buffer b, uint32_t n, uint8_t array[]) { 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]) { diff --git a/src/common/buffer.h b/src/common/buffer.h index d60b5e807..991d329ac 100644 --- a/src/common/buffer.h +++ b/src/common/buffer.h @@ -261,7 +261,7 @@ public: * 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 @@ private: typedef std::vector 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); diff --git a/src/common/packet.cc b/src/common/packet.cc index 519963480..778b4eba7 100644 --- a/src/common/packet.cc +++ b/src/common/packet.cc @@ -49,33 +49,23 @@ Packet::getSize (void) const } void -Packet::add (Chunk *chunk) +Packet::add (Chunk const&chunk) { - chunk->add (&m_buffer); + chunk.add (&m_buffer); } void -Packet::peek (Chunk *chunk) const +Packet::peek (Chunk &chunk) const { - chunk->peek (&m_buffer); + chunk.peek (&m_buffer); } void -Packet::remove (Chunk *chunk) +Packet::remove (Chunk &chunk) { - chunk->remove (&m_buffer); + chunk.remove (&m_buffer); } - -void -Packet::write (PacketReadWriteCallback callback) const -{ - uint8_t *data = m_buffer.peekData (); - uint32_t toWrite = getSize (); - callback (data, toWrite); -} - - void Packet::addAtEnd (Packet packet) { @@ -124,4 +114,10 @@ Packet::removeAllTags (void) m_tags.removeAll (); } +uint8_t const * +Packet::peekData (void) const +{ + return m_buffer.peekData (); +} + }; // namespace ns3 diff --git a/src/common/packet.h b/src/common/packet.h index 5404c3bd0..d045a46e3 100644 --- a/src/common/packet.h +++ b/src/common/packet.h @@ -86,7 +86,6 @@ namespace ns3 { */ class Packet { public: - typedef Callback PacketReadWriteCallback; /** * Create an empty packet. */ @@ -122,7 +121,7 @@ public: * * \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 @@ public: * * \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 @@ public: * * \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 @@ public: * \param tag a pointer to the tag to attach to this packet. */ template - 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 @@ public: * in this packet, false otherwise. */ template - 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 @@ public: * in this packet, false otherwise. */ template - 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 @@ public: * \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 @@ private: namespace ns3 { template -void Packet::addTag (T const*tag) +void Packet::addTag (T const& tag) { m_tags.add (tag); } template -bool Packet::removeTag (T *tag) +bool Packet::removeTag (T & tag) { return m_tags.remove (tag); } template -bool Packet::peekTag (T *tag) const +bool Packet::peekTag (T & tag) const { return m_tags.peek (tag); } diff --git a/src/common/pcap-writer.cc b/src/common/pcap-writer.cc index 73b541171..7c76bf310 100644 --- a/src/common/pcap-writer.cc +++ b/src/common/pcap-writer.cc @@ -39,7 +39,6 @@ enum { PcapWriter::PcapWriter () { m_writer = 0; - m_writeCallback = makeCallback (&PcapWriter::writeData, this); } PcapWriter::~PcapWriter () { @@ -76,7 +75,7 @@ PcapWriter::writePacket (Packet const packet) write_32 (us & 0xffffffff); write_32 (packet.getSize ()); write_32 (packet.getSize ()); - packet.write (m_writeCallback); + m_writer->write (packet.peekData (), packet.getSize ()); } } diff --git a/src/common/tags.cc b/src/common/tags.cc index 569a8084f..2342fa509 100644 --- a/src/common/tags.cc +++ b/src/common/tags.cc @@ -225,18 +225,18 @@ TagsTest::runTests (void) 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 @@ TagsTest::runTests (void) //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 @@ TagsTest::runTests (void) 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; } diff --git a/src/common/tags.h b/src/common/tags.h index 8f446da0b..ae5a38fdd 100644 --- a/src/common/tags.h +++ b/src/common/tags.h @@ -38,13 +38,13 @@ public: inline ~Tags (); template - void add (T const*tag); + void add (T const&tag); template - bool remove (T *tag); + bool remove (T &tag); template - bool peek (T *tag) const; + bool peek (T &tag) const; void prettyPrint (std::ostream &os); @@ -193,10 +193,10 @@ void (*TagPrettyPrinter::gPrettyPrinter) (T *, std::ostream &) = 0; template void -Tags::add (T const*tag) +Tags::add (T const&tag) { assert (sizeof (T) <= Tags::SIZE); - uint8_t const*buf = reinterpret_cast (tag); + uint8_t const*buf = reinterpret_cast (&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::getUid ()); @@ -212,7 +212,7 @@ Tags::add (T const*tag) template bool -Tags::remove (T *tag) +Tags::remove (T &tag) { assert (sizeof (T) <= Tags::SIZE); return remove (TypeUid::getUid ()); @@ -220,10 +220,10 @@ Tags::remove (T *tag) template bool -Tags::peek (T *tag) const +Tags::peek (T &tag) const { assert (sizeof (T) <= Tags::SIZE); - uint8_t *buf = reinterpret_cast (tag); + uint8_t *buf = reinterpret_cast (&tag); for (struct TagData *cur = m_next; cur != 0; cur = cur->m_next) { if (cur->m_id == TypeUid::getUid ()) { /* found tag */ diff --git a/src/core/system-file.h b/src/core/system-file.h index bc43af596..4cd395337 100644 --- a/src/core/system-file.h +++ b/src/core/system-file.h @@ -67,7 +67,7 @@ public: * 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; }; diff --git a/src/core/unix-system-file.cc b/src/core/unix-system-file.cc index 1ef8bd77a..03979c352 100644 --- a/src/core/unix-system-file.cc +++ b/src/core/unix-system-file.cc @@ -50,7 +50,7 @@ public: ~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 @@ SystemFilePrivate::open (char const *filename) #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 @@ SystemFile::open (char const *filename) 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); } diff --git a/src/core/win32-system-file.cc b/src/core/win32-system-file.cc index 3e7b716ea..1bb81614f 100644 --- a/src/core/win32-system-file.cc +++ b/src/core/win32-system-file.cc @@ -42,7 +42,7 @@ public: ~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 @@ SystemFilePrivate::open (char const *filename) } void -SystemFilePrivate::write (uint8_t *buffer, uint32_t size) +SystemFilePrivate::write (uint8_t const*buffer, uint32_t size) { } @@ -78,7 +78,7 @@ SystemFile::open (char const *filename) 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); }