Reworked once more the Dictionary utility class
This commit is contained in:
parent
18aeb123ba
commit
f63e7bf835
@ -12,6 +12,7 @@ class Dictionary
|
||||
{
|
||||
public:
|
||||
Dictionary() :_parameter(NULL), _value(NULL), _next(NULL), _head(this){}
|
||||
|
||||
~Dictionary()
|
||||
{
|
||||
if(_head == this)
|
||||
@ -27,11 +28,13 @@ public:
|
||||
Dictionary *dictionaryNode = new Dictionary(parameter, value);
|
||||
return addNewNodeAtTheEnd(dictionaryNode);
|
||||
}
|
||||
|
||||
boolean add(const char *parameter, T value)
|
||||
{
|
||||
Dictionary *dictionaryNode = new Dictionary(parameter, new T(value));
|
||||
return addNewNodeAtTheEnd(dictionaryNode);
|
||||
}
|
||||
|
||||
boolean remove(const char *parameter)
|
||||
{
|
||||
if(_head->_next == NULL) return false;
|
||||
@ -51,45 +54,51 @@ public:
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Dictionary get(const char *parameter)
|
||||
|
||||
T* get(const char *parameter)
|
||||
{
|
||||
if(isListEmpty(_head->_next))return Dictionary();
|
||||
if(parameter == NULL) return NULL;
|
||||
if(isListEmpty(_head->_next))return NULL;
|
||||
|
||||
Dictionary *cursor = _head->_next;
|
||||
|
||||
while(!isListEmpty(cursor))
|
||||
{
|
||||
if(strcmp(cursor->_parameter,parameter) == 0)
|
||||
return *cursor;
|
||||
return cursor->_value;
|
||||
cursor = cursor->_next;
|
||||
}
|
||||
|
||||
return Dictionary();
|
||||
return NULL;
|
||||
}
|
||||
Dictionary operator()(const char *parameter)
|
||||
|
||||
T* operator()(const char *parameter)
|
||||
{
|
||||
return get(parameter);
|
||||
}
|
||||
Dictionary get(const unsigned int index)
|
||||
|
||||
T* get(const unsigned int index)
|
||||
{
|
||||
unsigned int position(0);
|
||||
if(isListEmpty(_head->_next))return Dictionary();
|
||||
if(isListEmpty(_head->_next))return NULL;
|
||||
|
||||
Dictionary *cursor = _head->_next;
|
||||
|
||||
while(!isListEmpty(cursor))
|
||||
{
|
||||
if(position++ == index)
|
||||
return *cursor;
|
||||
return cursor->_value;
|
||||
cursor = cursor->_next;
|
||||
}
|
||||
|
||||
return Dictionary();
|
||||
return NULL;
|
||||
}
|
||||
Dictionary operator()(const unsigned int index)
|
||||
|
||||
T* operator()(const unsigned int index)
|
||||
{
|
||||
return get(index);
|
||||
return get(index);
|
||||
}
|
||||
|
||||
unsigned int count()
|
||||
{
|
||||
unsigned int counter(0);
|
||||
@ -103,7 +112,9 @@ public:
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
void clear() {this->dispose();}
|
||||
|
||||
void dispose()
|
||||
{
|
||||
if(isListEmpty(_head->_next))return;
|
||||
@ -120,6 +131,7 @@ public:
|
||||
_head = this;
|
||||
_next = NULL;
|
||||
}
|
||||
|
||||
const char *stringValue() const {return _value == NULL ? "" : _value->toString();}
|
||||
T getValue(){return T(*_value);}
|
||||
T* getValueRef(){return _value;}
|
||||
@ -133,16 +145,18 @@ protected:
|
||||
strcpy(_parameter, parameter);
|
||||
_value = value;
|
||||
}
|
||||
|
||||
Dictionary(Dictionary const& dictionaryToCopy) //Copy constructor needed because of pointers
|
||||
{
|
||||
_head = NULL;
|
||||
_next = NULL;
|
||||
|
||||
_parameter = (char *) malloc((strlen(dictionaryToCopy._parameter) * sizeof(char)) + 1); //+1 for the string terminating character
|
||||
_value = new T(*(dictionaryToCopy._value));
|
||||
_value = dictionaryToCopy._value;
|
||||
|
||||
strcpy(_parameter, dictionaryToCopy._parameter);
|
||||
}
|
||||
|
||||
boolean addNewNodeAtTheEnd(Dictionary *node)
|
||||
{
|
||||
if(node == NULL) return false;
|
||||
|
Loading…
Reference in New Issue
Block a user