edits
[iotcloud.git] / version2 / src / C / array.h
1 #ifndef ARRAY_H
2 #define ARRAY_H
3 #include <inttypes.h>
4
5 typedef uint32_t uint;
6
7 template<typename type>
8 class Array {
9  public:
10  Array() :
11   array(NULL),
12     size(0) {
13   }
14   
15  Array(uint32_t _size) :
16   array((type *) ourcalloc(1, sizeof(type) * _size)),
17                 size(_size)
18     {
19     }
20   
21  Array(type *_array, uint _size) :
22   array((type *) ourmalloc(sizeof(type) * _size)),
23                 size(_size) {
24       memcpy(array, _array, _size * sizeof(type));
25     }
26
27  Array(Array<type> *_array) :
28         array((type *) ourmalloc(sizeof(type) * _array->size)),
29                 size(_array->size) {
30                 memcpy(array, _array->array, size * sizeof(type));
31         }
32         
33         void init(uint _size) {
34                 array = (type *) ourcalloc(1, sizeof(type) * _size);
35                 size = _size;
36         }
37
38         void init(type *_array, uint _size) {
39                 array = (type *) ourmalloc(sizeof(type) * _size);
40                 size = _size;
41                 memcpy(array, _array, _size * sizeof(type));
42         }
43         
44         void init(Array<type> *_array) {
45                 array = (type *) ourmalloc(sizeof(type) * _array->size);
46                 size = _array->size;
47                 memcpy(array, _array->array, size * sizeof(type));
48         }
49         
50         ~Array() {
51                 if (array)
52                         ourfree(array);
53         }
54         
55         type get(uint index) const {
56                 return array[index];
57         }
58         
59         void set(uint index, type item) {
60                 array[index] = item;
61         }
62         
63         uint length() const {
64                 return size;
65         }
66
67         type *internalArray() {
68                 return array;
69         }
70         
71  private:
72         type *array;
73         uint size;
74 };
75 #endif