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