From 2b4c20d6f4d9fe96e5d278b46eccdba079211967 Mon Sep 17 00:00:00 2001 From: Luca Heltai Date: Thu, 7 Mar 2019 15:51:44 +0100 Subject: [PATCH] Type to string. --- doc/news/changes/minor/20190307LucaHeltai | 4 +++ include/deal.II/base/utilities.h | 23 +++++++++++++++ tests/base/utilities_15.cc | 35 +++++++++++++++++++++++ tests/base/utilities_15.output | 4 +++ 4 files changed, 66 insertions(+) create mode 100644 doc/news/changes/minor/20190307LucaHeltai create mode 100644 tests/base/utilities_15.cc create mode 100644 tests/base/utilities_15.output diff --git a/doc/news/changes/minor/20190307LucaHeltai b/doc/news/changes/minor/20190307LucaHeltai new file mode 100644 index 0000000000..3dc9cf177e --- /dev/null +++ b/doc/news/changes/minor/20190307LucaHeltai @@ -0,0 +1,4 @@ +New: Added a new function Utilities::type_to_string() to demangle type names. +
+(Luca Heltai, 2019/03/07) + diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index f49c302132..9e3c8c278f 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -39,6 +40,7 @@ #include #include +#include #include #include #include @@ -340,6 +342,18 @@ namespace Utilities double generate_normal_random_number(const double a, const double sigma); + /** + * Return a string description of the type of the variable @p t. + * + * In general, C++ uses mangled names to identify types. This function + * uses boost::core::demangle to return a human readable string describing + * the type of the variable passed as argument. + * + * @author Luca Heltai, 2019. + */ + template + std::string + type_to_string(const T &t); /** * Calculate a fixed power, provided as a template argument, of a number. @@ -1019,6 +1033,15 @@ namespace Utilities + template + inline std::string + type_to_string(const T &t) + { + return boost::core::demangle(typeid(t).name()); + } + + + template inline Iterator lower_bound(Iterator first, Iterator last, const T &val) diff --git a/tests/base/utilities_15.cc b/tests/base/utilities_15.cc new file mode 100644 index 0000000000..e4054b9d27 --- /dev/null +++ b/tests/base/utilities_15.cc @@ -0,0 +1,35 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2019 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +// test Utilities::type_to_string() + +#include + +#include "../tests.h" + +int +main() +{ + initlog(); + + double a = 1.0; + int b = 3; + Point<2> c; + + deallog << Utilities::type_to_string(a) << std::endl + << Utilities::type_to_string(b) << std::endl + << Utilities::type_to_string(c) << std::endl; +} diff --git a/tests/base/utilities_15.output b/tests/base/utilities_15.output new file mode 100644 index 0000000000..3e6cc9b262 --- /dev/null +++ b/tests/base/utilities_15.output @@ -0,0 +1,4 @@ + +DEAL::double +DEAL::int +DEAL::dealii::Point<2, double> -- 2.39.5