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