From: Leandro Lucarella Date: Tue, 19 Aug 2003 03:39:26 +0000 (+0000) Subject: Added a typeid() demangle example. X-Git-Tag: svn_import X-Git-Url: https://git.llucax.com/software/bife/bife%20%20.git/commitdiff_plain/refs/heads/master/software/bife/bife++.git/commitdiff_plain/refs/heads/master Added a typeid() demangle example. --- diff --git a/tests/test_typeid.cpp b/tests/test_typeid.cpp new file mode 100644 index 0000000..cb8cdc6 --- /dev/null +++ b/tests/test_typeid.cpp @@ -0,0 +1,65 @@ +// vim: set et ts=4 sw=4: + +#include +#include +#include +#include + +using namespace std; + +namespace tst { +class Test { +}; +class Test2 { +}; +} + +namespace test { +class Test { +}; +class MiTest { +}; +} + +string type(const type_info& type) { + string ret; + int status; + char* demangle = abi::__cxa_demangle(type.name(), 0, 0, &status); + if (status == 0) { + ret = demangle; + free(demangle); + } else if (status == -3) { + cerr << "Error: Invalid argument." << endl; + } else if (status == -2) { + cerr << "Error: " << type.name() + << " is not a valid name under the C++ ABI mangling rules." + << endl; + } else if (status == -1) { + cerr << "Error: Memory allocation problem." << endl; + } + return ret; +} + + +int main(void) { + tst::Test t; + tst::Test2 t2; + test::Test t3; + test::MiTest t4; + cout << "cout es un '" << type(typeid(cout)) << "'." << endl; + cout << "t es un '" << type(typeid(t)) << "'." << endl; + cout << "t2 es un '" << type(typeid(t2)) << "'." << endl; + cout << "t3 es un '" << type(typeid(t3)) << "'." << endl; + cout << "t4 es un '" << type(typeid(t4)) << "'." << endl; + cout << "tst::Test es un '" << type(typeid(tst::Test)) << "'." << endl; + cout << "int es un '" << type(typeid(int)) << "'." << endl; + + int status; + cout << abi::__cxa_demangle("_ZN4bife9TranslateC2Ev", 0, 0, &status) << endl; + cout << abi::__cxa_demangle("_ZN4bife9TranslateD0Ev", 0, 0, &status) << endl; + cout << abi::__cxa_demangle("_ZN4bife9TranslateD0Ev", 0, 0, &status) << endl; + cout << abi::__cxa_demangle("_ZN4bife9TranslateD1Ev", 0, 0, &status) << endl; + cout << abi::__cxa_demangle("_ZN4bife9TranslateC2ERKSt3mapISsSsSt4lessISsESaISt4pairIKSsSsEEE", 0, 0, &status) << endl; + //void* dlh = dlopen(sharedObject.c_str(), RTLD_LAZY); + //Widget* w = (Widget*)dlsym(dlh, "_ZN4bife9TranslateD2Ev"); +}