# HG changeset patch # User Alain Mazy # Date 1693488041 -7200 # Node ID b31c73bc7cb6326f1729144043a9ca849f7fc6b7 # Parent d857c6210c503b59dc37850708467abbd8fba8a1 Toolbox : more set functions diff -r d857c6210c50 -r b31c73bc7cb6 OrthancFramework/Sources/SerializationToolbox.cpp --- a/OrthancFramework/Sources/SerializationToolbox.cpp Thu Aug 24 16:35:55 2023 +0200 +++ b/OrthancFramework/Sources/SerializationToolbox.cpp Thu Aug 31 15:20:41 2023 +0200 @@ -178,23 +178,44 @@ value[field.c_str()].type() != Json::arrayValue) { throw OrthancException(ErrorCode_BadFileFormat, - "List of strings expected in field: " + field); + "List of strings expected in field: " + field); } const Json::Value& arr = value[field.c_str()]; - target.resize(arr.size()); + try + { + ReadArrayOfStrings(target, arr); + } + catch (OrthancException& ex) + { // more detailed error + throw OrthancException(ErrorCode_BadFileFormat, + "List of strings expected in field: " + field); + } + } + - for (Json::Value::ArrayIndex i = 0; i < arr.size(); i++) + void SerializationToolbox::ReadArrayOfStrings(std::vector& target, + const Json::Value& array) + { + if (array.type() != Json::arrayValue) { - if (arr[i].type() != Json::stringValue) + throw OrthancException(ErrorCode_BadFileFormat, + "List of strings expected"); + } + + target.resize(array.size()); + + for (Json::Value::ArrayIndex i = 0; i < array.size(); i++) + { + if (array[i].type() != Json::stringValue) { throw OrthancException(ErrorCode_BadFileFormat, - "List of strings expected in field: " + field); + "List of strings expected"); } else { - target[i] = arr[i].asString(); + target[i] = array[i].asString(); } } } @@ -230,6 +251,20 @@ } + void SerializationToolbox::ReadSetOfStrings(std::set& target, + const Json::Value& value) + { + std::vector tmp; + ReadArrayOfStrings(tmp, value); + + target.clear(); + for (size_t i = 0; i < tmp.size(); i++) + { + target.insert(tmp[i]); + } + } + + void SerializationToolbox::ReadSetOfTags(std::set& target, const Json::Value& value, const std::string& field) diff -r d857c6210c50 -r b31c73bc7cb6 OrthancFramework/Sources/SerializationToolbox.h --- a/OrthancFramework/Sources/SerializationToolbox.h Thu Aug 24 16:35:55 2023 +0200 +++ b/OrthancFramework/Sources/SerializationToolbox.h Thu Aug 31 15:20:41 2023 +0200 @@ -63,6 +63,9 @@ const Json::Value& value, const std::string& field); + static void ReadArrayOfStrings(std::vector& target, + const Json::Value& value); + static void ReadListOfStrings(std::list& target, const Json::Value& value, const std::string& field); @@ -71,6 +74,9 @@ const Json::Value& value, const std::string& field); + static void ReadSetOfStrings(std::set& target, + const Json::Value& value); + static void ReadSetOfTags(std::set& target, const Json::Value& value, const std::string& field); diff -r d857c6210c50 -r b31c73bc7cb6 OrthancFramework/Sources/Toolbox.h --- a/OrthancFramework/Sources/Toolbox.h Thu Aug 24 16:35:55 2023 +0200 +++ b/OrthancFramework/Sources/Toolbox.h Thu Aug 31 15:20:41 2023 +0200 @@ -259,6 +259,22 @@ } } + // returns true if all element of 'needles' are found in 'haystack' + template static void GetIntersection(std::set& target, const std::set& a, const std::set& b) + { + target.clear(); + + for (typename std::set::const_iterator it = a.begin(); + it != a.end(); ++it) + { + if (b.count(*it) > 0) + { + target.insert(*it); + } + } + } + + #if ORTHANC_ENABLE_PUGIXML == 1 static void JsonToXml(std::string& target, const Json::Value& source, diff -r d857c6210c50 -r b31c73bc7cb6 OrthancFramework/UnitTestsSources/ToolboxTests.cpp --- a/OrthancFramework/UnitTestsSources/ToolboxTests.cpp Thu Aug 24 16:35:55 2023 +0200 +++ b/OrthancFramework/UnitTestsSources/ToolboxTests.cpp Thu Aug 31 15:20:41 2023 +0200 @@ -280,6 +280,48 @@ } } +TEST(Toolbox, GetSetIntersection) +{ + { + std::set target; + std::set a; + std::set b; + + Toolbox::GetIntersection(target, a, b); + ASSERT_EQ(0u, target.size()); + } + + { + std::set target; + std::set a; + std::set b; + + a.insert(1); + b.insert(1); + + Toolbox::GetIntersection(target, a, b); + ASSERT_EQ(1u, target.size()); + ASSERT_EQ(1u, target.count(1)); + } + + { + std::set target; + std::set a; + std::set b; + + a.insert(1); + a.insert(2); + b.insert(2); + + Toolbox::GetIntersection(target, a, b); + ASSERT_EQ(1u, target.size()); + ASSERT_EQ(0u, target.count(1)); + ASSERT_EQ(1u, target.count(2)); + } + +} + + TEST(Toolbox, JoinStrings) { {