aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/common/array.hpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-01-12 21:48:33 +0800
committerchai <chaifix@163.com>2019-01-12 21:48:33 +0800
commit8b00d67febf133e89f6a0bfabc41feed555dc4a9 (patch)
treefe48ef17c250afa40c2588300fcdb5920dba6951 /src/libjin/common/array.hpp
parenta907c39756ef6b368d06643afa491c49a9044a8e (diff)
*去掉文件前缀je_
Diffstat (limited to 'src/libjin/common/array.hpp')
-rw-r--r--src/libjin/common/array.hpp123
1 files changed, 123 insertions, 0 deletions
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<typename T>
+ 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