From 89b3ddc7c749da73774de36f4939fc67cdd1f312 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 20 Jun 2021 17:14:52 +0200 Subject: [PATCH] Templating the tryParse and using std::chrono::nanosecond::rep --- sources/Parser/ParserYaml.cpp | 20 ++++++++++---------- sources/Utils/Utils.cpp | 25 ------------------------- sources/Utils/Utils.hpp | 13 +++++++------ 3 files changed, 17 insertions(+), 41 deletions(-) diff --git a/sources/Parser/ParserYaml.cpp b/sources/Parser/ParserYaml.cpp index 71a75899..d4fda9fe 100644 --- a/sources/Parser/ParserYaml.cpp +++ b/sources/Parser/ParserYaml.cpp @@ -243,16 +243,16 @@ namespace BBM { MapGenerator::MapBlock map; int size = -1; - if (!Utils::tryParseInteger(node.getProperty("width"), size)) { + if (!Utils::tryParse(node.getProperty("width"), size)) { throw ParserError("width property must be an int"); } Runner::mapWidth = size; - if (!Utils::tryParseInteger(node.getProperty("height"), size)) { + if (!Utils::tryParse(node.getProperty("height"), size)) { throw ParserError("width property must be an int"); } Runner::mapHeight = size; - long timer = 0; - if (!Utils::tryParseLong(node.getProperty("timer"), timer)) { + std::chrono::nanoseconds::rep timer = 0; + if (!Utils::tryParse(node.getProperty("timer"), timer)) { throw ParserError("timer property must be a long"); } Runner::timerDelay = std::chrono::nanoseconds(timer); @@ -313,7 +313,7 @@ namespace BBM { auto pos = Utils::splitStr(subStr, ','); if (pos.size() != 3) throw (ParserError("Error parsing position.")); - if (!Utils::tryParseFloat(pos[0], x) || !Utils::tryParseFloat(pos[1], y) || !Utils::tryParseFloat(pos[2], z)) + if (!Utils::tryParse(pos[0], x) || !Utils::tryParse(pos[1], y) || !Utils::tryParse(pos[2], z)) throw (ParserError("Error parsing position.")); } catch (const std::out_of_range &err) { throw (ParserError("Error parsing position.")); @@ -327,7 +327,7 @@ namespace BBM { if (str.find('-') != std::string::npos) throw (ParserError("Couldn't parse max bomb.")); - if (!Utils::tryParseInteger(str, maxBomb)) + if (!Utils::tryParse(str, maxBomb)) throw (ParserError("Couldn't parse max bomb.")); return (maxBomb); } @@ -338,7 +338,7 @@ namespace BBM { if (line.find('-') != std::string::npos) throw (ParserError("Couldn't parse explosion radius.")); - if (!Utils::tryParseInteger(line, explosionRadius)) + if (!Utils::tryParse(line, explosionRadius)) throw (ParserError("Couldn't parse explosion radius.")); return (explosionRadius); } @@ -349,7 +349,7 @@ namespace BBM { if (line.find('-') != std::string::npos) throw (ParserError("Couldn't parse speed.")); - if (!Utils::tryParseFloat(line, speed)) + if (!Utils::tryParse(line, speed)) throw (ParserError("Couldn't parse speed.")); return (speed); } @@ -359,7 +359,7 @@ namespace BBM { if (blockType.find('-') != std::string::npos) throw (ParserError("Couldn't parse block type.")); int block = 0; - if (!Utils::tryParseInteger(blockType, block)) + if (!Utils::tryParse(blockType, block)) throw (ParserError("Couldn't parse block type.")); return (static_cast(block)); } @@ -369,7 +369,7 @@ namespace BBM { if (bonusType.find('-') != std::string::npos) throw (ParserError("Couldn't parse bonus type.")); int bonus = 0; - if (!Utils::tryParseInteger(bonusType, bonus)) + if (!Utils::tryParse(bonusType, bonus)) throw (ParserError("Couldn't parse bonus type.")); return (static_cast(bonus)); } diff --git a/sources/Utils/Utils.cpp b/sources/Utils/Utils.cpp index e0d46c87..e124dee9 100644 --- a/sources/Utils/Utils.cpp +++ b/sources/Utils/Utils.cpp @@ -60,22 +60,6 @@ namespace BBM return static_cast(numberOfMatches); } - bool Utils::tryParseInteger(const std::string &s, int &i) - { - std::istringstream iss(s); - - iss >> std::noskipws >> i; - return iss.eof() && !iss.fail(); - } - - bool Utils::tryParseFloat(const std::string &s, float &f) - { - std::istringstream iss(s); - - iss >> std::noskipws >> f; - return iss.eof() && !iss.fail(); - } - std::vector Utils::splitStr(const std::string &str, char delim) { std::vector strings; @@ -90,13 +74,4 @@ namespace BBM } return strings; } - - bool Utils::tryParseLong(const std::string &s, long &l) - { - std::istringstream iss(s); - - iss >> std::noskipws >> l; - return iss.eof() && !iss.fail(); - } - } \ No newline at end of file diff --git a/sources/Utils/Utils.hpp b/sources/Utils/Utils.hpp index 64f43e44..761d225c 100644 --- a/sources/Utils/Utils.hpp +++ b/sources/Utils/Utils.hpp @@ -30,13 +30,14 @@ namespace BBM static int findFrequency(const std::string &s, const std::string &pattern); //! @brief return true if parsing has been successful result ill be in i - static bool tryParseInteger(const std::string &s, int &i); + template + static bool tryParse(const std::string &s, T &f) + { + std::istringstream iss(s); - //! @brief return true if parsing has been successful result ill be in f - static bool tryParseFloat(const std::string &s, float &f); - - //! @brief return true if parsing has been successful result ill be in l - static bool tryParseLong(const std::string &s, long &l); + iss >> std::noskipws >> f; + return iss.eof() && !iss.fail(); + } //! @brief split a string with a delim char static std::vector splitStr(const std::string &str, char delim);