ScummVM API documentation
squtils.h
1 /* see copyright notice in squirrel.h */
2 #ifndef _SQUTILS_H_
3 #define _SQUTILS_H_
4 
5 void *sq_vm_malloc(SQUnsignedInteger size);
6 void *sq_vm_realloc(void *p,SQUnsignedInteger oldsize,SQUnsignedInteger size);
7 void sq_vm_free(void *p,SQUnsignedInteger size);
8 
9 #define sq_new(__ptr,__type) {__ptr=(__type *)sq_vm_malloc(sizeof(__type));new (__ptr) __type;}
10 #define sq_delete(__ptr,__type) {__ptr->~__type();sq_vm_free(__ptr,sizeof(__type));}
11 #define SQ_MALLOC(__size) sq_vm_malloc((__size));
12 #define SQ_FREE(__ptr,__size) sq_vm_free((__ptr),(__size));
13 #define SQ_REALLOC(__ptr,__oldsize,__size) sq_vm_realloc((__ptr),(__oldsize),(__size));
14 
15 #define sq_aligning(v) (((size_t)(v) + (SQ_ALIGNMENT-1)) & (~(SQ_ALIGNMENT-1)))
16 
17 //sqvector mini vector class, supports objects by value
18 template<typename T> class sqvector
19 {
20 public:
21  sqvector()
22  {
23  _vals = NULL;
24  _size = 0;
25  _allocated = 0;
26  }
27  sqvector(const sqvector<T>& v)
28  {
29  _vals = NULL;
30  _size = 0;
31  _allocated = 0;
32  copy(v);
33  }
34  inline sqvector<T>& operator=(const sqvector<T>& v)
35  {
36  copy(v);
37  return *this;
38  }
39  void copy(const sqvector<T>& v)
40  {
41  if(_size) {
42  resize(0); //destroys all previous stuff
43  }
44  //resize(v._size);
45  if(v._size > _allocated) {
46  _realloc(v._size);
47  }
48  for(SQUnsignedInteger i = 0; i < v._size; i++) {
49  new ((void *)&_vals[i]) T(v._vals[i]);
50  }
51  _size = v._size;
52  }
53  ~sqvector()
54  {
55  if(_allocated) {
56  for(SQUnsignedInteger i = 0; i < _size; i++)
57  _vals[i].~T();
58  SQ_FREE(_vals, (_allocated * sizeof(T)));
59  }
60  }
61  void reserve(SQUnsignedInteger newsize) { _realloc(newsize); }
62  void resize(SQUnsignedInteger newsize, const T& fill = T())
63  {
64  if(newsize > _allocated)
65  _realloc(newsize);
66  if(newsize > _size) {
67  while(_size < newsize) {
68  new ((void *)&_vals[_size]) T(fill);
69  _size++;
70  }
71  }
72  else{
73  for(SQUnsignedInteger i = newsize; i < _size; i++) {
74  _vals[i].~T();
75  }
76  _size = newsize;
77  }
78  }
79  void shrinktofit() { if(_size > 4) { _realloc(_size); } }
80  T& top() const { return _vals[_size - 1]; }
81  inline SQUnsignedInteger size() const { return _size; }
82  bool empty() const { return (_size <= 0); }
83  inline T &push_back(const T& val = T())
84  {
85  if(_allocated <= _size)
86  _realloc(_size * 2);
87  return *(new ((void *)&_vals[_size++]) T(val));
88  }
89  inline void pop_back()
90  {
91  _size--; _vals[_size].~T();
92  }
93  void insert(SQUnsignedInteger idx, const T& val)
94  {
95  resize(_size + 1);
96  for(SQUnsignedInteger i = _size - 1; i > idx; i--) {
97  _vals[i] = _vals[i - 1];
98  }
99  _vals[idx] = val;
100  }
101  void remove(SQUnsignedInteger idx)
102  {
103  _vals[idx].~T();
104  if(idx < (_size - 1)) {
105  memmove(&_vals[idx], &_vals[idx+1], sizeof(T) * (_size - idx - 1));
106  }
107  _size--;
108  }
109  SQUnsignedInteger capacity() { return _allocated; }
110  inline T &back() const { return _vals[_size - 1]; }
111  inline T& operator[](SQUnsignedInteger pos) const{ return _vals[pos]; }
112  T* _vals;
113 private:
114  void _realloc(SQUnsignedInteger newsize)
115  {
116  newsize = (newsize > 0)?newsize:4;
117  _vals = (T*)SQ_REALLOC(_vals, _allocated * sizeof(T), newsize * sizeof(T));
118  _allocated = newsize;
119  }
120  SQUnsignedInteger _size;
121  SQUnsignedInteger _allocated;
122 };
123 
124 #endif //_SQUTILS_H_
Definition: squtils.h:18
signed char * fill(signed char *first, signed char *last, Value val)
Definition: algorithm.h:168