utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
authorKahrl <kahrl@gmx.net>
Mon, 7 Nov 2011 01:24:44 +0000 (02:24 +0100)
committerPerttu Ahola <celeron55@gmail.com>
Mon, 7 Nov 2011 09:19:56 +0000 (11:19 +0200)
src/connection.h
src/utility.h

index 570bc92ab22ba817873c0e549af2a59882b46940..dc61394face1a4eb53b3571babfb14ef8eff66f2 100644 (file)
@@ -430,7 +430,7 @@ struct ConnectionEvent
 {
        enum ConnectionEventType type;
        u16 peer_id;
-       SharedBuffer<u8> data;
+       Buffer<u8> data;
        bool timeout;
        Address address;
 
@@ -489,7 +489,7 @@ struct ConnectionCommand
        Address address;
        u16 peer_id;
        u8 channelnum;
-       SharedBuffer<u8> data;
+       Buffer<u8> data;
        bool reliable;
        
        ConnectionCommand(): type(CONNCMD_NONE) {}
index f89574468f2d7738cab6696df3add9d8ae1e6698..98fa83e8928e5a116d265b89666dfa3432fd5ec5 100644 (file)
@@ -343,26 +343,59 @@ template <typename T>
 class Buffer
 {
 public:
+       Buffer()
+       {
+               m_size = 0;
+               data = NULL;
+       }
        Buffer(unsigned int size)
        {
                m_size = size;
-               data = new T[size];
+               if(size != 0)
+                       data = new T[size];
+               else
+                       data = NULL;
        }
        Buffer(const Buffer &buffer)
        {
                m_size = buffer.m_size;
-               data = new T[buffer.m_size];
-               memcpy(data, buffer.data, buffer.m_size);
+               if(m_size != 0)
+               {
+                       data = new T[buffer.m_size];
+                       memcpy(data, buffer.data, buffer.m_size);
+               }
+               else
+                       data = NULL;
        }
        Buffer(T *t, unsigned int size)
        {
                m_size = size;
-               data = new T[size];
-               memcpy(data, t, size);
+               if(size != 0)
+               {
+                       data = new T[size];
+                       memcpy(data, t, size);
+               }
+               else
+                       data = NULL;
        }
        ~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
        {
@@ -377,6 +410,11 @@ public:
                return m_size;
        }
 private:
+       void drop()
+       {
+               if(data)
+                       delete[] data;
+       }
        T *data;
        unsigned int m_size;
 };
@@ -471,6 +509,10 @@ public:
        {
                return m_size;
        }
+       operator Buffer<T>() const
+       {
+               return Buffer<T>(data, m_size);
+       }
 private:
        void drop()
        {