https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MooseEnumBase.C
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#include "MooseEnumBase.h"
11#include "MooseUtils.h"
12#include "MooseError.h"
13#include "Conversion.h"
14
15#include <sstream>
16#include <algorithm>
17#include <iterator>
18#include <limits>
19#include <string>
20#include <iostream>
21#include <cstdlib>
22
23MooseEnumBase::MooseEnumBase(std::string names, bool allow_out_of_range)
24 : _allow_out_of_range(allow_out_of_range)
25{
26 if (names.find(',') != std::string::npos)
27 mooseError("Spaces are required to separate options, comma support has been removed.");
28 else
30}
31
33 : _items(other_enum._items),
34 _deprecated_items(other_enum._deprecated_items),
35 _allow_out_of_range(other_enum._allow_out_of_range)
36{
37}
38
42MooseEnumBase::MooseEnumBase() : _allow_out_of_range(false) {}
43
44void
45MooseEnumBase::deprecate(const std::string & name, const std::string & raw_name)
46{
47 std::set<MooseEnumItem>::const_iterator deprecated = find(name);
48 if (deprecated == _items.end())
49 mooseError("Cannot deprecate the enum item ", name, ", is not an available value.");
50
51 std::set<MooseEnumItem>::const_iterator replaced = find(raw_name);
52 if (replaced == _items.end())
53 mooseError("Cannot deprecate the enum item ",
54 name,
55 ", since the replaced item ",
56 raw_name,
57 " it is not an available value.");
58
59 _deprecated_items.emplace(std::make_pair(*deprecated, *replaced));
61}
62
63void
64MooseEnumBase::addEnumerationNames(const std::string & names)
65{
66 std::vector<std::string> elements;
67 MooseUtils::tokenize(names, elements, 1, " ");
68 for (const std::string & raw_name : elements)
69 addEnumerationName(raw_name);
70}
71
72const MooseEnumItem &
73MooseEnumBase::addEnumerationName(const std::string & raw_name)
74{
75 // Make sure the option is not malformed
76 if (raw_name.find_first_of('=') == 0 || raw_name.find_last_of('=') == raw_name.length() - 1)
77 mooseError("You cannot place whitespace around the '=' character in MooseEnumBase");
78
79 // Split on equals sign
80 std::vector<std::string> name_value;
81 MooseUtils::tokenize(MooseUtils::trim(raw_name), name_value, 1, "=");
82
83 // There should be one or two items in the name_value
84 if (name_value.size() < 1 || name_value.size() > 2)
85 mooseError("Invalid option supplied in MooseEnumBase: ", raw_name);
86
87 // Remove un-wanted space around string
88 name_value[0] = MooseUtils::trim(name_value[0]);
89
90 // See if there is a value supplied for this option
91 // strtol allows for proper conversions of both int and hex strings
92 int value;
93 if (name_value.size() == 2)
94 value = std::strtol(name_value[1].c_str(), NULL, 0);
95 else
96 value = getNextValidID();
97
98 return addEnumerationName(name_value[0], value);
99}
100
101int
103{
104 int value = -1; // Use -1 so if no values exist the first will be zero
105 for (const auto & item : _items)
106 value = std::max(value, item.id());
107 return ++value;
108}
109
110const MooseEnumItem &
111MooseEnumBase::addEnumerationName(const std::string & name, const int & value)
112{
113 return addEnumerationItem(MooseEnumItem(name, value));
114}
115
116const MooseEnumItem &
118{
119 const auto & item_it = find(item);
120 if (item_it != _items.end()) // do nothing for identical insertions
121 return *item_it;
122
123 if (find(item.id()) != _items.end())
124 mooseError("The supplied id ",
125 item.id(),
126 " already exists in the enumeration, cannot not add '",
127 item,
128 "'.");
129 if (find(item.name()) != _items.end())
130 mooseError("The name '", item.name(), "' already exists in the enumeration.");
131
132 return *_items.insert(item).first;
133}
134
135void
137{
138 std::map<MooseEnumItem, MooseEnumItem>::const_iterator it = _deprecated_items.find(item);
139 if (it != _deprecated_items.end())
140 {
141 if (it->second.name().empty())
142 mooseWarning(item.name() + " is deprecated");
143 else
144 mooseWarning(item.name() + " is deprecated, consider using " + it->second.name());
145 }
146}
147
148std::vector<std::string>
150{
151 std::vector<std::string> out;
152 out.reserve(_items.size());
153 for (const auto & item : _items)
154 out.push_back(item.name());
155 return out;
156}
157
158std::string
160{
161 return Moose::stringify(_items, " ");
162}
163
164std::vector<int>
166{
167 std::vector<int> out;
168 out.reserve(_items.size());
169 for (const auto & item : _items)
170 out.push_back(item.id());
171 return out;
172}
173
174void
175MooseEnumBase::addDocumentation(const std::string & name, const std::string & doc)
176{
177 auto it = find(name);
178 if (it == _items.end())
179 mooseError("Item '", name, "' not found in addDocumentation.");
180 _item_documentation[*it] = doc;
181}
182
183const std::map<MooseEnumItem, std::string> &
188
189std::set<MooseEnumItem>::const_iterator
190MooseEnumBase::find(const std::string & name) const
191{
192 std::string upper = MooseUtils::toUpper(name);
193 return std::find_if(_items.begin(),
194 _items.end(),
195 [&upper](MooseEnumItem const & item) { return item.name() == upper; });
196}
197
198std::set<MooseEnumItem>::const_iterator
200{
201 return std::find_if(
202 _items.begin(), _items.end(), [&id](MooseEnumItem const & item) { return item.id() == id; });
203}
204
205std::set<MooseEnumItem>::const_iterator
207{
208 const auto upper = MooseUtils::toUpper(other.name());
209 return std::find_if(_items.begin(),
210 _items.end(),
211 [&other, &upper](MooseEnumItem const & item)
212 { return item.id() == other.id() && item.name() == upper; });
213}
214
216MooseEnumBase::operator+=(const std::string & name)
217{
218 addEnumerationName(name);
220 return *this;
221}
222
224MooseEnumBase::operator+=(const std::initializer_list<std::string> & names)
225{
226 for (const auto & name : names)
227 *this += name;
228 return *this;
229}
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition MooseError.h:345
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
The base class for both the MooseEnum and MultiMooseEnum classes.
const std::map< MooseEnumItem, std::string > & getItemDocumentation() const
Get the map containing each item's documentation string.
MooseEnumBase & operator+=(const std::string &name)
Adds an enumeration item from name.
std::set< MooseEnumItem > _items
Storage for the assigned items.
int getNextValidID() const
Compute the next valid ID.
const MooseEnumItem & addEnumerationName(const std::string &raw_name)
std::set< MooseEnumItem >::const_iterator find(const MooseEnumItem &other) const
Locate an item.
const MooseEnumItem & addEnumerationItem(const MooseEnumItem &item)
MooseEnumBase()
Private constuctor for use by libmesh::Parameters.
virtual void checkDeprecated() const =0
Method that must be implemented to check derived class values against the _deprecated_names.
virtual void deprecate(const std::string &name, const std::string &raw_name="")
Deprecates various options in the MOOSE enum.
std::map< MooseEnumItem, MooseEnumItem > _deprecated_items
The map of deprecated names and optional replacements.
std::string getRawNames() const
Method for returning the raw name strings for this instance.
void addEnumerationNames(const std::string &names)
Methods to add possible enumeration value to the enum.
void addDocumentation(const std::string &name, const std::string &doc)
Add an item documentation string.
std::vector< std::string > getNames() const
Method for returning a vector of all valid enumeration names for this instance.
std::vector< int > getIDs() const
Method for returning a vector of ids for this instance.
std::map< MooseEnumItem, std::string > _item_documentation
The map of items and their respective documentation strings.
Class for containing MooseEnum item information.
const std::string & name() const
const int & id() const
Return the numeric, name, or raw name.
std::string toUpper(std::string name)
Convert supplied string to upper case.
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 ...
std::string trim(const std::string &str, const std::string &white_space=" \t\n\v\f\r")
Standard scripting language trim function.
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:64