Commit 14cb8728 authored by Bryan Berthou's avatar Bryan Berthou
Browse files

refs#16

In ElementaryUtils (trunk) :
- Add new Parameter class to represent a couple key = value.
- Update Parameters class to deal with the new implementation of Parameter class.
- Remove MultimapParameters.
- In GenericType class : rename toString() method to getString() to avoid confusion with toString() in BaseObject class ; Add some comments.
- In StringUtils class add some stuff to find some pattern in a string.
- Correct XMLParser : now it handles correctly XML comments. It doesn't take it anymore as an XML element.
- In LoggerManager replace std::runtime_exception by CustomException.
parent a3920aa3
......@@ -4,14 +4,8 @@
/**
* @file GenericType.h
* @author Bryan BERTHOU (SPhN / CEA Saclay)
* @date 03 July 2014
* @date July 03, 2014
* @version 1.0
*
* @class GenericType
*
* @brief A complex object to handle different simple type (like string, int, double, bool ...) into a unique stream.
* It used to provide a way to handle generic type.
* Then there is a method for each simple type that you want to get back.
*/
#include <stddef.h>
......@@ -21,6 +15,13 @@
namespace ElemUtils {
/**
* @class GenericType
*
* @brief A complex object to handle different simple type (like string, int, double, bool ...) into a unique stream.
* It used to provide a way to handle generic type.
* Then there is a method for each simple type that you want to get back.
*/
class GenericType {
public:
/**
......@@ -28,7 +29,7 @@ public:
* @param value
*/
template<class T>
GenericType(T value) {
GenericType(const T &value) {
m_stream << std::setprecision(17) << value;
}
......@@ -84,9 +85,11 @@ public:
* Convert stream to string
* @return std::string
*/
std::string toString() const;
std::string getString() const;
GenericType& operator=(const GenericType & rhs);
GenericType & operator=(const GenericType & rhs);
void setValue(const GenericType &value);
private:
std::stringstream m_stream; ///< stream to store serialized simple type
......
#ifndef MULTIMAP_PARAMETERS_H
#define MULTIMAP_PARAMETERS_H
/**
* @file MultimapParameters.h
* @author Bryan BERTHOU (SPhN / CEA Saclay)
* @date April 01, 2016
* @version 1.0
*
* @class MultimapParameters
*
* @brief Use to handle multiple parameters for a single key.
*/
#include <map>
#include <string>
#include <vector>
#include "Parameters.h"
namespace ElemUtils {
class MultimapParameters {
public:
MultimapParameters();
virtual ~MultimapParameters();
void addParameters(const std::string& key, const Parameters &parameters);
const Parameters& getParameters(const std::string &key) const;
bool isAvailableParameters(const std::string &key) const;
const Parameters& getLastAvailableParameters() const;
std::vector<Parameters> getListOfLastAvailableParameters(
const std::string &key) const;
void clear();
std::string toString() const;
private:
// <key, parameters<key, value> >
std::multimap<std::string, Parameters> m_multimapParameters;
mutable std::multimap<std::string, Parameters>::const_iterator m_it;
};
} /* namespace ElemUtils */
#endif /* MULTIMAP_PARAMETERS_H */
#ifndef PARAMETER_H
#define PARAMETER_H
/**
* @file Parameter.h
* @author Bryan BERTHOU (SPhN / CEA Saclay)
* @date October 26, 2016
* @version 1.0
*/
#include <string>
#include "GenericType.h"
namespace ElemUtils {
/**
* @class Parameter
*/
class Parameter: public GenericType {
public:
template<class T>
Parameter(const std::string &name, const T &value) :
GenericType(value), m_name(name) {
}
virtual ~Parameter();
std::string toString() const;
const std::string& getName() const;
private:
std::string m_name;
};
} /* namespace ElemUtils */
#endif /* PARAMETER_H */
#ifndef PARAMETERS_H_
#define PARAMETERS_H_
#ifndef PARAMETERS_H
#define PARAMETERS_H
/**
* @file Parameters.h
......@@ -12,7 +12,7 @@
#include <map>
#include <string>
#include "GenericType.h"
#include "Parameter.h"
namespace ElemUtils {
......@@ -25,9 +25,11 @@ class Parameters {
public:
Parameters();
Parameters(const std::string &key, const GenericType &value);
Parameters(const Parameter &parameter);
virtual ~Parameters();
void add(const std::string &key, const GenericType &value);
void add(const Parameter &parameter);
void add(const Parameters &parameters);
bool isAvailable(const std::string &parameterName) const;
......@@ -36,26 +38,27 @@ public:
void clear();
GenericType operator()(const std::string &key) const;
Parameter operator()(const std::string &key) const;
virtual std::string toString() const;
bool update(const std::string &key, const GenericType &value);
bool update(const std::string &key, const Parameter &parameter);
bool remove(const std::string &key);
const GenericType& get(const std::string &key) const;
const GenericType& getLastAvailable() const; /// Care ! Use it after isAvailable() to ensure that the iterator is in the map range.
const Parameter& get(const std::string &key) const;
const Parameter& getLastAvailable() const; /// Care ! Use it after isAvailable() to ensure that the iterator is in the map range.
std::string key(size_t index) const;
GenericType value(size_t index) const;
std::string stringValue(size_t index) const;
private:
// <key, value>
std::map<std::string, GenericType> m_parameters;
std::map<std::string, Parameter> m_parameters;
// mutable keyword is use too indicate that member can be modify in const function. In this case useful with iterator m_it & isAvailable() function.
mutable std::map<std::string, GenericType>::const_iterator m_it;
mutable std::map<std::string, Parameter>::const_iterator m_it;
};
} // namespace ElemUtils
#endif /* PARAMETERS_H_ */
#endif /* PARAMETERS_H */
......@@ -50,14 +50,15 @@ private:
static const char XML_COMMENT_CHARACTER_IDENTIFIER;
static const char XML_END_TAG_CHARACTER_IDENTIFIER;
static const std::string XML_END_TAG_VERSION_IDENTIFIER;
static const std::string XML_END_TAG_COMMENT_IDENTIFIER;
static const std::string XML_TAG_NAME_ALLOWED_CHARACTERS;
static const std::string XML_START_ELEMENT_TAG_NAME;
static const std::string XML_END_ELEMENT_TAG_NAME;
size_t findIndexOf(const std::string &string, size_t startIndex);
size_t m_fileSize;
void identifyElement(const std::string& file,
const size_t &xmlStartTagIndex, const size_t &xmlEndTagIndex);
size_t findIndexOf(const std::string &string, size_t startIndex);
void parseElement(const std::string& file, const size_t &xmlStartTagIndex,
const size_t &xmlEndTagIndex);
......
......@@ -4,11 +4,11 @@
/**
* @file StringUtils.h
* @author Bryan BERTHOU
* @date April 04, 2009
* @version 0.1
* @date Creation : 28/04/2009
* @date Derniere maj : 30/06/2016
*/
#include <stddef.h>
#include <string>
#include <utility>
#include <vector>
......@@ -104,6 +104,28 @@ public:
static std::string removeAfterLast(const std::string &chaine,
const char lastCharacter);
/**
* Find first occurrence of the pattern in the string and return index position on the string where the last character is matching pattern.
*
* @param file
* @param pattern
* @param startIndex
* @return
*/
static size_t findFirstJustAfter(const std::string &file,
const std::string &pattern, const size_t startIndex = 0);
/**
* Find first occurrence of the pattern in the string and return index position on the string where pattern is starting.
*
* @param file
* @param pattern
* @param startIndex
* @return
*/
static size_t findFirst(const std::string &file,
const std::string &pattern, const size_t startIndex = 0);
};
} // namespace ElemUtils
......
......@@ -64,10 +64,9 @@ void LoggerManager::parseConfigurationFile(const std::string &filePath) {
try {
iniFileParser.parse(filePath);
} catch (std::exception &e) {
throw std::runtime_error(
Formatter()
<< "(LoggerManager::parseConfigurationFile) Cannot read file : "
<< filePath << " " << e.what());
throw ElemUtils::CustomException("LoggerManager", __func__,
Formatter() << "Cannot read file : " << filePath << " "
<< e.what());
}
std::map<std::string, std::string> data = iniFileParser.getValues();
......@@ -78,10 +77,11 @@ void LoggerManager::parseConfigurationFile(const std::string &filePath) {
try {
m_active = GenericType(it->second).toBoolean();
} catch (std::exception &e) {
throw std::runtime_error(
"Cannot instantiated LoggerManager wrong value for ["
+ ENABLE_NAME
+ "] parameter in logger.cfg file");
throw ElemUtils::CustomException("LoggerManager", __func__,
ElemUtils::Formatter()
<< "Cannot instantiated LoggerManager wrong value for ["
<< ENABLE_NAME
<< "] parameter in logger.cfg file");
}
}
// retrieve default level value
......@@ -165,10 +165,9 @@ void LoggerManager::update() {
sf::sleep(sf::milliseconds(30));
}
} else {
throw std::runtime_error(
Formatter() << "(LoggerManager::update) Cannot open log file = "
<< m_outputFilePath << "\n"
<< "LoggerManager terminated !");
throw ElemUtils::CustomException("LoggerManager", __func__,
Formatter() << "Cannot open log file = " << m_outputFilePath
<< "\n" << "LoggerManager terminated !");
}
std::cout << "[LoggerManager] terminated ..." << std::endl;
......
......@@ -86,7 +86,7 @@ unsigned short GenericType::toUShort() const {
return us;
}
std::string GenericType::toString() const {
std::string GenericType::getString() const {
return m_stream.str();
}
......@@ -124,11 +124,15 @@ size_t GenericType::toSize_t() const {
}
GenericType& ElemUtils::GenericType::operator =(const GenericType& rhs) {
this->setValue(rhs);
return *this;
}
void ElemUtils::GenericType::setValue(const GenericType& value) {
// clear current stream then copy it from other object
m_stream.clear();
m_stream.str(StringUtils::EMPTY);
m_stream << rhs.m_stream.str();
return *this;
m_stream << value.m_stream.str();
}
} // namespace ElemUtils
......
#include "../../../include/ElementaryUtils/parameters/MultimapParameters.h"
#include <stdexcept>
#include <utility>
#include "../../../include/ElementaryUtils/string_utils/Formatter.h"
namespace ElemUtils {
MultimapParameters::MultimapParameters() {
}
MultimapParameters::~MultimapParameters() {
}
void MultimapParameters::addParameters(const std::string& key,
const Parameters& parameters) {
m_multimapParameters.insert(std::make_pair(key, parameters));
}
const Parameters& MultimapParameters::getParameters(
const std::string& key) const {
m_it = m_multimapParameters.find(key);
if (m_it == m_multimapParameters.end()) {
std::runtime_error(
Formatter()
<< "(MultimapParameters::getParameters) Missing parameters object for key = "
+ key);
}
return m_it->second;
}
bool MultimapParameters::isAvailableParameters(const std::string &key) const {
m_it = m_multimapParameters.find(key);
return (m_it != m_multimapParameters.end()) ? true : false;
}
const Parameters& MultimapParameters::getLastAvailableParameters() const {
return (m_it->second);
}
std::vector<Parameters> MultimapParameters::getListOfLastAvailableParameters(
const std::string &key) const {
std::vector<Parameters> listOfParameters;
std::multimap<std::string, Parameters>::const_iterator itUpperBound =
m_multimapParameters.upper_bound(key);
while (m_it != itUpperBound && m_it != m_multimapParameters.end()) {
listOfParameters.push_back(m_it->second);
m_it++;
}
return listOfParameters;
}
void ElemUtils::MultimapParameters::clear() {
m_multimapParameters.clear();
}
} /* namespace ElemUtils */
std::string ElemUtils::MultimapParameters::toString() const {
ElemUtils::Formatter formatter;
std::multimap<std::string, Parameters>::const_iterator it;
for (it = m_multimapParameters.begin(); it != m_multimapParameters.end();
it++) {
formatter << "Object name = " << (it->first) << '\n';
formatter << "Parameters = " << (it->second).toString();
}
return formatter;
}
#include "../../../include/ElementaryUtils/parameters/Parameter.h"
#include "../../../include/ElementaryUtils/string_utils/Formatter.h"
namespace ElemUtils {
Parameter::~Parameter() {
// Nothing to do.
}
std::string ElemUtils::Parameter::toString() const {
return ElemUtils::Formatter() << "Parameter[" << m_name << "] = "
<< GenericType::getString();
}
const std::string& ElemUtils::Parameter::getName() const {
return m_name;
}
} /* namespace ElemUtils */
#include "../../../include/ElementaryUtils/parameters/Parameters.h"
#include <iterator>
#include <stdexcept>
#include <utility>
#include "../../../include/ElementaryUtils/logger/CustomException.h"
#include "../../../include/ElementaryUtils/string_utils/Formatter.h"
namespace ElemUtils {
......@@ -17,12 +17,21 @@ Parameters::Parameters(const std::string& key, const GenericType& value) :
add(key, value);
}
Parameters::Parameters(const Parameter &parameter) :
m_it(m_parameters.begin()) {
add(parameter.getName(), parameter);
}
Parameters::~Parameters() {
m_parameters.clear();
}
void Parameters::add(const std::string& key, const GenericType& value) {
m_parameters.insert(std::make_pair(key, value));
m_parameters.insert(std::make_pair(key, Parameter(key, value)));
}
void Parameters::add(const Parameter &parameter) {
m_parameters.insert(std::make_pair(parameter.getName(), parameter));
}
void Parameters::add(const Parameters& parameters) {
......@@ -37,18 +46,17 @@ bool Parameters::isAvailable(const std::string& key) const {
return (m_it != m_parameters.end()) ? true : false;
}
const GenericType& Parameters::get(const std::string& key) const {
const Parameter& Parameters::get(const std::string& key) const {
if (isAvailable(key)) {
return getLastAvailable();
} else {
throw std::runtime_error(
Formatter()
<< "(Parameters::get) Cannot found parameter name = \""
<< key << "\"");
throw CustomException("Parameters", __func__,
Formatter() << "Cannot found parameter name = \"" << key
<< "\"");
}
}
const GenericType& Parameters::getLastAvailable() const {
const Parameter& Parameters::getLastAvailable() const {
//TODO tester l'iterateur pour etre sur de ne pas etre en dehors de la map ; throw exception
return m_it->second;
}
......@@ -61,15 +69,14 @@ void Parameters::clear() {
m_parameters.clear();
}
GenericType ElemUtils::Parameters::operator ()(const std::string& key) const {
std::map<std::string, GenericType>::const_iterator it;
Parameter ElemUtils::Parameters::operator ()(const std::string& key) const {
std::map<std::string, Parameter>::const_iterator it;
it = m_parameters.find(key);
if (it == m_parameters.end()) {
throw std::runtime_error(
Formatter()
<< "(Parameters::modify) Cannot found parameter name = "
<< key);
throw CustomException("Parameters", __func__,
Formatter() << "Cannot found parameter name = \"" << key
<< "\"");
}
return (it->second);
......@@ -78,10 +85,10 @@ GenericType ElemUtils::Parameters::operator ()(const std::string& key) const {
std::string ElemUtils::Parameters::toString() const {
Formatter formatter;
std::map<std::string, GenericType>::const_iterator it;
std::map<std::string, Parameter>::const_iterator it;
for (it = m_parameters.begin(); it != m_parameters.end(); it++) {
formatter << (it->first) << " = " << (it->second).toString() << '\n';
formatter << (it->second).toString() << '\n';
}
return formatter.str();
......@@ -91,11 +98,11 @@ bool ElemUtils::Parameters::update(const std::string& key,
const GenericType& value) {
bool result = false;
std::map<std::string, GenericType>::iterator it;
std::map<std::string, Parameter>::iterator it;
it = m_parameters.find(key);
if (it != m_parameters.end()) {
(it->second) = value;
(it->second).setValue(value);
result = true;
}
......@@ -103,27 +110,25 @@ bool ElemUtils::Parameters::update(const std::string& key,
}
std::string Parameters::key(size_t index) const {
std::map<std::string, GenericType>::const_iterator it =
m_parameters.begin();
std::map<std::string, Parameter>::const_iterator it = m_parameters.begin();
std::advance(it, index);
return it->first;
}
GenericType Parameters::value(size_t index) const {
std::map<std::string, GenericType>::const_iterator it =
m_parameters.begin();
std::string Parameters::stringValue(size_t index) const {
std::map<std::string, Parameter>::const_iterator it = m_parameters.begin();
std::advance(it, index);
return it->second;
return (it->second).getString();
}
bool ElemUtils::Parameters::remove(const std::string& key) {
bool erased = false;
std::map<std::string, GenericType>::iterator it = m_parameters.find(key);
std::map<std::string, Parameter>::iterator it = m_parameters.find(key);
if (it != m_parameters.end()) {
m_parameters.erase(it);
erased = true;
......@@ -133,3 +138,4 @@ bool ElemUtils::Parameters::remove(const std::string& key) {
}
} // namespace ElemUtils
......@@ -2,7 +2,9 @@
#include <stdexcept>
#include "../../../include/ElementaryUtils/logger/LoggerManager.h"
#include "../../../include/ElementaryUtils/parser/XMLAttributs.h"
#include "../../../include/ElementaryUtils/string_utils/Formatter.h"
#include "../../../include/ElementaryUtils/string_utils/StringUtils.h"
namespace ElemUtils {
......@@ -13,73 +15,107 @@ const char XMLParser::XML_VERSION_CHARACTER_IDENTIFIER = '?';
const char XMLParser::XML_COMMENT_CHARACTER_IDENTIFIER = '!';
const char XMLParser::XML_END_TAG_CHARACTER_IDENTIFIER = '/';
const std::string XMLParser::XML_END_TAG_VERSION_IDENTIFIER = "?>";
const std::string XMLParser::XML_END_TAG_COMMENT_IDENTIFIER = "-->";
const std::string XMLParser::XML_TAG_NAME_ALLOWED_CHARACTERS =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
const std::string XMLParser::XML_START_ELEMENT_TAG_NAME = "<";
const std::string XMLParser::XML_END_ELEMENT_TAG_NAME = ">";