Fixing initialization to just create a table on the cloud side.
[iotcloud.git] / version2 / src / C / IoTString.h
1 #ifndef IOTSTRING_H
2 #define IOTSTRING_H
3
4 #include "array.h"
5 #include <string.h>
6 #include <stdio.h>
7 /**
8  * IoTString wraps the underlying char string.
9  * @author Brian Demsky <bdemsky@uci.edu>
10  * @version 1.0
11  */
12
13 inline unsigned int hashCharArray(Array<char> *array) {
14         uint len = array->length();
15         unsigned int hash = 0;
16         for (uint i = 0; i < len; i++) {
17                 hash = 31 * hash + ((unsigned int) array->get(i));
18         }
19         return hash;
20 }
21
22 class IoTString {
23 private:
24         Array<char> *array;
25         unsigned int hashvalue;
26         unsigned int refCount;
27   IoTString() : refCount (1) {}
28
29         /**
30          * Builds an IoTString object around the char array.  This
31          * constructor makes a copy, so the caller is free to modify the char array.
32          */
33
34 public:
35         IoTString(Array<char> *_array) :
36                 array(new Array<char>(_array)),
37                         hashvalue(hashCharArray(array)),
38                         refCount(1) {
39         }
40
41         IoTString(const char *_array) {
42                 int32_t len = strlen(_array);
43                 array = new Array<char>(len);
44                 memcpy(array->internalArray(), _array, len);
45                 hashvalue = hashCharArray(array);
46                 refCount = 1;
47         }
48
49         IoTString(IoTString *string) :
50           array(new Array<char>(string->array)),
51                         hashvalue(string->hashvalue),
52                         refCount(1) {
53         }
54
55         IoTString * acquireRef() {
56                 refCount++;
57                 return this;
58         }
59
60         void releaseRef() {
61                 if ((--refCount) == 0)
62                         delete this;
63         }
64         
65         ~IoTString() {
66                 delete array;
67         }
68
69         void print() {
70                 for(uint i=0; i < array->length(); i++)
71                         printf("%c", array->get(i));
72         };
73         
74         char get(uint i) {return array->get(i);}
75
76         /**
77          * Internal method to grab a reference to our char array.  Caller
78          * must not modify it.
79          */
80
81         Array<char> *internalBytes() { return array; }
82
83         /**
84          * Returns a copy of the underlying char string.
85          */
86
87         Array<char> *getBytes() { return new Array<char>(array); }
88
89         /**
90          * Returns the length in chars of the IoTString.
91          */
92
93         
94         bool equals(IoTString *str) {
95                 uint strlength = str->array->length();
96                 uint thislength = array->length();
97                 if (strlength != thislength)
98                         return false;
99
100                 int result = memcmp(str->array->internalArray(), array->internalArray(), strlength);
101                 return result == 0;
102         }
103
104         unsigned int hashValue() { return hashvalue;}
105         int length() { return array->length(); }
106         friend IoTString *IoTString_shallow(Array<char> *_array);
107 };
108
109 inline IoTString *IoTString_shallow(Array<char> *_array) {
110         IoTString *str = new IoTString();
111         str->array = _array;
112         str->hashvalue = hashCharArray(_array);
113         return str;
114 }
115
116 inline unsigned int hashString(IoTString *a) {
117         return a->hashValue();
118 }
119
120 inline bool StringEquals(IoTString *a, IoTString *b) {
121         return a->equals(b);
122 }
123 #endif