Skip to content

Commit

Permalink
SafeString: as key of unordered_map
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed Feb 26, 2024
1 parent 7244936 commit e808b12
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/log/UniSmartLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SmartLog>();
logStore_[*aUniLogName] = smartLog_;
logStore_[aUniLogName] = smartLog_;
HID("creatd new log=" << (void*)(smartLog_.get()) << ", name=" << aUniLogName() << ", nLog=" << nLog());
}
else
Expand Down
6 changes: 3 additions & 3 deletions src/log/UniSmartLog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ namespace RLib
{
// ***********************************************************************************************
using SmartLog = StrCoutFSL;
using LogStore = unordered_map<shared_ptr<string>, shared_ptr<SmartLog> >;
using LogStore = unordered_map<SafeString, shared_ptr<SmartLog> >;

// ***********************************************************************************************
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()
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions src/safe_mem/SafeString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ bool operator!=(const SafeString& lhs, const SafeString& rhs)
}

} // namespace

// ***********************************************************************************************
// SafeString as key of unordered_map
template<>
struct std::hash<RLib::SafeString>
{
auto operator()(const RLib::SafeString& aSafeStr) const { return hash<string>()(aSafeStr()); }
};

// ***********************************************************************************************
// YYYY-MM-DD Who v)Modification Description
// .......... ......... .......................................................................
Expand Down
21 changes: 21 additions & 0 deletions ut/safe_mem/SafeStringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <gtest/gtest.h>
#include <unordered_map>

#include "SafeString.hpp"

Expand Down Expand Up @@ -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<SafeString, int> 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

0 comments on commit e808b12

Please sign in to comment.