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 : #include "RestartableDataMap.h" 11 : 12 : #include "DependencyResolver.h" 13 : 14 132259 : RestartableDataMap::RestartableDataMap() {} 15 : 16 : RestartableDataValue & 17 2336388 : RestartableDataMap::addData(std::unique_ptr<RestartableDataValue> data) 18 : { 19 : mooseAssert(data, "Not set"); 20 : mooseAssert(!hasData(data->name()), "Name is already added"); 21 : 22 2336388 : const auto & name = data->name(); 23 2336388 : auto & inserted_data = _data.addPointer(std::move(data), {}); 24 2336388 : _name_to_data_index.emplace(name, _data.size() - 1); 25 : 26 : mooseAssert(hasData(name), "Doesn't have data"); 27 : 28 2336388 : return inserted_data; 29 : } 30 : 31 : const RestartableDataValue * 32 3689839 : RestartableDataMap::findData(const std::string & name) const 33 : { 34 3689839 : auto find_index = _name_to_data_index.find(name); 35 : 36 : #ifndef NDEBUG 37 : auto find_it = std::find_if( 38 : _data.begin(), _data.end(), [&name](const auto & data) { return data.name() == name; }); 39 : #endif 40 : 41 3689839 : if (find_index == _name_to_data_index.end()) 42 : { 43 : mooseAssert(find_it == _data.end(), "Inconsistent map"); 44 2517461 : return nullptr; 45 : } 46 : 47 1172378 : const auto index = find_index->second; 48 : mooseAssert(index == (std::size_t)std::distance(_data.begin(), find_it), "Inconsistent map"); 49 : mooseAssert(_data.size() > index, "Invalid index"); 50 : 51 1172378 : auto & data = _data[index]; 52 : mooseAssert(data.name() == name, "Inconsistent name"); 53 : 54 1172378 : return &data; 55 : } 56 : 57 : RestartableDataValue * 58 3507358 : RestartableDataMap::findData(const std::string & name) 59 : { 60 : return const_cast<RestartableDataValue *>( 61 3507358 : const_cast<const RestartableDataMap *>(this)->findData(name)); 62 : } 63 : 64 : RestartableDataValue & 65 10 : RestartableDataMap::data(const std::string & name) 66 : { 67 10 : auto find_data = findData(name); 68 10 : if (!find_data) 69 5 : mooseError("Restartable data with the name ", name, " is not registered"); 70 5 : return *find_data; 71 : } 72 : 73 : bool 74 182481 : RestartableDataMap::hasData(const std::string & name) const 75 : { 76 182481 : return findData(name) != nullptr; 77 : }