8
8
namespace sd
9
9
{
10
10
11
- template <class TValue > class CacheItem ;
12
- struct CacheItemBase
11
+ template <class T > class CacheItem ;
12
+
13
+ struct ICacheItem
13
14
{
14
- using Ptr = std::unique_ptr<CacheItemBase>;
15
+ using Ptr = std::unique_ptr<ICacheItem>;
16
+
17
+ template <class T > const CacheItem<T> *Upcast () const { return dynamic_cast <const CacheItem<T> *>(this ); }
15
18
16
- virtual const std::string &GetKey () const = 0;
17
- virtual const void *GetValue () const = 0;
19
+ template <class T > CacheItem<T> *Upcast () { return dynamic_cast <CacheItem<T> *>(this ); }
18
20
19
- template <class TValue > const CacheItem<TValue> * Upcast () const
21
+ template <class T > const T * As () const
20
22
{
21
- return dynamic_cast <const CacheItem<TValue> *>(this );
23
+ auto casted = Upcast<T>();
24
+ return casted ? casted->Typed () : nullptr ;
22
25
}
23
26
24
- template <class TValue > const TValue * GetValueAs () const
27
+ template <class T > T * As ()
25
28
{
26
- if (auto casted = Upcast<TValue>())
27
- {
28
- return casted->GetTypedValue ();
29
- }
30
- return nullptr ;
29
+ auto casted = Upcast<T>();
30
+ return casted ? casted->Typed () : nullptr ;
31
31
}
32
32
33
- virtual ~CacheItemBase (){};
33
+ virtual const void *Raw () const = 0;
34
+ virtual void *Raw () = 0;
35
+
36
+ virtual ~ICacheItem (){};
34
37
};
35
38
36
- template <class TValue > class CacheItem final : public CacheItemBase
39
+ template <class T > struct CacheItem : public ICacheItem
37
40
{
38
41
private:
39
- std::string _key;
40
- TValue _value;
42
+ T _value;
41
43
42
44
public:
43
- using Ptr = std::unique_ptr<CacheItem<TValue >>;
45
+ using Ptr = std::unique_ptr<CacheItem<T >>;
44
46
45
- static Ptr Make (const std::string &key, TValue &&value)
46
- {
47
- return Ptr (new CacheItem<TValue>(key, std::move (value)));
48
- }
47
+ static Ptr Make (T &&value) { return Ptr (new CacheItem<T>(std::move (value))); }
48
+
49
+ CacheItem (T &&value) : _value(std::move(value)) {}
50
+
51
+ const void *Raw () const { return &_value; }
52
+
53
+ void *Raw () { return &_value; }
49
54
50
- CacheItem ( const std::string &key, TValue &&value) : _key(key), _value(value) { }
55
+ const T * Typed () const { return & _value; }
51
56
52
- const std::string &GetKey () const final { return _key; }
53
- const void *GetValue () const final { return GetTypedValue (); }
54
- const TValue *GetTypedValue () const { return &_value; }
57
+ T *Typed () { return &_value; }
58
+
59
+ const T &operator ->() const { return _value; }
60
+
61
+ const T &operator *() const { return _value; }
62
+
63
+ T &operator ->() { return _value; }
64
+
65
+ T &operator *() { return _value; }
55
66
};
56
67
57
68
struct ICachePolicy
58
69
{
59
70
using Ptr = std::unique_ptr<ICachePolicy>;
60
71
61
- virtual void CallOnRemove (const CacheItemBase *value) const = 0;
62
- virtual void CallOnUpdate (const CacheItemBase *oldValue, const CacheItemBase *newValue) const = 0;
72
+ virtual void CallOnRemove (const ICacheItem *value) const = 0;
73
+ virtual void CallOnUpdate (const ICacheItem *oldValue, const ICacheItem *newValue) const = 0;
63
74
64
75
virtual ~ICachePolicy () {}
65
76
};
@@ -91,32 +102,32 @@ namespace sd
91
102
92
103
void SetOnRemoveCallback (RemoveCallback removeCallback) { _removeCallback = removeCallback; }
93
104
94
- void CallOnRemove (const CacheItemBase *item ) const final
105
+ void CallOnRemove (const ICacheItem *value ) const final
95
106
{
96
- if (_removeCallback && item )
107
+ if (_removeCallback && value )
97
108
{
98
- _removeCallback (item-> GetValueAs <TValue>());
109
+ _removeCallback (value-> As <TValue>());
99
110
}
100
111
}
101
112
102
- void CallOnUpdate (const CacheItemBase *oldItem , const CacheItemBase *newItem ) const final
113
+ void CallOnUpdate (const ICacheItem *oldValue , const ICacheItem *newValue ) const final
103
114
{
104
- if (_updateCallback && oldItem && newItem )
115
+ if (_updateCallback && oldValue && newValue )
105
116
{
106
- _updateCallback (oldItem-> GetValueAs <TValue>(), newItem-> GetValueAs <TValue>());
117
+ _updateCallback (oldValue-> As <TValue>(), newValue-> As <TValue>());
107
118
}
108
119
}
109
120
};
110
121
111
122
struct ICache
112
123
{
113
- virtual bool Add (CacheItemBase:: Ptr itemPtr , ICachePolicy::Ptr policy = nullptr ) = 0;
124
+ virtual bool Add (const std::string &key, ICacheItem:: Ptr value , ICachePolicy::Ptr policy = nullptr ) = 0;
114
125
115
- virtual bool Set (CacheItemBase:: Ptr itemPtr , ICachePolicy::Ptr policy = nullptr ) = 0;
126
+ virtual bool Set (const std::string &key, ICacheItem:: Ptr value , ICachePolicy::Ptr policy = nullptr ) = 0;
116
127
117
128
virtual const void *Get (const std::string &key) const = 0;
118
129
119
- virtual const CacheItemBase *GetItem (const std::string &key) const = 0;
130
+ virtual const ICacheItem *GetItem (const std::string &key) const = 0;
120
131
121
132
virtual bool Remove (const std::string &key) = 0;
122
133
@@ -132,7 +143,7 @@ namespace sd
132
143
private:
133
144
struct Data
134
145
{
135
- CacheItemBase ::Ptr item;
146
+ ICacheItem ::Ptr item;
136
147
ICachePolicy::Ptr policy;
137
148
};
138
149
@@ -146,40 +157,40 @@ namespace sd
146
157
Cache &operator =(const Cache &) = delete ;
147
158
Cache &operator =(Cache &&) = delete ;
148
159
149
- template <class TValue >
150
- bool Add (const std::string &key, TValue &&value, typename CachePolicy<TValue>::Ptr policy = nullptr )
160
+ template <class T > bool Add (const std::string &key, T &&value, typename CachePolicy<T>::Ptr policy = nullptr )
151
161
{
152
- return Add<TValue>( CacheItem<TValue >::Make (key, std::move (value)), std::move (policy));
162
+ return Add<T>(key, CacheItem<T >::Make (std::move (value)), std::move (policy));
153
163
}
154
164
155
- template <class TValue >
156
- bool Add (typename CacheItem<TValue>::Ptr itemPtr, typename CachePolicy<TValue>::Ptr policy = nullptr )
165
+ template <class T >
166
+ bool Add (const std::string &key, typename CacheItem<T>::Ptr itemPtr,
167
+ typename CachePolicy<T>::Ptr policy = nullptr )
157
168
{
158
- CacheItemBase ::Ptr itemPtrCasted = std::move (itemPtr);
159
- return Add (std::move (itemPtrCasted), std::move (policy));
169
+ ICacheItem ::Ptr itemPtrCasted = std::move (itemPtr);
170
+ return Add (key, std::move (itemPtrCasted), std::move (policy));
160
171
}
161
172
162
- bool Add (CacheItemBase ::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr ) final ;
173
+ bool Add (const std::string &key, ICacheItem ::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr ) final ;
163
174
164
- template <class TValue >
165
- bool Set (const std::string &key, TValue &&value, typename CachePolicy<TValue>::Ptr policy = nullptr )
175
+ template <class T > bool Set (const std::string &key, T &&value, typename CachePolicy<T>::Ptr policy = nullptr )
166
176
{
167
- return Set<TValue>( CacheItem<TValue >::Make (key, std::move (value)), std::move (policy));
177
+ return Set<T>(key, CacheItem<T >::Make (std::move (value)), std::move (policy));
168
178
}
169
179
170
- template <class TValue >
171
- bool Set (typename CacheItem<TValue>::Ptr itemPtr, typename CachePolicy<TValue>::Ptr policy = nullptr )
180
+ template <class T >
181
+ bool Set (const std::string &key, typename CacheItem<T>::Ptr itemPtr,
182
+ typename CachePolicy<T>::Ptr policy = nullptr )
172
183
{
173
- CacheItemBase ::Ptr itemPtrCasted = std::move (itemPtr);
174
- return Set (std::move (itemPtrCasted), std::move (policy));
184
+ ICacheItem ::Ptr itemPtrCasted = std::move (itemPtr);
185
+ return Set (key, std::move (itemPtrCasted), std::move (policy));
175
186
}
176
187
177
- bool Set (CacheItemBase ::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr ) final ;
188
+ bool Set (const std::string &key, ICacheItem ::Ptr itemPtr, ICachePolicy::Ptr policy = nullptr ) final ;
178
189
179
190
template <class TValue > const TValue *Get (const std::string &key) const
180
191
{
181
192
auto item = GetItem (key);
182
- return item ? item->GetValueAs <TValue>() : nullptr ;
193
+ return item ? item->As <TValue>() : nullptr ;
183
194
}
184
195
185
196
const void *Get (const std::string &key) const final ;
@@ -190,7 +201,7 @@ namespace sd
190
201
return item ? item->Upcast <TValue>() : nullptr ;
191
202
}
192
203
193
- const CacheItemBase *GetItem (const std::string &key) const final ;
204
+ const ICacheItem *GetItem (const std::string &key) const final ;
194
205
195
206
bool Remove (const std::string &key) final ;
196
207
@@ -199,7 +210,7 @@ namespace sd
199
210
size_t Count () const final ;
200
211
201
212
private:
202
- bool AddData (Data data);
213
+ bool AddData (const std::string &key, Data data);
203
214
204
215
Data *GetEditableData (const std::string &key);
205
216
0 commit comments