ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
ALib.Strings.StdFunctors.H
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef H_ALIB_STRINGS_STDFUNCTORS
9#define H_ALIB_STRINGS_STDFUNCTORS
10#pragma once
11#ifndef INL_ALIB
12# include "alib/alib.inl"
13#endif
14#if ALIB_STRINGS
15#include "ALib.Lang.H"
16#include "ALib.Strings.H"
17
18#if DOXYGEN
19 namespace alib::strings {
20 /// This namespace contains sub-namespaces that provide compatibility of 3rd-party types and
21 /// module \alib_strings_nl.<br>
22 /// The entities of those namespaces become available with the inclusion of specific headers
23 /// that import a certain C++20 Module or inject the functionality into a namespace in a
24 /// traditional fashion, for example, header \implude{Strings.StdFunctors}.
25 namespace compatibility {
26
27 /// This namespace documents compatibility features of \alib_strings_nl and the
28 /// standard C++ class library found in namespace \c std.
29 namespace std {
30}}}
31#endif
32
33// #################################################################################################
34// #### std::hash and std::equal_to for ALib string types
35// #################################################################################################
36#if DOXYGEN
38#else
39 namespace std {
40#endif
41
42//==================================================================================================
43/// Specialization of functor <c>std::hash</c> for type \alib{strings;TString<TChar>;String}.
44/// Note that this specialization can also be used in combination with derived string-types, like
45/// \alib{strings;TAString<TChar>;AString}.
46///
47/// This specialization is provided with the inclusion of header-file
48/// \implude{Strings.StdFunctors}.<br>
49///
50/// \note
51/// While the documentation indicates namespace <c>alib::strings::compatibility::std</c>,
52/// the true location is namespace <c>std</c> (as mandatory).
53///
54/// @tparam TChar The character type of the string.
55//==================================================================================================
56template<typename TChar>
57struct hash<alib::strings::TString<TChar>> {
58 /// Calculates the hash code for \alib strings.
59 /// @param src The string object to hash.
60 /// @return The hash code.
61 std::size_t operator()(const alib::strings::TString<TChar>& src) const
62 { return src.Hashcode(); }
63};
64
65//==================================================================================================
66/// Specialization of functor <c>std::equal_to</c> for type \alib{strings;TString<TChar>;String}.
67/// Note that this specialization can also be used in combination with derived string-types, like
68/// \alib{strings;TAString<TChar>;AString}.
69///
70/// This specialization is provided with the inclusion of header-file
71/// \implude{Strings.StdFunctors}.<br>
72///
73/// \note
74/// While the documentation indicates namespace <c>alib::strings::compatibility::std</c>,
75/// the true location is namespace <c>std</c> (as mandatory).
76///
77/// @tparam TChar The character type of the strings to compare.
78//==================================================================================================
79template<typename TChar>
80struct equal_to<alib::strings::TString<TChar>>
81{
82 /// Invokes \alib{strings;TString::Equals;String::Equals} on \p{lhs}, passing \p{rhs}
83 /// and returns the result.
84 /// @param lhs The first string object.
85 /// @param rhs The second string object.
86 /// @return The result of the comparison.
88 const alib::strings::TString<TChar>& rhs ) const
89 {
90 return lhs.Equals( rhs );
91 }
92};
93
94//==================================================================================================
95/// Specialization of functor <c>std::less</c> for strings of templated character type.
96///
97/// For the comparison, \alib{strings::TString;CompareTo} is invoked on \p{lhs} with passing
98/// \p{rhs}.
99///
100/// This specialization is provided with the inclusion of header-file
101/// \implude{Strings.StdFunctors}.<br>
102///
103/// \note
104/// While the documentation indicates namespace <c>alib::strings::compatibility::std</c>,
105/// the true location is namespace <c>std</c> (as mandatory).
106///
107/// @tparam TChar The character type of the strings to compare.
108//==================================================================================================
109template<typename TChar>
110struct less<alib::strings::TString<TChar>>
111{
112 /// Compares the two given strings.
113 /// @param lhs The left-hand side string.
114 /// @param rhs The right-hand side string.
115 /// @return The result of the comparison.
117 const alib::strings::TString<TChar>& rhs) const
118 {
119 return lhs.CompareTo(rhs) < 0;
120 }
121};
122
123#if !DOXYGEN
124} // namespace [std]
125namespace alib {
126#endif
127
128//==================================================================================================
129/// Functor that can be used as an explicitly given replacement for <c>std::hash</c> with template
130/// types (containers) found in namespace \c std.<br>
131/// While the specialization of <c>std::hash</c> for type \alib{strings;TString<TChar>;String},
132/// which is used by the default values of template arguments of the corresponding types in namespace
133/// \c std, performs a case-sensitive hash code calculation, this version creates the same
134/// hash code for two strings that only differ in the letter case of one or more characters.
135///
136/// Note that this specialization can also be used in combination with derived string-types, like
137/// \alib{strings;TAString<TChar>;AString}.
138///
139/// This functor is provided with the inclusion of header-file
140/// \implude{Strings.StdFunctors}.
141///
142/// \note
143/// While the documentation indicates namespace <c>alib::strings::compatibility::std</c>,
144/// the true location is namespace <c>std</c> (as mandatory).
145///
146/// @tparam TChar The character type of the string. Defaults to \alib{characters;character}.
147//==================================================================================================
148template<typename TChar= characters::character>
150{
151 /// Calculates the hash code \alib strings, ignoring the letter case.
152 /// @param src The string object to hash.
153 /// @return The hash code.
154 size_t operator()(const strings::TString<TChar>& src) const
155 {
156 return src.HashcodeIgnoreCase();
157 }
158};
159
160//==================================================================================================
161/// Functor that can be used as an explicitly given replacement for <c>std::equal_to</c> with template
162/// types (containers) found in namespace \c std.<br>
163/// While the specialization of <c>std::equal_to</c> for type \alib{strings;TString<TChar>;String},
164/// which is used by the default values of template arguments of the corresponding types in namespace
165/// \c std, performs a case-sensitive comparison, this version ignores the letter case of the
166/// characters of the given strings.
167///
168/// Note that this specialization can also be used in combination with derived string-types, like
169/// \alib{strings;TAString<TChar>;AString}.
170///
171/// This functor is provided with the inclusion of header-file
172/// \implude{Strings.StdFunctors}.
173///
174/// \note
175/// While the documentation indicates namespace <c>alib::strings::compatibility::std</c>,
176/// the true location is namespace <c>alib</c>.
177///
178/// @tparam TChar The character type of the strings to compare.
179/// Defaults to \alib{characters;character}.
180//==================================================================================================
181template<typename TChar= characters::character>
183{
184 /// Invokes \alib{strings;TString::Equals;String::Equals} on \p{lhs}, passing \p{rhs}
185 /// and returns the result.
186 ///
187 /// @param lhs The first string object.
188 /// @param rhs The second string object.
189 /// @return The result of the comparison.
191 const strings::TString<TChar>& rhs ) const
192 {
193 return lhs.template Equals<alib::CHK, lang::Case::Ignore>( rhs );
194 }
195};
196
197//==================================================================================================
198/// Specialization of functor <c>std::less</c> for strings of templated character type.
199///
200/// For the comparison, \alib{strings::TString;CompareTo;CompareTo<Case::Ignore>} is invoked on
201/// \p{lhs} with passing \p{rhs}.
202///
203/// This specialization is provided with the inclusion of header-file
204/// \implude{Strings.StdFunctors}.<br>
205///
206/// \note
207/// While the documentation indicates namespace <c>alib::strings::compatibility::std</c>,
208/// the true location is namespace <c>std</c> (as mandatory).
209///
210/// @tparam TChar The character type of the strings to compare.
211/// Defaults to \alib{characters;character}.
212//==================================================================================================
213template<typename TChar= characters::character>
215{
216 /// Compares the two given strings.
217 /// @param lhs The left-hand side string.
218 /// @param rhs The right-hand side string.
219 /// @return \c true if \p{lhs} is less than \p{rhs}.
221 const strings::TString<TChar>& rhs) const
222 {
223 return lhs.template CompareTo<lang::Case::Ignore>( rhs ) < 0;
224 }
225};
226
227} // namespace alib
228
229#endif // ALIB_STRINGS
230#endif // H_ALIB_STRINGS_STDFUNCTORS
231
232
std::size_t HashcodeIgnoreCase() const
std::size_t Hashcode() const
bool Equals(const TString< TChar > &rhs) const
Definition string.inl:541
int CompareTo(const TString< TChar > &rhs) const
Definition string.inl:596
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