1 | // (C) Copyright 2009 Andrew Sutton |
2 | // |
3 | // Use, modification and distribution are subject to the |
4 | // Boost Software License, Version 1.0 (See accompanying file |
5 | // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) |
6 | |
7 | #ifndef ORIGIN_TYPESTR_HPP |
8 | #define ORIGIN_TYPESTR_HPP |
9 | |
10 | #include <string> |
11 | #include <cstring> |
12 | #include <typeinfo> |
13 | |
14 | #if defined(__GNUC__) |
15 | #include <cxxabi.h> |
16 | #endif |
17 | |
18 | template < typename T > struct type_name |
19 | { |
20 | }; |
21 | |
22 | /** |
23 | * Return a string that describes the type of the given template parameter. |
24 | * The type name depends on the results of the typeid operator. |
25 | * |
26 | * @todo Rewrite this so that demangle will dynamically allocate the memory. |
27 | */ |
28 | template < typename T > std::string typestr() |
29 | { |
30 | #if defined(__GNUC__) |
31 | std::size_t const BUFSIZE = 8192; |
32 | std::size_t n = BUFSIZE; |
33 | char buf[BUFSIZE]; |
34 | abi::__cxa_demangle(mangled_name: typeid(type_name< T >).name(), output_buffer: buf, length: &n, status: 0); |
35 | return std::string(buf, ::strlen(s: buf)); |
36 | #else |
37 | return typeid(type_name< T >).name(); |
38 | #endif |
39 | } |
40 | |
41 | /** |
42 | * Return a string that describes the type of the given parameter. The type |
43 | * name depends on the results of the typeid operator. |
44 | */ |
45 | template < typename T > inline std::string typestr(T const&) |
46 | { |
47 | return typestr< T >(); |
48 | } |
49 | |
50 | #endif |
51 | |