Skip to content

Commit a72b30a

Browse files
committed
code refactor
1 parent cf51ac6 commit a72b30a

File tree

3 files changed

+93
-79
lines changed

3 files changed

+93
-79
lines changed

Source/Library/Cache.cpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33

44
namespace sd
55
{
6-
bool Cache::Add(CacheItemBase::Ptr itemPtr, ICachePolicy::Ptr policy)
6+
bool Cache::Add(const std::string &key, ICacheItem::Ptr itemPtr, ICachePolicy::Ptr policy)
77
{
88
if (!itemPtr)
99
{
1010
return false;
1111
}
12-
return AddData({.item = std::move(itemPtr), .policy = std::move(policy)});
12+
return AddData(key, {.item = std::move(itemPtr), .policy = std::move(policy)});
1313
}
1414

15-
bool Cache::Set(CacheItemBase::Ptr itemPtr, ICachePolicy::Ptr newPolicy)
15+
bool Cache::Set(const std::string &key, ICacheItem::Ptr itemPtr, ICachePolicy::Ptr newPolicy)
1616
{
1717
if (!itemPtr)
1818
{
1919
return false;
2020
}
21-
auto data = GetEditableData(itemPtr->GetKey());
21+
auto data = GetEditableData(key);
2222
if (!data)
2323
{
2424
return false;
@@ -41,10 +41,10 @@ namespace sd
4141
const void *Cache::Get(const std::string &key) const
4242
{
4343
auto item = GetItem(key);
44-
return item ? item->GetValue() : nullptr;
44+
return item ? item->Raw() : nullptr;
4545
}
4646

47-
const CacheItemBase *Cache::GetItem(const std::string &key) const
47+
const ICacheItem *Cache::GetItem(const std::string &key) const
4848
{
4949
if (auto data = GetData(key); data && data->item)
5050
{
@@ -75,7 +75,10 @@ namespace sd
7575

7676
Cache::Data *Cache::GetEditableData(const std::string &key) { return const_cast<Data *>(GetData(key)); }
7777

78-
bool Cache::AddData(Cache::Data data) { return _items.insert({data.item->GetKey(), std::move(data)}).second; }
78+
bool Cache::AddData(const std::string &key, Cache::Data data)
79+
{
80+
return _items.insert({key, std::move(data)}).second;
81+
}
7982

8083
std::optional<Cache::Data> Cache::RemoveData(const std::string &key)
8184
{

Source/Library/h/Cache.hpp

+68-57
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,69 @@
88
namespace sd
99
{
1010

11-
template <class TValue> class CacheItem;
12-
struct CacheItemBase
11+
template <class T> class CacheItem;
12+
13+
struct ICacheItem
1314
{
14-
using Ptr = std::unique_ptr<CacheItemBase>;
15+
using Ptr = std::unique_ptr<ICacheItem>;
16+
17+
template <class T> const CacheItem<T> *Upcast() const { return dynamic_cast<const CacheItem<T> *>(this); }
1518

16-
virtual const std::string &GetKey() const = 0;
17-
virtual const void *GetValue() const = 0;
19+
template <class T> CacheItem<T> *Upcast() { return dynamic_cast<CacheItem<T> *>(this); }
1820

19-
template <class TValue> const CacheItem<TValue> *Upcast() const
21+
template <class T> const T *As() const
2022
{
21-
return dynamic_cast<const CacheItem<TValue> *>(this);
23+
auto casted = Upcast<T>();
24+
return casted ? casted->Typed() : nullptr;
2225
}
2326

24-
template <class TValue> const TValue *GetValueAs() const
27+
template <class T> T *As()
2528
{
26-
if (auto casted = Upcast<TValue>())
27-
{
28-
return casted->GetTypedValue();
29-
}
30-
return nullptr;
29+
auto casted = Upcast<T>();
30+
return casted ? casted->Typed() : nullptr;
3131
}
3232

33-
virtual ~CacheItemBase(){};
33+
virtual const void *Raw() const = 0;
34+
virtual void *Raw() = 0;
35+
36+
virtual ~ICacheItem(){};
3437
};
3538

36-
template <class TValue> class CacheItem final : public CacheItemBase
39+
template <class T> struct CacheItem : public ICacheItem
3740
{
3841
private:
39-
std::string _key;
40-
TValue _value;
42+
T _value;
4143

4244
public:
43-
using Ptr = std::unique_ptr<CacheItem<TValue>>;
45+
using Ptr = std::unique_ptr<CacheItem<T>>;
4446

45-
static Ptr Make(const std::string &key, TValue &&value)
46-
{
47-
return Ptr(new CacheItem<TValue>(key, std::move(value)));
48-
}
47+
static Ptr Make(T &&value) { return Ptr(new CacheItem<T>(std::move(value))); }
48+
49+
CacheItem(T &&value) : _value(std::move(value)) {}
50+
51+
const void *Raw() const { return &_value; }
52+
53+
void *Raw() { return &_value; }
4954

50-
CacheItem(const std::string &key, TValue &&value) : _key(key), _value(value) {}
55+
const T *Typed() const { return &_value; }
5156

52-
const std::string &GetKey() const final { return _key; }
53-
const void *GetValue() const final { return GetTypedValue(); }
54-
const TValue *GetTypedValue() const { return &_value; }
57+
T *Typed() { return &_value; }
58+
59+
const T &operator->() const { return _value; }
60+
61+
const T &operator*() const { return _value; }
62+
63+
T &operator->() { return _value; }
64+
65+
T &operator*() { return _value; }
5566
};
5667

5768
struct ICachePolicy
5869
{
5970
using Ptr = std::unique_ptr<ICachePolicy>;
6071

61-
virtual void CallOnRemove(const CacheItemBase *value) const = 0;
62-
virtual void CallOnUpdate(const CacheItemBase *oldValue, const CacheItemBase *newValue) const = 0;
72+
virtual void CallOnRemove(const ICacheItem *value) const = 0;
73+
virtual void CallOnUpdate(const ICacheItem *oldValue, const ICacheItem *newValue) const = 0;
6374

6475
virtual ~ICachePolicy() {}
6576
};
@@ -91,32 +102,32 @@ namespace sd
91102

92103
void SetOnRemoveCallback(RemoveCallback removeCallback) { _removeCallback = removeCallback; }
93104

94-
void CallOnRemove(const CacheItemBase *item) const final
105+
void CallOnRemove(const ICacheItem *value) const final
95106
{
96-
if (_removeCallback && item)
107+
if (_removeCallback && value)
97108
{
98-
_removeCallback(item->GetValueAs<TValue>());
109+
_removeCallback(value->As<TValue>());
99110
}
100111
}
101112

102-
void CallOnUpdate(const CacheItemBase *oldItem, const CacheItemBase *newItem) const final
113+
void CallOnUpdate(const ICacheItem *oldValue, const ICacheItem *newValue) const final
103114
{
104-
if (_updateCallback && oldItem && newItem)
115+
if (_updateCallback && oldValue && newValue)
105116
{
106-
_updateCallback(oldItem->GetValueAs<TValue>(), newItem->GetValueAs<TValue>());
117+
_updateCallback(oldValue->As<TValue>(), newValue->As<TValue>());
107118
}
108119
}
109120
};
110121

111122
struct ICache
112123
{
113-
virtual bool Add(CacheItemBase::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr) = 0;
124+
virtual bool Add(const std::string &key, ICacheItem::Ptr value, ICachePolicy::Ptr policy = nullptr) = 0;
114125

115-
virtual bool Set(CacheItemBase::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr) = 0;
126+
virtual bool Set(const std::string &key, ICacheItem::Ptr value, ICachePolicy::Ptr policy = nullptr) = 0;
116127

117128
virtual const void *Get(const std::string &key) const = 0;
118129

119-
virtual const CacheItemBase *GetItem(const std::string &key) const = 0;
130+
virtual const ICacheItem *GetItem(const std::string &key) const = 0;
120131

121132
virtual bool Remove(const std::string &key) = 0;
122133

@@ -132,7 +143,7 @@ namespace sd
132143
private:
133144
struct Data
134145
{
135-
CacheItemBase::Ptr item;
146+
ICacheItem::Ptr item;
136147
ICachePolicy::Ptr policy;
137148
};
138149

@@ -146,40 +157,40 @@ namespace sd
146157
Cache &operator=(const Cache &) = delete;
147158
Cache &operator=(Cache &&) = delete;
148159

149-
template <class TValue>
150-
bool Add(const std::string &key, TValue &&value, typename CachePolicy<TValue>::Ptr policy = nullptr)
160+
template <class T> bool Add(const std::string &key, T &&value, typename CachePolicy<T>::Ptr policy = nullptr)
151161
{
152-
return Add<TValue>(CacheItem<TValue>::Make(key, std::move(value)), std::move(policy));
162+
return Add<T>(key, CacheItem<T>::Make(std::move(value)), std::move(policy));
153163
}
154164

155-
template <class TValue>
156-
bool Add(typename CacheItem<TValue>::Ptr itemPtr, typename CachePolicy<TValue>::Ptr policy = nullptr)
165+
template <class T>
166+
bool Add(const std::string &key, typename CacheItem<T>::Ptr itemPtr,
167+
typename CachePolicy<T>::Ptr policy = nullptr)
157168
{
158-
CacheItemBase::Ptr itemPtrCasted = std::move(itemPtr);
159-
return Add(std::move(itemPtrCasted), std::move(policy));
169+
ICacheItem::Ptr itemPtrCasted = std::move(itemPtr);
170+
return Add(key, std::move(itemPtrCasted), std::move(policy));
160171
}
161172

162-
bool Add(CacheItemBase::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr) final;
173+
bool Add(const std::string &key, ICacheItem::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr) final;
163174

164-
template <class TValue>
165-
bool Set(const std::string &key, TValue &&value, typename CachePolicy<TValue>::Ptr policy = nullptr)
175+
template <class T> bool Set(const std::string &key, T &&value, typename CachePolicy<T>::Ptr policy = nullptr)
166176
{
167-
return Set<TValue>(CacheItem<TValue>::Make(key, std::move(value)), std::move(policy));
177+
return Set<T>(key, CacheItem<T>::Make(std::move(value)), std::move(policy));
168178
}
169179

170-
template <class TValue>
171-
bool Set(typename CacheItem<TValue>::Ptr itemPtr, typename CachePolicy<TValue>::Ptr policy = nullptr)
180+
template <class T>
181+
bool Set(const std::string &key, typename CacheItem<T>::Ptr itemPtr,
182+
typename CachePolicy<T>::Ptr policy = nullptr)
172183
{
173-
CacheItemBase::Ptr itemPtrCasted = std::move(itemPtr);
174-
return Set(std::move(itemPtrCasted), std::move(policy));
184+
ICacheItem::Ptr itemPtrCasted = std::move(itemPtr);
185+
return Set(key, std::move(itemPtrCasted), std::move(policy));
175186
}
176187

177-
bool Set(CacheItemBase::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr) final;
188+
bool Set(const std::string &key, ICacheItem::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr) final;
178189

179190
template <class TValue> const TValue *Get(const std::string &key) const
180191
{
181192
auto item = GetItem(key);
182-
return item ? item->GetValueAs<TValue>() : nullptr;
193+
return item ? item->As<TValue>() : nullptr;
183194
}
184195

185196
const void *Get(const std::string &key) const final;
@@ -190,7 +201,7 @@ namespace sd
190201
return item ? item->Upcast<TValue>() : nullptr;
191202
}
192203

193-
const CacheItemBase *GetItem(const std::string &key) const final;
204+
const ICacheItem *GetItem(const std::string &key) const final;
194205

195206
bool Remove(const std::string &key) final;
196207

@@ -199,7 +210,7 @@ namespace sd
199210
size_t Count() const final;
200211

201212
private:
202-
bool AddData(Data data);
213+
bool AddData(const std::string &key, Data data);
203214

204215
Data *GetEditableData(const std::string &key);
205216

Tests/CacheTest.cpp

+15-15
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ TEST_F(CacheTest, AddTest)
3333
{
3434
sd::Cache cache;
3535

36-
auto intItem = sd::CacheItem<int>::Make("int", 12);
37-
auto stringItem = sd::CacheItem<std::string>::Make("string", "hello"s);
38-
auto boolItem = sd::CacheItem<bool>::Make("bool", false);
39-
auto classItem = sd::CacheItem<CacheTest::ExampleClass>::Make("class", CacheTest::ExampleClass{});
40-
auto boolItem2 = sd::CacheItem<bool>::Make("bool", true);
41-
42-
EXPECT_TRUE(cache.Add(std::move(intItem)));
43-
EXPECT_TRUE(cache.Add(std::move(stringItem)));
44-
EXPECT_TRUE(cache.Add(std::move(boolItem)));
45-
EXPECT_TRUE(cache.Add(std::move(classItem)));
46-
EXPECT_FALSE(cache.Add(std::move(boolItem2)));
36+
auto intItem = sd::CacheItem<int>::Make(12);
37+
auto stringItem = sd::CacheItem<std::string>::Make("hello"s);
38+
auto boolItem = sd::CacheItem<bool>::Make(false);
39+
auto classItem = sd::CacheItem<CacheTest::ExampleClass>::Make(CacheTest::ExampleClass{});
40+
auto boolItem2 = sd::CacheItem<bool>::Make(true);
41+
42+
EXPECT_TRUE(cache.Add("int", std::move(intItem)));
43+
EXPECT_TRUE(cache.Add("string", std::move(stringItem)));
44+
EXPECT_TRUE(cache.Add("bool", std::move(boolItem)));
45+
EXPECT_TRUE(cache.Add("class", std::move(classItem)));
46+
EXPECT_FALSE(cache.Add("bool", std::move(boolItem2)));
4747
}
4848

4949
TEST_F(CacheTest, AddDirectTest)
@@ -130,10 +130,10 @@ TEST_F(CacheTest, GetItemTest)
130130
cache.Add("class", CacheTest::ExampleClass{});
131131
cache.Add("bool", false);
132132

133-
EXPECT_EQ(*cache.GetItem<int>("int")->GetTypedValue(), 12);
134-
EXPECT_EQ(*cache.GetItem<std::string>("string")->GetTypedValue(), "hello");
135-
EXPECT_EQ(*cache.GetItem<bool>("bool")->GetTypedValue(), false);
136-
EXPECT_EQ(cache.GetItem<CacheTest::ExampleClass>("class")->GetTypedValue()->name, "example"s);
133+
EXPECT_EQ(*cache.GetItem<int>("int")->Typed(), 12);
134+
EXPECT_EQ(*cache.GetItem<std::string>("string")->Typed(), "hello");
135+
EXPECT_EQ(*cache.GetItem<bool>("bool")->Typed(), false);
136+
EXPECT_EQ(cache.GetItem<CacheTest::ExampleClass>("class")->Typed()->name, "example"s);
137137
EXPECT_FALSE(cache.GetItem<int>("bool"));
138138
}
139139

0 commit comments

Comments
 (0)