ClassName.hpp
Go to the documentation of this file.
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  Copyright (C) 2012-2013 by Andreas Lauser
5  Copyright (C) 2010 by Oliver Sander
6  Copyright (C) 2011 by Martin Nolte
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, version 2.
11 
12  This file is based on works developed by the DUNE project, see
13  <http://www.dune-project.org>. The following license exception
14  applies to it:
15 
16  As a special exception, you may use the DUNE library without
17  restriction. Specifically, if other files instantiate templates or
18  use macros or inline functions from one or more of the DUNE source
19  files, or you compile one or more of the DUNE source files and link
20  them with other files to produce an executable, this does not by
21  itself cause the resulting executable to be covered by the GNU
22  General Public License. This exception does not however invalidate
23  any other reasons why the executable file might be covered by the
24  GNU General Public License.
25 
26  This program is distributed in the hope that it will be useful,
27  but WITHOUT ANY WARRANTY; without even the implied warranty of
28  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29  GNU General Public License for more details.
30 
31  You should have received a copy of the GNU General Public License
32  along with this program. If not, see <http://www.gnu.org/licenses/>.
33 */
39 #ifndef OPM_CLASS_NAME_HPP
40 #define OPM_CLASS_NAME_HPP
41 
42 #include <cstdlib>
43 #include <string>
44 #include <typeinfo>
45 
46 #if HAVE_CXA_DEMANGLE
47 #include <cxxabi.h>
48 #endif
49 
50 namespace Opm {
52 template <class T>
53 std::string className()
54 {
55  std::string className = typeid( T ).name();
56 #if HAVE_CXA_DEMANGLE
57  int status;
58  char *demangled = abi::__cxa_demangle( className.c_str(), 0, 0, &status );
59  if( demangled )
60  {
61  className = demangled;
62  std::free( demangled );
63  }
64 #endif // HAVE_CXA_DEMANGLE
65  return className;
66 }
67 
68 #if HAVE_QUAD
69 // specialize for quad precision floating point values to avoid
70 // needing a type_info structure
71 template <>
72 std::string className<__float128>()
73 { return "quad"; }
74 #endif
75 
77 template <class T>
78 std::string className(const T &)
79 {
80  return className<T>();
81 }
82 } // namespace Opm
83 
84 #endif // OPM_CLASS_NAME_HPP
Definition: Air_Mesitylene.hpp:31
std::string className()
Provide the demangled class name of a given object as a string.
Definition: ClassName.hpp:53