Reworked once more the Dictionary utility class

This commit is contained in:
Anatole SCHRAMM 2019-04-03 17:09:52 +02:00
parent 18aeb123ba
commit f63e7bf835

View File

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