replace pointers with references

This commit is contained in:
Mathieu Lacage
2006-09-08 18:45:48 +02:00
parent 40dac3634e
commit 1a510b4a26
11 changed files with 73 additions and 73 deletions

View File

@@ -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;
}

View File

@@ -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]) {

View File

@@ -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<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);

View File

@@ -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

View File

@@ -86,7 +86,6 @@ namespace ns3 {
*/
class Packet {
public:
typedef Callback<void,uint8_t *,uint32_t> 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 <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 @@ public:
* 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 @@ public:
* 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 @@ 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 <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);
}

View File

@@ -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 ());
}
}

View File

@@ -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;
}

View File

@@ -38,13 +38,13 @@ public:
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 @@ void (*TagPrettyPrinter<T>::gPrettyPrinter) (T *, std::ostream &) = 0;
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 @@ Tags::add (T const*tag)
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 @@ Tags::remove (T *tag)
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 */

View File

@@ -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;
};

View File

@@ -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);
}

View File

@@ -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);
}