https://mooseframework.inl.gov
Loading...
Searching...
No Matches
ControllableItem.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 "libmesh/parameters.h"
14#include "MooseError.h"
15#include "ControlOutput.h"
16
32{
33public:
36 const std::set<ExecFlagType> & flags = {});
37 virtual ~ControllableItem() = default;
38
41
44
49 void connect(ControllableItem * item, bool type_check = true);
50
57 template <typename T>
58 void set(const T & value, bool type_check = true);
59
63 template <typename T>
64 std::vector<T> get(bool type_check = true) const;
65
69 template <typename T>
70 bool check() const;
71
73
76 bool operator==(const ControllableItem & rhs) const { return name() == rhs.name(); }
77 bool operator!=(const ControllableItem & rhs) const { return name() != rhs.name(); }
78 bool operator<(const ControllableItem & rhs) const { return name() < rhs.name(); }
80
84 virtual const MooseObjectParameterName & name() const;
85
89 std::string type() const;
90
94 virtual std::string dump(unsigned int indent = 0) const;
95
97
101 void resetChanged() { _changed = false; }
102 bool isChanged() { return _changed; }
104
108 const std::set<ExecFlagType> & getExecuteOnFlags() const { return _execute_flags; }
109
111 friend std::ostream & operator<<(std::ostream & stream, const ControllableItem & obj);
112
113protected:
118
120 std::vector<std::pair<MooseObjectParameterName, libMesh::Parameters::Value *>> _pairs;
121
123 bool _changed = false;
124
126 std::set<ExecFlagType> _execute_flags;
127};
128
129template <typename T>
130void
131ControllableItem::set(const T & value, bool type_check /*=true*/)
132{
133 for (auto & pair : _pairs)
134 {
136 dynamic_cast<libMesh::Parameters::Parameter<T> *>(pair.second);
137 if (type_check && param == nullptr)
138 mooseError("Failed to set the '",
139 pair.first,
140 "' parameter the supplied template argument must be of type '",
141 pair.second->type(),
142 "'.");
143 else if (param != nullptr)
144 {
145 param->set() = value;
146 _changed = true;
147 }
148 }
149}
150
151template <typename T>
152std::vector<T>
153ControllableItem::get(bool type_check /*=true*/) const
154{
155 std::vector<T> output;
156 output.reserve(_pairs.size());
157 for (const auto & pair : _pairs)
158 {
160 dynamic_cast<libMesh::Parameters::Parameter<T> *>(pair.second);
161 if (type_check && param == nullptr)
162 mooseError("Failed to get the '",
163 pair.first,
164 "' parameter the supplied template argument must be of type '",
165 pair.second->type(),
166 "'.");
167 else if (param != nullptr)
168 output.push_back(param->get());
169 }
170 return output;
171}
172
173template <typename T>
174bool
176{
177 return std::all_of(_pairs.begin(),
178 _pairs.end(),
179 [](std::pair<MooseObjectParameterName, libMesh::Parameters::Value *> pair)
180 {
181 libMesh::Parameters::Parameter<T> * param =
182 dynamic_cast<libMesh::Parameters::Parameter<T> *>(pair.second);
183 return param != nullptr;
184 });
185}
186
191{
192public:
194 virtual const MooseObjectParameterName & name() const override;
195 virtual std::string dump(unsigned int indent = 0) const override;
196
197private:
199};
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
Allows for aliases to be defined via InputParameterWarehouse::addControllableParameterAlias.
virtual std::string dump(unsigned int indent=0) const override
Returns a string displaying the parameter name and current value.
MooseObjectParameterName _name
virtual const MooseObjectParameterName & name() const override
Return the name of the master parameter.
An intermediate object for building a "controllable item", where an "item" can refer to multiple inpu...
std::vector< T > get(bool type_check=true) const
Return a copy of all values for this "item".
ControllableItem(const ControllableItem &)=default
std::vector< std::pair< MooseObjectParameterName, libMesh::Parameters::Value * > > _pairs
List of names for this item.
ControllableItem()
Constructor for creating an empty item (see ControllableAlias)
bool operator!=(const ControllableItem &rhs) const
bool check() const
Return true if the template argument is valid for ALL items.
bool operator<(const ControllableItem &rhs) const
virtual std::string dump(unsigned int indent=0) const
Returns a string displaying the parameter name and current value.
ControllableItem & operator=(ControllableItem &&)=delete
std::set< ExecFlagType > _execute_flags
Flags to which the control is restricted (if not set it is unrestricted)
bool operator==(const ControllableItem &rhs) const
Use the master name for comparison operators to allow object to work within a set/map.
const std::set< ExecFlagType > & getExecuteOnFlags() const
Return the execute flag restrictions, an empty set is un-restricted.
void set(const T &value, bool type_check=true)
Set the value(s) of the controlled parameters stored in this class.
void resetChanged()
Methods for ControlOutput::outputChangedControls, these don't have meaning outside of this function.
ControllableItem(ControllableItem &&)=default
friend std::ostream & operator<<(std::ostream &stream, const ControllableItem &obj)
Allows this to be used with std:: cout.
bool _changed
Flag for ControlOutput, allows output objects to keep track of when a parameter is altered.
virtual ~ControllableItem()=default
ControllableItem & operator=(const ControllableItem &)=delete
virtual const MooseObjectParameterName & name() const
Return the name of the master parameter.
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.
std::string type() const
Return the type of the master parameter.
A class for storing an input parameter name.