Skip to content

Commit 876d0b7

Browse files
author
alexeykovalev
committed
feat postgresql: add static assertions for custom containers in PostgreCache
Add static assertions for custom container in PostgreCache to check if it has `size` and `insert_or_assign`-like methods commit_hash:8bf2ebbf98c3036ac82f49450f5b1d9e7d9a2c6b
1 parent e58db4f commit 876d0b7

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

postgresql/include/userver/cache/base_postgres_cache.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ namespace components {
101101
///
102102
/// In case one provides a custom CacheContainer within Policy, it is notified
103103
/// of Update completion via its public member function OnWritesDone, if any.
104+
/// Custom CacheContainer must provide size method and insert_or_assign method
105+
/// similar to std::unordered_map's one or CacheInsertOrAssign function similar
106+
/// to one defined in namespace utils::impl::projected_set (i.e. used for
107+
/// utils::ProjectedUnorderedSet).
104108
/// See the following code snippet for an example of usage:
105109
///
106110
/// @snippet cache/postgres_cache_test.cpp Pg Cache Policy Custom Container With Write Notification Example
@@ -193,6 +197,33 @@ inline constexpr bool kHasKeyMember = meta::IsDetected<KeyMemberTypeImpl, T>;
193197
template <typename T>
194198
using KeyMemberType = meta::DetectedType<KeyMemberTypeImpl, T>;
195199

200+
// size method in custom container in policy
201+
template <typename T>
202+
using SizeMethodInvokeResultImpl = decltype(std::declval<T>().size());
203+
template <typename T>
204+
inline constexpr bool kHasSizeMethod = meta::IsDetected<SizeMethodInvokeResultImpl, T> &&
205+
std::is_convertible_v<SizeMethodInvokeResultImpl<T>, std::size_t>;
206+
207+
// insert_or_assign method in custom container in policy
208+
template <typename T>
209+
using InsertOrAssignMethodInvokeResultImpl = decltype(std::declval<typename T::CacheContainer>().insert_or_assign(
210+
std::declval<KeyMemberTypeImpl<T>>(),
211+
std::declval<ValueType<T>>()
212+
));
213+
template <typename T>
214+
inline constexpr bool kHasInsertOrAssignMethod = meta::IsDetected<InsertOrAssignMethodInvokeResultImpl, T>;
215+
216+
// CacheInsertOrAssign function for custom container in policy
217+
template <typename T>
218+
using CacheInsertOrAssignFunctionInvokeResultImpl = decltype(CacheInsertOrAssign(
219+
std::declval<typename T::CacheContainer&>(),
220+
std::declval<ValueType<T>>(),
221+
std::declval<KeyMemberTypeImpl<T>>()
222+
));
223+
template <typename T>
224+
inline constexpr bool kHasCacheInsertOrAssignFunction =
225+
meta::IsDetected<CacheInsertOrAssignFunctionInvokeResultImpl, T>;
226+
196227
// Data container for cache
197228
template <typename T, typename = USERVER_NAMESPACE::utils::void_t<>>
198229
struct DataCacheContainer {
@@ -206,6 +237,13 @@ struct DataCacheContainer {
206237

207238
template <typename T>
208239
struct DataCacheContainer<T, USERVER_NAMESPACE::utils::void_t<typename T::CacheContainer>> {
240+
static_assert(kHasSizeMethod<typename T::CacheContainer>, "Custom CacheContainer must provide `size` method");
241+
static_assert(
242+
kHasInsertOrAssignMethod<T> || kHasCacheInsertOrAssignFunction<T>,
243+
"Custom CacheContainer must provide `insert_or_assign` method similar to std::unordered_map's "
244+
"one or CacheInsertOrAssign function"
245+
);
246+
209247
using type = typename T::CacheContainer;
210248
};
211249

0 commit comments

Comments
 (0)