https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Conversion.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
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#include <variant>
22
23// Forward declarations
24class MultiMooseEnum;
25namespace libMesh
26{
27class Point;
28}
29
30namespace Moose
31{
32// Scalar conversions
33template <typename T>
34T stringToEnum(const std::string & s);
35
36template <>
38
39template <>
41
42template <>
44
45template <>
46SolveType stringToEnum<SolveType>(const std::string & s);
47
48template <>
49LineSearchType stringToEnum<LineSearchType>(const std::string & s);
50
51template <>
53
54template <>
56
57// Vector conversions
58template <typename T>
59std::vector<T> vectorStringsToEnum(const MultiMooseEnum & v);
60
62template <typename T>
63std::string
64stringify(const T & t)
65{
66 std::ostringstream os;
67 os << t;
68 return os.str();
69}
70
71// overload for boolean type instead of simply printing 0 or 1
72inline std::string
73stringify(bool v)
74{
75 return v ? "true" : "false";
76}
77
78// overloads for integer types where std::to_string gives the same result and is faster
79inline std::string
81{
82 return std::to_string(v);
83}
84inline std::string
85stringify(long v)
86{
87 return std::to_string(v);
88}
89inline std::string
90stringify(long long v)
91{
92 return std::to_string(v);
93}
94inline std::string
95stringify(unsigned int v)
96{
97 return std::to_string(v);
98}
99inline std::string
100stringify(unsigned long v)
101{
102 return std::to_string(v);
103}
104inline std::string
105stringify(unsigned long long v)
106{
107 return std::to_string(v);
108}
109
110namespace internal
111{
112template <typename T, typename V>
113inline std::string
114stringify_variant(const V & value)
115{
116 return std::holds_alternative<T>(value) ? stringify(std::get<T>(value)) : "";
117}
118}
119
120template <typename... T>
121inline std::string
122stringify(std::variant<T...> v)
123{
124 return (internal::stringify_variant<T>(v) + ...);
125}
126
128std::string stringify(const SolveType & t);
129
131std::string stringify(const EigenSolveType & t);
132
134std::string stringify(const VarFieldType & t);
135
137std::string stringify(const std::string & s);
138
140std::string stringify(libMesh::FEFamily f);
141
143std::string stringify(SolutionIterationType t);
144
146std::string stringify(ElementType t);
147
149std::string stringify(libMesh::ElemType t);
150
152template <typename T, typename U>
153std::string
154stringify(const std::pair<T, U> & p, const std::string & delim = ":")
155{
156 return stringify(p.first) + delim + stringify(p.second);
157}
158
160template <typename... Args>
161std::string
162stringify(const std::tuple<Args...> & t, const std::string & delim = ":")
163{
164 if constexpr (sizeof...(Args) == 0)
165 {
166 return "";
167 }
168
169 return std::apply(
170 [&delim](const auto &... args)
171 {
172 std::size_t n{0};
173 std::string result;
174 ((result += (n++ == 0 ? "" : delim) + stringify(args)), ...);
175 return result;
176 },
177 t);
178}
179
193template <template <typename...> class T, typename... U>
194std::string
195stringify(const T<U...> & c,
196 const std::string & delim = ", ",
197 const std::string & elem_encl = "",
198 bool enclose_list_in_curly_braces = false)
199{
200 std::string str;
201 if (enclose_list_in_curly_braces)
202 str += "{";
203 const auto begin = c.begin();
204 const auto end = c.end();
205 for (auto i = begin; i != end; ++i)
206 str += (i != begin ? delim : "") + elem_encl + stringify(*i) + elem_encl;
207 if (enclose_list_in_curly_braces)
208 str += "}";
209 return str;
210}
211
216std::string stringifyExact(Real);
217}
218
219template <typename T1, typename T2>
220std::vector<T2>
221vectorCast(std::vector<T2> in)
222{
223 std::vector<T2> out(in.begin(), in.end());
224 return out;
225}
226
232Point toPoint(const std::vector<Real> & pos);
Point toPoint(const std::vector< Real > &pos)
Convert point represented as std::vector into Point.
std::vector< T2 > vectorCast(std::vector< T2 > in)
Definition Conversion.h:221
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type.
std::string stringify_variant(const V &value)
Definition Conversion.h:114
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
SolveType stringToEnum< SolveType >(const std::string &s)
Definition Conversion.C:203
LineSearchType stringToEnum< LineSearchType >(const std::string &s)
Definition Conversion.C:263
std::vector< T > vectorStringsToEnum(const MultiMooseEnum &v)
T stringToEnum(const std::string &s)
SolveType
Type of the solve.
Definition MooseTypes.h:897
libMesh::Order stringToEnum< libMesh::Order >(const std::string &s)
TimeIntegratorType
Time integrators.
Definition MooseTypes.h:957
RelationshipManagerType stringToEnum< RelationshipManagerType >(const std::string &s)
Definition Conversion.C:308
LineSearchType
Type of the line search.
Definition MooseTypes.h:980
TimeIntegratorType stringToEnum< TimeIntegratorType >(const std::string &s)
Definition Conversion.C:278
CoordinateSystemType stringToEnum< CoordinateSystemType >(const std::string &s)
Definition Conversion.C:188
libMesh::QuadratureType stringToEnum< libMesh::QuadratureType >(const std::string &s)
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:64
RelationshipManagerType
Main types of Relationship Managers.
CoordinateSystemType
Definition MooseTypes.h:864
SolutionIterationType
Definition MooseTypes.h:270
std::string stringifyExact(Real)
Stringify Reals with enough precision to guarantee lossless Real -> string -> Real roundtrips.
Definition Conversion.C:495
EigenSolveType
Type of the eigen solve.
Definition MooseTypes.h:909
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...