From cb6e80153760efa34143c80d3a02f3c23eb04568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnter=20Obiltschnig?= Date: Sun, 11 Apr 2021 16:31:35 +0200 Subject: [PATCH] merge changes from 1.10.2 --- Foundation/include/Poco/AbstractCache.h | 17 +++++++++ Foundation/include/Poco/Dynamic/Pair.h | 4 +-- Foundation/include/Poco/Dynamic/Struct.h | 16 ++++----- Foundation/src/JSONString.cpp | 3 +- Foundation/testsuite/src/LRUCacheTest.cpp | 24 +++++++++++++ Foundation/testsuite/src/LRUCacheTest.h | 1 + Foundation/testsuite/src/StringTest.cpp | 5 ++- Foundation/testsuite/src/VarTest.cpp | 42 +++++++++++------------ 8 files changed, 76 insertions(+), 36 deletions(-) diff --git a/Foundation/include/Poco/AbstractCache.h b/Foundation/include/Poco/AbstractCache.h index 15decc492..2ad8c031a 100644 --- a/Foundation/include/Poco/AbstractCache.h +++ b/Foundation/include/Poco/AbstractCache.h @@ -176,6 +176,23 @@ public: return result; } + template + void forEach(Fn&& fn) const + /// Iterates over all key-value pairs in the + /// cache, using a functor or lambda expression. + /// + /// The given functor must take the key and value + /// as parameters. Note that the value is passed + /// as the actual value (or reference), + /// not a Poco::SharedPtr. + { + typename TMutex::ScopedLock lock(_mutex); + for (const auto& p: _data) + { + fn(p.first, *p.second); + } + } + protected: mutable FIFOEvent> IsValid; mutable FIFOEvent Replace; diff --git a/Foundation/include/Poco/Dynamic/Pair.h b/Foundation/include/Poco/Dynamic/Pair.h index e51921cd5..645a2d9d2 100644 --- a/Foundation/include/Poco/Dynamic/Pair.h +++ b/Foundation/include/Poco/Dynamic/Pair.h @@ -189,7 +189,7 @@ public: val.append("{ "); Var key(_val.first()); Impl::appendJSONKey(val, key); - val.append(" : "); + val.append(": "); Impl::appendJSONValue(val, _val.second()); val.append(" }"); } @@ -338,7 +338,7 @@ public: val.append("{ "); Var key(_val.first()); Impl::appendJSONKey(val, key); - val.append(" : "); + val.append(": "); Impl::appendJSONValue(val, _val.second()); val.append(" }"); } diff --git a/Foundation/include/Poco/Dynamic/Struct.h b/Foundation/include/Poco/Dynamic/Struct.h index 1e52c31f2..548562835 100644 --- a/Foundation/include/Poco/Dynamic/Struct.h +++ b/Foundation/include/Poco/Dynamic/Struct.h @@ -339,7 +339,7 @@ public: { Var key(it->first); Impl::appendJSONKey(val, key); - val.append(" : "); + val.append(": "); Impl::appendJSONValue(val, it->second); ++it; } @@ -348,7 +348,7 @@ public: val.append(", "); Var key(it->first); Impl::appendJSONKey(val, key); - val.append(" : "); + val.append(": "); Impl::appendJSONValue(val, it->second); } val.append(" }"); @@ -525,7 +525,7 @@ public: { Var key(it->first); Impl::appendJSONKey(val, key); - val.append(" : "); + val.append(": "); Impl::appendJSONValue(val, it->second); ++it; } @@ -534,7 +534,7 @@ public: val.append(", "); Var key(it->first); Impl::appendJSONKey(val, key); - val.append(" : "); + val.append(": "); Impl::appendJSONValue(val, it->second); } val.append(" }"); @@ -711,7 +711,7 @@ public: { Var key(it->first); Impl::appendJSONKey(val, key); - val.append(" : "); + val.append(": "); Impl::appendJSONValue(val, it->second); ++it; } @@ -720,7 +720,7 @@ public: val.append(", "); Var key(it->first); Impl::appendJSONKey(val, key); - val.append(" : "); + val.append(": "); Impl::appendJSONValue(val, it->second); } val.append(" }"); @@ -897,7 +897,7 @@ public: { Var key(it->first); Impl::appendJSONKey(val, key); - val.append(" : "); + val.append(": "); Impl::appendJSONValue(val, it->second); ++it; } @@ -906,7 +906,7 @@ public: val.append(", "); Var key(it->first); Impl::appendJSONKey(val, key); - val.append(" : "); + val.append(": "); Impl::appendJSONValue(val, it->second); } val.append(" }"); diff --git a/Foundation/src/JSONString.cpp b/Foundation/src/JSONString.cpp index 16049765d..2e901a1e3 100644 --- a/Foundation/src/JSONString.cpp +++ b/Foundation/src/JSONString.cpp @@ -49,8 +49,7 @@ void writeString(const std::string &value, T& obj, typename WriteFunc::Typ { for(std::string::const_iterator it = value.begin(), end = value.end(); it != end; ++it) { - // Forward slash isn't strictly required by JSON spec, but some parsers expect it - if((*it >= 0 && *it <= 31) || (*it == '"') || (*it == '\\') || (*it == '/')) + if((*it >= 0 && *it <= 31) || (*it == '"') || (*it == '\\')) { std::string str = Poco::UTF8::escape(it, it + 1, true); (obj.*write)(str.c_str(), str.size()); diff --git a/Foundation/testsuite/src/LRUCacheTest.cpp b/Foundation/testsuite/src/LRUCacheTest.cpp index cb4a83a9d..416301e65 100644 --- a/Foundation/testsuite/src/LRUCacheTest.cpp +++ b/Foundation/testsuite/src/LRUCacheTest.cpp @@ -221,6 +221,29 @@ void LRUCacheTest::testUpdate() } +void LRUCacheTest::testForEach() +{ + LRUCache aCache(3); + + std::map values; + aCache.add(1, 100); + aCache.add(2, 200); + aCache.add(3, 300); + + aCache.forEach( + [&values](int key, int value) + { + values[key] = value; + } + ); + + assertEquals (values.size(), 3); + assertEquals (values[1], 100); + assertEquals (values[2], 200); + assertEquals (values[3], 300); +} + + void LRUCacheTest::onUpdate(const void* pSender, const Poco::KeyValueArgs& args) { ++updateCnt; @@ -260,6 +283,7 @@ CppUnit::Test* LRUCacheTest::suite() CppUnit_addTest(pSuite, LRUCacheTest, testCacheSizeN); CppUnit_addTest(pSuite, LRUCacheTest, testDuplicateAdd); CppUnit_addTest(pSuite, LRUCacheTest, testUpdate); + CppUnit_addTest(pSuite, LRUCacheTest, testForEach); return pSuite; } diff --git a/Foundation/testsuite/src/LRUCacheTest.h b/Foundation/testsuite/src/LRUCacheTest.h index 8c8bb2f7d..a0c5e29fb 100644 --- a/Foundation/testsuite/src/LRUCacheTest.h +++ b/Foundation/testsuite/src/LRUCacheTest.h @@ -31,6 +31,7 @@ public: void testCacheSizeN(); void testDuplicateAdd(); void testUpdate(); + void testForEach(); void setUp(); void tearDown(); diff --git a/Foundation/testsuite/src/StringTest.cpp b/Foundation/testsuite/src/StringTest.cpp index 540fed6a0..b880bf3ed 100644 --- a/Foundation/testsuite/src/StringTest.cpp +++ b/Foundation/testsuite/src/StringTest.cpp @@ -1378,7 +1378,6 @@ void StringTest::testJSONString() { assertTrue (toJSON("\\", false) == "\\\\"); assertTrue (toJSON("\"", false) == "\\\""); - assertTrue (toJSON("/", false) == "\\/"); assertTrue (toJSON("\a", false) == "\\u0007"); assertTrue (toJSON("\b", false) == "\\b"); assertTrue (toJSON("\f", false) == "\\f"); @@ -1395,7 +1394,7 @@ void StringTest::testJSONString() std::string str = "\"foo\\\\\""; assertTrue (toJSON("foo\\") == str); - assertTrue (toJSON("bar/") == "\"bar\\/\""); + assertTrue (toJSON("bar/") == "\"bar/\""); assertTrue (toJSON("baz") == "\"baz\""); assertTrue (toJSON("q\"uote\"d") == "\"q\\\"uote\\\"d\""); assertTrue (toJSON("bs\b") == "\"bs\\b\""); @@ -1412,7 +1411,7 @@ void StringTest::testJSONString() ostr.str(""); toJSON("foo\\", ostr); - assertTrue (toJSON("bar/") == "\"bar\\/\""); + assertTrue (toJSON("bar/") == "\"bar/\""); ostr.str(""); toJSON("baz", ostr); assertTrue (ostr.str() == "\"baz\""); diff --git a/Foundation/testsuite/src/VarTest.cpp b/Foundation/testsuite/src/VarTest.cpp index c0776becf..a476fb8ac 100644 --- a/Foundation/testsuite/src/VarTest.cpp +++ b/Foundation/testsuite/src/VarTest.cpp @@ -2438,14 +2438,14 @@ void VarTest::testDynamicPair() catch (InvalidAccessException&) { } Var va(aPair); - assertTrue ("{ \"0\" : null }" == va.convert()); + assertTrue ("{ \"0\": null }" == va.convert()); assertTrue (aPair.toString() == va.convert()); aPair = Pair(4, "123"); assertTrue ("123" == aPair.second()); va = aPair; - assertTrue ("{ \"4\" : \"123\" }" == va.convert()); + assertTrue ("{ \"4\": \"123\" }" == va.convert()); assertTrue (aPair.toString() == va.convert()); int i = 1; @@ -2464,11 +2464,11 @@ void VarTest::testDynamicPair() assertTrue ("2" == pPair.second()); Var vp(pPair); - assertTrue ("{ \"1\" : \"2\" }" == vp.convert()); + assertTrue ("{ \"1\": \"2\" }" == vp.convert()); assertTrue (pPair.toString() == vp.convert()); Var vs(sPair); - assertTrue ("{ \"2\" : 1 }" == vs.convert()); + assertTrue ("{ \"2\": 1 }" == vs.convert()); assertTrue (sPair.toString() == vs.convert()); } @@ -2509,7 +2509,7 @@ void VarTest::testStructToString() aStruct["Age"] = 1; Var a1(aStruct); std::string res = a1.convert(); - std::string expected = "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }"; + std::string expected = "{ \"Age\": 1, \"First Name\": \"Junior\", \"Last Name\": \"POCO\" }"; assertTrue (res == expected); assertTrue (aStruct.toString() == res); } @@ -2523,7 +2523,7 @@ void VarTest::testOrderedStructToString() aStruct["Age"] = 1; Var a1(aStruct); std::string res = a1.convert(); - std::string expected = "{ \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\", \"Age\" : 1 }"; + std::string expected = "{ \"First Name\": \"Junior\", \"Last Name\": \"POCO\", \"Age\": 1 }"; assertTrue(res == expected); assertTrue(aStruct.toString() == res); } @@ -2535,7 +2535,7 @@ void VarTest::testStructToStringEscape() aStruct["Value"] = "Value with \" and \n"; Var a1(aStruct); std::string res = a1.convert(); - std::string expected = "{ \"Value\" : \"Value with \\\" and \\n\" }"; + std::string expected = "{ \"Value\": \"Value with \\\" and \\n\" }"; assertTrue (res == expected); assertTrue (aStruct.toString() == res); } @@ -2560,14 +2560,14 @@ void VarTest::testArrayOfStructsToString() Var a1(s16); std::string res = a1.convert(); std::string expected = "[ " - "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, " - "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" }, " + "{ \"Age\": 1, \"First Name\": \"Junior\", \"Last Name\": \"POCO\" }, " + "{ \"Age\": 100, \"First Name\": \"Senior\", \"Last Name\": \"POCO\" }, " "[ " - "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, " - "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" }, " + "{ \"Age\": 1, \"First Name\": \"Junior\", \"Last Name\": \"POCO\" }, " + "{ \"Age\": 100, \"First Name\": \"Senior\", \"Last Name\": \"POCO\" }, " "[ " - "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, " - "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" } " + "{ \"Age\": 1, \"First Name\": \"Junior\", \"Last Name\": \"POCO\" }, " + "{ \"Age\": 100, \"First Name\": \"Senior\", \"Last Name\": \"POCO\" } " "] ] ]"; assertTrue (res == expected); @@ -2594,8 +2594,8 @@ void VarTest::testStructWithArraysToString() aStruct["Address"] = addr; Var a2(aStruct); std::string res = a2.convert(); - std::string expected = "{ \"Address\" : { \"Country\" : \"Carinthia\", \"Number\" : 4, \"Street\" : \"Unknown\" }, " - "\"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : [ \"string\", 23 ] }"; + std::string expected = "{ \"Address\": { \"Country\": \"Carinthia\", \"Number\": 4, \"Street\": \"Unknown\" }, " + "\"Age\": 1, \"First Name\": \"Junior\", \"Last Name\": [ \"string\", 23 ] }"; assertTrue (res == expected); assertTrue (aStruct.toString() == res); @@ -2615,17 +2615,17 @@ void VarTest::testJSONDeserializeString() char cc = b2.convert(); assertTrue (cc == 'c'); - tst = "{ \"a\" : \"1\", \"b\" : \"2\" \n}"; + tst = "{ \"a\": \"1\", \"b\": \"2\" \n}"; a = Var::parse(tst); - assertTrue (a.toString() == "{ \"a\" : \"1\", \"b\" : \"2\" }"); + assertTrue (a.toString() == "{ \"a\": \"1\", \"b\": \"2\" }"); - tst = "{ \"a\" : \"1\", \"b\" : \"2\"\n}"; + tst = "{ \"a\": \"1\", \"b\": \"2\"\n}"; a = Var::parse(tst); - assertTrue (a.toString() == "{ \"a\" : \"1\", \"b\" : \"2\" }"); + assertTrue (a.toString() == "{ \"a\": \"1\", \"b\": \"2\" }"); - tst = "{ \"message\" : \"escape\\b\\f\\n\\r\\t\", \"path\" : \"\\/dev\\/null\" }"; + tst = "{ \"message\": \"escape\\b\\f\\n\\r\\t\", \"path\": \"\\/dev\\/null\" }"; a = Var::parse(tst); - assertTrue(a.toString() == "{ \"message\" : \"escape\\b\\f\\n\\r\\t\", \"path\" : \"\\/dev\\/null\" }"); + assertTrue(a.toString() == "{ \"message\": \"escape\\b\\f\\n\\r\\t\", \"path\": \"/dev/null\" }"); }