Skip to content

Instantly share code, notes, and snippets.

@takumakei
Last active August 29, 2015 14:22
Show Gist options
  • Save takumakei/9fe42a58e140e2b14b6e to your computer and use it in GitHub Desktop.
Save takumakei/9fe42a58e140e2b14b6e to your computer and use it in GitHub Desktop.
// Copyright (C) 2011 RiSK (sscrisk)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if !defined(SSCRISK_DEMANGLE_HPP)
#define SSCRISK_DEMANGLE_HPP
#if defined(_MSC_VER) && _MSC_VER >= 1020
#pragma once
#endif
// demangle.hpp
#include<string>
#include<memory>
#include<new>
#include<stdexcept>
#include<typeinfo>
#include<cstdlib>
#include<cxxabi.h>
namespace sscrisk{
inline std::string demangle(char const * mangled_name)
{
int status;
std::unique_ptr<char, void (*)(void*)> p(abi::__cxa_demangle(mangled_name, 0, 0, &status), std::free);
switch(status) {
case 0: return std::string(p.get());
case -1: throw std::bad_alloc();
case -2: throw std::invalid_argument("mangled_name is not a valid name under the C++ ABI mangling rules.");
case -3: throw std::invalid_argument("One of the arguments is invalid.");
default: throw std::logic_error("abi::__cxa_demangle status is unknown.");
}
}
template<class T>
std::string demangle()
{
return demangle(typeid(T).name());
}
inline std::string demangle(std::type_info const & ti)
{
return demangle(ti.name());
}
}
#define SSCRISK_DEMANGLE(expression) sscrisk::demangle(typeid(expression))
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment