www.mooseframework.org
MooseEnumBase.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "MooseEnumBase.h"
16 #include "MooseUtils.h"
17 #include "MooseError.h"
18 #include "Conversion.h"
19 
20 #include <sstream>
21 #include <algorithm>
22 #include <iterator>
23 #include <limits>
24 #include <string>
25 #include <iostream>
26 
27 const int MooseEnumBase::INVALID_ID = std::numeric_limits<int>::min();
28 
29 MooseEnumBase::MooseEnumBase(std::string names, bool allow_out_of_range)
30  : _out_of_range_index(allow_out_of_range ? INVALID_ID + 1 : 0)
31 {
32  if (names.find(',') != std::string::npos)
33  {
34  mooseDeprecated("Please use a space to separate options in a MooseEnum, commas are "
35  "deprecated\nMooseEnum initialized with names: \"",
36  names,
37  '\"');
38  fillNames(names, ",");
39  }
40  else
41  fillNames(names);
42 }
43 
45  : _items(other_enum._items),
48 {
49 }
50 
55 
56 void
57 MooseEnumBase::deprecate(const std::string & name, const std::string & new_name)
58 {
59  std::string upper(MooseUtils::toUpper(name));
60  std::string upper_new(MooseUtils::toUpper(new_name));
61  _deprecated_names[upper] = upper_new;
63 }
64 
65 void
66 MooseEnumBase::fillNames(std::string names, std::string option_delim)
67 {
68  std::vector<std::string> elements;
69  // split on spaces
70  MooseUtils::tokenize(names, elements, 1, option_delim);
71 
72  int value = 0;
73  for (unsigned int i = 0; i < elements.size(); ++i)
74  {
75  std::vector<std::string> name_value;
76 
77  // Make sure the option is not malformed
78  if (elements[i].find_first_of('=') == 0 ||
79  elements[i].find_last_of('=') == elements[i].length() - 1)
80  mooseError("You cannot place whitespace around the '=' character in MooseEnumBase");
81 
82  // split on equals sign
83  MooseUtils::tokenize(MooseUtils::trim(elements[i]), name_value, 1, "=");
84 
85  if (name_value.size() < 1 || name_value.size() > 2)
86  mooseError("Invalid option supplied in MooseEnumBase: ", elements[i]);
87 
88  // See if there is a value supplied for this option
89  // strtol allows for proper conversions of both int and hex strings
90  if (name_value.size() == 2)
91  value = strtol(name_value[1].c_str(), NULL, 0);
92 
93  // create item entry
94  _items.emplace(name_value[0], value++);
95  }
96 }
97 
98 void
99 MooseEnumBase::checkDeprecatedBase(const std::string & name_upper) const
100 {
101  std::map<std::string, std::string>::const_iterator it = _deprecated_names.find(name_upper);
102 
103  if (it != _deprecated_names.end())
104  {
105  if (it->second != "")
106  mooseWarning(name_upper + " is deprecated, consider using " + it->second);
107  else
108  mooseWarning(name_upper + " is deprecated");
109  }
110 }
111 
112 std::vector<std::string>
114 {
115  std::vector<std::string> out;
116  out.reserve(_items.size());
117  for (auto it = _items.begin(); it != _items.end(); ++it)
118  out.push_back(it->name());
119  return out;
120 }
121 
122 std::string
124 {
125  return Moose::stringify(_items, " ");
126 }
127 
128 std::set<MooseEnumItem>::const_iterator
129 MooseEnumBase::find(const std::string & name) const
130 {
131  return std::find_if(_items.begin(), _items.end(), [&name](MooseEnumItem const & item) {
132  return item.name() == name;
133  });
134 }
135 
136 std::set<MooseEnumItem>::const_iterator
137 MooseEnumBase::find(int id) const
138 {
139  return std::find_if(
140  _items.begin(), _items.end(), [&id](MooseEnumItem const & item) { return item.id() == id; });
141 }
142 
143 std::set<MooseEnumItem>::const_iterator
144 MooseEnumBase::find(const MooseEnumItem & other) const
145 {
146  return std::find_if(
147  _items.begin(), _items.end(), [&other](MooseEnumItem const & item) { return item == other; });
148 }
std::vector< std::string > getNames() const
Method for returning a vector of all valid enumeration names for this instance.
void checkDeprecatedBase(const std::string &name_upper) const
Check and warn deprecated values.
Definition: MooseEnumBase.C:99
void tokenize(const std::string &str, std::vector< T > &elements, unsigned int min_len=1, const std::string &delims="/")
This function will split the passed in string on a set of delimiters appending the substrings to the ...
Definition: MooseUtils.h:350
std::string toUpper(const std::string &name)
Convert supplied string to upper case.
Definition: MooseUtils.C:555
void fillNames(std::string names, std::string option_delim=" ")
Populates the _names vector.
Definition: MooseEnumBase.C:66
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
void mooseDeprecated(Args &&...args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:202
MooseEnumBase()
Private constuctor for use by libmesh::Parameters.
Definition: MooseEnumBase.C:54
std::map< std::string, std::string > _deprecated_names
The map of deprecated names and optional replacements.
int _out_of_range_index
The index of values assigned that are NOT values in this enum.
The base class for both the MooseEnum and MultiMooseEnum classes.
Definition: MooseEnumBase.h:30
static const int INVALID_ID
Constants.
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:66
std::set< MooseEnumItem > _items
Storage for the assigned items.
std::string getRawNames() const
Method for returning the raw name strings for this instance.
virtual void checkDeprecated() const =0
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:24
std::string trim(const std::string &str, const std::string &white_space=" \t\n\v\f\r")
Standard scripting language trim function.
Definition: MooseUtils.C:95
std::set< MooseEnumItem >::const_iterator find(const MooseEnumItem &other) const
Locate an item.
void deprecate(const std::string &name, const std::string &new_name="")
Deprecates various options in the MOOSE enum.
Definition: MooseEnumBase.C:57
void mooseWarning(Args &&...args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:194