ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
stdtypeinfofunctors.hpp
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/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_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 #"TypeFunctors::Hash", #"TypeFunctors::EqualTo" and #"TypeFunctors::Less".
14///
15/// If, for example, a <c>std::unordered_map</c> should be instantiated with runtime 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 /// The key type
27 using Key= const ::std::type_info*;
28
29 /// Hash code functor for type <c>const std::type_info*</c>.
30 struct Hash {
31 /// Invokes <c>std::type_info::hash_code</c> on the wrapped type.
32 /// @param typeinfo Pointer to the runtime type information struct.
33 /// @return The hash code
34 size_t operator()( Key typeinfo ) const { return typeinfo->hash_code(); }
35 };
36
37 /// Comparison functor for type <c>const std::type_info*</c>.
38 struct EqualTo {
39 /// Invokes <c>operator ==</c> with \p{lhs} and \p{rhs}.
40 /// @param lhs The left-hand side value.
41 /// @param rhs The right-hand side value.
42 /// @return \c true if the objects represent the same type, \c false otherwise.
43 bool operator()( Key lhs, Key rhs ) const { return *lhs == *rhs; }
44 };
45
46 /// Comparison functor for type <c>const std::type_info*</c>.
47 struct Less {
48 /// Invokes <c>std::type_index::operator<</c> on corresponding temporaries created from
49 /// \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 \p{lhs} is "less" than \p{rhs}, \c false otherwise.
53 bool operator()( Key lhs, Key rhs ) const
54 { return ::std::type_index( *lhs ) < ::std::type_index( *rhs ); }
55 };
56}; // struct TypeFunctors
57
58} // namespace [alib::lang]
#define ALIB_EXPORT
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.