From ee98b4553226dc0c172c65cab6c74975a99b1aac Mon Sep 17 00:00:00 2001 From: Henry Winkel Date: Fri, 13 Jan 2023 20:41:01 +0100 Subject: [PATCH] ADD: added serialization and deserialization for the IdentifierClass --- include/SimCore/Identifier.hpp | 6 +++++- include/SimCore/UtilFunctions.hpp | 3 +++ libs/whisper-com | 2 +- src/SimCore/Identifier.cpp | 23 ++++++++++++++++++++++- src/SimCore/UtilFunctions.cpp | 7 +++++++ tests/test_IdentifierClass.cpp | 5 +++++ 6 files changed, 43 insertions(+), 3 deletions(-) diff --git a/include/SimCore/Identifier.hpp b/include/SimCore/Identifier.hpp index 02b8ae4..0981c2b 100644 --- a/include/SimCore/Identifier.hpp +++ b/include/SimCore/Identifier.hpp @@ -1,7 +1,8 @@ #pragma once -#include "SimCore/SimCore.hpp" +#include +#include #include #include #include @@ -12,6 +13,7 @@ namespace SimCore { Identifier() = default; Identifier(int number,SimCore::ObjectSource ObjectSource) ; Identifier(std::pair id); + Identifier(std::string str); int getNumber(); @@ -21,6 +23,8 @@ namespace SimCore { std::pair getPair(); + std::string serialize(); + friend bool operator==(const Identifier &lhs,const Identifier &rhs); private: diff --git a/include/SimCore/UtilFunctions.hpp b/include/SimCore/UtilFunctions.hpp index 4eba591..9a016fa 100644 --- a/include/SimCore/UtilFunctions.hpp +++ b/include/SimCore/UtilFunctions.hpp @@ -13,6 +13,9 @@ namespace SimCore { public: static std::vector explode(std::string const & s, char delim); + static bool isNumber(const std::string& s); + + }; } \ No newline at end of file diff --git a/libs/whisper-com b/libs/whisper-com index 01e4f66..6b8d6ad 160000 --- a/libs/whisper-com +++ b/libs/whisper-com @@ -1 +1 @@ -Subproject commit 01e4f666c251c5e1fab9e2e49ba3174a37ebce51 +Subproject commit 6b8d6ad2655db00509bf692dd07e1254858c81fc diff --git a/src/SimCore/Identifier.cpp b/src/SimCore/Identifier.cpp index 3bebd2e..eb38a68 100644 --- a/src/SimCore/Identifier.cpp +++ b/src/SimCore/Identifier.cpp @@ -1,8 +1,10 @@ #include "SimCore/SimCore.hpp" #include +#include #include +#include - +#define StringDelimiter ';' namespace SimCore { @@ -15,6 +17,25 @@ Identifier::Identifier(std::pair id):number_(id.firs { } +Identifier::Identifier(std::string str) +{ + std::vector var = UtilFunctions::explode(str,StringDelimiter); + if (UtilFunctions::isNumber(var[0])) { + number_ = stoi(var[0]); + + } + if (UtilFunctions::isNumber(var[1])) { + objectSource_ = (SimCore::ObjectSource)stoi(var[1]); + + } +} + +std::string Identifier::serialize() +{ +return std::to_string(number_) + StringDelimiter + std::to_string(objectSource_); + +} + int Identifier::getNumber() { diff --git a/src/SimCore/UtilFunctions.cpp b/src/SimCore/UtilFunctions.cpp index 19ecffe..c894a72 100644 --- a/src/SimCore/UtilFunctions.cpp +++ b/src/SimCore/UtilFunctions.cpp @@ -16,4 +16,11 @@ namespace SimCore { return result; } + + bool UtilFunctions::isNumber(const std::string& s) + { + return !s.empty() && std::find_if(s.begin(), + s.end(), [](unsigned char c) { return !std::isdigit(c); }) == s.end(); + } + } \ No newline at end of file diff --git a/tests/test_IdentifierClass.cpp b/tests/test_IdentifierClass.cpp index dc71a60..407350e 100644 --- a/tests/test_IdentifierClass.cpp +++ b/tests/test_IdentifierClass.cpp @@ -19,6 +19,9 @@ SCENARIO("Testing the SimCorePositionClass") WHEN("constructing Position Object with data") { + + std::string serID = ID1.serialize(); + SimCore::Identifier ID5(serID); THEN("positions attributes are correct") { @@ -30,6 +33,8 @@ SCENARIO("Testing the SimCorePositionClass") REQUIRE(ID1 == ID2); REQUIRE(ID1 != ID3); REQUIRE(ID4 != ID3); + + REQUIRE(ID5 == ID1);