Demangle the type when printing

release/4.3a0
Varun Agrawal 2020-05-18 02:19:54 -04:00
parent a76c0a20ee
commit 95e7eea4b7
2 changed files with 29 additions and 2 deletions

View File

@ -31,6 +31,33 @@
namespace gtsam { namespace gtsam {
#ifdef __GNUG__
#include <cstdlib>
#include <cxxabi.h>
#include <string>
/// Pretty print Value type name
static std::string demangle(const char* name) {
int status = -4; // some arbitrary value to eliminate the compiler warning
// enable c++11 by passing the flag -std=c++11 to g++
std::unique_ptr<char, void(*)(void*)> res {
abi::__cxa_demangle(name, NULL, NULL, &status),
std::free
};
return (status==0) ? res.get() : name ;
}
#else
// does nothing if not g++
static std::string demangle(const char* name) {
return name;
}
#endif
/** /**
* Wraps any type T so it can play as a Value * Wraps any type T so it can play as a Value
*/ */
@ -83,7 +110,7 @@ public:
/// Virtual print function, uses traits /// Virtual print function, uses traits
virtual void print(const std::string& str) const { virtual void print(const std::string& str) const {
std::cout << "(" << typeid(T).name() << ") "; std::cout << "(" << demangle(typeid(T).name()) << ") ";
traits<T>::Print(value_, str); traits<T>::Print(value_, str);
} }

View File

@ -235,7 +235,7 @@ namespace gtsam {
if(message_.empty()) if(message_.empty())
message_ = message_ =
"Attempting to retrieve value with key \"" + DefaultKeyFormatter(key_) + "\", type stored in Values is " + "Attempting to retrieve value with key \"" + DefaultKeyFormatter(key_) + "\", type stored in Values is " +
std::string(storedTypeId_.name()) + " but requested type was " + std::string(requestedTypeId_.name()); std::string(demangle(storedTypeId_.name())) + " but requested type was " + std::string(demangle(requestedTypeId_.name()));
return message_.c_str(); return message_.c_str();
} }