From 8b00d67febf133e89f6a0bfabc41feed555dc4a9 Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 12 Jan 2019 21:48:33 +0800 Subject: =?UTF-8?q?*=E5=8E=BB=E6=8E=89=E6=96=87=E4=BB=B6=E5=89=8D=E7=BC=80?= =?UTF-8?q?je=5F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/common/array.hpp | 123 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 src/libjin/common/array.hpp (limited to 'src/libjin/common/array.hpp') diff --git a/src/libjin/common/array.hpp b/src/libjin/common/array.hpp new file mode 100644 index 0000000..8a5cbf1 --- /dev/null +++ b/src/libjin/common/array.hpp @@ -0,0 +1,123 @@ +#ifndef __JE_COMMON_ARRAY_H__ +#define __JE_COMMON_ARRAY_H__ + +namespace JinEngine +{ + + /// + /// A array created on heap. + /// + template + class Array + { + public: + /// + /// Array constructor. + /// + Array() + : length(0) + , data(nullptr) + { + } + + /// + /// Array constructor. + /// + /// @param l Length of array. + /// + Array(int l) + { + length = l; + data = new T[l]; + } + + /// + /// Array destructor. + /// + ~Array() + { + delete[] data; + length = 0; + } + + /// + /// Get address of data. + /// + /// @return Address of data. + /// + T* operator &() + { + return data; + } + + /// + /// Get specific element of array. + /// + /// @return Element of array. + /// + T& operator[](int index) + { + return data[index]; + } + + /// + /// Bind data with given data. + /// + /// @param data Data pointer. + /// @param length Length of data. + /// + void bind(T* data, int length) + { + if (data != nullptr) + delete data; + this->data = data; + this->length = length; + } + + /// + /// Add an element. + /// + /// @param v Value of element. + /// + void add(T value) + { + int len = length + 1; + T* d = new T[len]; + memcpy(d, data, size()); + d[length] = value; + bind(d, len); + } + + /// + /// Get size of data in byte. + /// + /// @return Size of data in byte. + /// + int size() + { + return sizeof(T) * length; + } + + /// + /// Get length of data. + /// + /// @return Count of data. + /// + int count() + { + return length; + } + + private: + // Disable new and delete. + void* operator new(size_t t); + void operator delete(void* ptr); + + T * data; + unsigned int length; + + }; + +} // namespace JinEngine + +#endif \ No newline at end of file -- cgit v1.1-26-g67d0