ALib C++ Library
Library Version: 2510 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 //==============================================================================================
31 /// Hash code functor for type <c>const std::type_info*</c>.
32 //==============================================================================================
33 struct Hash
34 {
35 /// Invokes <c>std::type_info::hash_code</c> on the wrapped type.
36 /// @param typeinfo Pointer to the run-time type information struct.
37 /// @return The hash code
38 size_t operator()( Key typeinfo ) const
39 {
40 return typeinfo->hash_code();
41 }
42 };
43
44 //==============================================================================================
45 /// Comparison functor for type <c>const std::type_info*</c>.
46 //==============================================================================================
47 struct EqualTo
48 {
49 /// Invokes <c>operator ==</c> with \p{lhs} and \p{rhs}.
50 /// @param lhs The left-hand side value.
51 /// @param rhs The right-hand side value.
52 /// @return \c true if the objects represent the same type, \c false otherwise.
53 bool operator()( Key lhs, Key rhs ) const
54 {
55 return *lhs == *rhs;
56 }
57 };
58
59 //==============================================================================================
60 /// Comparison functor for type <c>const std::type_info*</c>.
61 //==============================================================================================
62 struct Less
63 {
64 /// Invokes <c>std::type_index::operator<</c> on corresponding temporaries created from
65 /// \p{lhs} and \p{rhs}.
66 /// @param lhs The left-hand side value.
67 /// @param rhs The right-hand side value.
68 /// @return \c true if \p{lhs} is "less" than \p{rhs}, \c false otherwise.
69 bool operator()( Key lhs, Key rhs ) const
70 {
71 return ::std::type_index( *lhs ) < ::std::type_index( *rhs );
72 }
73 };
74}; // struct TypeFunctors
75
76} // namespace [alib::lang]
#define ALIB_EXPORT
Definition alib.inl:488
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.