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