tracetools  master
Tracing tools and instrumentation for ROS 2
utils.hpp
Go to the documentation of this file.
1 // Copyright 2019 Robert Bosch GmbH
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef TRACETOOLS__UTILS_HPP_
16 #define TRACETOOLS__UTILS_HPP_
17 
18 #include <stddef.h>
19 #include <functional>
20 
21 #include "tracetools/config.h"
22 
24 #define TRACETOOLS_SYMBOL_UNKNOWN "UNKNOWN"
25 
26 #ifndef TRACETOOLS_DISABLED
27 
28 namespace tracetools
29 {
30 
31 namespace detail
32 {
33 
35 
38 const char * demangle_symbol(const char * mangled);
39 
41 
44 const char * get_symbol_funcptr(void * funcptr);
45 
46 } // namespace detail
47 
49 
56 template<typename T, typename ... U>
57 const char * get_symbol(std::function<T(U...)> f)
58 {
59  typedef T (fnType)(U...);
60  fnType ** fnPointer = f.template target<fnType *>();
61  // If we get an actual address
62  if (fnPointer != nullptr) {
63  void * funcptr = reinterpret_cast<void *>(*fnPointer);
64  return detail::get_symbol_funcptr(funcptr);
65  }
66  // Otherwise we have to go through target_type()
67  return detail::demangle_symbol(f.target_type().name());
68 }
69 
71 
77 template<typename L>
78 const char * get_symbol(L && l)
79 {
80  return detail::demangle_symbol(typeid(l).name());
81 }
82 
83 } // namespace tracetools
84 
85 [[deprecated("use tracetools::detail::demangle_symbol")]]
86 inline const char * _demangle_symbol(const char * mangled)
87 {
88  return tracetools::detail::demangle_symbol(mangled);
89 }
90 
91 [[deprecated("use tracetools::detail::get_symbol_funcptr")]]
92 inline const char * _get_symbol_funcptr(void * funcptr)
93 {
94  return tracetools::detail::get_symbol_funcptr(funcptr);
95 }
96 
97 template<typename T, typename ... U>
98 [[deprecated("use tracetools::get_symbol")]]
99 const char * get_symbol(std::function<T(U...)> f)
100 {
101  return tracetools::get_symbol<T, U...>(f);
102 }
103 
104 template<typename L>
105 [[deprecated("use tracetools::get_symbol")]]
106 const char * get_symbol(L && l)
107 {
108  return tracetools::get_symbol<L>(l);
109 }
110 
111 #endif // TRACETOOLS_DISABLED
112 
113 #endif // TRACETOOLS__UTILS_HPP_
tracetools::get_symbol
const char * get_symbol(std::function< T(U...)> f)
Get symbol from an std::function object.
Definition: utils.hpp:57
tracetools
Definition: utils.hpp:28
config.h
get_symbol
const char * get_symbol(std::function< T(U...)> f)
Definition: utils.hpp:99