[Scummvm-cvs-logs] CVS: scummvm/gui util.h,1.2,1.3 util.cpp,1.3,1.4
Max Horn
fingolfin at users.sourceforge.net
Mon Jul 15 13:12:12 CEST 2002
Update of /cvsroot/scummvm/scummvm/gui
In directory usw-pr-cvs1:/tmp/cvs-serv20792/gui
Modified Files:
util.h util.cpp
Log Message:
replaced EventList and StringList by a universtal List<T> template; remade EventList and StringList using that generic type
Index: util.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/util.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- util.h 13 Jul 2002 11:59:39 -0000 1.2
+++ util.h 15 Jul 2002 20:11:38 -0000 1.3
@@ -29,6 +29,69 @@
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 @@
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
Index: util.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/util.cpp,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- util.cpp 13 Jul 2002 13:12:06 -0000 1.3
+++ util.cpp 15 Jul 2002 20:11:39 -0000 1.4
@@ -110,7 +110,8 @@
ensureCapacity(len, false);
_len = len;
- memcpy(_str, str, _len + 1);
+ if (_str)
+ memcpy(_str, str, _len + 1);
return *this;
}
@@ -121,7 +122,8 @@
ensureCapacity(len, false);
_len = len;
- memcpy(_str, str._str, _len + 1);
+ if (_str)
+ memcpy(_str, str._str, _len + 1);
return *this;
}
@@ -131,7 +133,8 @@
int len = strlen(str);
ensureCapacity(_len + len, true);
- memcpy(_str + _len, str, len + 1);
+ if (_str)
+ memcpy(_str + _len, str, len + 1);
_len += len;
return *this;
@@ -142,7 +145,8 @@
int len = str._len;
ensureCapacity(_len + len, true);
- memcpy(_str + _len, str._str, len + 1);
+ if (_str && str._str)
+ memcpy(_str + _len, str._str, len + 1);
_len += len;
return *this;
@@ -161,9 +165,11 @@
void String::clear()
{
- _len = 0;
if (_str)
- _str[0] = 0;
+ free(_str);
+ _capacity = 0;
+ _len = 0;
+ _str = 0;
}
void String::ensureCapacity(int new_len, bool keep_old)
@@ -179,83 +185,5 @@
if (keep_old)
memcpy(_str, old_str, _len+1);
free(old_str);
- }
-}
-
-
-#pragma mark -
-
-
-StringList::StringList(const StringList& list)
- : _capacity(0), _size(0), _data(0)
-{
- printf("EEEEK! StringList copy constructor called!\n");
- assert(0);
-}
-
-StringList::~StringList()
-{
- if (_data) {
- for (int i = 0; i < _capacity; i++)
- if (_data[_size])
- delete _data[_size];
- free(_data);
- }
-}
-
-
-void StringList::push_back(const char* str)
-{
- ensureCapacity(_size + 1);
-
- if (!_data[_size])
- _data[_size] = new String(str);
- else
- *_data[_size] = str;
-
- _size++;
-}
-
-void StringList::push_back(const String& str)
-{
- ensureCapacity(_size + 1);
-
- if (!_data[_size])
- _data[_size] = new String(str);
- else
- *_data[_size] = str;
-
- _size++;
-}
-
-String& StringList::operator [](int idx)
-{
- assert(idx >= 0 && idx < _size);
- return *_data[idx];
-}
-
-const String& StringList::operator [](int idx) const
-{
- assert(idx >= 0 && idx < _size);
- return *_data[idx];
-}
-
-void StringList::clear()
-{
- _size = 0;
-}
-
-void StringList::ensureCapacity(int new_len)
-{
- if (new_len <= _capacity)
- return;
-
- String **old_data = _data;
- _capacity = new_len + 32;
- _data = (String **)calloc(sizeof(String*), _capacity);
-
- if (old_data) {
- memcpy(_data, old_data, _size * sizeof(String*));
- free(old_data);
}
}
More information about the Scummvm-git-logs
mailing list