https://mooseframework.inl.gov
Loading...
Searching...
No Matches
RestartableDataMap.C
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#include "RestartableDataMap.h"
11
12#include "DependencyResolver.h"
13
15
17RestartableDataMap::addData(std::unique_ptr<RestartableDataValue> data)
18{
19 mooseAssert(data, "Not set");
20 mooseAssert(!hasData(data->name()), "Name is already added");
21
22 const auto & name = data->name();
23 auto & inserted_data = _data.addPointer(std::move(data), {});
24 _name_to_data_index.emplace(name, _data.size() - 1);
25
26 mooseAssert(hasData(name), "Doesn't have data");
27
28 return inserted_data;
29}
30
32RestartableDataMap::findData(const std::string & name) const
33{
34 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 if (find_index == _name_to_data_index.end())
42 {
43 mooseAssert(find_it == _data.end(), "Inconsistent map");
44 return nullptr;
45 }
46
47 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 auto & data = _data[index];
52 mooseAssert(data.name() == name, "Inconsistent name");
53
54 return &data;
55}
56
58RestartableDataMap::findData(const std::string & name)
59{
60 return const_cast<RestartableDataValue *>(
61 const_cast<const RestartableDataMap *>(this)->findData(name));
62}
63
65RestartableDataMap::data(const std::string & name)
66{
67 auto find_data = findData(name);
68 if (!find_data)
69 mooseError("Restartable data with the name ", name, " is not registered");
70 return *find_data;
71}
72
73bool
74RestartableDataMap::hasData(const std::string & name) const
75{
76 return findData(name) != nullptr;
77}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
RestartableDataValue & addPointer(std::unique_ptr< RestartableDataValue > &&ptr, const WriteKey)
Storage for restartable data that is ordered based on insertion order.
const RestartableDataValue * findData(const std::string &name) const
Tries to find data with the name name; returns nullptr if not found.
RestartableDataValue & addData(std::unique_ptr< RestartableDataValue > data)
Adds the restartable data data to the map.
std::unordered_map< std::string, std::size_t > _name_to_data_index
Mapping from data name -> index in _data for quick indexing.
RestartableDataValue & data(const std::string &name)
bool hasData(const std::string &name) const
RestartableDataMap::Data _data
The registered data.
Abstract definition of a RestartableData value.
const std::string & name() const
The full (unique) name of this particular piece of data.
iterator begin()
Begin and end iterators to the underlying data.
std::size_t size() const
iterator end()