ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
valuedescriptor.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header file is part of module \alib_containers of the \aliblong.
4///
5/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_MONOMEM_CONTAINERS_VALUE_DESCRIPTOR
9#define HPP_ALIB_MONOMEM_CONTAINERS_VALUE_DESCRIPTOR 1
10#pragma once
11#if !defined(DOXYGEN)
12# include "alib/alib.hpp"
13#endif
14ALIB_ASSERT_MODULE(CONTAINERS)
15#include <utility>
16
17namespace alib::containers {
18
19/// Implements template type \p{TValueDescriptor}, which is, for example, offered by types
20/// \alib{containers;HashTable} and \alib{containers;LRUCacheTable}.
21///
22/// Specifically, this implementation is used when the custom type to be stored with a container
23/// should be associated to a \p{TKkey}-type, which is not included in the stored type itself.
24/// Consequently, to associate custom type \p{TMapped} with the key, the #StoredType
25/// results in <c>std::pair<TKey,TMapped</c>.
26///
27/// Type definitions \alib{containers;HashMap} and \alib{containers;LRUCacheMap}
28/// establish a shortcut to their corresponding base type, which incorporates this helper.
29/// @tparam TKey The key type.
30/// @tparam TMapped The type of the mapped objects.
31template<typename TKey, typename TMapped>
33{
34 /// The type stored in the container.<br>
35 /// Container types typically publish an alias to this type.
36 /// For example, see \alib{containers::LRUCacheTable;StoredType}, or
37 /// see \alib{containers::HashTable;StoredType}.
38 using StoredType = std::pair<TKey, TMapped>;
39
40 /// The key type. (Exposes template parameter \p{TKey}).<br>
41 /// Container types typically publish an alias to this type.
42 /// For example, see \alib{containers::LRUCacheTable;KeyType}, or
43 /// see \alib{containers::HashTable;KeyType}.
44 using KeyType = TKey;
45
46 /// The type associated to a key. (Exposes template parameter \p{TMapped}).
47 /// The type of the mapped portion of the data.<br>
48 /// Container types typically publish an alias to this type.
49 /// For example, see \alib{containers::LRUCacheTable;MappedType}, or
50 /// see \alib{containers::HashTable;MappedType}.
51 using MappedType = TMapped;
52
53 /// Returns the first element of the given <c>std::pair</c>.
54 /// @param src The value to extract from.
55 /// @return The key-portion of the given value.
56 TKey& Key(std::pair<TKey, TMapped>& src) const { return src.first; }
57
58 /// Returns the second element of the given <c>std::pair</c>.
59 /// @param src The value to extract from.
60 /// @return The mapped-portion of the given value.
61 TMapped& Mapped(std::pair<TKey, TMapped>& src) const { return src.second; }
62};
63
64/// Implements template type \p{TValueDescriptor}, which is, for example, offered by types
65/// \alib{containers;HashTable} and \alib{containers;LRUCacheTable}.
66///
67/// Specifically, this implementation is used with "sets", hence cases where the full portion
68/// of a type that is to be stored in a container should serve as the key to itself.
69///
70/// Type definitions \alib{containers;HashSet} and \alib{containers;LRUCacheSet}
71/// establish a shortcut to their corresponding base type, which incorporates this helper.
72///
73/// @tparam T The type stored in the container, serving likewise as the key-type.
74template<typename T>
76{
77 /// The type stored in the container.<br>
78 /// Container types typically publish an alias to this type.
79 /// For example, see \alib{containers::LRUCacheTable;StoredType}, or
80 /// see \alib{containers::HashTable;StoredType}.
81 using StoredType = T;
82
83 /// Exposes template parameter \p{T} and thus equals #StoredType and
84 /// #MappedType.<br>
85 /// Container types typically publish an alias to this type.
86 /// For example, see \alib{containers::LRUCacheTable;KeyType}, or
87 /// see \alib{containers::HashTable;KeyType}.
88 using KeyType = T;
89
90 /// Exposes template parameter \p{T} and thus equals #StoredType and
91 /// #KeyType.<br>
92 /// Container types typically publish an alias to this type.
93 /// For example, see \alib{containers::LRUCacheTable;MappedType}, or
94 /// see \alib{containers::HashTable;MappedType}.
95 using MappedType = T;
96
97 /// Returns the given \p{src} as is.
98 /// @param src The value to extract from.
99 /// @return The given reference.
100 KeyType& Key (T& src) const { return src; }
101
102 /// Returns the given \p{src} as is.
103 /// @param src The value to extract from.
104 /// @return The given reference.
105 MappedType& Mapped(T& src) const { return src; }
106};
107
108
109/// Implements template type \p{TValueDescriptor}, which is, for example, offered by types
110/// \alib{containers;HashTable} and \alib{containers;LRUCacheTable}.
111///
112/// Specifically, this implementation causes a container neither to add a specific key-type to
113/// every stored custom object nor to expect instances of a custom object as the key-type itself.
114/// Instead, only a subset of the custom type is used as the key-type.
115/// @tparam T The type stored in the container.
116/// @tparam TKey The key type which is to be extracted out of \p{T} by method #Key.
117template<typename T, typename TKey>
119{
120 /// Exposes template parameter \p{T}.<br>
121 /// Container types typically publish an alias to this type.
122 /// For example, see \alib{containers::LRUCacheTable;StoredType}, or
123 /// see \alib{containers::HashTable;StoredType}.
124 using StoredType = T;
125
126 /// Exposes template parameter \p{TKey}.<br>
127 /// Container types typically publish an alias to this type.
128 /// For example, see \alib{containers::LRUCacheTable;KeyType}, or
129 /// see \alib{containers::HashTable;KeyType}.
130 using KeyType = TKey;
131
132 /// Exposes template parameter \p{T} and thus equals #StoredType.<br>
133 /// Container types typically publish an alias to this type.
134 /// For example, see \alib{containers::LRUCacheTable;MappedType}, or
135 /// see \alib{containers::HashTable;MappedType}.
136 using MappedType = T;
137
138 /// A custom implementation has to return the key-portion of given \p{src}.
139 /// \attention This method is just declared. A type-specific implementation has to be
140 /// provided by the using code!
141 /// @param src The stored value to extract the key from.
142 /// @return The given reference.
143 KeyType& Key (T& src);
144
145 /// Returns the given \p{src} as is.
146 /// @param src The stored value to extract the mapped portion from.
147 /// @return The given reference.
148 MappedType& Mapped(T& src) const { return src; }
149};
150
151} // namespace [alib::containers]
152
153#endif // HPP_ALIB_MONOMEM_CONTAINERS_VALUE_DESCRIPTOR
154
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:223
MappedType & Mapped(T &src) const
std::pair< TKey, TMapped > StoredType
TKey & Key(std::pair< TKey, TMapped > &src) const
TMapped & Mapped(std::pair< TKey, TMapped > &src) const