ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
std_strings_functional.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of the \aliblong.<br>
4 * With the inclusion of this header compatibility features between \alib and the C++ standard
5 * library are provided.
6 *
7 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
8 * Published under \ref mainpage_license "Boost Software License".
9 **************************************************************************************************/
10#ifndef HPP_ALIB_COMPATIBILITY_STD_STRINGS_FUNCTIONAL
11#define HPP_ALIB_COMPATIBILITY_STD_STRINGS_FUNCTIONAL 1
12
13#if !defined(HPP_ALIB) && !defined(ALIB_DOX)
14# include "alib/alib.hpp"
15#endif
16
17ALIB_ASSERT_MODULE(STRINGS)
18
19#if !defined (HPP_ALIB_STRINGS_STRING)
21#endif
22
23#if !defined(_GLIBCXX_FUNCTIONAL) && !defined(_FUNCTIONAL_)
24 #include <functional>
25#endif
26
27
28// #################################################################################################
29// #################################################################################################
30// #### Namespace dox
31// #################################################################################################
32// #################################################################################################
33namespace alib { namespace strings {
34/**
35 * This namespace contains sub-namespaces that provide compatibility of 3rd-party types and
36 * module \alib_strings_nl.<br>
37 * The entities of those namespaces become available with the inclusion of optional "compatibility"
38 * headers found in folder \alibsrcdir{compatibility}.
39 */
40namespace compatibility {
41
42/**
43 * This namespace documents compatibility features of \alib_strings_nl and the
44 * standard C++ class library found in namespace \c std.
45 */
46namespace std {
47}}}}
48
49
50// #################################################################################################
51// #################################################################################################
52// #### std::hash and std::equal_to for ALib String types
53// #################################################################################################
54// #################################################################################################
55#if defined(ALIB_DOX)
56 namespace alib { namespace strings { namespace compatibility { namespace std {
57#else
58 namespace std {
59#endif
60
61/** ************************************************************************************************
62 * Specialization of functor <c>std::hash</c> for type \alib{strings;TString<TChar>;String}.
63 * Note that this specialization can also be used in combination with derived string types, like
64 * \alib{strings;TAString<TChar>;AString}.
65 *
66 * This specialization is provided with the inclusion of header file
67 * \alibheader{compatibility/std_strings_functional.hpp}.<br>
68 *
69 * \note
70 * While the documentation indicates namespace <c>alib::strings::compatibility::std</c>,
71 * the true location is namespace <c>std</c> (as mandatory).
72 *
73 * @tparam TChar The character type of the string.
74 **************************************************************************************************/
75template<typename TChar>
76struct hash<alib::strings::TString<TChar>>
77{
78 /**
79 * Calculates the hash code for \alib strings.
80 * @param src The string object to hash.
81 * @return The hash code.
82 */
83 std::size_t operator()(const alib::strings::TString<TChar>& src) const
84 {
85 return src.Hashcode();
86 }
87};
88
89/** ************************************************************************************************
90 * Specialization of functor <c>std::equal_to</c> for type \alib{strings;TString<TChar>;String}.
91 * Note that this specialization can also be used in combination with derived string types, like
92 * \alib{strings;TAString<TChar>;AString}.
93 *
94 * This specialization is provided with the inclusion of header file
95 * \alibheader{compatibility/std_strings_functional.hpp}.<br>
96 *
97 * \note
98 * While the documentation indicates namespace <c>alib::strings::compatibility::std</c>,
99 * the true location is namespace <c>std</c> (as mandatory).
100 *
101 * @tparam TChar The character type of the strings to compare.
102 **************************************************************************************************/
103template<typename TChar>
104struct equal_to<alib::strings::TString<TChar>>
105{
106 /**
107 * Invokes \alib{strings;TString::Equals;String::Equals} on \p{lhs}, passing \p{rhs}
108 * and returns the result.
109 * @param lhs The first string object.
110 * @param rhs The second string object.
111 * @return The result of the comparison.
112 */
114 const alib::strings::TString<TChar>& rhs ) const
115 {
116 return lhs.Equals( rhs );
117 }
118};
119
120/** ************************************************************************************************
121 * Specialization of functor <c>std::less</c> for strings of templated character type.
122 *
123 * For the comparison, \alib{strings::TString;CompareTo} is invoked on \p{lhs} with passing
124 * \p{rhs}.
125 *
126 * This specialization is provided with the inclusion of header file
127 * \alibheader{compatibility/std_strings_functional.hpp}.<br>
128 *
129 * \note
130 * While the documentation indicates namespace <c>alib::strings::compatibility::std</c>,
131 * the true location is namespace <c>std</c> (as mandatory).
132 *
133 * @tparam TChar The character type of the strings to compare.
134 **************************************************************************************************/
135template<typename TChar>
136struct less<alib::strings::TString<TChar>>
137{
138 /**
139 * Compares the two given strings.
140 * @param lhs The left-hand side string.
141 * @param rhs The right-hand side string.
142 * @return The result of the comparison.
143 */
145 const alib::strings::TString<TChar>& rhs) const
146 {
147 return lhs.CompareTo(rhs) < 0;
148 }
149};
150
151#if !defined(ALIB_DOX)
152} // namespace [std]
153namespace alib {
154#endif
155
156
157/** ************************************************************************************************
158 * Functor that can be used as an explicitly given replacement for <c>std::hash</c> with template
159 * types (containers) found in namespace \c std.<br>
160 * While the specialization of <c>std::hash</c> for type \alib{strings;TString<TChar>;String},
161 * which is used by the default values of template arguments of the corresponding types in namespace
162 * \c std, performs a case sensitive hash code calculation, this version creates the same
163 * hash code for two strings that only differ in the letter case of one or more characters.
164 *
165 * Note that this specialization can also be used in combination with derived string types, like
166 * \alib{strings;TAString<TChar>;AString}.
167 *
168 * This functor is provided with the inclusion of header file
169 * \alibheader{compatibility/std_strings_functional.hpp}.
170 *
171 * \note
172 * While the documentation indicates namespace <c>alib::strings::compatibility::std</c>,
173 * the true location is namespace <c>std</c> (as mandatory).
174 *
175 * @tparam TChar The character type of the string. Defaults to \alib{characters;character}.
176 **************************************************************************************************/
177template<typename TChar= characters::character>
179{
180 /**
181 * Calculates the hash code \alib strings, ignoring the letter case.
182 * @param src The string object to hash.
183 * @return The hash code.
184 */
185 size_t operator()(const strings::TString<TChar>& src) const
186 {
187 return src.HashcodeIgnoreCase();
188 }
189};
190
191/** ************************************************************************************************
192 * Functor that can be used as an explicitly given replacement for <c>std::equal_to</c> with template
193 * types (containers) found in namespace \c std.<br>
194 * While the specialization of <c>std::equal_to</c> for type \alib{strings;TString<TChar>;String},
195 * which is used by the default values of template arguments of the corresponding types in namespace
196 * \c std, performs a case sensitive comparison, this version ignores the letter case of the
197 * characters of the given strings.
198 *
199 * Note that this specialization can also be used in combination with derived string types, like
200 * \alib{strings;TAString<TChar>;AString}.
201 *
202 * This functor is provided with the inclusion of header file
203 * \alibheader{compatibility/std_strings_functional.hpp}.
204 *
205 * \note
206 * While the documentation indicates namespace <c>alib::strings::compatibility::std</c>,
207 * the true location is namespace <c>alib</c>.
208 *
209 * @tparam TChar The character type of the strings to compare.
210 * Defaults to \alib{characters;character}.
211 **************************************************************************************************/
212template<typename TChar= characters::character>
214{
215 /**
216 * Invokes \alib{strings;TString::Equals;String::Equals} on \p{lhs}, passing \p{rhs}
217 * and returns the result.
218 *
219 * @param lhs The first string object.
220 * @param rhs The second string object.
221 * @return The result of the comparison.
222 */
224 const strings::TString<TChar>& rhs ) const
225 {
226 return lhs.template Equals<true, lang::Case::Ignore>( rhs );
227 }
228};
229
230/** ************************************************************************************************
231 * Specialization of functor <c>std::less</c> for strings of templated character type.
232 *
233 * For the comparison, \alib{strings::TString;CompareTo;CompareTo<Case::Ignore>} is invoked on
234 * \p{lhs} with passing \p{rhs}.
235 *
236 * This specialization is provided with the inclusion of header file
237 * \alibheader{compatibility/std_strings_functional.hpp}.<br>
238 *
239 * \note
240 * While the documentation indicates namespace <c>alib::strings::compatibility::std</c>,
241 * the true location is namespace <c>std</c> (as mandatory).
242 *
243 * @tparam TChar The character type of the strings to compare.
244 * Defaults to \alib{characters;character}.
245 **************************************************************************************************/
246template<typename TChar= characters::character>
248{
249 /**
250 * Compares the two given strings.
251 * @param lhs The left-hand side string.
252 * @param rhs The right-hand side string.
253 * @return \c true if \p{lhs} is less than \p{rhs}.
254 */
256 const strings::TString<TChar>& rhs) const
257 {
258 return lhs.template CompareTo<lang::Case::Ignore>( rhs ) < 0;
259 }
260};
261
262
263#if defined(ALIB_DOX)
264}}}} // namespace [alib::strings::compatibility::std]
265#else
266} // namespace alib
267#endif
268
269#endif // HPP_ALIB_COMPATIBILITY_STD_STRINGS_FUNCTIONAL
int CompareTo(const TString< TChar > &rhs) const
Definition string.hpp:624
std::size_t Hashcode() const
std::size_t HashcodeIgnoreCase() const
bool Equals(const TString< TChar > &rhs) const
Definition string.hpp:573
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:190
Definition alib.cpp:57
bool operator()(const alib::strings::TString< TChar > &lhs, const alib::strings::TString< TChar > &rhs) const
bool operator()(const strings::TString< TChar > &lhs, const strings::TString< TChar > &rhs) const
std::size_t operator()(const alib::strings::TString< TChar > &src) const
size_t operator()(const strings::TString< TChar > &src) const
bool operator()(const alib::strings::TString< TChar > &lhs, const alib::strings::TString< TChar > &rhs) const
bool operator()(const strings::TString< TChar > &lhs, const strings::TString< TChar > &rhs) const