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 : private: 85 : /// The MooseApp that owns this system 86 : MooseApp & _app; 87 : 88 : /// Map of chain control data name to its value 89 : std::map<std::string, std::unique_ptr<ChainControlDataBase>> _chain_control_data_map; 90 : }; 91 : 92 : template <typename T> 93 : bool 94 960 : ChainControlDataSystem::hasChainControlDataOfType(const std::string & data_name) const 95 : { 96 960 : if (hasChainControlData(data_name)) 97 880 : return dynamic_cast<ChainControlData<T> *>(_chain_control_data_map.at(data_name).get()) != 98 880 : nullptr; 99 : else 100 80 : return false; 101 : } 102 : 103 : template <typename T> 104 : ChainControlData<T> & 105 872 : ChainControlDataSystem::getChainControlData(const std::string & data_name) 106 : { 107 872 : if (hasChainControlData(data_name)) 108 : { 109 424 : if (!hasChainControlDataOfType<T>(data_name)) 110 0 : mooseError("The chain control data '", 111 : data_name, 112 : "' was requested with the type '", 113 : MooseUtils::prettyCppType<T>(), 114 : "' but has the type '", 115 0 : _chain_control_data_map[data_name]->type(), 116 : "'."); 117 : } 118 : else 119 448 : _chain_control_data_map[data_name] = std::make_unique<ChainControlData<T>>(_app, data_name); 120 : 121 872 : return static_cast<ChainControlData<T> &>(*_chain_control_data_map[data_name]); 122 : } 123 : 124 : template <typename T> 125 : ChainControlData<T> & 126 448 : ChainControlDataSystem::declareChainControlData(const std::string & data_name, 127 : ChainControl & chain_control) 128 : { 129 448 : auto & data = getChainControlData<T>(data_name); 130 448 : if (!data.getDeclared()) 131 : { 132 448 : data.setDeclared(); 133 448 : data.setChainControl(chain_control); 134 : } 135 : else 136 0 : mooseError("The chain control data '", data_name, "' has already been declared."); 137 : 138 448 : return data; 139 : }