Skip to content

Commit

Permalink
DataStore: 1)no replacement 2)template DataKey
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed Jun 8, 2024
1 parent 7ac674b commit 9e5a7e6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
21 changes: 10 additions & 11 deletions src/obj_anywhere/DataStore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
// ***********************************************************************************************
#pragma once

#include <string>
#include <unordered_map>

#include "SafePtr.hpp" // can't UniPtr.hpp since ut(=req) build-err
Expand All @@ -26,26 +25,26 @@ using namespace std;

namespace RLib
{
using DataKey = string;

// ***********************************************************************************************
template<typename aDataKey>
class DataStore
{
public:
template<typename aDataT> SafePtr<aDataT> get(const DataKey& aKey) const;
void set(const DataKey& aKey, SafePtr<void> aData);
template<typename aDataT> SafePtr<aDataT> get(const aDataKey& aKey) const;
void set(const aDataKey& aKey, SafePtr<void> aData);
size_t nData() const { return key_data_S_.size(); }

~DataStore() { HID("(DataStore) discard nData=" << nData()); } // debug

private:
// -------------------------------------------------------------------------------------------
unordered_map<DataKey, SafePtr<void>> key_data_S_;
unordered_map<aDataKey, SafePtr<void>> key_data_S_;
};

// ***********************************************************************************************
template<typename aDataKey>
template<typename aDataT>
SafePtr<aDataT> DataStore::get(const DataKey& aKey) const
SafePtr<aDataT> DataStore<aDataKey>::get(const aDataKey& aKey) const
{
auto&& key_data = key_data_S_.find(aKey);
if (key_data == key_data_S_.end())
Expand All @@ -57,8 +56,8 @@ SafePtr<aDataT> DataStore::get(const DataKey& aKey) const
}

// ***********************************************************************************************
inline
void DataStore::set(const DataKey& aKey, SafePtr<void> aData)
template<typename aDataKey>
void DataStore<aDataKey>::set(const aDataKey& aKey, SafePtr<void> aData)
{
if (aData.get() == nullptr)
{
Expand All @@ -67,8 +66,8 @@ void DataStore::set(const DataKey& aKey, SafePtr<void> 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); }
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/obj_anywhere/ObjAnywhere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace RLib
{
shared_ptr<DataStore> ObjAnywhere::name_obj_S_;
shared_ptr<DataStore<ObjName>> ObjAnywhere::name_obj_S_;

// ***********************************************************************************************
void ObjAnywhere::deinit()
Expand All @@ -24,7 +24,7 @@ void ObjAnywhere::init(UniLog& oneLog)
else
{
HID("(ObjAnywhere) Succeed.");
name_obj_S_ = make_shared<DataStore>();
name_obj_S_ = make_shared<DataStore<ObjName>>();
}
}
} // namespace
14 changes: 9 additions & 5 deletions src/obj_anywhere/ObjAnywhere.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@
// ***********************************************************************************************
#pragma once

#include <string>

#include "DataStore.hpp"
#include "UniLog.hpp"

using namespace std;

namespace RLib
{
using ObjName = string;

// ***********************************************************************************************
class ObjAnywhere
{
Expand All @@ -50,18 +54,18 @@ class ObjAnywhere

// typeid().name() is to compatible with previous interface (w/o aObjName), eg ::get<TypeParam>
template<typename aObjType> static
void setObj(SafePtr<aObjType>, UniLog& = UniLog::defaultUniLog_, const DataKey& = typeid(aObjType).name());
void setObj(SafePtr<aObjType>, UniLog& = UniLog::defaultUniLog_, const ObjName& = typeid(aObjType).name());
template<typename aObjType> static
SafePtr<aObjType> getObj(const DataKey& = typeid(aObjType).name());
SafePtr<aObjType> getObj(const ObjName& = typeid(aObjType).name());

private:
// -------------------------------------------------------------------------------------------
static shared_ptr<DataStore> name_obj_S_; // store aObj w/o include aObj.hpp
static shared_ptr<DataStore<ObjName>> name_obj_S_; // store aObj w/o include aObj.hpp
};

// ***********************************************************************************************
template<typename aObjType>
SafePtr<aObjType> ObjAnywhere::getObj(const DataKey& aObjName)
SafePtr<aObjType> ObjAnywhere::getObj(const ObjName& aObjName)
{
if (isInit())
return name_obj_S_->get<aObjType>(aObjName);
Expand All @@ -70,7 +74,7 @@ SafePtr<aObjType> ObjAnywhere::getObj(const DataKey& aObjName)

// ***********************************************************************************************
template<typename aObjType>
void ObjAnywhere::setObj(SafePtr<aObjType> aObj, UniLog& oneLog, const DataKey& aObjName)
void ObjAnywhere::setObj(SafePtr<aObjType> aObj, UniLog& oneLog, const ObjName& aObjName)
{
if (isInit())
name_obj_S_->set(aObjName, aObj);
Expand Down
13 changes: 12 additions & 1 deletion ut/obj_anywhere/DataStoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
// ***********************************************************************************************
#include <gtest/gtest.h>
#include <string>
#include <type_traits>

#include "DataStore.hpp"
Expand All @@ -17,7 +18,7 @@ namespace RLib
// ***********************************************************************************************
struct DataStoreTest : public Test
{
DataStore dataStore_;
DataStore<string> dataStore_;
};

#define STORE
Expand Down Expand Up @@ -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<string>("hello"));
dataStore_.set("string", make_safe<string>("world"));
EXPECT_EQ("hello", *(dataStore_.get<string>("string").get())) << "req: refuse replace";

dataStore_.set("string", nullptr);
dataStore_.set("string", make_safe<string>("world"));
EXPECT_EQ("world", *(dataStore_.get<string>("string").get())) << "req: explicit rm then set ok";
}

#define SAFE
// ***********************************************************************************************
Expand Down
6 changes: 5 additions & 1 deletion ut/obj_anywhere/ObjAnywhereTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ TEST_F(ObjAnywhereTest, get_replacement)
ObjAnywhere::setObj(p1, *this);
auto p2 = make_safe<int>(2);
ObjAnywhere::setObj(p2, *this); // req: replace set
EXPECT_EQ(p2.get(), ObjAnywhere::getObj<int>().get()) << "REQ: get p2 itself";
EXPECT_EQ(p1.get(), ObjAnywhere::getObj<int>().get()) << "REQ: refuse replacement";

ObjAnywhere::setObj<int>(nullptr, *this);
ObjAnywhere::setObj(p2, *this);
EXPECT_EQ(p2.get(), ObjAnywhere::getObj<int>().get()) << "REQ: explicit rm then set ok";
ObjAnywhere::deinit();
}
TEST_F(ObjAnywhereTest, noSet_getNull)
Expand Down

0 comments on commit 9e5a7e6

Please sign in to comment.