8
8
namespace sd
9
9
{
10
10
11
- template <class T > class CacheItem ;
12
-
13
- struct ICacheItem
11
+ template <class TValue > class CacheItem ;
12
+ struct CacheItemBase
14
13
{
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>;
18
15
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;
20
18
21
- template <class T > const T * As () const
19
+ template <class TValue > const CacheItem<TValue> * Upcast () const
22
20
{
23
- auto casted = Upcast<T>();
24
- return casted ? casted->Ptr () : nullptr ;
21
+ return dynamic_cast <const CacheItem<TValue> *>(this );
25
22
}
26
23
27
- template <class T > T * As ()
24
+ template <class TValue > const TValue * GetValueAs () const
28
25
{
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 ;
31
31
}
32
32
33
- virtual const void *RawPtr () const = 0;
34
- virtual void *RawPtr () = 0;
35
-
36
- virtual ~ICacheItem (){};
33
+ virtual ~CacheItemBase (){};
37
34
};
38
35
39
- template <class T > struct CacheItem : public ICacheItem
36
+ template <class TValue > class CacheItem final : public CacheItemBase
40
37
{
41
38
private:
42
- T _value;
39
+ std::string _key;
40
+ TValue _value;
43
41
44
42
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>>;
54
44
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
+ }
62
49
63
- T * operator ->() { return & _value; }
50
+ CacheItem ( const std::string &key, TValue &&value) : _key(key), _value(value) { }
64
51
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; }
66
55
};
67
56
68
57
struct ICachePolicy
69
58
{
70
- using UPtr = std::unique_ptr<ICachePolicy>;
59
+ using Ptr = std::unique_ptr<ICachePolicy>;
71
60
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;
74
63
75
64
virtual ~ICachePolicy () {}
76
65
};
@@ -86,11 +75,11 @@ namespace sd
86
75
RemoveCallback _removeCallback;
87
76
88
77
public:
89
- using UPtr = std::unique_ptr<CachePolicy<TValue>>;
78
+ using Ptr = std::unique_ptr<CachePolicy<TValue>>;
90
79
91
- UPtr static Make (UpdateCallback updateCallback = nullptr , RemoveCallback removeCallback = nullptr )
80
+ Ptr static Make (UpdateCallback updateCallback = nullptr , RemoveCallback removeCallback = nullptr )
92
81
{
93
- return UPtr (new CachePolicy<TValue>(updateCallback, removeCallback));
82
+ return Ptr (new CachePolicy<TValue>(updateCallback, removeCallback));
94
83
}
95
84
96
85
CachePolicy (UpdateCallback updateCallback = nullptr , RemoveCallback removeCallback = nullptr )
@@ -102,32 +91,32 @@ namespace sd
102
91
103
92
void SetOnRemoveCallback (RemoveCallback removeCallback) { _removeCallback = removeCallback; }
104
93
105
- void CallOnRemove (const ICacheItem *value ) const final
94
+ void CallOnRemove (const CacheItemBase *item ) const final
106
95
{
107
- if (_removeCallback && value )
96
+ if (_removeCallback && item )
108
97
{
109
- _removeCallback (value-> As <TValue>());
98
+ _removeCallback (item-> GetValueAs <TValue>());
110
99
}
111
100
}
112
101
113
- void CallOnUpdate (const ICacheItem *oldValue , const ICacheItem *newValue ) const final
102
+ void CallOnUpdate (const CacheItemBase *oldItem , const CacheItemBase *newItem ) const final
114
103
{
115
- if (_updateCallback && oldValue && newValue )
104
+ if (_updateCallback && oldItem && newItem )
116
105
{
117
- _updateCallback (oldValue-> As <TValue>(), newValue-> As <TValue>());
106
+ _updateCallback (oldItem-> GetValueAs <TValue>(), newItem-> GetValueAs <TValue>());
118
107
}
119
108
}
120
109
};
121
110
122
111
struct ICache
123
112
{
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;
125
114
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;
127
116
128
117
virtual const void *Get (const std::string &key) const = 0;
129
118
130
- virtual const ICacheItem *GetItem (const std::string &key) const = 0;
119
+ virtual const CacheItemBase *GetItem (const std::string &key) const = 0;
131
120
132
121
virtual bool Remove (const std::string &key) = 0;
133
122
@@ -143,8 +132,8 @@ namespace sd
143
132
private:
144
133
struct Data
145
134
{
146
- ICacheItem::UPtr item;
147
- ICachePolicy::UPtr policy;
135
+ CacheItemBase:: Ptr item;
136
+ ICachePolicy::Ptr policy;
148
137
};
149
138
150
139
std::unordered_map<std::string, Data> _items;
@@ -157,40 +146,40 @@ namespace sd
157
146
Cache &operator =(const Cache &) = delete ;
158
147
Cache &operator =(Cache &&) = delete ;
159
148
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 )
161
151
{
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));
163
153
}
164
154
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 )
168
157
{
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));
171
160
}
172
161
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 ;
174
163
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 )
176
166
{
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));
178
168
}
179
169
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 )
183
172
{
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));
186
175
}
187
176
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 ;
189
178
190
179
template <class TValue > const TValue *Get (const std::string &key) const
191
180
{
192
181
auto item = GetItem (key);
193
- return item ? item->As <TValue>() : nullptr ;
182
+ return item ? item->GetValueAs <TValue>() : nullptr ;
194
183
}
195
184
196
185
const void *Get (const std::string &key) const final ;
@@ -201,7 +190,7 @@ namespace sd
201
190
return item ? item->Upcast <TValue>() : nullptr ;
202
191
}
203
192
204
- const ICacheItem *GetItem (const std::string &key) const final ;
193
+ const CacheItemBase *GetItem (const std::string &key) const final ;
205
194
206
195
bool Remove (const std::string &key) final ;
207
196
@@ -210,7 +199,7 @@ namespace sd
210
199
size_t Count () const final ;
211
200
212
201
private:
213
- bool AddData (const std::string &key, Data data);
202
+ bool AddData (Data data);
214
203
215
204
Data *GetEditableData (const std::string &key);
216
205
@@ -238,13 +227,13 @@ namespace sd
238
227
CacheWrapper &operator =(CacheWrapper &&) = delete ;
239
228
240
229
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 )
242
231
{
243
232
return _cache.Add <TValue>(BuildKey<TValue>(key), std::move (value), std::move (policy));
244
233
}
245
234
246
235
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 )
248
237
{
249
238
return _cache.Set <TValue>(BuildKey<TValue>(key), std::move (value), std::move (policy));
250
239
}
0 commit comments