From: Luca Heltai Date: Thu, 7 Mar 2019 14:51:44 +0000 (+0100) Subject: Type to string. X-Git-Tag: v9.1.0-rc1~301^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F7786%2Fhead;p=dealii.git Type to string. --- 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>