updated demangle code to not use unique_ptr, support for Windows, and a simple test

release/4.3a0
Varun Agrawal 2020-05-18 16:39:03 -05:00
parent 95e7eea4b7
commit d05279ee5e
4 changed files with 89 additions and 27 deletions

View File

@ -20,6 +20,7 @@
#pragma once
#include <gtsam/base/Manifold.h>
#include <gtsam/base/types.h>
#include <gtsam/base/Value.h>
#include <boost/make_shared.hpp>
@ -31,33 +32,6 @@
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
*/

68
gtsam/base/types.cpp Normal file
View File

@ -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 <gtsam/base/types.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <DbgHelp.h>
#endif
#ifdef __GNUG__
#include <cstdlib>
#include <cxxabi.h>
#include <string>
#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 */

View File

@ -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;

View File

@ -589,6 +589,23 @@ TEST(Values, MatrixDynamicInsertFixedRead) {
CHECK_EXCEPTION(values.at<Matrix23>(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<double, 1, 3, 1, 1, 3>) [\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); }
/* ************************************************************************* */