diff options
Diffstat (limited to 'gui/util.h')
-rw-r--r-- | gui/util.h | 93 |
1 files changed, 69 insertions, 24 deletions
diff --git a/gui/util.h b/gui/util.h index 0d2a9560d7..c1bd9bbc77 100644 --- a/gui/util.h +++ b/gui/util.h @@ -29,6 +29,69 @@ int Blend(int src, int dst, byte *palette); void ClearBlendCache(byte *palette, int weight); +template <class T> +class List { +protected: + int _capacity; + int _size; + T *_data; + +public: + List<T>() : _capacity(0), _size(0), _data(0) {} + List<T>(const List<T>& list) : _capacity(0), _size(0), _data(0) + { + error("EEEEK! List copy constructor called"); + } + + ~List<T>() + { + if (_data) + delete [] _data; + } + + void push_back(const T& str) + { + ensureCapacity(_size + 1); + _data[_size++] = str; + } + + // TODO: insert, remove, ... + + T& operator [](int idx) + { + assert(idx >= 0 && idx < _size); + return _data[idx]; + } + + const T& operator [](int idx) const + { + assert(idx >= 0 && idx < _size); + return _data[idx]; + } + + int size() const { return _size; } + + void clear() { _size = 0; } + +protected: + void ensureCapacity(int new_len) + { + if (new_len <= _capacity) + return; + + T *old_data = _data; + _capacity = new_len + 32; + _data = new T[_capacity]; + + if (old_data) { + // Copy old data + for (int i = 0; i < _size; i++) + _data[i] = old_data[i]; + delete [] old_data; + } + } +}; + class String { protected: int _capacity; @@ -57,31 +120,13 @@ protected: void ensureCapacity(int new_len, bool keep_old); }; -class StringList { -protected: - int _capacity; - int _size; - String **_data; +class StringList : public List<String> { public: - StringList() : _capacity(0), _size(0), _data(0) {} - StringList(const StringList& list); - ~StringList(); - - void push_back(const char* str); - void push_back(const String& str); - - // TODO: insert, remove, ... - - String& operator [](int idx); - const String& operator [](int idx) const; - - int size() const { return _size; } - - void clear(); - -protected: - void ensureCapacity(int new_len); + void push_back(const char* str) + { + ensureCapacity(_size + 1); + _data[_size++] = str; + } }; - #endif |