DUNE-ACFEM (unstable)

tostring.hh
1#ifndef __DUNE_ACFEM_COMMON_TOSTRING_HH__
2#define __DUNE_ACFEM_COMMON_TOSTRING_HH__
3
4#include <string>
5#include <sstream>
6
7#include <dune/common/streamoperators.hh>
8#include "types.hh"
9#include "typestring.hh"
10
11namespace Dune
12{
13 namespace ACFem
14 {
15 using Dune::operator<<;
16
17 template<class T, class SFINAE = void>
18 struct HasStdToString
19 : FalseType
20 {};
21
22 template<class T>
23 struct HasStdToString<T, VoidType<decltype(std::to_string(std::declval<T>()))> >
24 : TrueType
25 {};
26
27 template<class T, class SFINAE = void>
28 struct IsPrintable
29 : FalseType
30 {};
31
32 template<class T>
33 struct IsPrintable<T, VoidType<decltype(std::declval<std::ostream&>() << std::declval<T>())> >
34 : TrueType
35 {};
36
37 template<class T, class SFINAE = void>
38 struct HasNameMethod
39 : FalseType
40 {};
41
42 template<class T>
43 struct HasNameMethod<T, VoidType<decltype(std::declval<T>().name())> >
44 : TrueType
45 {};
46
47 namespace {
48
50 template<class T, std::enable_if_t<!IsPrintable<T>::value, int> = 0>
51 std::string toString(T&& t, PriorityTag<0>)
52 {
53 return typeString(std::forward<T>(t));
54 }
55
57 template<class T, std::enable_if_t<IsPrintable<T>::value, int> = 0>
58 std::string toString(T&& t, PriorityTag<8>)
59 {
60 std::ostringstream ss;
61 ss << std::forward<T>(t);
62 return ss.str();
63 }
64
65 template<class T, std::enable_if_t<HasStdToString<T>::value, int> = 0>
66 std::string toString(T&& t, PriorityTag<9>)
67 {
68#if 1
69 // assume that anything with std::to_string() can also be printed ...
70 std::ostringstream ss;
71 ss << std::forward<T>(t);
72 return ss.str();
73#else
74 // just too unflexible
75 return std::to_string(std::forward<T>(t));
76#endif
77 }
78
80 template<class T, std::enable_if_t<(sizeof(std::declval<T>().name()) > 0), int> = 0>
81 std::string toString(T&& t, PriorityTag<10>)
82 {
83 return std::forward<T>(t).name();
84 }
85 }
86
87 template<class T>
88 std::string toString(T&& t)
89 {
90 return toString(std::forward<T>(t), PriorityTag<42>{});
91 }
92
93 } // NS ACFem
94
95} // NS Dune
96
97#endif // __DUNE_ACFEM_COMMON_TOSTRING_HH__
auto typeString(T &&t, bool stripNameSpaces=true)
Generator for TypeString.
Definition: typestring.hh:144
typename MakeType< void, Other... >::Type VoidType
Generate void regardless of the template argument list.
Definition: types.hh:151
BoolConstant< false > FalseType
Alias for std::false_type.
Definition: types.hh:110
BoolConstant< true > TrueType
Alias for std::true_type.
Definition: types.hh:107
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Jun 19, 23:17, 2026)