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 
22 
24 #define SYMBOL_UNKNOWN "UNKNOWN"
25 
26 TRACETOOLS_PUBLIC const char * _demangle_symbol(const char * mangled);
27 
28 TRACETOOLS_PUBLIC const char * _get_symbol_funcptr(void * funcptr);
29 
31 
38 template<typename T, typename ... U>
39 const char * get_symbol(std::function<T(U...)> f)
40 {
41  typedef T (fnType)(U...);
42  fnType ** fnPointer = f.template target<fnType *>();
43  // If we get an actual address
44  if (fnPointer != nullptr) {
45  void * funcptr = reinterpret_cast<void *>(*fnPointer);
46  return _get_symbol_funcptr(funcptr);
47  }
48  // Otherwise we have to go through target_type()
49  return _demangle_symbol(f.target_type().name());
50 }
51 
53 
59 template<typename L>
60 const char * get_symbol(L && l)
61 {
62  return _demangle_symbol(typeid(l).name());
63 }
64 
65 #endif // TRACETOOLS__UTILS_HPP_
std::function
TRACETOOLS_PUBLIC
#define TRACETOOLS_PUBLIC
Definition: visibility_control.hpp:50
visibility_control.hpp
get_symbol
const char * get_symbol(std::function< T(U...)> f)
Get symbol from an std::function object.
Definition: utils.hpp:39