Skip to content
Snippets Groups Projects
Commit 09ec4971 authored by Andreas Dedner's avatar Andreas Dedner
Browse files

Improve the className function to provide more information

parent 0031e54d
No related branches found
No related tags found
No related merge requests found
......@@ -18,11 +18,12 @@
namespace Dune {
/** \brief Provide the demangled class name of a given object as a string */
/** \brief Provide the demangled class name of a type T as a string */
template <class T>
std::string className ( T &t )
std::string className ()
{
std::string className = typeid( t ).name();
typedef typename std::remove_reference<T>::type TR;
std::string className = typeid( TR ).name();
#if HAVE_CXA_DEMANGLE
int status;
char *demangled = abi::__cxa_demangle( className.c_str(), 0, 0, &status );
......@@ -32,26 +33,23 @@ namespace Dune {
std::free( demangled );
}
#endif // #if HAVE_CXA_DEMANGLE
if (std::is_const<TR>::value)
className += " const";
if (std::is_volatile<TR>::value)
className += " volatile";
if (std::is_lvalue_reference<T>::value)
className += "&";
else if (std::is_rvalue_reference<T>::value)
className += "&&";
return className;
}
/** \brief Provide the demangled class name of a type T as a string */
/** \brief Provide the demangled class name of a given object as a string */
template <class T>
std::string className ()
std::string className ( T& t )
{
std::string className = typeid( T ).name();
#if HAVE_CXA_DEMANGLE
int status;
char *demangled = abi::__cxa_demangle( className.c_str(), 0, 0, &status );
if( demangled )
{
className = demangled;
std::free( demangled );
}
#endif // #if HAVE_CXA_DEMANGLE
return className;
return className<T>();
}
} // namespace Dune
#endif // DUNE_CLASSNAME_HH
......@@ -6,6 +6,7 @@ set(TESTS
bitsetvectortest
calloncetest
check_fvector_size
classnametest
conversiontest
diagonalmatrixtest
dynmatrixtest
......@@ -68,6 +69,7 @@ add_executable("check_fvector_size_fail1" EXCLUDE_FROM_ALL check_fvector_size_fa
set_target_properties(check_fvector_size_fail1 PROPERTIES COMPILE_FLAGS "-DDIM=1")
add_executable("check_fvector_size_fail2" EXCLUDE_FROM_ALL check_fvector_size_fail.cc)
set_target_properties(check_fvector_size_fail2 PROPERTIES COMPILE_FLAGS "-DDIM=3")
add_executable("classnametest" classnametest.cc)
add_executable("conversiontest" conversiontest.cc)
add_executable("dynmatrixtest" dynmatrixtest.cc)
......
......@@ -7,6 +7,7 @@ TESTPROGS = \
bitsetvectortest \
calloncetest \
check_fvector_size \
classnametest \
conversiontest \
diagonalmatrixtest \
dynmatrixtest \
......@@ -104,6 +105,8 @@ check_fvector_size_fail2_CPPFLAGS = $(AM_CPPFLAGS) -DDIM=3
check_fvector_size_SOURCES = check_fvector_size.cc
classnametest_SOURCES = classnametest.cc
conversiontest_SOURCES = conversiontest.cc
diagonalmatrixtest_SOURCES = diagonalmatrixtest.cc
......
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <dune/common/fvector.hh>
#include <dune/common/classname.hh>
#include <iostream>
#include <complex>
using Dune::FieldVector;
using std::complex;
int main()
{
try {
std::cout << "First three simple class names extracted from variables:" << std::endl;
FieldVector<int, 3> xi;
std::cout << className(xi) << std::endl;
FieldVector<double, 1> xd;
std::cout << className(xd) << std::endl;
FieldVector<complex<double>, 10> xcd;
std::cout << className(xcd) << std::endl;
std::cout << std::endl;
std::cout << "Adding const:" << std::endl;
const FieldVector<int, 3> cxi;
std::cout << className(cxi) << std::endl;
std::cout << std::endl;
std::cout << "If a variable is a reference can not be extracted (needs decltype as used below): " << std::endl;
FieldVector<double, 1> &rxd = xd;
std::cout << className(rxd) << std::endl;
std::cout << std::endl;
std::cout << "Extracting the class name using a type directly - "
<< "also extractes references correctly: " << std::endl;
std::cout << Dune::className<decltype(rxd)>() << std::endl;
const FieldVector<double, 1> &rcxd = xd;
std::cout << Dune::className<decltype(rcxd)>() << std::endl;
const FieldVector<int, 3> &rcxi = cxi;
std::cout << Dune::className<decltype(rcxi)>() << std::endl;
std::cout << std::endl;
std::cout << "Test some further types:" << std::endl;
std::cout << Dune::className< volatile FieldVector<complex<double>, 10>& >() << std::endl;
std::cout << Dune::className< FieldVector<complex<double>, 10>&& >() << std::endl;
std::cout << std::endl;
} catch (Dune::Exception& e) {
std::cerr << e << std::endl;
return 1;
} catch (...) {
std::cerr << "Generic exception!" << std::endl;
return 2;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment