From f63e7bf8351284f5c9cd340c5fc2356bc3c72b46 Mon Sep 17 00:00:00 2001 From: Anatole SCHRAMM Date: Wed, 3 Apr 2019 17:09:52 +0200 Subject: [PATCH] Reworked once more the Dictionary utility class --- src/app/Dictionary.h | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/app/Dictionary.h b/src/app/Dictionary.h index d49e3b8..1b30e39 100644 --- a/src/app/Dictionary.h +++ b/src/app/Dictionary.h @@ -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; @@ -166,7 +180,7 @@ protected: if(cursor->_next->_value == NULL)return false; delete node; - + return true; } cursor = cursor->_next; @@ -176,7 +190,7 @@ protected: return true; } boolean isListEmpty(Dictionary *node) {return node == NULL;} - + char *_parameter; T *_value; Dictionary *_next;