ADTF  3.18.3
address_info.h
Go to the documentation of this file.
1 
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
16 #include <a_util/filesystem/path.h>
17 
18 namespace a_util {
19 namespace system {
20 
26 class AddressInfo {
27 public:
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 
62 public:
71 
72 private:
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 
101 private:
102  const_handle_t _address;
103 };
104 
105 } // namespace system
106 } // namespace a_util
107 
108 #endif // A_UTIL_SYSTEM_ADDRESS_INFO_HEADER_INCLUDED
File/Directory path class.
Definition: path.h:45
Address info class This class can be used to query information about a memory address (e....
Definition: address_info.h:26
AddressInfo(const T &variable) noexcept
Constructor for global or static variables addresses.
Definition: address_info.h:56
filesystem::Path getFilePath() const
Get the fully qualified file path to the executable or shared library the address is located in.
const_handle_t toDataPointer(Return(*const function)(Args...)) noexcept
Casts a function pointer to a data pointer.
Definition: address_info.h:82
AddressInfo(ReturnType(*const function)(Args...)) noexcept
Constructor for function addresses.
Definition: address_info.h:46
Serves as the root component, with common functionality documented in core functionality.
Definition: base.h:24
Public API for Path type and functions.
Public types and functions defining a_util core functionality.