Line data Source code
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 "ChainControlData.h" 13 : #include "MooseError.h" 14 : #include "MooseUtils.h" 15 : 16 : class ChainControl; 17 : 18 : /** 19 : * System that manages ChainControls. 20 : * 21 : * To be owned by the MooseApp. 22 : */ 23 : class ChainControlDataSystem 24 : { 25 : public: 26 : ChainControlDataSystem(MooseApp & app); 27 : 28 : /** 29 : * Queries if the chain control data of the given name exists. 30 : * 31 : * @param[in] data_name Chain control data name 32 : * @return True if the chain control data of the given name exists 33 : */ 34 : bool hasChainControlData(const std::string & data_name) const; 35 : 36 : /** 37 : * Queries if the chain control data of the given name and type exists. 38 : * 39 : * @tparam T Type of the data 40 : * @param[in] data_name Chain control data name 41 : * @return True if the chain control data of the given name and type exists 42 : */ 43 : template <typename T> 44 : bool hasChainControlDataOfType(const std::string & data_name) const; 45 : 46 : /** 47 : * Gets the chain control data of the given name and type and creates if it does not exist. 48 : * 49 : * If the data exists but of another type, then an error is thrown. 50 : * 51 : * @tparam T Type of the data 52 : * @param[in] data_name Chain control data name 53 : * @return Reference to the requested chain control data 54 : */ 55 : template <typename T> 56 : ChainControlData<T> & getChainControlData(const std::string & data_name); 57 : 58 : /** 59 : * Declares chain control data of of the given name and type and creates if it does not exist. 60 : * 61 : * If the data has already been declared, an error is thrown; otherwise the 62 : * data is now set to be declared, and the declaring ChainControl is captured. 63 : * 64 : * @tparam T Type of the data 65 : * @param[in] data_name Chain control data name 66 : * @param[in] chain_control Chain control that is declaring the data 67 : * @return Reference to the declared chain control data 68 : */ 69 : template <typename T> 70 : ChainControlData<T> & declareChainControlData(const std::string & data_name, 71 : ChainControl & chain_control); 72 : 73 : /** 74 : * Copies current chain control data values into old values 75 : */ 76 : void copyValuesBack(); 77 : 78 : /** 79 : * Gets the map of ChainControlData names to the relevant ChainControlDataBase 80 : */ 81 : const std::map<std::string, std::unique_ptr<ChainControlDataBase>> & 82 : getChainControlDataMap() const; 83 : 84 : /// Output the chain control map to a string 85 : std::string outputChainControlMap() const; 86 : 87 : private: 88 : /// The MooseApp that owns this system 89 : MooseApp & _app; 90 : 91 : /// Map of chain control data name to its value 92 : std::map<std::string, std::unique_ptr<ChainControlDataBase>> _chain_control_data_map; 93 : }; 94 : 95 : template <typename T> 96 : bool 97 1277 : ChainControlDataSystem::hasChainControlDataOfType(const std::string & data_name) const 98 : { 99 1277 : if (hasChainControlData(data_name)) 100 1191 : return dynamic_cast<ChainControlData<T> *>(_chain_control_data_map.at(data_name).get()) != 101 1191 : nullptr; 102 : else 103 86 : return false; 104 : } 105 : 106 : template <typename T> 107 : ChainControlData<T> & 108 1155 : ChainControlDataSystem::getChainControlData(const std::string & data_name) 109 : { 110 1155 : if (hasChainControlData(data_name)) 111 : { 112 572 : if (!hasChainControlDataOfType<T>(data_name)) 113 0 : mooseError("The chain control data '", 114 : data_name, 115 : "' was requested with the type '", 116 : MooseUtils::prettyCppType<T>(), 117 : "' but has the type '", 118 0 : _chain_control_data_map[data_name]->type(), 119 : "'."); 120 : } 121 : else 122 583 : _chain_control_data_map[data_name] = std::make_unique<ChainControlData<T>>(_app, data_name); 123 : 124 1155 : return static_cast<ChainControlData<T> &>(*_chain_control_data_map[data_name]); 125 : } 126 : 127 : template <typename T> 128 : ChainControlData<T> & 129 583 : ChainControlDataSystem::declareChainControlData(const std::string & data_name, 130 : ChainControl & chain_control) 131 : { 132 583 : auto & data = getChainControlData<T>(data_name); 133 583 : if (!data.getDeclared()) 134 : { 135 583 : data.setDeclared(); 136 583 : data.setChainControl(chain_control); 137 : } 138 : else 139 0 : mooseError("The chain control data '", data_name, "' has already been declared."); 140 : 141 583 : return data; 142 : }