From 9f7993d21c9fb856ca6a615dad3e64e1ee983f9b Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 23 Apr 2012 23:07:35 +0000 Subject: [PATCH] core: add copy constructor and assignment operator to Array. --- src/array.h | 68 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/src/array.h b/src/array.h index 57a7fa90..e75a0c07 100644 --- a/src/array.h +++ b/src/array.h @@ -30,6 +30,24 @@ public: inline Array() : m_data(0), m_count(0), m_reserved(0) {} inline ~Array() { delete[] m_data; } + Array(Array const& that) : m_data(0), m_count(0), m_reserved(0) + { + Reserve(that.m_reserved); + memcpy(m_data, that.m_data, m_count * sizeof(Element)); + m_count = that.m_count; + } + + Array& operator=(Array const& that) + { + m_data = 0; + m_count = 0; + m_reserved = 0; + Reserve(that.m_reserved); + memcpy(m_data, that.m_data, that.m_count * sizeof(Element)); + m_count = that.m_count; + return *this; + } + inline Element& operator[](int n) { return m_data[n]; @@ -117,6 +135,24 @@ public: inline Array() : m_data(0), m_count(0), m_reserved(0) {} inline ~Array() { delete[] m_data; } + Array(Array const& that) : m_data(0), m_count(0), m_reserved(0) + { + Reserve(that.m_reserved); + memcpy(m_data, that.m_data, m_count * sizeof(Element)); + m_count = that.m_count; + } + + Array& operator=(Array const& that) + { + m_data = 0; + m_count = 0; + m_reserved = 0; + Reserve(that.m_reserved); + memcpy(m_data, that.m_data, that.m_count * sizeof(Element)); + m_count = that.m_count; + return *this; + } + inline Element& operator[](int n) { return m_data[n]; @@ -197,15 +233,35 @@ private: template class Array { public: + typedef T1 Element; + inline Array() : m_data(0), m_count(0), m_reserved(0) {} inline ~Array() { delete[] m_data; } - inline T1& operator[](int n) + Array(Array const& that) : m_data(0), m_count(0), m_reserved(0) + { + Reserve(that.m_reserved); + memcpy(m_data, that.m_data, m_count * sizeof(Element)); + m_count = that.m_count; + } + + Array& operator=(Array const& that) + { + m_data = 0; + m_count = 0; + m_reserved = 0; + Reserve(that.m_reserved); + memcpy(m_data, that.m_data, that.m_count * sizeof(Element)); + m_count = that.m_count; + return *this; + } + + inline Element& operator[](int n) { return m_data[n]; } - inline T1 const& operator[](int n) const + inline Element const& operator[](int n) const { return m_data[n]; } @@ -247,10 +303,10 @@ public: if (count <= (int)m_reserved) return; - T1 *tmp = new T1[count]; + Element *tmp = new Element[count]; if (m_data) { - memcpy(tmp, m_data, m_count * sizeof(T1)); + memcpy(tmp, m_data, m_count * sizeof(Element)); delete[] m_data; } m_data = tmp; @@ -258,10 +314,10 @@ public: } inline int Count() const { return m_count; } - inline int Bytes() const { return m_count * sizeof(T1); } + inline int Bytes() const { return m_count * sizeof(Element); } private: - T1 *m_data; + Element *m_data; int m_count, m_reserved; };