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 146628 : RestartableDataMap::RestartableDataMap() {} 15 : 16 : RestartableDataValue & 17 2825570 : RestartableDataMap::addData(std::unique_ptr<RestartableDataValue> data) 18 : { 19 : mooseAssert(data, "Not set"); 20 : mooseAssert(!hasData(data->name()), "Name is already added"); 21 : 22 2825570 : const auto & name = data->name(); 23 2825570 : auto & inserted_data = _data.addPointer(std::move(data), {}); 24 2825570 : _name_to_data_index.emplace(name, _data.size() - 1); 25 : 26 : mooseAssert(hasData(name), "Doesn't have data"); 27 : 28 2825570 : return inserted_data; 29 : } 30 : 31 : const RestartableDataValue * 32 4747748 : RestartableDataMap::findData(const std::string & name) const 33 : { 34 4747748 : 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 4747748 : if (find_index == _name_to_data_index.end()) 42 : { 43 : mooseAssert(find_it == _data.end(), "Inconsistent map"); 44 3030133 : return nullptr; 45 : } 46 : 47 1717615 : 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 1717615 : auto & data = _data[index]; 52 : mooseAssert(data.name() == name, "Inconsistent name"); 53 : 54 1717615 : return &data; 55 : } 56 : 57 : RestartableDataValue * 58 4541638 : RestartableDataMap::findData(const std::string & name) 59 : { 60 : return const_cast<RestartableDataValue *>( 61 4541638 : const_cast<const RestartableDataMap *>(this)->findData(name)); 62 : } 63 : 64 : RestartableDataValue & 65 20 : RestartableDataMap::data(const std::string & name) 66 : { 67 20 : auto find_data = findData(name); 68 20 : if (!find_data) 69 10 : mooseError("Restartable data with the name ", name, " is not registered"); 70 10 : return *find_data; 71 : } 72 : 73 : bool 74 206110 : RestartableDataMap::hasData(const std::string & name) const 75 : { 76 206110 : return findData(name) != nullptr; 77 : }