Skip to content

Commit c09ab8e

Browse files
committed
revert changes
1 parent 3788d3e commit c09ab8e

File tree

3 files changed

+86
-100
lines changed

3 files changed

+86
-100
lines changed

Source/Library/Cache.cpp

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

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

15-
bool Cache::Set(const std::string &key, ICacheItem::UPtr itemPtr, ICachePolicy::UPtr newPolicy)
15+
bool Cache::Set(CacheItemBase::Ptr itemPtr, ICachePolicy::Ptr newPolicy)
1616
{
1717
if (!itemPtr)
1818
{
1919
return false;
2020
}
21-
auto data = GetEditableData(key);
21+
auto data = GetEditableData(itemPtr->GetKey());
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->RawPtr() : nullptr;
44+
return item ? item->GetRawValue() : nullptr;
4545
}
4646

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

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

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

8380
std::optional<Cache::Data> Cache::RemoveData(const std::string &key)
8481
{

Source/Library/h/Cache.hpp

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

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

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

21-
template <class T> const T *As() const
19+
template <class TValue> const CacheItem<TValue> *Upcast() const
2220
{
23-
auto casted = Upcast<T>();
24-
return casted ? casted->Ptr() : nullptr;
21+
return dynamic_cast<const CacheItem<TValue> *>(this);
2522
}
2623

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

33-
virtual const void *RawPtr() const = 0;
34-
virtual void *RawPtr() = 0;
35-
36-
virtual ~ICacheItem(){};
33+
virtual ~CacheItemBase(){};
3734
};
3835

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

4442
public:
45-
using UPtr = std::unique_ptr<CacheItem<T>>;
46-
47-
static UPtr Make(T &&value) { return UPtr(new CacheItem<T>(std::move(value))); }
48-
49-
CacheItem(T &&value) : _value(std::move(value)) {}
50-
51-
const void *RawPtr() const { return &_value; }
52-
53-
void *RawPtr() { return &_value; }
43+
using Ptr = std::unique_ptr<CacheItem<TValue>>;
5444

55-
const T *Ptr() const { return &_value; }
56-
57-
T *Ptr() { return &_value; }
58-
59-
const T *operator->() const { return &_value; }
60-
61-
const T &operator*() const { return _value; }
45+
static Ptr Make(const std::string &key, TValue &&value)
46+
{
47+
return Ptr(new CacheItem<TValue>(key, std::move(value)));
48+
}
6249

63-
T *operator->() { return &_value; }
50+
CacheItem(const std::string &key, TValue &&value) : _key(key), _value(value) {}
6451

65-
T &operator*() { return _value; }
52+
const std::string &GetKey() const final { return _key; }
53+
const void *GetRawValue() const final { return GetValue(); }
54+
const TValue *GetValue() const { return &_value; }
6655
};
6756

6857
struct ICachePolicy
6958
{
70-
using UPtr = std::unique_ptr<ICachePolicy>;
59+
using Ptr = std::unique_ptr<ICachePolicy>;
7160

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

7564
virtual ~ICachePolicy() {}
7665
};
@@ -86,11 +75,11 @@ namespace sd
8675
RemoveCallback _removeCallback;
8776

8877
public:
89-
using UPtr = std::unique_ptr<CachePolicy<TValue>>;
78+
using Ptr = std::unique_ptr<CachePolicy<TValue>>;
9079

91-
UPtr static Make(UpdateCallback updateCallback = nullptr, RemoveCallback removeCallback = nullptr)
80+
Ptr static Make(UpdateCallback updateCallback = nullptr, RemoveCallback removeCallback = nullptr)
9281
{
93-
return UPtr(new CachePolicy<TValue>(updateCallback, removeCallback));
82+
return Ptr(new CachePolicy<TValue>(updateCallback, removeCallback));
9483
}
9584

9685
CachePolicy(UpdateCallback updateCallback = nullptr, RemoveCallback removeCallback = nullptr)
@@ -102,32 +91,32 @@ namespace sd
10291

10392
void SetOnRemoveCallback(RemoveCallback removeCallback) { _removeCallback = removeCallback; }
10493

105-
void CallOnRemove(const ICacheItem *value) const final
94+
void CallOnRemove(const CacheItemBase *item) const final
10695
{
107-
if (_removeCallback && value)
96+
if (_removeCallback && item)
10897
{
109-
_removeCallback(value->As<TValue>());
98+
_removeCallback(item->GetValueAs<TValue>());
11099
}
111100
}
112101

113-
void CallOnUpdate(const ICacheItem *oldValue, const ICacheItem *newValue) const final
102+
void CallOnUpdate(const CacheItemBase *oldItem, const CacheItemBase *newItem) const final
114103
{
115-
if (_updateCallback && oldValue && newValue)
104+
if (_updateCallback && oldItem && newItem)
116105
{
117-
_updateCallback(oldValue->As<TValue>(), newValue->As<TValue>());
106+
_updateCallback(oldItem->GetValueAs<TValue>(), newItem->GetValueAs<TValue>());
118107
}
119108
}
120109
};
121110

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

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

128117
virtual const void *Get(const std::string &key) const = 0;
129118

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

132121
virtual bool Remove(const std::string &key) = 0;
133122

@@ -143,8 +132,8 @@ namespace sd
143132
private:
144133
struct Data
145134
{
146-
ICacheItem::UPtr item;
147-
ICachePolicy::UPtr policy;
135+
CacheItemBase::Ptr item;
136+
ICachePolicy::Ptr policy;
148137
};
149138

150139
std::unordered_map<std::string, Data> _items;
@@ -157,40 +146,40 @@ namespace sd
157146
Cache &operator=(const Cache &) = delete;
158147
Cache &operator=(Cache &&) = delete;
159148

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

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

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

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

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

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

190179
template <class TValue> const TValue *Get(const std::string &key) const
191180
{
192181
auto item = GetItem(key);
193-
return item ? item->As<TValue>() : nullptr;
182+
return item ? item->GetValueAs<TValue>() : nullptr;
194183
}
195184

196185
const void *Get(const std::string &key) const final;
@@ -201,7 +190,7 @@ namespace sd
201190
return item ? item->Upcast<TValue>() : nullptr;
202191
}
203192

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

206195
bool Remove(const std::string &key) final;
207196

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

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

215204
Data *GetEditableData(const std::string &key);
216205

@@ -238,13 +227,13 @@ namespace sd
238227
CacheWrapper &operator=(CacheWrapper &&) = delete;
239228

240229
template <class TValue>
241-
bool Add(const std::string &key, TValue &&value, typename CachePolicy<TValue>::UPtr policy = nullptr)
230+
bool Add(const std::string &key, TValue &&value, typename CachePolicy<TValue>::Ptr policy = nullptr)
242231
{
243232
return _cache.Add<TValue>(BuildKey<TValue>(key), std::move(value), std::move(policy));
244233
}
245234

246235
template <class TValue>
247-
bool Set(const std::string &key, TValue &&value, typename CachePolicy<TValue>::UPtr policy = nullptr)
236+
bool Set(const std::string &key, TValue &&value, typename CachePolicy<TValue>::Ptr policy = nullptr)
248237
{
249238
return _cache.Set<TValue>(BuildKey<TValue>(key), std::move(value), std::move(policy));
250239
}

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

0 commit comments

Comments
 (0)