Dev Essential
Loading...
Searching...
No Matches
address_info.h
Go to the documentation of this file.
1
11
12#ifndef A_UTIL_SYSTEM_ADDRESS_INFO_HEADER_INCLUDED
13#define A_UTIL_SYSTEM_ADDRESS_INFO_HEADER_INCLUDED
14
15#include <a_util/base/types.h> // handle_t
17
18namespace a_util {
19namespace system {
20
27public:
28#if ((defined _MSC_VER) && (_MSVC_LANG >= 201510L)) || (__cplusplus >= 201510L)
29 // The noexcept-specification in C++17 is a part of the function type and
30 // may appear as part of any function declarator.
31 // See https://en.cppreference.com/w/cpp/language/noexcept_spec
35 template <typename ReturnType, typename... Args>
36 explicit AddressInfo(ReturnType (*const function)(Args...) noexcept) noexcept
37 : _address{toDataPointer(function)}
38 {
39 }
40#endif
41
45 template <typename ReturnType, typename... Args>
46 explicit AddressInfo(ReturnType (*const function)(Args...)) noexcept
47 : _address{toDataPointer(function)}
48 {
49 }
50
55 template <typename T>
56 explicit AddressInfo(const T& variable) noexcept : _address(&variable)
57 {
58 static_assert(!std::is_function<T>::value,
59 "ERROR: CTOR for data pointer is called with function pointer");
60 }
61
62public:
71
72private:
81 template <typename Return, typename... Args>
82 const_handle_t toDataPointer(Return (*const function)(Args...)) noexcept
83 {
84 // According to the C-standard, casting a function pointer to a void* is not allowed.
85 // It is however allowed to cast a function pointer to a function pointer of a different
86 // type. Taking the address of the function pointer will return a data pointer.
87 //
88 // Read explanation from inner to outer static_cast.
89 // Type punning, cast the data pointer back to a pointer to a data pointer and return the
90 // address of the data pointer (const correctness)
91 return *static_cast<void* const*>(
92 // Type punning, cast the const data pointer (pointing to the function pointer) to
93 // const void*
94 static_cast<const void*>(
95 // cast temporary function pointer to lvalue reference to const, which we can take
96 // the address of. The address is the resulting data pointer pointing to the
97 // function pointer
98 &static_cast<Return (*const&)(Args...)>(function)));
99 }
100
101private:
102 const_handle_t _address;
103};
104
105} // namespace system
106} // namespace a_util
107
108#endif // A_UTIL_SYSTEM_ADDRESS_INFO_HEADER_INCLUDED
Definition path.h:46
filesystem::Path getFilePath() const
AddressInfo(const T &variable) noexcept
Definition address_info.h:56
AddressInfo(ReturnType(*const function)(Args...)) noexcept
Definition address_info.h:46
const void * const_handle_t
Type of a const handle value.
Definition types.h:29
Serves as component for portable OS (Windows, Linux, ...) functionality.
Definition system.h:20
Serves as the root component, with common functionality documented in core functionality.
Definition base.h:24