Entirely reworked the dictionary class with templates

This commit is contained in:
anschrammh 2019-03-27 23:59:53 +01:00
parent 1596394b60
commit 7ae2541999
5 changed files with 191 additions and 239 deletions

View File

@ -1,16 +0,0 @@
#include "CFGDictionary.h"
CFGDictionary::CFGDictionary()
{
}
CFGDictionary::CFGDictionary(const char *parameter, const char *value) : Dictionary(parameter, value)
{
}
CFGDictionary::~CFGDictionary()
{
}

View File

@ -1,16 +1,16 @@
#ifndef CFGDICTIONARY_H #ifndef CFGDICTIONARY_H
#define CFGDICTIONARY_H #define CFGDICTIONARY_H
#include "Dictionary.h" #include "Dictionary.h"
class CFGDictionary : public Dictionary template <typename T>
class CFGDictionary : public Dictionary<T>
{ {
public: public:
CFGDictionary(); CFGDictionary(){}
~CFGDictionary(); ~CFGDictionary(){}
protected: protected:
private: private:
CFGDictionary(const char *parameter, const char *value); CFGDictionary(const char *parameter, const char *value) : Dictionary<T>(parameter, value){}
}; };
#endif //CFGDICTIONARY_H #endif //CFGDICTIONARY_H

View File

@ -1,174 +0,0 @@
#include "Dictionary.h"
Dictionary::Dictionary() :_parameter(NULL), _value(NULL), _next(NULL), _head(this)
{
}
Dictionary::Dictionary(const char *parameter, const char *value) : Dictionary()
{
//We copy the parameter and the value
_parameter = (char *) malloc((strlen(parameter) * sizeof(char)) + 1); //+1 for the string terminating character
_value = (char *) malloc((strlen(value) * sizeof(char)) + 1);
strcpy(_parameter, parameter);
strcpy(_value, value);
}
Dictionary::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 = (char *) malloc((strlen(dictionaryToCopy._value) * sizeof(char)) + 1);
strcpy(_parameter, dictionaryToCopy._parameter);
strcpy(_value, dictionaryToCopy._value);
}
Dictionary::~Dictionary()
{
if(_head == this)
dispose();
free(_parameter);
//_parameter = NULL; Useless, just my c habits
free(_value);
//_value = NULL; Useless, just my c habits
}
boolean Dictionary::addParameter(const char *parameter, const char *value)
{
Dictionary *dictionaryNode = new Dictionary(parameter, value);
return addNewNodeAtTheEnd(dictionaryNode);
}
Dictionary Dictionary::get(const char *parameter)
{
if(isListEmpty(_head->_next))return Dictionary();
Dictionary *cursor = _head->_next;
while(!isListEmpty(cursor))
{
if(strcmp(cursor->_parameter,parameter) == 0)
return *cursor;
cursor = cursor->_next;
}
return Dictionary();
}
Dictionary Dictionary::operator()(const char *parameter)
{
return get(parameter);
}
Dictionary Dictionary::get(const unsigned int index)
{
unsigned int position(0);
if(isListEmpty(_head->_next))return Dictionary();
Dictionary *cursor = _head->_next;
while(!isListEmpty(cursor))
{
if(position++ == index)
return *cursor;
cursor = cursor->_next;
}
return Dictionary();
}
Dictionary Dictionary::operator()(const unsigned int index)
{
return get(index);
}
unsigned int Dictionary::count()
{
unsigned int counter(0);
if(isListEmpty(_head->_next))return counter;
Dictionary *cursor = _head->_next;
while(!isListEmpty(cursor))
{
counter++;
cursor = cursor->_next;
}
return counter;
}
boolean Dictionary::addNewNodeAtTheEnd(Dictionary *node)
{
if(node == NULL) return false;
node->_head = _head; //Every node should point to the first node
if(_next == NULL) //This is our first node then
{
_next = node;
return true;
}
// /!\ We have to work with the _next reference in the loop, if we don't it won't work as expected
Dictionary *cursor = _head;
while(!isListEmpty(cursor->_next))
{
if(strcmp(cursor->_next->_parameter, node->_parameter) == 0)//If we find the same parameter name, we replace it
{
free(cursor->_next->_value);
cursor->_next->_value = (char *) malloc((strlen(node->_value) * sizeof(char)) + 1);
if(cursor->_next->_value == NULL)return false;
strcpy(cursor->_next->_value, node->_value);
delete node;
return true;
}
cursor = cursor->_next;
}
cursor->_next = node;
return true;
}
boolean Dictionary::deleteParameter(const char *parameter)
{
if(_head->_next == NULL) return false;
Dictionary *cursor = _head, *toDelete(NULL);
while(!isListEmpty(cursor->_next))
{
if(strcmp(cursor->_next->_parameter, parameter) == 0)
{
toDelete = cursor->_next;
cursor->_next = cursor->_next->_next;
delete toDelete;
return true;
}
cursor = cursor->_next;
}
return false;
}
void Dictionary::dispose()
{
if(isListEmpty(_head->_next))return;
Dictionary *cursor = _head->_next, *toDelete(NULL);
while(!isListEmpty(cursor))
{
toDelete = cursor;
cursor = cursor->_next;
delete toDelete;
}
_head = this;
_next = NULL;
}
void Dictionary::clear() {this->dispose();}

View File

@ -6,63 +6,183 @@
#include <stdio.h> #include <stdio.h>
#include <Arduino.h> #include <Arduino.h>
template <typename T>
class Dictionary class Dictionary
{ {
public: public:
~Dictionary(); ~Dictionary()
boolean addParameter(const char *parameter, const char *value);
boolean addParameter(const char *parameter, const int value)
{ {
char buffer[12] = ""; if(_head == this)
sprintf(buffer,"%d",value); dispose();
return addParameter(parameter, buffer); free(_parameter);
}; //_parameter = NULL; Useless, just my c habits
boolean addParameter(const char *parameter, const float value) delete _value;
{ //_value = NULL; //Useless, just my c habits
return addParameter(parameter, (double) value);
};
boolean addParameter(const char *parameter, const double value)
{
char buffer[50] = "";
sprintf(buffer,"%f",value);
return addParameter(parameter, buffer);
};
boolean deleteParameter(const char *parameter);
Dictionary get(const char *parameter);
Dictionary operator()(const char *parameter);
Dictionary get(const unsigned int index);
Dictionary operator()(const unsigned int index);
unsigned int count();
void clear();
void dispose();
long longValue() const {return _value == NULL ? 0 : strtol(_value, NULL, 10);}
int intValue() const {return (int) (_value == NULL ? 0 : strtol(_value, NULL, 10));}
unsigned long uintValue() const {return _value == NULL ? 0 : strtoul(_value, NULL, 10);}
const char *stringValue() const {return _value == NULL ? "" : _value;}
double doubleValue() const {return _value == NULL ? 0 : strtod(_value, NULL);}
float floatValue() const {return _value == NULL ? 0 : strtof(_value, NULL);}
boolean booleanValue() const
{
if(_value == NULL)
return false;
return strcmp(_value,"true") == 0 || strcmp(_value,"TRUE") == 0 ? true : false;
} }
boolean addParameter(const char *parameter, T *value)
{
Dictionary *dictionaryNode = new Dictionary(parameter, value);
return addNewNodeAtTheEnd(dictionaryNode);
}
boolean addParameter(const char *parameter, T value)
{
Dictionary *dictionaryNode = new Dictionary(parameter, new T(value));
return addNewNodeAtTheEnd(dictionaryNode);
}
boolean deleteParameter(const char *parameter)
{
if(_head->_next == NULL) return false;
Dictionary *cursor = _head, *toDelete(NULL);
while(!isListEmpty(cursor->_next))
{
if(strcmp(cursor->_next->_parameter, parameter) == 0)
{
toDelete = cursor->_next;
cursor->_next = cursor->_next->_next;
delete toDelete;
return true;
}
cursor = cursor->_next;
}
return false;
}
Dictionary get(const char *parameter)
{
if(isListEmpty(_head->_next))return Dictionary();
Dictionary *cursor = _head->_next;
while(!isListEmpty(cursor))
{
if(strcmp(cursor->_parameter,parameter) == 0)
return *cursor;
cursor = cursor->_next;
}
return Dictionary();
}
Dictionary operator()(const char *parameter)
{
return get(parameter);
}
Dictionary get(const unsigned int index)
{
unsigned int position(0);
if(isListEmpty(_head->_next))return Dictionary();
Dictionary *cursor = _head->_next;
while(!isListEmpty(cursor))
{
if(position++ == index)
return *cursor;
cursor = cursor->_next;
}
return Dictionary();
}
Dictionary operator()(const unsigned int index)
{
return get(index);
}
unsigned int count()
{
unsigned int counter(0);
if(isListEmpty(_head->_next))return counter;
Dictionary *cursor = _head->_next;
while(!isListEmpty(cursor))
{
counter++;
cursor = cursor->_next;
}
return counter;
}
void clear() {this->dispose();}
void dispose()
{
if(isListEmpty(_head->_next))return;
Dictionary *cursor = _head->_next, *toDelete(NULL);
while(!isListEmpty(cursor))
{
toDelete = cursor;
cursor = cursor->_next;
delete toDelete;
}
_head = this;
_next = NULL;
}
const char *stringValue() const {return _value == NULL ? "" : _value->toString();}
T getValue(){return T(*_value);}
T* getValueRef(){return _value;}
const char *getParameter() const{return _parameter == NULL ? "" : _parameter;} const char *getParameter() const{return _parameter == NULL ? "" : _parameter;}
protected: protected:
Dictionary(); Dictionary() :_parameter(NULL), _value(NULL), _next(NULL), _head(this){}
Dictionary(const char *parameter, const char *value); Dictionary(const char *parameter, T *value) : Dictionary()
Dictionary(Dictionary const& dictionaryToCopy); //Copy constructor {
//We copy the parameter and the value
_parameter = (char *) malloc((strlen(parameter) * sizeof(char)) + 1); //+1 for the string terminating character
boolean addNewNodeAtTheEnd(Dictionary *node); 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));
strcpy(_parameter, dictionaryToCopy._parameter);
}
boolean addNewNodeAtTheEnd(Dictionary *node)
{
if(node == NULL) return false;
node->_head = _head; //Every node should point to the first node
if(_next == NULL) //This is our first node then
{
_next = node;
return true;
}
// /!\ We have to work with the _next reference in the loop, if we don't it won't work as expected
Dictionary *cursor = _head;
while(!isListEmpty(cursor->_next))
{
if(strcmp(cursor->_next->_parameter, node->_parameter) == 0)//If we find the same parameter name, we replace it
{
delete (cursor->_next->_value);
cursor->_next->_value = new T(*(node->_value));
if(cursor->_next->_value == NULL)return false;
delete node;
return true;
}
cursor = cursor->_next;
}
cursor->_next = node;
return true;
}
boolean isListEmpty(Dictionary *node) {return node == NULL;} boolean isListEmpty(Dictionary *node) {return node == NULL;}
char *_parameter; char *_parameter;
char *_value; T *_value;
Dictionary *_next; Dictionary *_next;
Dictionary *_head; Dictionary *_head;
private: private:
}; };
#endif //DICTIONARY_H #endif //DICTIONARY_H

View File

@ -0,0 +1,22 @@
#ifndef DICTIONARYINTERFACE_H
#define DICTIONARYINTERFACE_H
class DictionaryInterface
{
public:
protected:
DictionaryInterface()
{
}
DictionaryInterface(const DictionaryInterface &Object)
{
}
virtual ~DictionaryInterface(){}
virtual const char *toString() = 0;
private:
};
#endif //DICTIONARYINTERFACE_H