https://mooseframework.inl.gov
THMEnums.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 #include <algorithm>
13 #include "MooseEnum.h"
14 
15 namespace THM
16 {
17 
27 template <typename T>
28 T stringToEnum(const std::string & s);
29 
37 template <typename T>
38 T stringToEnum(const std::string & s, const std::map<std::string, T> & enum_map);
39 
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 THM::stringToEnum(const std::string & s, const std::map<std::string, T> & enum_map)
54 {
55  std::string upper(s);
56  std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
57 
58  if (!enum_map.count(upper))
59  return static_cast<T>(-100);
60  else
61  return enum_map.at(upper);
62 }
63 
64 template <typename T>
66 THM::getMooseEnum(const std::string & default_key, const std::map<std::string, T> & enum_map)
67 {
68  std::string keys_string;
69  for (typename std::map<std::string, T>::const_iterator it = enum_map.begin();
70  it != enum_map.end();
71  it++)
72  if (it == enum_map.begin())
73  keys_string += it->first;
74  else
75  keys_string += " " + it->first;
76 
77  return MooseEnum(keys_string, default_key, true);
78 }
Component1DConnection::EEndType stringToEnum(const std::string &s)
MooseEnum getMooseEnum(const std::string &default_key, const std::map< std::string, T > &enum_map)
Gets MooseEnum corresponding to an enum, using a map of string to enum.
Definition: THMEnums.h:66