diff --git a/src/obj_anywhere/DataStore.hpp b/src/obj_anywhere/DataStore.hpp index 2035d63..ad8fc5b 100644 --- a/src/obj_anywhere/DataStore.hpp +++ b/src/obj_anywhere/DataStore.hpp @@ -16,7 +16,6 @@ // *********************************************************************************************** #pragma once -#include #include #include "SafePtr.hpp" // can't UniPtr.hpp since ut(=req) build-err @@ -26,26 +25,26 @@ using namespace std; namespace RLib { -using DataKey = string; - // *********************************************************************************************** +template class DataStore { public: - template SafePtr get(const DataKey& aKey) const; - void set(const DataKey& aKey, SafePtr aData); + template SafePtr get(const aDataKey& aKey) const; + void set(const aDataKey& aKey, SafePtr aData); size_t nData() const { return key_data_S_.size(); } ~DataStore() { HID("(DataStore) discard nData=" << nData()); } // debug private: // ------------------------------------------------------------------------------------------- - unordered_map> key_data_S_; + unordered_map> key_data_S_; }; // *********************************************************************************************** +template template -SafePtr DataStore::get(const DataKey& aKey) const +SafePtr DataStore::get(const aDataKey& aKey) const { auto&& key_data = key_data_S_.find(aKey); if (key_data == key_data_S_.end()) @@ -57,8 +56,8 @@ SafePtr DataStore::get(const DataKey& aKey) const } // *********************************************************************************************** -inline -void DataStore::set(const DataKey& aKey, SafePtr aData) +template +void DataStore::set(const aDataKey& aKey, SafePtr aData) { if (aData.get() == nullptr) { @@ -67,8 +66,8 @@ void DataStore::set(const DataKey& aKey, SafePtr aData) } else { - HID("(DataStore) new/replace key=" << aKey); - key_data_S_[aKey] = aData; + auto&& it_ok = key_data_S_.emplace(aKey, aData); + if (it_ok.second == false) { HID("(DataStore) ERR!!! not support to replace key=" << aKey); } } } diff --git a/src/obj_anywhere/ObjAnywhere.cpp b/src/obj_anywhere/ObjAnywhere.cpp index 83093b0..d3fd682 100644 --- a/src/obj_anywhere/ObjAnywhere.cpp +++ b/src/obj_anywhere/ObjAnywhere.cpp @@ -7,7 +7,7 @@ namespace RLib { -shared_ptr ObjAnywhere::name_obj_S_; +shared_ptr> ObjAnywhere::name_obj_S_; // *********************************************************************************************** void ObjAnywhere::deinit() @@ -24,7 +24,7 @@ void ObjAnywhere::init(UniLog& oneLog) else { HID("(ObjAnywhere) Succeed."); - name_obj_S_ = make_shared(); + name_obj_S_ = make_shared>(); } } } // namespace diff --git a/src/obj_anywhere/ObjAnywhere.hpp b/src/obj_anywhere/ObjAnywhere.hpp index 79b09ac..cd3c7d6 100644 --- a/src/obj_anywhere/ObjAnywhere.hpp +++ b/src/obj_anywhere/ObjAnywhere.hpp @@ -32,6 +32,8 @@ // *********************************************************************************************** #pragma once +#include + #include "DataStore.hpp" #include "UniLog.hpp" @@ -39,6 +41,8 @@ using namespace std; namespace RLib { +using ObjName = string; + // *********************************************************************************************** class ObjAnywhere { @@ -50,18 +54,18 @@ class ObjAnywhere // typeid().name() is to compatible with previous interface (w/o aObjName), eg ::get template static - void setObj(SafePtr, UniLog& = UniLog::defaultUniLog_, const DataKey& = typeid(aObjType).name()); + void setObj(SafePtr, UniLog& = UniLog::defaultUniLog_, const ObjName& = typeid(aObjType).name()); template static - SafePtr getObj(const DataKey& = typeid(aObjType).name()); + SafePtr getObj(const ObjName& = typeid(aObjType).name()); private: // ------------------------------------------------------------------------------------------- - static shared_ptr name_obj_S_; // store aObj w/o include aObj.hpp + static shared_ptr> name_obj_S_; // store aObj w/o include aObj.hpp }; // *********************************************************************************************** template -SafePtr ObjAnywhere::getObj(const DataKey& aObjName) +SafePtr ObjAnywhere::getObj(const ObjName& aObjName) { if (isInit()) return name_obj_S_->get(aObjName); @@ -70,7 +74,7 @@ SafePtr ObjAnywhere::getObj(const DataKey& aObjName) // *********************************************************************************************** template -void ObjAnywhere::setObj(SafePtr aObj, UniLog& oneLog, const DataKey& aObjName) +void ObjAnywhere::setObj(SafePtr aObj, UniLog& oneLog, const ObjName& aObjName) { if (isInit()) name_obj_S_->set(aObjName, aObj); diff --git a/ut/obj_anywhere/DataStoreTest.cpp b/ut/obj_anywhere/DataStoreTest.cpp index c8e22af..ca0ac4e 100644 --- a/ut/obj_anywhere/DataStoreTest.cpp +++ b/ut/obj_anywhere/DataStoreTest.cpp @@ -5,6 +5,7 @@ */ // *********************************************************************************************** #include +#include #include #include "DataStore.hpp" @@ -17,7 +18,7 @@ namespace RLib // *********************************************************************************************** struct DataStoreTest : public Test { - DataStore dataStore_; + DataStore dataStore_; }; #define STORE @@ -59,6 +60,16 @@ TEST_F(DataStoreTest, noNeedToStoreNull) dataStore_.set("string", nullptr); EXPECT_EQ(0u, dataStore_.nData()) << "REQ: erase OK"; } +TEST_F(DataStoreTest, replace) +{ + dataStore_.set("string", make_safe("hello")); + dataStore_.set("string", make_safe("world")); + EXPECT_EQ("hello", *(dataStore_.get("string").get())) << "req: refuse replace"; + + dataStore_.set("string", nullptr); + dataStore_.set("string", make_safe("world")); + EXPECT_EQ("world", *(dataStore_.get("string").get())) << "req: explicit rm then set ok"; +} #define SAFE // *********************************************************************************************** diff --git a/ut/obj_anywhere/ObjAnywhereTest.cpp b/ut/obj_anywhere/ObjAnywhereTest.cpp index 6b2a340..e39d4f1 100644 --- a/ut/obj_anywhere/ObjAnywhereTest.cpp +++ b/ut/obj_anywhere/ObjAnywhereTest.cpp @@ -44,7 +44,11 @@ TEST_F(ObjAnywhereTest, get_replacement) ObjAnywhere::setObj(p1, *this); auto p2 = make_safe(2); ObjAnywhere::setObj(p2, *this); // req: replace set - EXPECT_EQ(p2.get(), ObjAnywhere::getObj().get()) << "REQ: get p2 itself"; + EXPECT_EQ(p1.get(), ObjAnywhere::getObj().get()) << "REQ: refuse replacement"; + + ObjAnywhere::setObj(nullptr, *this); + ObjAnywhere::setObj(p2, *this); + EXPECT_EQ(p2.get(), ObjAnywhere::getObj().get()) << "REQ: explicit rm then set ok"; ObjAnywhere::deinit(); } TEST_F(ObjAnywhereTest, noSet_getNull)