Skip to content

Commit 3788d3e

Browse files
committed
extend
1 parent a72b30a commit 3788d3e

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

Source/Library/Cache.cpp

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

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

15-
bool Cache::Set(const std::string &key, ICacheItem::Ptr itemPtr, ICachePolicy::Ptr newPolicy)
15+
bool Cache::Set(const std::string &key, ICacheItem::UPtr itemPtr, ICachePolicy::UPtr newPolicy)
1616
{
1717
if (!itemPtr)
1818
{
@@ -41,7 +41,7 @@ namespace sd
4141
const void *Cache::Get(const std::string &key) const
4242
{
4343
auto item = GetItem(key);
44-
return item ? item->Raw() : nullptr;
44+
return item ? item->RawPtr() : nullptr;
4545
}
4646

4747
const ICacheItem *Cache::GetItem(const std::string &key) const

Source/Library/h/Cache.hpp

+33-33
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace sd
1212

1313
struct ICacheItem
1414
{
15-
using Ptr = std::unique_ptr<ICacheItem>;
15+
using UPtr = std::unique_ptr<ICacheItem>;
1616

1717
template <class T> const CacheItem<T> *Upcast() const { return dynamic_cast<const CacheItem<T> *>(this); }
1818

@@ -21,17 +21,17 @@ namespace sd
2121
template <class T> const T *As() const
2222
{
2323
auto casted = Upcast<T>();
24-
return casted ? casted->Typed() : nullptr;
24+
return casted ? casted->Ptr() : nullptr;
2525
}
2626

2727
template <class T> T *As()
2828
{
2929
auto casted = Upcast<T>();
30-
return casted ? casted->Typed() : nullptr;
30+
return casted ? casted->Ptr() : nullptr;
3131
}
3232

33-
virtual const void *Raw() const = 0;
34-
virtual void *Raw() = 0;
33+
virtual const void *RawPtr() const = 0;
34+
virtual void *RawPtr() = 0;
3535

3636
virtual ~ICacheItem(){};
3737
};
@@ -42,32 +42,32 @@ namespace sd
4242
T _value;
4343

4444
public:
45-
using Ptr = std::unique_ptr<CacheItem<T>>;
45+
using UPtr = std::unique_ptr<CacheItem<T>>;
4646

47-
static Ptr Make(T &&value) { return Ptr(new CacheItem<T>(std::move(value))); }
47+
static UPtr Make(T &&value) { return UPtr(new CacheItem<T>(std::move(value))); }
4848

4949
CacheItem(T &&value) : _value(std::move(value)) {}
5050

51-
const void *Raw() const { return &_value; }
51+
const void *RawPtr() const { return &_value; }
5252

53-
void *Raw() { return &_value; }
53+
void *RawPtr() { return &_value; }
5454

55-
const T *Typed() const { return &_value; }
55+
const T *Ptr() const { return &_value; }
5656

57-
T *Typed() { return &_value; }
57+
T *Ptr() { return &_value; }
5858

59-
const T &operator->() const { return _value; }
59+
const T *operator->() const { return &_value; }
6060

6161
const T &operator*() const { return _value; }
6262

63-
T &operator->() { return _value; }
63+
T *operator->() { return &_value; }
6464

6565
T &operator*() { return _value; }
6666
};
6767

6868
struct ICachePolicy
6969
{
70-
using Ptr = std::unique_ptr<ICachePolicy>;
70+
using UPtr = std::unique_ptr<ICachePolicy>;
7171

7272
virtual void CallOnRemove(const ICacheItem *value) const = 0;
7373
virtual void CallOnUpdate(const ICacheItem *oldValue, const ICacheItem *newValue) const = 0;
@@ -86,11 +86,11 @@ namespace sd
8686
RemoveCallback _removeCallback;
8787

8888
public:
89-
using Ptr = std::unique_ptr<CachePolicy<TValue>>;
89+
using UPtr = std::unique_ptr<CachePolicy<TValue>>;
9090

91-
Ptr static Make(UpdateCallback updateCallback = nullptr, RemoveCallback removeCallback = nullptr)
91+
UPtr static Make(UpdateCallback updateCallback = nullptr, RemoveCallback removeCallback = nullptr)
9292
{
93-
return Ptr(new CachePolicy<TValue>(updateCallback, removeCallback));
93+
return UPtr(new CachePolicy<TValue>(updateCallback, removeCallback));
9494
}
9595

9696
CachePolicy(UpdateCallback updateCallback = nullptr, RemoveCallback removeCallback = nullptr)
@@ -121,9 +121,9 @@ namespace sd
121121

122122
struct ICache
123123
{
124-
virtual bool Add(const std::string &key, ICacheItem::Ptr value, ICachePolicy::Ptr policy = nullptr) = 0;
124+
virtual bool Add(const std::string &key, ICacheItem::UPtr value, ICachePolicy::UPtr policy = nullptr) = 0;
125125

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

128128
virtual const void *Get(const std::string &key) const = 0;
129129

@@ -143,8 +143,8 @@ namespace sd
143143
private:
144144
struct Data
145145
{
146-
ICacheItem::Ptr item;
147-
ICachePolicy::Ptr policy;
146+
ICacheItem::UPtr item;
147+
ICachePolicy::UPtr policy;
148148
};
149149

150150
std::unordered_map<std::string, Data> _items;
@@ -157,35 +157,35 @@ namespace sd
157157
Cache &operator=(const Cache &) = delete;
158158
Cache &operator=(Cache &&) = delete;
159159

160-
template <class T> bool Add(const std::string &key, T &&value, typename CachePolicy<T>::Ptr policy = nullptr)
160+
template <class T> bool Add(const std::string &key, T &&value, typename CachePolicy<T>::UPtr policy = nullptr)
161161
{
162162
return Add<T>(key, CacheItem<T>::Make(std::move(value)), std::move(policy));
163163
}
164164

165165
template <class T>
166-
bool Add(const std::string &key, typename CacheItem<T>::Ptr itemPtr,
167-
typename CachePolicy<T>::Ptr policy = nullptr)
166+
bool Add(const std::string &key, typename CacheItem<T>::UPtr itemPtr,
167+
typename CachePolicy<T>::UPtr policy = nullptr)
168168
{
169-
ICacheItem::Ptr itemPtrCasted = std::move(itemPtr);
169+
ICacheItem::UPtr itemPtrCasted = std::move(itemPtr);
170170
return Add(key, std::move(itemPtrCasted), std::move(policy));
171171
}
172172

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

175-
template <class T> bool Set(const std::string &key, T &&value, typename CachePolicy<T>::Ptr policy = nullptr)
175+
template <class T> bool Set(const std::string &key, T &&value, typename CachePolicy<T>::UPtr policy = nullptr)
176176
{
177177
return Set<T>(key, CacheItem<T>::Make(std::move(value)), std::move(policy));
178178
}
179179

180180
template <class T>
181-
bool Set(const std::string &key, typename CacheItem<T>::Ptr itemPtr,
182-
typename CachePolicy<T>::Ptr policy = nullptr)
181+
bool Set(const std::string &key, typename CacheItem<T>::UPtr itemPtr,
182+
typename CachePolicy<T>::UPtr policy = nullptr)
183183
{
184-
ICacheItem::Ptr itemPtrCasted = std::move(itemPtr);
184+
ICacheItem::UPtr itemPtrCasted = std::move(itemPtr);
185185
return Set(key, std::move(itemPtrCasted), std::move(policy));
186186
}
187187

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

190190
template <class TValue> const TValue *Get(const std::string &key) const
191191
{
@@ -238,13 +238,13 @@ namespace sd
238238
CacheWrapper &operator=(CacheWrapper &&) = delete;
239239

240240
template <class TValue>
241-
bool Add(const std::string &key, TValue &&value, typename CachePolicy<TValue>::Ptr policy = nullptr)
241+
bool Add(const std::string &key, TValue &&value, typename CachePolicy<TValue>::UPtr policy = nullptr)
242242
{
243243
return _cache.Add<TValue>(BuildKey<TValue>(key), std::move(value), std::move(policy));
244244
}
245245

246246
template <class TValue>
247-
bool Set(const std::string &key, TValue &&value, typename CachePolicy<TValue>::Ptr policy = nullptr)
247+
bool Set(const std::string &key, TValue &&value, typename CachePolicy<TValue>::UPtr policy = nullptr)
248248
{
249249
return _cache.Set<TValue>(BuildKey<TValue>(key), std::move(value), std::move(policy));
250250
}

Tests/CacheTest.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -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")->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);
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);
137137
EXPECT_FALSE(cache.GetItem<int>("bool"));
138138
}
139139

0 commit comments

Comments
 (0)