diff options
author | Kahrl <kahrl@gmx.net> | 2011-11-07 02:24:44 +0100 |
---|---|---|
committer | Perttu Ahola <celeron55@gmail.com> | 2011-11-07 11:19:56 +0200 |
commit | 28660b4c1af1b1b6ac2d3fda6984bda2a1199dc1 (patch) | |
tree | 2d3a8fca8343d5f73ab1b21ded7ad7197a1654a8 /src/utility.h | |
parent | fa72e65b59364d204db8bce59989673034e59988 (diff) | |
download | minetest-28660b4c1af1b1b6ac2d3fda6984bda2a1199dc1.tar.gz minetest-28660b4c1af1b1b6ac2d3fda6984bda2a1199dc1.tar.bz2 minetest-28660b4c1af1b1b6ac2d3fda6984bda2a1199dc1.zip |
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
Diffstat (limited to 'src/utility.h')
-rw-r--r-- | src/utility.h | 54 |
1 files changed, 48 insertions, 6 deletions
diff --git a/src/utility.h b/src/utility.h index f89574468..98fa83e89 100644 --- a/src/utility.h +++ b/src/utility.h @@ -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() { |