Stroika Library 3.0d23
 
Loading...
Searching...
No Matches
Foundation/Cache/Common.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Cache_Common_h_
5#define _Stroika_Foundation_Cache_Common_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <functional>
10#include <optional>
11#include <type_traits>
12
13#include "Stroika/Foundation/Common/Common.h"
14#include "Stroika/Foundation/Common/Concepts.h"
16
17/**
18 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
19 */
20
22
23 /**
24 * @brief This sentinal value can be used as the KEY type for a Cache to indicate it is un-keyed, and there is just one.
25 */
27
28 /**
29 * @brief A KEY is any copyable value (or the sentinal type void - indicating a keyless - single valued - cache)
30 */
31 template <typename T>
32 concept IKey = same_as<T, NonKeyedKeySentinalType> or copyable<T>;
33
34 /**
35 * @brief does this cache have a KEY type (overwhelming YES, but sometimes handy to have 'singleton' cache, where you cache something, but just one of them)
36 */
37 template <typename KEY>
38 static constexpr bool IKeyedCache = not same_as<KEY, NonKeyedKeySentinalType>;
39
40 /**
41 * @brief This sentinal value can be used as the VALUE type for a Cache to indicate it just stores ONLY the KEYS (and sometimes related time/expiration) information.
42 *
43 * Also, this can be used like KeyedCollection - where the main object acts like a KEY and value at the same time (often just internally has a KEY field).
44 */
46
47 /**
48 * @brief any copyable type can use used as the value, or the special sentinal type - ValuelessSentinalType, used to indicate we are just caching presence/absense of the KEY in the cache (and its expiration date).
49 */
50 template <typename T>
51 concept IValue = same_as<T, ValuelessSentinalType> or copyable<T>;
52
53 /**
54 * @brief Some caches (rare) only support a KEY, with no associated value (the value is stored INSIDE the key itself)
55 *
56 * \par Example Usage:
57 * \code
58 * \endcode
59 */
60 template <typename VALUE>
61 static constexpr bool IValuelessCache = same_as<VALUE, ValuelessSentinalType>;
62
63 /**
64 * @brief Check if the argument CACHE is a valid 'stroika cache' class, following its api
65 *
66 * \note ICache allows for both Valueless (IValuelessCache) and Keyless (IKeyedCache) caches, but some cache implementations may not support
67 * one or the other.
68 *
69 * Also, this API/Interface does NOT support BloomFilters, because they don't have the lookup () function
70 * (due to false positives).
71 */
72 template <typename CACHE, typename KEY, typename VALUE>
73 concept ICache = IKey<KEY> and IValue<VALUE> and not(not IKeyedCache<KEY> and IValuelessCache<VALUE>) and
74 ( // IKeyedCache case constraints
75 (IKeyedCache<KEY> and ((IValuelessCache<VALUE> and
76 requires (CACHE c, KEY k) {
77 { c.Add (k) };
78 { c.Lookup (k) } -> convertible_to<optional<KEY>>;
79 { c.LookupValue (k, function<VALUE (KEY)>{}) } -> convertible_to<KEY>;
80 }) or
81 (not IValuelessCache<VALUE> and
82 requires (CACHE c, KEY k, VALUE v) {
83 { c.Add (k, v) };
84 { c.Lookup (k) } -> convertible_to<optional<VALUE>>;
85 { c.LookupValue (k, function<VALUE (KEY)>{}) } -> convertible_to<VALUE>;
86 })))
87 // not IKeyedCache case constraints
88 or (not IKeyedCache<KEY> and requires (CACHE c, VALUE v) {
89 { c.Add (v) };
90 { c.Lookup () } -> convertible_to<optional<VALUE>>;
91 { c.LookupValue (function<VALUE ()>{}) } -> convertible_to<VALUE>;
92 }));
93
94}
95
96/*
97 ********************************************************************************
98 ***************************** Implementation Details ***************************
99 ********************************************************************************
100 */
101
102#endif /*_Stroika_Foundation_Cache_Common_h_ */
Check if the argument CACHE is a valid 'stroika cache' class, following its api.
A KEY is any copyable value (or the sentinal type void - indicating a keyless - single valued - cache...
any copyable type can use used as the value, or the special sentinal type - ValuelessSentinalType,...
static constexpr bool IKeyedCache
does this cache have a KEY type (overwhelming YES, but sometimes handy to have 'singleton' cache,...
void NonKeyedKeySentinalType
This sentinal value can be used as the KEY type for a Cache to indicate it is un-keyed,...
static constexpr bool IValuelessCache
Some caches (rare) only support a KEY, with no associated value (the value is stored INSIDE the key i...
void ValuelessSentinalType
This sentinal value can be used as the VALUE type for a Cache to indicate it just stores ONLY the KEY...