diff options
author | faketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2011-11-08 02:25:01 +0100 |
---|---|---|
committer | faketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2011-11-08 02:25:01 +0100 |
commit | 8285a11a26d78784f26b76e6bcdfa479f6c1a345 (patch) | |
tree | 6d567d22754b2d65258b1557e18ea8b590c2c709 /squirrel_3_0_1_stable/squirrel/squtils.h | |
parent | Fixed bug in cChunk.cpp not calculating RedstoneCircuits at the correct positions. Also, forgot to mention you can now place colored wool. (diff) | |
download | cuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.tar cuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.tar.gz cuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.tar.bz2 cuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.tar.lz cuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.tar.xz cuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.tar.zst cuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.zip |
Diffstat (limited to 'squirrel_3_0_1_stable/squirrel/squtils.h')
-rw-r--r-- | squirrel_3_0_1_stable/squirrel/squtils.h | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/squirrel_3_0_1_stable/squirrel/squtils.h b/squirrel_3_0_1_stable/squirrel/squtils.h new file mode 100644 index 000000000..bff7d24bb --- /dev/null +++ b/squirrel_3_0_1_stable/squirrel/squtils.h @@ -0,0 +1,112 @@ +/* see copyright notice in squirrel.h */
+#ifndef _SQUTILS_H_
+#define _SQUTILS_H_
+
+#define sq_new(__ptr,__type) {__ptr=(__type *)sq_vm_malloc(sizeof(__type));new (__ptr) __type;}
+#define sq_delete(__ptr,__type) {__ptr->~__type();sq_vm_free(__ptr,sizeof(__type));}
+#define SQ_MALLOC(__size) sq_vm_malloc((__size));
+#define SQ_FREE(__ptr,__size) sq_vm_free((__ptr),(__size));
+#define SQ_REALLOC(__ptr,__oldsize,__size) sq_vm_realloc((__ptr),(__oldsize),(__size));
+
+#define sq_aligning(v) (((size_t)(v) + (SQ_ALIGNMENT-1)) & (~(SQ_ALIGNMENT-1)))
+
+//sqvector mini vector class, supports objects by value
+template<typename T> class sqvector
+{
+public:
+ sqvector()
+ {
+ _vals = NULL;
+ _size = 0;
+ _allocated = 0;
+ }
+ sqvector(const sqvector<T>& v)
+ {
+ copy(v);
+ }
+ void copy(const sqvector<T>& v)
+ {
+ if(_size) {
+ resize(0); //destroys all previous stuff
+ }
+ //resize(v._size);
+ if(v._size > _allocated) {
+ _realloc(v._size);
+ }
+ for(SQUnsignedInteger i = 0; i < v._size; i++) {
+ new ((void *)&_vals[i]) T(v._vals[i]);
+ }
+ _size = v._size;
+ }
+ ~sqvector()
+ {
+ if(_allocated) {
+ for(SQUnsignedInteger i = 0; i < _size; i++)
+ _vals[i].~T();
+ SQ_FREE(_vals, (_allocated * sizeof(T)));
+ }
+ }
+ void reserve(SQUnsignedInteger newsize) { _realloc(newsize); }
+ void resize(SQUnsignedInteger newsize, const T& fill = T())
+ {
+ if(newsize > _allocated)
+ _realloc(newsize);
+ if(newsize > _size) {
+ while(_size < newsize) {
+ new ((void *)&_vals[_size]) T(fill);
+ _size++;
+ }
+ }
+ else{
+ for(SQUnsignedInteger i = newsize; i < _size; i++) {
+ _vals[i].~T();
+ }
+ _size = newsize;
+ }
+ }
+ void shrinktofit() { if(_size > 4) { _realloc(_size); } }
+ T& top() const { return _vals[_size - 1]; }
+ inline SQUnsignedInteger size() const { return _size; }
+ bool empty() const { return (_size <= 0); }
+ inline T &push_back(const T& val = T())
+ {
+ if(_allocated <= _size)
+ _realloc(_size * 2);
+ return *(new ((void *)&_vals[_size++]) T(val));
+ }
+ inline void pop_back()
+ {
+ _size--; _vals[_size].~T();
+ }
+ void insert(SQUnsignedInteger idx, const T& val)
+ {
+ resize(_size + 1);
+ for(SQUnsignedInteger i = _size - 1; i > idx; i--) {
+ _vals[i] = _vals[i - 1];
+ }
+ _vals[idx] = val;
+ }
+ void remove(SQUnsignedInteger idx)
+ {
+ _vals[idx].~T();
+ if(idx < (_size - 1)) {
+ memcpy(&_vals[idx], &_vals[idx+1], sizeof(T) * (_size - idx - 1));
+ }
+ _size--;
+ }
+ SQUnsignedInteger capacity() { return _allocated; }
+ inline T &back() const { return _vals[_size - 1]; }
+ inline T& operator[](SQUnsignedInteger pos) const{ return _vals[pos]; }
+ T* _vals;
+private:
+ void _realloc(SQUnsignedInteger newsize)
+ {
+ newsize = (newsize > 0)?newsize:4;
+ _vals = (T*)SQ_REALLOC(_vals, _allocated * sizeof(T), newsize * sizeof(T));
+ _allocated = newsize;
+ }
+ SQUnsignedInteger _size;
+ SQUnsignedInteger _allocated;
+};
+
+#endif //_SQUTILS_H_
|