Skip to content

Commit 713ad02

Browse files
committed
code refactor
1 parent c09ab8e commit 713ad02

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

Source/Library/Cache.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@
33

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

15-
bool Cache::Set(CacheItemBase::Ptr itemPtr, ICachePolicy::Ptr newPolicy)
15+
bool Cache::Set(CacheItemBase::Ptr item, ICachePolicy::Ptr newPolicy)
1616
{
17-
if (!itemPtr)
17+
if (!item)
1818
{
1919
return false;
2020
}
21-
auto data = GetEditableData(itemPtr->GetKey());
21+
auto data = GetEditableData(item->GetKey());
2222
if (!data)
2323
{
2424
return false;
2525
}
26-
std::swap(data->item, itemPtr);
27-
auto &oldItem = itemPtr;
26+
std::swap(data->item, item);
27+
auto &oldItem = item;
2828
auto &newItem = data->item;
2929
auto &oldPolicy = data->policy;
3030
if (oldPolicy && oldItem && newItem)

Source/Library/h/Cache.hpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ namespace sd
2323

2424
template <class TValue> const TValue *GetValueAs() const
2525
{
26-
if (auto casted = Upcast<TValue>())
27-
{
28-
return casted->GetValue();
29-
}
30-
return nullptr;
26+
auto casted = Upcast<TValue>();
27+
return casted ? casted->GetValue() : nullptr;
3128
}
3229

3330
virtual ~CacheItemBase(){};
@@ -47,7 +44,7 @@ namespace sd
4744
return Ptr(new CacheItem<TValue>(key, std::move(value)));
4845
}
4946

50-
CacheItem(const std::string &key, TValue &&value) : _key(key), _value(value) {}
47+
CacheItem(const std::string &key, TValue &&value) : _key(key), _value(std::move(value)) {}
5148

5249
const std::string &GetKey() const final { return _key; }
5350
const void *GetRawValue() const final { return GetValue(); }
@@ -110,9 +107,9 @@ namespace sd
110107

111108
struct ICache
112109
{
113-
virtual bool Add(CacheItemBase::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr) = 0;
110+
virtual bool Add(CacheItemBase::Ptr item, ICachePolicy::Ptr policy = nullptr) = 0;
114111

115-
virtual bool Set(CacheItemBase::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr) = 0;
112+
virtual bool Set(CacheItemBase::Ptr item, ICachePolicy::Ptr policy = nullptr) = 0;
116113

117114
virtual const void *Get(const std::string &key) const = 0;
118115

@@ -153,13 +150,13 @@ namespace sd
153150
}
154151

155152
template <class TValue>
156-
bool Add(typename CacheItem<TValue>::Ptr itemPtr, typename CachePolicy<TValue>::Ptr policy = nullptr)
153+
bool Add(typename CacheItem<TValue>::Ptr item, typename CachePolicy<TValue>::Ptr policy = nullptr)
157154
{
158-
CacheItemBase::Ptr itemPtrCasted = std::move(itemPtr);
159-
return Add(std::move(itemPtrCasted), std::move(policy));
155+
CacheItemBase::Ptr itemCasted = std::move(item);
156+
return Add(std::move(itemCasted), std::move(policy));
160157
}
161158

162-
bool Add(CacheItemBase::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr) final;
159+
bool Add(CacheItemBase::Ptr item, ICachePolicy::Ptr policy = nullptr) final;
163160

164161
template <class TValue>
165162
bool Set(const std::string &key, TValue &&value, typename CachePolicy<TValue>::Ptr policy = nullptr)
@@ -168,13 +165,13 @@ namespace sd
168165
}
169166

170167
template <class TValue>
171-
bool Set(typename CacheItem<TValue>::Ptr itemPtr, typename CachePolicy<TValue>::Ptr policy = nullptr)
168+
bool Set(typename CacheItem<TValue>::Ptr item, typename CachePolicy<TValue>::Ptr policy = nullptr)
172169
{
173-
CacheItemBase::Ptr itemPtrCasted = std::move(itemPtr);
174-
return Set(std::move(itemPtrCasted), std::move(policy));
170+
CacheItemBase::Ptr itemCasted = std::move(item);
171+
return Set(std::move(itemCasted), std::move(policy));
175172
}
176173

177-
bool Set(CacheItemBase::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr) final;
174+
bool Set(CacheItemBase::Ptr item, ICachePolicy::Ptr policy = nullptr) final;
178175

179176
template <class TValue> const TValue *Get(const std::string &key) const
180177
{

0 commit comments

Comments
 (0)