www.mooseframework.org
Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
ControllableItem Class Reference

An intermediate object for building a "controllable item", where an "item" can refer to multiple input parameters with different names. More...

#include <ControllableItem.h>

Inheritance diagram for ControllableItem:
[legend]

Public Member Functions

 ControllableItem (const MooseObjectParameterName &name, libMesh::Parameters::Value *value)
 
virtual ~ControllableItem ()=default
 
 ControllableItem (const ControllableItem &)=default
 
 ControllableItem (ControllableItem &&)=default
 
ControllableItemoperator= (const ControllableItem &)=delete
 
ControllableItemoperator= (ControllableItem &&)=delete
 
void connect (ControllableItem *item, bool type_check=true)
 Connects the supplied item with this item to allow for multiple parameters to be changed by one. More...
 
template<typename T >
void set (const T &value, bool type_check=true)
 Set the value(s) of the controlled parameters stored in this class. More...
 
template<typename T >
std::vector< T > get (bool type_check=true) const
 Return a copy of all values for this "item". More...
 
template<typename T >
bool check () const
 Return true if the template argument is valid for ALL items. More...
 
virtual const MooseObjectParameterNamename () const
 Return the name of the master parameter. More...
 
std::string type () const
 Return the type of the master parameter. More...
 
virtual std::string dump (unsigned int indent=0) const
 Returns a string displaying the parameter name and current value. More...
 
bool operator== (const ControllableItem &rhs) const
 Use the master name for comparison operators to allow object to work within a set/map. More...
 
bool operator!= (const ControllableItem &rhs) const
 
bool operator< (const ControllableItem &rhs) const
 
void resetChanged ()
 Methods for ControlOutput::outputChangedControls, these don't have meaning outside of this function. More...
 
bool isChanged ()
 

Protected Member Functions

 ControllableItem ()
 Constructor for creating an empty item (see ControllableAlias) More...
 

Protected Attributes

std::vector< std::pair< MooseObjectParameterName, libMesh::Parameters::Value * > > _pairs
 List of names for this item. More...
 
bool _changed = false
 Flag for ControlOutput, allows output objects to keep track of when a parameter is altered. More...
 

Friends

std::ostream & operator<< (std::ostream &stream, const ControllableItem &obj)
 Allows this to be used with std:: cout. More...
 

Detailed Description

An intermediate object for building a "controllable item", where an "item" can refer to multiple input parameters with different names.

The name supplied to the constructor is considered the "master" parameter. The parameter(s) added via the connect method are considered the slaves.

In general, an ControllableItem will have a one-to-one relationship with an input parameter value, but in some instances it is desirable to connect parameters with different names together. For example, within MOOSE when a material is defined in an input file multiple Material objects are generated automatically. If a parameter is controlled on one of these objects it is necessary to also have the values on the other controlled as well. This example is the driver behind the creation of this intermediate class.

Definition at line 32 of file ControllableItem.h.

Constructor & Destructor Documentation

◆ ControllableItem() [1/4]

ControllableItem::ControllableItem ( const MooseObjectParameterName name,
libMesh::Parameters::Value *  value 
)

Definition at line 13 of file ControllableItem.C.

15 {
16  _pairs.emplace_back(name, value);
17 }
virtual const MooseObjectParameterName & name() const
Return the name of the master parameter.
std::vector< std::pair< MooseObjectParameterName, libMesh::Parameters::Value * > > _pairs
List of names for this item.

◆ ~ControllableItem()

virtual ControllableItem::~ControllableItem ( )
virtualdefault

◆ ControllableItem() [2/4]

ControllableItem::ControllableItem ( const ControllableItem )
default

◆ ControllableItem() [3/4]

ControllableItem::ControllableItem ( ControllableItem &&  )
default

◆ ControllableItem() [4/4]

ControllableItem::ControllableItem ( )
protected

Constructor for creating an empty item (see ControllableAlias)

Definition at line 19 of file ControllableItem.C.

19 {}

Member Function Documentation

◆ check()

template<typename T >
bool ControllableItem::check ( ) const

Return true if the template argument is valid for ALL items.

Definition at line 166 of file ControllableItem.h.

167 {
168  return std::all_of(_pairs.begin(),
169  _pairs.end(),
170  [](std::pair<MooseObjectParameterName, libMesh::Parameters::Value *> pair) {
171  libMesh::Parameters::Parameter<T> * param =
172  dynamic_cast<libMesh::Parameters::Parameter<T> *>(pair.second);
173  return param != nullptr;
174  });
175 }
std::vector< std::pair< MooseObjectParameterName, libMesh::Parameters::Value * > > _pairs
List of names for this item.

◆ connect()

void ControllableItem::connect ( ControllableItem item,
bool  type_check = true 
)

Connects the supplied item with this item to allow for multiple parameters to be changed by one.

Definition at line 22 of file ControllableItem.C.

Referenced by ControllableAlias::ControllableAlias().

23 {
24  for (const auto & pair : item->_pairs)
25  {
26  if (type_check && type() != pair.second->type())
27  mooseError("The master parameter (",
28  name(),
29  ") has a type '",
30  type(),
31  "' and cannot be connected to the parameter (",
32  pair.first,
33  ") with a different type of '",
34  pair.second->type(),
35  "'.");
36 
37  _pairs.emplace_back(pair.first, pair.second);
38  }
39 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
virtual const MooseObjectParameterName & name() const
Return the name of the master parameter.
std::vector< std::pair< MooseObjectParameterName, libMesh::Parameters::Value * > > _pairs
List of names for this item.
std::string type() const
Return the type of the master parameter.

◆ dump()

std::string ControllableItem::dump ( unsigned int  indent = 0) const
virtual

Returns a string displaying the parameter name and current value.

Reimplemented in ControllableAlias.

Definition at line 42 of file ControllableItem.C.

Referenced by operator<<().

43 {
44 
45  // Count of objects, for printing a number with the parameter
46  unsigned int index = 0;
47 
48  std::ostringstream oss;
49  for (const auto & pair : _pairs)
50  {
51  if (index == 0) // master parameter
52  oss << ConsoleUtils::indent(indent) << COLOR_GREEN << pair.first << COLOR_DEFAULT << " = ";
53  else // slave parameters
54  oss << ConsoleUtils::indent(indent + 2) << COLOR_YELLOW << pair.first << COLOR_DEFAULT
55  << " = ";
56 
57  pair.second->print(oss);
58  oss << " <" << pair.second->type() << ">" << '\n';
59  index++;
60  }
61  return oss.str();
62 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:29
std::vector< std::pair< MooseObjectParameterName, libMesh::Parameters::Value * > > _pairs
List of names for this item.

◆ get()

template<typename T >
std::vector< T > ControllableItem::get ( bool  type_check = true) const

Return a copy of all values for this "item".

Definition at line 144 of file ControllableItem.h.

145 {
146  std::vector<T> output;
147  output.reserve(_pairs.size());
148  for (const auto & pair : _pairs)
149  {
150  libMesh::Parameters::Parameter<T> * param =
151  dynamic_cast<libMesh::Parameters::Parameter<T> *>(pair.second);
152  if (type_check && param == nullptr)
153  mooseError("Failed to get the '",
154  pair.first,
155  "' parameter the supplied template argument must be of type '",
156  pair.second->type(),
157  "'.");
158  else if (param != nullptr)
159  output.push_back(param->get());
160  }
161  return output;
162 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
std::vector< std::pair< MooseObjectParameterName, libMesh::Parameters::Value * > > _pairs
List of names for this item.

◆ isChanged()

bool ControllableItem::isChanged ( )
inline

Definition at line 101 of file ControllableItem.h.

101 { return _changed; }
bool _changed
Flag for ControlOutput, allows output objects to keep track of when a parameter is altered...

◆ name()

const MooseObjectParameterName & ControllableItem::name ( ) const
virtual

Return the name of the master parameter.

Reimplemented in ControllableAlias.

Definition at line 71 of file ControllableItem.C.

Referenced by connect(), ControllableItem(), operator!=(), operator<(), and operator==().

72 {
73  return _pairs[0].first;
74 }
std::vector< std::pair< MooseObjectParameterName, libMesh::Parameters::Value * > > _pairs
List of names for this item.

◆ operator!=()

bool ControllableItem::operator!= ( const ControllableItem rhs) const
inline

Definition at line 76 of file ControllableItem.h.

76 { return name() != rhs.name(); }
virtual const MooseObjectParameterName & name() const
Return the name of the master parameter.

◆ operator<()

bool ControllableItem::operator< ( const ControllableItem rhs) const
inline

Definition at line 77 of file ControllableItem.h.

77 { return name() < rhs.name(); }
virtual const MooseObjectParameterName & name() const
Return the name of the master parameter.

◆ operator=() [1/2]

ControllableItem& ControllableItem::operator= ( const ControllableItem )
delete

◆ operator=() [2/2]

ControllableItem& ControllableItem::operator= ( ControllableItem &&  )
delete

◆ operator==()

bool ControllableItem::operator== ( const ControllableItem rhs) const
inline

Use the master name for comparison operators to allow object to work within a set/map.

Definition at line 75 of file ControllableItem.h.

75 { return name() == rhs.name(); }
virtual const MooseObjectParameterName & name() const
Return the name of the master parameter.

◆ resetChanged()

void ControllableItem::resetChanged ( )
inline

Methods for ControlOutput::outputChangedControls, these don't have meaning outside of this function.

Definition at line 100 of file ControllableItem.h.

100 { _changed = false; }
bool _changed
Flag for ControlOutput, allows output objects to keep track of when a parameter is altered...

◆ set()

template<typename T >
void ControllableItem::set ( const T &  value,
bool  type_check = true 
)

Set the value(s) of the controlled parameters stored in this class.

The 'skip_type_check' flag allows this object to work with ControllableParameter that can store values of varying types.

Definition at line 122 of file ControllableItem.h.

123 {
124  for (auto & pair : _pairs)
125  {
126  libMesh::Parameters::Parameter<T> * param =
127  dynamic_cast<libMesh::Parameters::Parameter<T> *>(pair.second);
128  if (type_check && param == nullptr)
129  mooseError("Failed to set the '",
130  pair.first,
131  "' parameter the supplied template argument must be of type '",
132  pair.second->type(),
133  "'.");
134  else if (param != nullptr)
135  {
136  param->set() = value;
137  _changed = true;
138  }
139  }
140 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
std::vector< std::pair< MooseObjectParameterName, libMesh::Parameters::Value * > > _pairs
List of names for this item.
bool _changed
Flag for ControlOutput, allows output objects to keep track of when a parameter is altered...

◆ type()

std::string ControllableItem::type ( ) const

Return the type of the master parameter.

Definition at line 65 of file ControllableItem.C.

Referenced by connect().

66 {
67  return _pairs[0].second->type();
68 }
std::vector< std::pair< MooseObjectParameterName, libMesh::Parameters::Value * > > _pairs
List of names for this item.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  stream,
const ControllableItem obj 
)
friend

Allows this to be used with std:: cout.

Definition at line 104 of file ControllableItem.C.

105 {
106  return stream << obj.dump();
107 }
virtual std::string dump(unsigned int indent=0) const
Returns a string displaying the parameter name and current value.

Member Data Documentation

◆ _changed

bool ControllableItem::_changed = false
protected

Flag for ControlOutput, allows output objects to keep track of when a parameter is altered.

Definition at line 117 of file ControllableItem.h.

Referenced by isChanged(), resetChanged(), and set().

◆ _pairs

std::vector<std::pair<MooseObjectParameterName, libMesh::Parameters::Value *> > ControllableItem::_pairs
protected

List of names for this item.

Definition at line 114 of file ControllableItem.h.

Referenced by check(), connect(), ControllableItem(), dump(), ControllableAlias::dump(), get(), name(), set(), and type().


The documentation for this class was generated from the following files: