Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a6e4534

Browse files
committed
Pretty demangled names and obsolate comments removed
1 parent 29bac92 commit a6e4534

File tree

3 files changed

+124
-13
lines changed

3 files changed

+124
-13
lines changed

include/behaviortree_cpp/basic_types.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
#include <stdexcept>
77
#include <vector>
88
#include <sstream>
9+
#include <exception>
910
#include "behaviortree_cpp/string_view.hpp"
11+
#include "behaviortree_cpp/blackboard/demangle_util.h"
1012

1113
namespace BT
1214
{
13-
// Enumerates the possible types of a node, for drawinf we
14-
// have do discriminate whoich control node it is:
15+
// Enumerates the possible types of nodes
1516
enum class NodeType
1617
{
1718
UNDEFINED = 0,
@@ -23,14 +24,7 @@ enum class NodeType
2324
};
2425

2526
// Enumerates the states every node can be in after execution during a particular
26-
// time step:
27-
// - "Success" indicates that the node has completed running during this time step;
28-
// - "Failure" indicates that the node has determined it will not be able to complete
29-
// its task;
30-
// - "Running" indicates that the node has successfully moved forward during this
31-
// time step, but the task is not yet complete;
32-
// - "Idle" indicates that the node hasn't run yet.
33-
// - "Halted" indicates that the node has been halted by its father.
27+
// time step.
3428
enum class NodeStatus
3529
{
3630
IDLE = 0,
@@ -63,8 +57,20 @@ enum SuccessPolicy
6357

6458
typedef nonstd::string_view StringView;
6559

66-
template <typename T>
67-
T convertFromString(const StringView& str);
60+
/// TreeNode::getParam requires convertFromString to be implemented for your specific type,
61+
/// unless you are try to read it from a blackboard.
62+
///
63+
template <typename T> inline
64+
T convertFromString(const StringView& /*str*/)
65+
{
66+
auto type_name = BT::demangle( typeid(T).name() );
67+
68+
std::cerr << "You (maybe indirectly) called BT::convertFromString() for type [" <<
69+
type_name <<"], but I can't find the template specialization.\n" << std::endl;
70+
71+
throw std::logic_error(std::string("You didn't implement the template specialization of "
72+
"convertFromString for this type: ") + type_name );
73+
}
6874

6975
//------------------------------------------------------------------
7076

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#ifndef DEMANGLE_UTIL_H
2+
#define DEMANGLE_UTIL_H
3+
4+
#include <string>
5+
6+
7+
#if defined( __clang__ ) && defined( __has_include )
8+
# if __has_include(<cxxabi.h>)
9+
# define HAS_CXXABI_H
10+
# endif
11+
#elif defined( __GLIBCXX__ ) || defined( __GLIBCPP__ )
12+
# define HAS_CXXABI_H
13+
#endif
14+
15+
#if defined( HAS_CXXABI_H )
16+
# include <cxxabi.h>
17+
# include <cstdlib>
18+
# include <cstddef>
19+
#endif
20+
21+
namespace BT
22+
{
23+
24+
inline char const * demangle_alloc( char const * name ) noexcept;
25+
inline void demangle_free( char const * name ) noexcept;
26+
27+
class scoped_demangled_name
28+
{
29+
private:
30+
char const * m_p;
31+
32+
public:
33+
explicit scoped_demangled_name( char const * name ) noexcept :
34+
m_p( demangle_alloc( name ) )
35+
{
36+
}
37+
38+
~scoped_demangled_name() noexcept
39+
{
40+
demangle_free( m_p );
41+
}
42+
43+
char const * get() const noexcept
44+
{
45+
return m_p;
46+
}
47+
48+
scoped_demangled_name( scoped_demangled_name const& ) = delete;
49+
scoped_demangled_name& operator= ( scoped_demangled_name const& ) = delete;
50+
};
51+
52+
53+
#if defined( HAS_CXXABI_H )
54+
55+
inline char const * demangle_alloc( char const * name ) noexcept
56+
{
57+
int status = 0;
58+
std::size_t size = 0;
59+
return abi::__cxa_demangle( name, NULL, &size, &status );
60+
}
61+
62+
inline void demangle_free( char const * name ) noexcept
63+
{
64+
std::free( const_cast< char* >( name ) );
65+
}
66+
67+
inline std::string demangle( char const * name )
68+
{
69+
scoped_demangled_name demangled_name( name );
70+
char const * const p = demangled_name.get();
71+
if( p )
72+
{
73+
return p;
74+
}
75+
else
76+
{
77+
return name;
78+
}
79+
}
80+
81+
#else
82+
83+
inline char const * demangle_alloc( char const * name ) noexcept
84+
{
85+
return name;
86+
}
87+
88+
inline void demangle_free( char const * ) noexcept
89+
{
90+
}
91+
92+
inline std::string demangle( char const * name )
93+
{
94+
return name;
95+
}
96+
97+
#endif
98+
99+
} // namespace BT
100+
101+
#undef HAS_CXXABI_H
102+
103+
#endif // DEMANGLE_UTIL_H

include/behaviortree_cpp/blackboard/safe_any.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <cstring>
1010
#include <type_traits>
1111
#include "any.hpp"
12+
#include "demangle_util.h"
1213
#include "convert_impl.hpp"
1314

1415
namespace SafeAny
@@ -183,7 +184,8 @@ class Any
183184
{
184185
char buffer[1024];
185186
sprintf(buffer, "[Any::convert]: no known safe conversion between %s and %s",
186-
_any.type().name(), typeid(T).name());
187+
BT::demangle( _any.type().name() ),
188+
BT::demangle( typeid(T).name() ) );
187189
return std::runtime_error(buffer);
188190
}
189191
};

0 commit comments

Comments
 (0)