From 95e7eea4b77cd5aa780cad2a999aa33519d968fe Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Mon, 18 May 2020 02:19:54 -0400 Subject: [PATCH] Demangle the type when printing --- gtsam/base/GenericValue.h | 29 ++++++++++++++++++++++++++++- gtsam/nonlinear/Values.cpp | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/gtsam/base/GenericValue.h b/gtsam/base/GenericValue.h index aee6c0e62..b4a74913b 100644 --- a/gtsam/base/GenericValue.h +++ b/gtsam/base/GenericValue.h @@ -31,6 +31,33 @@ namespace gtsam { +#ifdef __GNUG__ +#include +#include +#include + +/// 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 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 */ @@ -83,7 +110,7 @@ public: /// Virtual print function, uses traits virtual void print(const std::string& str) const { - std::cout << "(" << typeid(T).name() << ") "; + std::cout << "(" << demangle(typeid(T).name()) << ") "; traits::Print(value_, str); } diff --git a/gtsam/nonlinear/Values.cpp b/gtsam/nonlinear/Values.cpp index 1bd8b3a73..c5a7c8c32 100644 --- a/gtsam/nonlinear/Values.cpp +++ b/gtsam/nonlinear/Values.cpp @@ -235,7 +235,7 @@ namespace gtsam { if(message_.empty()) message_ = "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(); }