ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
stdtypeinfofunctors.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_lang of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib::lang {
9
10//==================================================================================================
11/// This is a simple struct that features the use of <c>std::type_info</c> as key values of
12/// container types like hash tables and sets. For this, this struct provides inner
13/// functors \b hash, \b equal_to and \b less.
14///
15/// If, for example, a <c>std::unordered_map</c> should be instantiated with run-time type
16/// information as the hash key, a definition looks as follows:
17///
18/// std::unordered_map<TypeFunctors::Key, MyMappedType,
19/// TypeFunctors::Hash,
20/// TypeFunctors::EqualTo > myMap;
21///
22/// Note that the key type evaluates to <c>const std::type_info*</c>, hence pointers to
23/// the structs have to be given when interfacing with the container.
24//==================================================================================================
26{
27 /// The key type
28 using Key= const ::std::type_info*;
29
30 /// Hash code functor for type <c>const std::type_info*</c>.
31 struct Hash
32 {
33 /// Invokes <c>std::type_info::hash_code</c> on the wrapped type.
34 /// @param typeinfo Pointer to the run-time type information struct.
35 /// @return The hash code
36 size_t operator()( Key typeinfo ) const { return typeinfo->hash_code(); }
37 };
38
39 /// Comparison functor for type <c>const std::type_info*</c>.
40 struct EqualTo
41 {
42 /// Invokes <c>operator ==</c> with \p{lhs} and \p{rhs}.
43 /// @param lhs The left-hand side value.
44 /// @param rhs The right-hand side value.
45 /// @return \c true if the objects represent the same type, \c false otherwise.
46 bool operator()( Key lhs, Key rhs ) const { return *lhs == *rhs; }
47 };
48
49 /// Comparison functor for type <c>const std::type_info*</c>.
50 struct Less
51 {
52 /// Invokes <c>std::type_index::operator<</c> on corresponding temporaries created from
53 /// \p{lhs} and \p{rhs}.
54 /// @param lhs The left-hand side value.
55 /// @param rhs The right-hand side value.
56 /// @return \c true if \p{lhs} is "less" than \p{rhs}, \c false otherwise.
57 bool operator()( Key lhs, Key rhs ) const
58 { return ::std::type_index( *lhs ) < ::std::type_index( *rhs ); }
59 };
60}; // struct TypeFunctors
61
62} // namespace [alib::lang]
#define ALIB_EXPORT
Definition alib.inl:497
Comparison functor for type const std::type_info*.
bool operator()(Key lhs, Key rhs) const
Hash code functor for type const std::type_info*.
size_t operator()(Key typeinfo) const
Comparison functor for type const std::type_info*.
bool operator()(Key lhs, Key rhs) const
const ::std::type_info * Key
The key type.