|
CppGC
1.01
|
00001 #ifndef __GCCLASSES_H__ 00002 #define __GCCLASSES_H__ 00003 00004 #include <string.h> 00005 00006 #include "gc.h" 00007 00008 namespace GC 00009 { 00013 template<class T> 00014 class ScalarArray : public Object 00015 { 00016 protected: 00017 size_t length; 00018 T body[1]; 00019 00020 public: 00021 static ScalarArray* create(size_t len) { 00022 return new ((len-1)*sizeof(T)) ScalarArray(len); 00023 } 00024 00025 T& operator[](size_t index) { 00026 assert(index < length); 00027 return body[index]; 00028 } 00029 00030 T operator[](size_t index) const{ 00031 assert(index < length); 00032 return body[index]; 00033 } 00034 00035 size_t size() const { 00036 return length; 00037 } 00038 00039 operator T*() { 00040 return body; 00041 } 00042 00043 operator T const*() const { 00044 return body; 00045 } 00046 00047 protected: 00048 ScalarArray(size_t len) : length(len) 00049 { 00050 memset(body, 0, len*sizeof(T)); 00051 } 00052 }; 00053 00057 template<class T> 00058 class ObjectArray : public Object 00059 { 00060 size_t length; 00061 T* body[1]; 00062 00063 public: 00064 static ObjectArray* create(size_t len) { 00065 return new ((len-1)*sizeof(T*)) ObjectArray(len); 00066 } 00067 00068 T*& operator[](size_t index) { 00069 assert(index < length); 00070 return body[index]; 00071 } 00072 00073 T* operator[](size_t index) const{ 00074 assert(index < length); 00075 return body[index]; 00076 } 00077 00078 size_t size() const { 00079 return length; 00080 } 00081 00082 operator T**() { 00083 return body; 00084 } 00085 00086 operator T* const*() const { 00087 return body; 00088 } 00089 00090 protected: 00091 ObjectArray(size_t len) : length(len) 00092 { 00093 memset(body, 0, len*sizeof(T*)); 00094 } 00095 virtual void mark(MemoryAllocator* allocator) { 00096 allocator->_mark((Object**)body, length); 00097 } 00098 }; 00099 00100 00104 template<class T> 00105 class ScalarVector : public Object 00106 { 00107 GC::Ref< ScalarArray<T> > body; 00108 size_t length; 00109 00110 virtual void mark(MemoryAllocator*) { GC_MARK(ScalarVector); } 00111 00112 public: 00113 ScalarVector(size_t reserve = 8) { 00114 body = ScalarArray<T>::create(reserve); 00115 length = 0; 00116 } 00117 00118 void resize(size_t newSize) { 00119 size_t i; 00120 size_t allocated = body->size(); 00121 if (newSize > allocated) { 00122 allocated = allocated*2 > newSize ? allocated*2 : newSize; 00123 ScalarArray<T>* newBody = ScalarArray<T>::create(allocated); 00124 for (i = 0; i < length; i++) { 00125 newBody[i] = body[i]; 00126 } 00127 body = newBody; 00128 } 00129 length = newSize; 00130 } 00131 00132 void push(T val) { 00133 resize(length+1); 00134 body[length-1] = val; 00135 } 00136 00137 T pop() { 00138 assert(length != 0); 00139 return body[--length]; 00140 } 00141 00142 T top() const { 00143 assert(length != 0); 00144 return body[length-1]; 00145 } 00146 00147 T& operator[](size_t index) { 00148 assert(index < length); 00149 return body[index]; 00150 } 00151 00152 T operator[](size_t index) const{ 00153 assert(index < length); 00154 return body[index]; 00155 } 00156 00157 size_t size() const { 00158 return length; 00159 } 00160 00161 operator T*() { 00162 return body; 00163 } 00164 00165 operator T const*() const { 00166 return body; 00167 } 00168 }; 00169 00173 template<class T> 00174 class ObjectVector : public Object 00175 { 00176 GC::Ref< ObjectArray<T> > body; 00177 size_t length; 00178 00179 virtual void mark(MemoryAllocator*) { GC_MARK(ObjectVector); } 00180 00181 public: 00182 ObjectVector(size_t reserve = 8) { 00183 body = ObjectArray<T>::create(reserve); 00184 length = 0; 00185 } 00186 00187 void resize(size_t newSize) { 00188 size_t i; 00189 size_t allocated = body->size(); 00190 if (newSize > allocated) { 00191 allocated = allocated*2 > newSize ? allocated*2 : newSize; 00192 ObjectArray<T>* newBody = ObjectArray<T>::create(allocated); 00193 for (i = 0; i < length; i++) { 00194 newBody[i] = body[i]; 00195 } 00196 body = newBody; 00197 } 00198 length = newSize; 00199 } 00200 00201 void push(T const* obj) { 00202 resize(length+1); 00203 (*body)[length-1] = (T*)obj; 00204 } 00205 00206 T* pop() { 00207 assert(length != 0); 00208 return (*body)[--length]; 00209 } 00210 00211 T* top() const { 00212 assert(length != 0); 00213 return (*body)[length-1]; 00214 } 00215 00216 T*& operator[](size_t index) { 00217 assert(index < length); 00218 return (*body)[index]; 00219 } 00220 00221 T* operator[](size_t index) const{ 00222 assert(index < length); 00223 return (*body)[index]; 00224 } 00225 00226 size_t size() const { 00227 return length; 00228 } 00229 00230 operator T**() { 00231 return body; 00232 } 00233 00234 operator T* const*() const { 00235 return body; 00236 } 00237 }; 00238 00239 00243 class String : public Object 00244 { 00245 size_t length; 00246 char body[1]; 00247 public: 00248 static String* create(size_t len) { 00249 return new (len) String(len); 00250 } 00251 static String* create(char const* str) { 00252 return create(str, strlen(str)); 00253 } 00254 00255 static String* create(char const* str, size_t len) { 00256 return new (len) String(str, len); 00257 } 00258 00259 int compare(char const* other) { 00260 return strcmp(body, other); 00261 } 00262 00263 bool equals(char const* other) { 00264 return compare(other) == 0; 00265 } 00266 00267 int compare(GC::Ref<String> const& other) { 00268 return compare(other->body); 00269 } 00270 00271 bool equals(GC::Ref<String> const& other) { 00272 return compare(other) == 0; 00273 } 00274 00275 size_t size() const { 00276 return length; 00277 } 00278 00279 operator char*() { 00280 return body; 00281 } 00282 00283 operator char const*() const { 00284 return body; 00285 } 00286 00287 protected: 00288 String(char const* str, size_t len) { 00289 memcpy(body, str, len); 00290 length = len; 00291 body[len] = '\0'; 00292 } 00293 String(size_t len) { 00294 length = len; 00295 body[len] = '\0'; 00296 } 00297 }; 00298 00303 template<class T> 00304 class Wrapper : public Object, public T 00305 { 00306 }; 00307 00308 }; 00309 00310 00311 #endif
1.7.6.1