ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
std_typeinfo.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_TYPEINFO
11#define HPP_ALIB_COMPATIBILITY_STD_TYPEINFO 1
12#pragma once
13#if !defined(DOXYGEN)
14# include "alib/alib.hpp"
15#endif
16
17#include <typeindex>
18#include <functional>
19
20
21namespace alib {
22
23/// This namespace contains sub-namespaces that provide compatibility of 3rd-party types and
24/// \alib that are not specific to dedicated \alibmods..<br>
25/// The entities of those namespaces become available with the inclusion of optional "compatibility"
26/// headers found in folder \alibsrcdir{compatibility}.
27namespace compatibility {
28
29/// This namespace documents compatibility features of \alib (that are not specific to one of the
30/// \alibmods) and the standard C++ class library found in namespace \c std.
31namespace std {
32
33
34//==================================================================================================
35/// This is a simple struct that features the use of <c>std::type_info</c> as key values of
36/// container types like hash tables and sets. For this, this struct provides inner
37/// functors \b hash, \b equal_to and \b less.
38///
39/// If for example a <c>std::unordered_map</c> should be instantiated with run-time type
40/// information as the hash key, a definition looks as follows:
41///
42/// std::unordered_map<TypeFunctors::Key, MyMappedType,
43/// TypeFunctors::Hash,
44/// TypeFunctors::EqualTo > myMap;
45///
46/// Note that the key type evaluates to <c>const std::type_info*</c>, hence pointers to
47/// the structs have to be given when interfacing with the container.
48//==================================================================================================
50{
51 /// The key type
52 using Key= const ::std::type_info*;
53
54 //==============================================================================================
55 /// Hash code functor for type <c>const std::type_info*</c>.
56 //==============================================================================================
57 struct Hash
58 {
59 /// Invokes <c>std::type_info::hash_code</c> on the wrapped type.
60 /// @param typeinfo Pointer to the run-time type information struct.
61 /// @return The hash code
62 size_t operator()( Key typeinfo ) const
63 {
64 return typeinfo->hash_code();
65 }
66 };
67
68 //==============================================================================================
69 /// Comparison functor for type <c>const std::type_info*</c>.
70 //==============================================================================================
71 struct EqualTo
72 {
73 /// Invokes <c>operator ==</c> with \p{lhs} and \p{rhs}.
74 /// @param lhs The left-hand side value.
75 /// @param rhs The right-hand side value.
76 /// @return \c true if the objects represent the same type, \c false otherwise.
77 bool operator()( Key lhs, Key rhs ) const
78 {
79 return *lhs == *rhs;
80 }
81 };
82
83 //==============================================================================================
84 /// Comparison functor for type <c>const std::type_info*</c>.
85 //==============================================================================================
86 struct Less
87 {
88 /// Invokes <c>std::type_index::operator<</c> on corresponding temporaries created from
89 /// \p{lhs} and \p{rhs}.
90 /// @param lhs The left-hand side value.
91 /// @param rhs The right-hand side value.
92 /// @return \c true if \p{lhs} is "less" than \p{rhs}, \c false otherwise.
93 bool operator()( Key lhs, Key rhs ) const
94 {
95 return ::std::type_index( *lhs ) < ::std::type_index( *rhs );
96 }
97 };
98}; // struct TypeFunctors
99
100}} // namespace alib[::compatibility::std]
101
102/// Type alias in namespace \b alib.
104
105} // namespace [alib]
106
107#endif // HPP_ALIB_COMPATIBILITY_STD_TYPEINFO
108
Definition alib.cpp:69
Comparison functor for type const std::type_info*.
Hash code functor for type const std::type_info*.
Comparison functor for type const std::type_info*.
bool operator()(Key lhs, Key rhs) const
const ::std::type_info * Key
The key type.