diff --git a/gtsam/base/GenericValue.h b/gtsam/base/GenericValue.h index b4a74913b..e1cb3bc2c 100644 --- a/gtsam/base/GenericValue.h +++ b/gtsam/base/GenericValue.h @@ -20,6 +20,7 @@ #pragma once #include +#include #include #include @@ -31,33 +32,6 @@ 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 */ diff --git a/gtsam/base/types.cpp b/gtsam/base/types.cpp new file mode 100644 index 000000000..827d71cd6 --- /dev/null +++ b/gtsam/base/types.cpp @@ -0,0 +1,68 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file types.cpp + * @brief Functions for handling type information + * @author Varun Agrawal + * @date May 18, 2020 + * @addtogroup base + */ + +#include + +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#include +#endif + +#ifdef __GNUG__ +#include +#include +#include +#endif + +namespace gtsam { + +/// Pretty print Value type name +std::string demangle(const char* name) { + // by default set to the original mangled name + std::string demangled_name = std::string(name); + +#ifdef __GNUG__ + // g++ version of demangle + char* demangled = nullptr; + int status = -1; // some arbitrary value to eliminate the compiler warning + demangled = abi::__cxa_demangle(name, nullptr, nullptr, &status), + + demangled_name = (status == 0) ? std::string(demangled) : name; + + std::free(demangled); + +#endif +#ifdef _WIN32 + char undecorated_name[1024]; + + if (UnDecorateSymbolName( + name, undecorated_name, sizeof(undecorated_name), + UNDNAME_COMPLETE)) + { + // successful conversion, take value from: undecorated_name + demangled_name = std::string(undecorated_name); + } + // else keep using mangled name +#endif + + return demangled_name; +} + +} /* namespace gtsam */ diff --git a/gtsam/base/types.h b/gtsam/base/types.h index d12a4209a..2fa6eebb6 100644 --- a/gtsam/base/types.h +++ b/gtsam/base/types.h @@ -53,6 +53,9 @@ namespace gtsam { + /// Function to demangle type name of variable, e.g. demangle(typeid(x).name()) + std::string demangle(const char* name); + /// Integer nonlinear key type typedef std::uint64_t Key; diff --git a/gtsam/nonlinear/tests/testValues.cpp b/gtsam/nonlinear/tests/testValues.cpp index b8eee540d..2f624f527 100644 --- a/gtsam/nonlinear/tests/testValues.cpp +++ b/gtsam/nonlinear/tests/testValues.cpp @@ -589,6 +589,23 @@ TEST(Values, MatrixDynamicInsertFixedRead) { CHECK_EXCEPTION(values.at(key1), exception); } +TEST(Values, Demangle) { + Values values; + Matrix13 v; v << 5.0, 6.0, 7.0; + values.insert(key1, v); + string expected = "Values with 1 values:\nValue v1: (Eigen::Matrix) [\n 5, 6, 7\n]\n\n"; + + stringstream buffer; + streambuf * old = cout.rdbuf(buffer.rdbuf()); + + values.print(); + + string actual = buffer.str(); + cout.rdbuf(old); + + EXPECT(assert_equal(expected, actual)); +} + /* ************************************************************************* */ int main() { TestResult tr; return TestRegistry::runAllTests(tr); } /* ************************************************************************* */