www.mooseframework.org
Conversion.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 // MOOSE includes
13 #include "MooseTypes.h"
14 
15 // libMesh
16 #include "libmesh/enum_order.h"
17 #include "libmesh/enum_quadrature_type.h"
18 #include "libmesh/enum_fe_family.h"
19 #include "libmesh/enum_elem_type.h"
20 
21 // Forward declarations
22 class MultiMooseEnum;
23 namespace libMesh
24 {
25 class Point;
26 }
27 
28 namespace Moose
29 {
30 // Scalar conversions
31 template <typename T>
32 T stringToEnum(const std::string & s);
33 
34 template <>
35 QuadratureType stringToEnum<QuadratureType>(const std::string & s);
36 
37 template <>
38 Order stringToEnum<Order>(const std::string & s);
39 
40 template <>
42 
43 template <>
44 SolveType stringToEnum<SolveType>(const std::string & s);
45 
46 template <>
47 LineSearchType stringToEnum<LineSearchType>(const std::string & s);
48 
49 template <>
51 
52 template <>
54 
55 // Vector conversions
56 template <typename T>
57 std::vector<T> vectorStringsToEnum(const MultiMooseEnum & v);
58 
60 template <typename T>
61 std::string
62 stringify(const T & t)
63 {
64  std::ostringstream os;
65  os << t;
66  return os.str();
67 }
68 
69 // overload for boolean type instead of simply printing 0 or 1
70 inline std::string
71 stringify(bool v)
72 {
73  return v ? "true" : "false";
74 }
75 
76 // overloads for integer types where std::to_string gives the same result and is faster
77 inline std::string
78 stringify(int v)
79 {
80  return std::to_string(v);
81 }
82 inline std::string
83 stringify(long v)
84 {
85  return std::to_string(v);
86 }
87 inline std::string
88 stringify(long long v)
89 {
90  return std::to_string(v);
91 }
92 inline std::string
93 stringify(unsigned int v)
94 {
95  return std::to_string(v);
96 }
97 inline std::string
98 stringify(unsigned long v)
99 {
100  return std::to_string(v);
101 }
102 inline std::string
103 stringify(unsigned long long v)
104 {
105  return std::to_string(v);
106 }
107 
109 std::string stringify(const SolveType & t);
110 
112 std::string stringify(const EigenSolveType & t);
113 
115 std::string stringify(const VarFieldType & t);
116 
118 std::string stringify(const std::string & s);
119 
121 std::string stringify(FEFamily f);
122 
124 std::string stringify(SolutionIterationType t);
125 
127 std::string stringify(ElementType t);
128 
130 std::string stringify(libMesh::ElemType t);
131 
133 template <typename T, typename U>
134 std::string
135 stringify(const std::pair<T, U> & p, const std::string & delim = ":")
136 {
137  return stringify(p.first) + delim + stringify(p.second);
138 }
139 
153 template <template <typename...> class T, typename... U>
154 std::string
155 stringify(const T<U...> & c,
156  const std::string & delim = ", ",
157  const std::string & elem_encl = "",
158  bool enclose_list_in_curly_braces = false)
159 {
160  std::string str;
161  if (enclose_list_in_curly_braces)
162  str += "{";
163  const auto begin = c.begin();
164  const auto end = c.end();
165  for (auto i = begin; i != end; ++i)
166  str += (i != begin ? delim : "") + elem_encl + stringify(*i) + elem_encl;
167  if (enclose_list_in_curly_braces)
168  str += "}";
169  return str;
170 }
171 
176 std::string stringifyExact(Real);
177 }
178 
179 template <typename T1, typename T2>
180 std::vector<T2>
181 vectorCast(std::vector<T2> in)
182 {
183  std::vector<T2> out(in.begin(), in.end());
184  return out;
185 }
186 
192 Point toPoint(const std::vector<Real> & pos);
VarFieldType
Definition: MooseTypes.h:634
Order
EigenSolveType
Type of the eigen solve.
Definition: MooseTypes.h:767
RelationshipManagerType
Main types of Relationship Managers.
Definition: MooseTypes.h:876
T stringToEnum(const std::string &s)
CoordinateSystemType stringToEnum< CoordinateSystemType >(const std::string &s)
Definition: Conversion.C:186
QuadratureType
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
ElementType
Definition: MooseTypes.h:676
std::basic_ostream< charT, traits > * os
Definition: InfixIterator.h:33
TimeIntegratorType stringToEnum< TimeIntegratorType >(const std::string &s)
Definition: Conversion.C:276
Order stringToEnum< Order >(const std::string &s)
Definition: Conversion.C:173
std::string stringifyExact(Real)
Stringify Reals with enough precision to guarantee lossless Real -> string -> Real roundtrips...
Definition: Conversion.C:464
LineSearchType
Type of the line search.
Definition: MooseTypes.h:838
Point toPoint(const std::vector< Real > &pos)
Convert point represented as std::vector into Point.
Definition: Conversion.C:476
QuadratureType stringToEnum< QuadratureType >(const std::string &s)
Definition: Conversion.C:166
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:62
LineSearchType stringToEnum< LineSearchType >(const std::string &s)
Definition: Conversion.C:261
SolveType stringToEnum< SolveType >(const std::string &s)
Definition: Conversion.C:201
std::vector< T2 > vectorCast(std::vector< T2 > in)
Definition: Conversion.h:181
std::vector< T > vectorStringsToEnum(const MultiMooseEnum &v)
CoordinateSystemType
Definition: MooseTypes.h:722
OStreamProxy out
TimeIntegratorType
Time integrators.
Definition: MooseTypes.h:815
RelationshipManagerType stringToEnum< RelationshipManagerType >(const std::string &s)
Definition: Conversion.C:306
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
FEFamily
SolveType
Type of the solve.
Definition: MooseTypes.h:755
SolutionIterationType
Definition: MooseTypes.h:229