1
0
mirror of https://github.com/moparisthebest/minetest synced 2024-11-15 22:05:07 -05:00

utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interface, connection.h: use Buffer instead of SharedBuffer in command and event queues

This commit is contained in:
Kahrl 2011-11-07 02:24:44 +01:00 committed by Perttu Ahola
parent fa72e65b59
commit 28660b4c1a
2 changed files with 50 additions and 8 deletions

View File

@ -430,7 +430,7 @@ struct ConnectionEvent
{ {
enum ConnectionEventType type; enum ConnectionEventType type;
u16 peer_id; u16 peer_id;
SharedBuffer<u8> data; Buffer<u8> data;
bool timeout; bool timeout;
Address address; Address address;
@ -489,7 +489,7 @@ struct ConnectionCommand
Address address; Address address;
u16 peer_id; u16 peer_id;
u8 channelnum; u8 channelnum;
SharedBuffer<u8> data; Buffer<u8> data;
bool reliable; bool reliable;
ConnectionCommand(): type(CONNCMD_NONE) {} ConnectionCommand(): type(CONNCMD_NONE) {}

View File

@ -343,26 +343,59 @@ template <typename T>
class Buffer class Buffer
{ {
public: public:
Buffer()
{
m_size = 0;
data = NULL;
}
Buffer(unsigned int size) Buffer(unsigned int size)
{ {
m_size = size; m_size = size;
data = new T[size]; if(size != 0)
data = new T[size];
else
data = NULL;
} }
Buffer(const Buffer &buffer) Buffer(const Buffer &buffer)
{ {
m_size = buffer.m_size; m_size = buffer.m_size;
data = new T[buffer.m_size]; if(m_size != 0)
memcpy(data, buffer.data, buffer.m_size); {
data = new T[buffer.m_size];
memcpy(data, buffer.data, buffer.m_size);
}
else
data = NULL;
} }
Buffer(T *t, unsigned int size) Buffer(T *t, unsigned int size)
{ {
m_size = size; m_size = size;
data = new T[size]; if(size != 0)
memcpy(data, t, size); {
data = new T[size];
memcpy(data, t, size);
}
else
data = NULL;
} }
~Buffer() ~Buffer()
{ {
delete[] data; drop();
}
Buffer& operator=(const Buffer &buffer)
{
if(this == &buffer)
return *this;
drop();
m_size = buffer.m_size;
if(m_size != 0)
{
data = new T[buffer.m_size];
memcpy(data, buffer.data, buffer.m_size);
}
else
data = NULL;
return *this;
} }
T & operator[](unsigned int i) const T & operator[](unsigned int i) const
{ {
@ -377,6 +410,11 @@ public:
return m_size; return m_size;
} }
private: private:
void drop()
{
if(data)
delete[] data;
}
T *data; T *data;
unsigned int m_size; unsigned int m_size;
}; };
@ -471,6 +509,10 @@ public:
{ {
return m_size; return m_size;
} }
operator Buffer<T>() const
{
return Buffer<T>(data, m_size);
}
private: private:
void drop() void drop()
{ {