Skip to content

Commit 05553f8

Browse files
committed
update methods
1 parent c48cfba commit 05553f8

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

Source/Library/Cache.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace sd
2626
return nullptr;
2727
}
2828

29-
bool Cache::Set(std::unique_ptr<ICacheItem> itemPtr, std::unique_ptr<ICachePolicy> policy)
29+
bool Cache::Set(std::unique_ptr<ICacheItem> itemPtr, std::unique_ptr<ICachePolicy> newPolicy)
3030
{
3131
if (!itemPtr)
3232
{
@@ -37,14 +37,17 @@ namespace sd
3737
{
3838
return false;
3939
}
40-
itemPtr.swap(data->item);
41-
if (data->policy && data->item && itemPtr)
40+
std::swap(itemPtr, data->item);
41+
auto &oldItem = itemPtr;
42+
auto &newItem = data->item;
43+
auto &oldPolicy = data->policy;
44+
if (oldPolicy && oldItem && newItem)
4245
{
43-
data->policy->CallOnUpdate(itemPtr->GetValue(), data->item->GetValue());
46+
oldPolicy->CallOnUpdate(oldItem->GetValue(), newItem->GetValue());
4447
}
45-
if (policy)
48+
if (newPolicy)
4649
{
47-
data->policy = std::move(policy);
50+
data->policy = std::move(newPolicy);
4851
}
4952
return true;
5053
}

Source/Library/h/Cache.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ namespace sd
125125
return Add(CacheItem<TValue>::Make(key, std::move(value)), std::move(policy));
126126
}
127127

128+
template <class TValue>
129+
bool Add(std::unique_ptr<CacheItem<TValue>> itemPtr, std::unique_ptr<CachePolicy<TValue>> policy = nullptr)
130+
{
131+
std::unique_ptr<ICacheItem> itemPtrCasted = std::move(itemPtr);
132+
return Add(std::move(itemPtrCasted), std::move(policy));
133+
}
134+
128135
bool Add(std::unique_ptr<ICacheItem> itemPtr, std::unique_ptr<ICachePolicy> policy = nullptr) final;
129136

130137
template <class TValue>
@@ -138,7 +145,8 @@ namespace sd
138145
const TValue *AddOrGetExisting(std::unique_ptr<CacheItem<TValue>> itemPtr,
139146
std::unique_ptr<CachePolicy<TValue>> policy = nullptr)
140147
{
141-
return static_cast<const TValue *>(AddOrGetExisting(std::move(itemPtr), std::move(policy)));
148+
std::unique_ptr<ICacheItem> itemPtrCasted = std::move(itemPtr);
149+
return static_cast<const TValue *>(AddOrGetExisting(std::move(itemPtrCasted), std::move(policy)));
142150
}
143151

144152
const void *AddOrGetExisting(std::unique_ptr<ICacheItem> itemPtr,
@@ -153,7 +161,8 @@ namespace sd
153161
template <class TValue>
154162
bool Set(std::unique_ptr<CacheItem<TValue>> itemPtr, std::unique_ptr<CachePolicy<TValue>> policy = nullptr)
155163
{
156-
return Set(std::move(itemPtr), std::move(policy));
164+
std::unique_ptr<ICacheItem> itemPtrCasted = std::move(itemPtr);
165+
return Set(std::move(itemPtrCasted), std::move(policy));
157166
}
158167

159168
bool Set(std::unique_ptr<ICacheItem> itemPtr, std::unique_ptr<ICachePolicy> policy = nullptr) final;

Tests/CacheTest.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <thread>
66
#include <utility>
77

8+
using namespace std::string_literals;
9+
810
class CacheTest : public ::testing::Test
911
{
1012
protected:
@@ -26,7 +28,7 @@ TEST_F(CacheTest, AddTest)
2628
sd::Cache cache;
2729

2830
auto intItem = sd::CacheItem<int>::Make("int", 12);
29-
auto stringItem = sd::CacheItem<std::string>::Make("string", std::string("hello"));
31+
auto stringItem = sd::CacheItem<std::string>::Make("string", "hello"s);
3032
auto boolItem = sd::CacheItem<bool>::Make("bool", false);
3133
auto boolItem2 = sd::CacheItem<bool>::Make("bool", true);
3234

@@ -41,7 +43,7 @@ TEST_F(CacheTest, AddDirectTest)
4143
sd::Cache cache;
4244

4345
EXPECT_TRUE(cache.Add("int", 12));
44-
EXPECT_TRUE(cache.Add("string", std::string("hello")));
46+
EXPECT_TRUE(cache.Add("string", "hello"s));
4547
EXPECT_TRUE(cache.Add("bool", true));
4648
EXPECT_FALSE(cache.Add("bool", false));
4749
}
@@ -51,7 +53,7 @@ TEST_F(CacheTest, CountTest)
5153
sd::Cache cache;
5254

5355
cache.Add("int", 12);
54-
cache.Add("string", std::string("hello"));
56+
cache.Add("string", "hello"s);
5557
cache.Add("bool", true);
5658
cache.Add("bool", false);
5759

@@ -63,7 +65,7 @@ TEST_F(CacheTest, ContainsTest)
6365
sd::Cache cache;
6466

6567
cache.Add("int", 12);
66-
cache.Add("string", std::string("hello"));
68+
cache.Add("string", "hello"s);
6769
cache.Add("bool", true);
6870
cache.Add("bool", false);
6971

@@ -85,7 +87,7 @@ TEST_F(CacheTest, GetTest)
8587
sd::Cache cache;
8688

8789
cache.Add("int", 12);
88-
cache.Add("string", std::string("hello"));
90+
cache.Add("string", "hello"s);
8991
cache.Add("bool", false);
9092

9193
EXPECT_EQ(*cache.Get<int>("int"), 12);
@@ -98,7 +100,7 @@ TEST_F(CacheTest, RemoveTest)
98100
sd::Cache cache;
99101

100102
cache.Add("int", 12);
101-
cache.Add("string", std::string("hello"));
103+
cache.Add("string", "hello"s);
102104
cache.Add("bool", false);
103105

104106
EXPECT_TRUE(cache.Remove("int"));

0 commit comments

Comments
 (0)