Stroika Library 3.0d24
 
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 * \note the KEY type NEED NOT be equality_comparable, as cache classes (liked TimedCache) may allow specification of the comparer
32 * through TRAITS, or other means.
33 */
34 template <typename T>
35 concept IKey = same_as<T, NonKeyedKeySentinalType> or copyable<T>;
36
37 /**
38 * @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).
39 */
40 template <typename KEY>
41 static constexpr bool IKeyedCache = not same_as<KEY, NonKeyedKeySentinalType>;
42
43 /**
44 * @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.
45 *
46 * 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).
47 */
49
50 /**
51 * @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).
52 */
53 template <typename T>
54 concept IValue = same_as<T, ValuelessSentinalType> or copyable<T>;
55
56 /**
57 * @brief Some caches (rare) only support a KEY, with no associated value (the value is stored INSIDE the key itself)
58 *
59 * \par Example Usage:
60 * \code
61 * \endcode
62 */
63 template <typename VALUE>
64 static constexpr bool IValuelessCache = same_as<VALUE, ValuelessSentinalType>;
65
66 /**
67 * @brief Check if the argument CACHE is a valid 'stroika cache' class, following its api
68 *
69 * \note ICache allows for both Valueless (IValuelessCache) and Keyless (IKeyedCache) caches, but some cache implementations may not support
70 * one or the other.
71 *
72 * Also, this API/Interface does NOT support BloomFilters, because they don't have the lookup () function
73 * (due to false positives).
74 */
75 template <typename CACHE, typename KEY, typename VALUE>
76 concept ICache = IKey<KEY> and IValue<VALUE> and not(not IKeyedCache<KEY> and IValuelessCache<VALUE>) and
77 ( // IKeyedCache case constraints
78 (IKeyedCache<KEY> and ((IValuelessCache<VALUE> and
79 requires (CACHE c, KEY k) {
80 { c.Add (k) };
81 { c.Lookup (k) } -> convertible_to<optional<KEY>>;
82 { c.LookupValue (k, function<VALUE (KEY)>{}) } -> convertible_to<KEY>;
83 }) or
84 (not IValuelessCache<VALUE> and
85 requires (CACHE c, KEY k, VALUE v) {
86 { c.Add (k, v) };
87 { c.Lookup (k) } -> convertible_to<optional<VALUE>>;
88 { c.LookupValue (k, function<VALUE (KEY)>{}) } -> convertible_to<VALUE>;
89 })))
90 // not IKeyedCache case constraints
91 or (not IKeyedCache<KEY> and requires (CACHE c, VALUE v) {
92 { c.Add (v) };
93 { c.Lookup () } -> convertible_to<optional<VALUE>>;
94 { c.LookupValue (function<VALUE ()>{}) } -> convertible_to<VALUE>;
95 }));
96
97}
98
99/*
100 ********************************************************************************
101 ***************************** Implementation Details ***************************
102 ********************************************************************************
103 */
104
105#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...