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 : #include <algorithm> 13 : #include "MooseEnum.h" 14 : 15 : namespace THM 16 : { 17 : 18 : /** 19 : * Converts a string to an enum 20 : * 21 : * This template is designed to be specialized and use the other version of this 22 : * function in conjunction with the correct map. 23 : * 24 : * @tparam T enum type 25 : * @param[in] s string to convert 26 : */ 27 : template <typename T> 28 : T stringToEnum(const std::string & s); 29 : 30 : /** 31 : * Converts a string to an enum using a map of string to enum 32 : * 33 : * @tparam T enum type 34 : * @param[in] s string to convert 35 : * @param[in] enum_map map of string to enum 36 : */ 37 : template <typename T> 38 : T stringToEnum(const std::string & s, const std::map<std::string, T> & enum_map); 39 : 40 : /** 41 : * Gets MooseEnum corresponding to an enum, using a map of string to enum 42 : * 43 : * @tparam T enum type 44 : * @param[in] default_key key corresponding to default value 45 : * @param[in] enum_map map of string to enum 46 : */ 47 : template <typename T> 48 : MooseEnum getMooseEnum(const std::string & default_key, const std::map<std::string, T> & enum_map); 49 : } 50 : 51 : template <typename T> 52 : T 53 32005 : THM::stringToEnum(const std::string & s, const std::map<std::string, T> & enum_map) 54 : { 55 32005 : std::string upper(s); 56 : std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper); 57 : 58 : if (!enum_map.count(upper)) 59 21 : return static_cast<T>(-100); 60 : else 61 31984 : return enum_map.at(upper); 62 : } 63 : 64 : template <typename T> 65 : MooseEnum 66 45558 : THM::getMooseEnum(const std::string & default_key, const std::map<std::string, T> & enum_map) 67 : { 68 : std::string keys_string; 69 45558 : for (typename std::map<std::string, T>::const_iterator it = enum_map.begin(); 70 239224 : it != enum_map.end(); 71 : it++) 72 193666 : if (it == enum_map.begin()) 73 : keys_string += it->first; 74 : else 75 296216 : keys_string += " " + it->first; 76 : 77 91116 : return MooseEnum(keys_string, default_key, true); 78 : }