LCOV - code coverage report
Current view: top level - include/utils - Conversion.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 24 31 77.4 %
Date: 2025-07-17 01:28:37 Functions: 38 49 77.6 %
Legend: Lines: hit not hit

          Line data    Source code
       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
      24             : class MultiMooseEnum;
      25             : namespace libMesh
      26             : {
      27             : class Point;
      28             : }
      29             : 
      30             : namespace Moose
      31             : {
      32             : // Scalar conversions
      33             : template <typename T>
      34             : T stringToEnum(const std::string & s);
      35             : 
      36             : template <>
      37             : libMesh::QuadratureType stringToEnum<libMesh::QuadratureType>(const std::string & s);
      38             : 
      39             : template <>
      40             : libMesh::Order stringToEnum<libMesh::Order>(const std::string & s);
      41             : 
      42             : template <>
      43             : CoordinateSystemType stringToEnum<CoordinateSystemType>(const std::string & s);
      44             : 
      45             : template <>
      46             : SolveType stringToEnum<SolveType>(const std::string & s);
      47             : 
      48             : template <>
      49             : LineSearchType stringToEnum<LineSearchType>(const std::string & s);
      50             : 
      51             : template <>
      52             : TimeIntegratorType stringToEnum<TimeIntegratorType>(const std::string & s);
      53             : 
      54             : template <>
      55             : RelationshipManagerType stringToEnum<RelationshipManagerType>(const std::string & s);
      56             : 
      57             : // Vector conversions
      58             : template <typename T>
      59             : std::vector<T> vectorStringsToEnum(const MultiMooseEnum & v);
      60             : 
      61             : /// conversion to string
      62             : template <typename T>
      63             : std::string
      64     6689446 : stringify(const T & t)
      65             : {
      66     6689446 :   std::ostringstream os;
      67     6689446 :   os << t;
      68    13378892 :   return os.str();
      69     6689446 : }
      70             : 
      71             : // overload for boolean type instead of simply printing 0 or 1
      72             : inline std::string
      73           0 : stringify(bool v)
      74             : {
      75           0 :   return v ? "true" : "false";
      76             : }
      77             : 
      78             : // overloads for integer types where std::to_string gives the same result and is faster
      79             : inline std::string
      80       62762 : stringify(int v)
      81             : {
      82       62762 :   return std::to_string(v);
      83             : }
      84             : inline std::string
      85             : stringify(long v)
      86             : {
      87             :   return std::to_string(v);
      88             : }
      89             : inline std::string
      90             : stringify(long long v)
      91             : {
      92             :   return std::to_string(v);
      93             : }
      94             : inline std::string
      95       13149 : stringify(unsigned int v)
      96             : {
      97       13149 :   return std::to_string(v);
      98             : }
      99             : inline std::string
     100       73062 : stringify(unsigned long v)
     101             : {
     102       73062 :   return std::to_string(v);
     103             : }
     104             : inline std::string
     105             : stringify(unsigned long long v)
     106             : {
     107             :   return std::to_string(v);
     108             : }
     109             : 
     110             : namespace internal
     111             : {
     112             : template <typename T, typename V>
     113             : inline std::string
     114           0 : stringify_variant(const V & value)
     115             : {
     116           0 :   return std::holds_alternative<T>(value) ? stringify(std::get<T>(value)) : "";
     117             : }
     118             : }
     119             : 
     120             : template <typename... T>
     121             : inline std::string
     122           0 : stringify(std::variant<T...> v)
     123             : {
     124           0 :   return (internal::stringify_variant<T>(v) + ...);
     125             : }
     126             : 
     127             : /// Convert solve type into human readable string
     128             : std::string stringify(const SolveType & t);
     129             : 
     130             : /// Convert eigen solve type into human readable string
     131             : std::string stringify(const EigenSolveType & t);
     132             : 
     133             : /// Convert variable field type into human readable string
     134             : std::string stringify(const VarFieldType & t);
     135             : 
     136             : /// Add no-op stringify if the argument already is a string (must use overloading)
     137             : std::string stringify(const std::string & s);
     138             : 
     139             : /// Convert FEType from libMesh into string
     140             : std::string stringify(libMesh::FEFamily f);
     141             : 
     142             : /// Convert SolutionIterationType into string
     143             : std::string stringify(SolutionIterationType t);
     144             : 
     145             : /// Convert ElementType into string
     146             : std::string stringify(ElementType t);
     147             : 
     148             : /// Convert the libmesh ElemType into string
     149             : std::string stringify(libMesh::ElemType t);
     150             : 
     151             : /// Add pair stringify to support maps
     152             : template <typename T, typename U>
     153             : std::string
     154           3 : stringify(const std::pair<T, U> & p, const std::string & delim = ":")
     155             : {
     156           3 :   return stringify(p.first) + delim + stringify(p.second);
     157             : }
     158             : 
     159             : /**
     160             :  * Convert a container to a string with elements separated by delimiter of user's choice
     161             :  *
     162             :  * Optionally, the container elements can be enclosed by curly braces and can
     163             :  * enclose elements in quotations (or other characters) to make the separation
     164             :  * of elements more clear.
     165             :  *
     166             :  * @param[in] c           Container to stringify
     167             :  * @param[in] delim       String to print between elements
     168             :  * @param[in] elem_encl   String to use at the beginning and end of each element,
     169             :  *                        typically quotation marks
     170             :  * @param[in] enclose_list_in_curly_braces   Enclose the list string in curly braces?
     171             :  */
     172             : template <template <typename...> class T, typename... U>
     173             : std::string
     174     1874904 : stringify(const T<U...> & c,
     175             :           const std::string & delim = ", ",
     176             :           const std::string & elem_encl = "",
     177             :           bool enclose_list_in_curly_braces = false)
     178             : {
     179     1874904 :   std::string str;
     180     1874904 :   if (enclose_list_in_curly_braces)
     181         344 :     str += "{";
     182     1874904 :   const auto begin = c.begin();
     183     1874904 :   const auto end = c.end();
     184    10736561 :   for (auto i = begin; i != end; ++i)
     185     8861657 :     str += (i != begin ? delim : "") + elem_encl + stringify(*i) + elem_encl;
     186     1874904 :   if (enclose_list_in_curly_braces)
     187         344 :     str += "}";
     188     3749808 :   return str;
     189           0 : }
     190             : 
     191             : /**
     192             :  * Stringify Reals with enough precision to guarantee lossless
     193             :  * Real -> string -> Real roundtrips.
     194             :  */
     195             : std::string stringifyExact(Real);
     196             : }
     197             : 
     198             : template <typename T1, typename T2>
     199             : std::vector<T2>
     200             : vectorCast(std::vector<T2> in)
     201             : {
     202             :   std::vector<T2> out(in.begin(), in.end());
     203             :   return out;
     204             : }
     205             : 
     206             : /**
     207             :  * Convert point represented as std::vector into Point
     208             :  * @param pos Point represented as a vector
     209             :  * @return Converted point
     210             :  */
     211             : Point toPoint(const std::vector<Real> & pos);

Generated by: LCOV version 1.14