rmw  master
C API providing a middleware abstraction layer which is used to implement the rest of ROS.
demangle.hpp
Go to the documentation of this file.
1 // Copyright 2015 Open Source Robotics Foundation, Inc.
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 RMW__IMPL__CPP__DEMANGLE_HPP_
16 #define RMW__IMPL__CPP__DEMANGLE_HPP_
17 
18 #ifndef _WIN32
19 // Includes for abi::__cxa_demangle.
20 #include <cxxabi.h>
21 #include <cstdlib>
22 #include <memory>
23 #endif
24 #include <iostream>
25 #include <string>
26 
27 #include "rmw/impl/config.h"
28 
29 namespace rmw
30 {
31 namespace impl
32 {
33 namespace cpp
34 {
35 
37 template<typename T>
38 std::string
39 demangle(const T & instance)
40 {
41  (void)instance;
42 // Cannot do demangling if on Windows or if we want to avoid memory allocation.
43 #if !defined(_WIN32) || RMW_AVOID_MEMORY_ALLOCATION
44  int status = 0;
45  std::string mangled_typeid_name = typeid(T).name();
46 
47  std::unique_ptr<char, void (*)(void *)> res {
48  abi::__cxa_demangle(mangled_typeid_name.c_str(), NULL, NULL, &status),
49  std::free
50  };
51 
52  return (status == 0) ? res.get() : mangled_typeid_name;
53 #else
54  return typeid(T).name();
55 #endif
56 }
57 
58 } // namespace cpp
59 } // namespace impl
60 } // namespace rmw
61 
62 #endif // RMW__IMPL__CPP__DEMANGLE_HPP_
std::string demangle(const T &instance)
Return the demangle name of the instance of type T.
Definition: demangle.hpp:39
Definition: demangle.hpp:29