diff --git a/src/log/UniSmartLog.cpp b/src/log/UniSmartLog.cpp index bdc63a4..abfd1de 100644 --- a/src/log/UniSmartLog.cpp +++ b/src/log/UniSmartLog.cpp @@ -12,11 +12,11 @@ namespace RLib UniSmartLog::UniSmartLog(const SafeString& aUniLogName) : uniLogName_(aUniLogName) { //for(auto&& it : logStore_) cout << it.first<<", p=" << it.second.get() << endl; - auto&& it = logStore_.find(*aUniLogName); + auto&& it = logStore_.find(aUniLogName); if (it == logStore_.end()) { smartLog_ = make_shared(); - logStore_[*aUniLogName] = smartLog_; + logStore_[aUniLogName] = smartLog_; HID("creatd new log=" << (void*)(smartLog_.get()) << ", name=" << aUniLogName() << ", nLog=" << nLog()); } else diff --git a/src/log/UniSmartLog.hpp b/src/log/UniSmartLog.hpp index 7b7c8ad..d2446bb 100644 --- a/src/log/UniSmartLog.hpp +++ b/src/log/UniSmartLog.hpp @@ -34,14 +34,14 @@ namespace RLib { // *********************************************************************************************** using SmartLog = StrCoutFSL; -using LogStore = unordered_map, shared_ptr >; +using LogStore = unordered_map >; // *********************************************************************************************** class UniSmartLog { public: explicit UniSmartLog(const SafeString& = ULN_DEFAULT); - ~UniSmartLog() { if (smartLog_.use_count() == 2) logStore_.erase(*uniLogName_); } + ~UniSmartLog() { if (smartLog_.use_count() == 2) logStore_.erase(uniLogName_); } SmartLog& oneLog() const; // for logging; ret ref is not mem-safe when use the ref after del SmartLog& operator()() const { return oneLog(); } // not mem-safe as oneLog() @@ -73,7 +73,7 @@ class UniSmartLog static size_t logLen(const SafeString& aUniLogName = ULN_DEFAULT) { - auto&& it = logStore_.find(*aUniLogName); + auto&& it = logStore_.find(aUniLogName); return it == logStore_.end() ? 0 : it->second->str().size(); diff --git a/src/safe_mem/SafeString.hpp b/src/safe_mem/SafeString.hpp index 534f38f..2fc7181 100644 --- a/src/safe_mem/SafeString.hpp +++ b/src/safe_mem/SafeString.hpp @@ -59,6 +59,15 @@ bool operator!=(const SafeString& lhs, const SafeString& rhs) } } // namespace + +// *********************************************************************************************** +// SafeString as key of unordered_map +template<> +struct std::hash +{ + auto operator()(const RLib::SafeString& aSafeStr) const { return hash()(aSafeStr()); } +}; + // *********************************************************************************************** // YYYY-MM-DD Who v)Modification Description // .......... ......... ....................................................................... diff --git a/ut/safe_mem/SafeStringTest.cpp b/ut/safe_mem/SafeStringTest.cpp index 9dfae4f..8905361 100644 --- a/ut/safe_mem/SafeStringTest.cpp +++ b/ut/safe_mem/SafeStringTest.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ #include +#include #include "SafeString.hpp" @@ -73,4 +74,24 @@ TEST(SafeStringTest, GOLD_compare) EXPECT_TRUE (null() != hs() ) << "REQ: nullptr !="; } +#define UNORDERED_MAP_ETC +// *********************************************************************************************** +TEST(SafeStringTest, GOLD_asKeyOf_unorderedMap) +{ + unordered_map store; + EXPECT_EQ(0, store.size()) << "REQ: SafeString can be key"; + + store[SafeString()] = 100; + EXPECT_EQ(nullptr, *SafeString()); + EXPECT_EQ(1, store.size()) << "REQ: key=nullptr is OK"; + + store[SafeString()] = 200; + EXPECT_EQ(1, store.size()) << "REQ: no dup key"; + EXPECT_EQ(200, store[SafeString()]) << "REQ: dup key overwrites value"; + + store[SafeString("hello")] = 300; + EXPECT_EQ(2, store.size()) << "REQ: store diff keys"; + EXPECT_EQ(300, store[SafeString("hello")]) << "REQ: get normal key's value"; +} + } // namespace