]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Type to string. 7786/head
authorLuca Heltai <luca.heltai@sissa.it>
Thu, 7 Mar 2019 14:51:44 +0000 (15:51 +0100)
committerLuca Heltai <luca.heltai@sissa.it>
Thu, 7 Mar 2019 15:29:19 +0000 (16:29 +0100)
doc/news/changes/minor/20190307LucaHeltai [new file with mode: 0644]
include/deal.II/base/utilities.h
tests/base/utilities_15.cc [new file with mode: 0644]
tests/base/utilities_15.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20190307LucaHeltai b/doc/news/changes/minor/20190307LucaHeltai
new file mode 100644 (file)
index 0000000..3dc9cf1
--- /dev/null
@@ -0,0 +1,4 @@
+New: Added a new function Utilities::type_to_string() to demangle type names.
+<br>
+(Luca Heltai, 2019/03/07)
+
index f49c3021321b07040b3bb5086cc86a13646c2203..9e3c8c278fc47b137c2d1aef65b3eb1bd426ea25 100644 (file)
@@ -24,6 +24,7 @@
 #include <string>
 #include <tuple>
 #include <type_traits>
+#include <typeinfo>
 #include <utility>
 #include <vector>
 
@@ -39,6 +40,7 @@
 
 #include <boost/archive/binary_iarchive.hpp>
 #include <boost/archive/binary_oarchive.hpp>
+#include <boost/core/demangle.hpp>
 #include <boost/serialization/array.hpp>
 #include <boost/serialization/complex.hpp>
 #include <boost/serialization/vector.hpp>
@@ -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 <class T>
+  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 <class T>
+  inline std::string
+  type_to_string(const T &t)
+  {
+    return boost::core::demangle(typeid(t).name());
+  }
+
+
+
   template <typename Iterator, typename T>
   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 (file)
index 0000000..e4054b9
--- /dev/null
@@ -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 <deal.II/base/utilities.h>
+
+#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 (file)
index 0000000..3e6cc9b
--- /dev/null
@@ -0,0 +1,4 @@
+
+DEAL::double
+DEAL::int
+DEAL::dealii::Point<2, double>

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.