www.mooseframework.org
MooseEnumItem.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 // MOOSE includes
11 #include "MooseEnumItem.h"
12 #include "MooseUtils.h"
13 
14 MooseEnumItem::MooseEnumItem(const std::string & name, const int & id)
15  : _raw_name(MooseUtils::trim(name)), _name(MooseUtils::toUpper(_raw_name)), _id(id)
16 {
17 }
18 
20  : _raw_name(other._raw_name), _name(other._name), _id(other._id)
21 {
22 }
23 
26 {
27  _raw_name = other._raw_name;
28  _name = other._name;
29  _id = other._id;
30  return *this;
31 }
32 
33 bool
34 MooseEnumItem::operator==(const char * value) const
35 {
36  std::string name(MooseUtils::toUpper(value));
37  return _name == name;
38 }
39 
40 bool
41 MooseEnumItem::operator!=(const char * value) const
42 {
43  std::string name(MooseUtils::toUpper(value));
44  return _name != name;
45 }
46 
47 bool
48 MooseEnumItem::operator==(const std::string & value) const
49 {
50  std::string name(MooseUtils::toUpper(value));
51  return _name == name;
52 }
53 
54 bool
55 MooseEnumItem::operator!=(const std::string & value) const
56 {
57  std::string name(MooseUtils::toUpper(value));
58  return _name != name;
59 }
60 
61 bool
63 {
64  return _id == item.id() && _name == MooseUtils::toUpper(item.name());
65 }
66 
67 bool
69 {
70  return _id != item.id() && _name != MooseUtils::toUpper(item.name());
71 }
72 
73 std::ostream &
74 operator<<(std::ostream & out, const MooseEnumItem & item)
75 {
76  out << item.rawName();
77  return out;
78 }
79 
80 void
81 MooseEnumItem::setID(const int & id)
82 {
83  if (_id != INVALID_ID)
84  mooseError("The ID of a MooseEnumItem can not be changed if it is valid, the item ",
85  _name,
86  " has a valid id of ",
87  _id,
88  ".");
89  _id = id;
90 }
91 
92 const int MooseEnumItem::INVALID_ID = std::numeric_limits<int>::min();
void setID(const int &id)
Method to change the ID of the item, but only if it is an INVALID_ID.
Definition: MooseEnumItem.C:81
const std::string & name() const
Definition: MooseEnumItem.h:37
std::string toUpper(const std::string &name)
Convert supplied string to upper case.
Definition: MooseUtils.C:651
int _id
The numeric value for item.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
std::string _name
Upper case name.
Definition: MooseEnumItem.h:98
static const int INVALID_ID
Definition: MooseEnumItem.h:24
MooseEnumItem & operator=(const MooseEnumItem &other)
Definition: MooseEnumItem.C:25
std::string _raw_name
The name as provided in constructor.
Definition: MooseEnumItem.h:95
bool operator==(const char *value) const
Comparison operators.
Definition: MooseEnumItem.C:34
std::ostream & operator<<(std::ostream &out, const MooseEnumItem &item)
Definition: MooseEnumItem.C:74
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.
Definition: MooseUtils.C:113
const std::string & rawName() const
Definition: MooseEnumItem.h:38
bool operator!=(const char *value) const
Definition: MooseEnumItem.C:41
const int & id() const
Return the numeric, name, or raw name.
Definition: MooseEnumItem.h:36
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:21
MooseEnumItem(const std::string &name, const int &id=INVALID_ID)
Definition: MooseEnumItem.C:14