https://mooseframework.inl.gov
Loading...
Searching...
No Matches
RestartableData.h
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#pragma once
11
12// MOOSE includes
13#include "DataIO.h"
14#include "MooseUtils.h"
15
16// C++ includes
17#include <memory>
18#include <vector>
19#include <unordered_set>
20#include <unordered_map>
21
22#include "nlohmann/json.h"
23
26class MooseApp;
27
32{
33public:
39 RestartableDataValue(const std::string & name, void * const context);
40
44 virtual ~RestartableDataValue() = default;
45
50 virtual std::string type() const = 0;
51
55 virtual const std::type_info & typeId() const = 0;
56
60 const std::string & name() const { return _name; }
61
65 void * context() { return _context; }
66
70 bool hasContext() const { return _context != nullptr; }
71
76 {
77 friend class MooseApp;
80 };
81
85 bool declared() const { return _declared; }
86
90 void setDeclared(const SetDeclaredKey);
91
98 bool loaded() const { return _loaded; }
99
109
113 void setNotLoaded(const SetNotLoadedKey) { _loaded = false; }
114
121 bool stored() const { return _stored; }
122
132
136 void setNotStored(const SetNotStoredKey) { _stored = false; }
137
141 void store(std::ostream & stream);
145 void load(std::istream & stream);
146
151 virtual void storeInternal(std::ostream & stream) = 0;
156 virtual void loadInternal(std::istream & stream) = 0;
157
161 virtual bool hasStoreJSON() const = 0;
162
167 {
168 StoreJSONParams() {} // fixes a compiler bug with default constructors
169 bool value = true;
170 bool type = true;
171 bool name = false;
172 bool declared = false;
173 bool loaded = false;
174 bool stored = false;
175 bool has_context = false;
176 };
177
185 void store(nlohmann::json & json, const StoreJSONParams & params = StoreJSONParams{}) const;
186
187protected:
191 virtual void storeJSONValue(nlohmann::json & json) const = 0;
192
194 const std::string _name;
195
197 void * const _context;
198
199private:
202
205
208};
209
214template <typename T>
216{
217public:
219 static constexpr bool has_store_json = std::is_constructible_v<nlohmann::json, T>;
220
227 template <typename... Params>
228 RestartableData(const std::string & name, void * const context, Params &&... args)
230 _value(std::make_unique<T>(std::forward<Params>(args)...))
231 {
232 }
233
237 const T & get() const;
238
242 T & set();
243
247 void reset();
248
252 virtual std::string type() const override final;
253
254 virtual const std::type_info & typeId() const override final { return typeid(T); }
255
256 virtual bool hasStoreJSON() const override final { return has_store_json; }
257
258protected:
262 virtual void storeInternal(std::ostream & stream) override;
263
267 virtual void loadInternal(std::istream & stream) override;
268
269 virtual void storeJSONValue(nlohmann::json & json) const override final;
270
271private:
273 std::unique_ptr<T> _value;
274};
275
276// ------------------------------------------------------------
277// RestartableData<> class inline methods
278template <typename T>
279inline const T &
281{
282 mooseAssert(_value, "Not valid");
283 return *_value;
284}
285
286template <typename T>
287inline T &
289{
290 mooseAssert(_value, "Not valid");
291 return *_value;
292}
293
294template <typename T>
295inline void
297{
298 mooseAssert(_value, "Not valid"); // shouldn't really call this twice
299 _value.reset();
300}
301
302template <typename T>
303inline std::string
305{
306 return MooseUtils::prettyCppType<T>();
307}
308
309template <typename T>
310inline void
312{
313 storeHelper(stream, set(), _context);
314}
315
316template <typename T>
317inline void
319{
320 loadHelper(stream, set(), _context);
321}
322
323template <typename T>
324inline void
325RestartableData<T>::storeJSONValue(nlohmann::json & json) const
326{
328 nlohmann::to_json(json, get());
329 else
330 mooseAssert(false, "Should not be called");
331}
332
333using DataNames = std::unordered_set<std::string>;
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition DataIO.h:989
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition DataIO.h:1081
std::unordered_set< std::string > DataNames
Base class for MOOSE-based applications.
Definition MooseApp.h:110
Reader for restartable data written by the RestartableDataWriter.
Helper that protects access to setDeclared() to only MooseApp.
SetDeclaredKey(const SetDeclaredKey &)
Helper that protects access to setNotLoaded() to only RestartableDataReader.
SetNotLoadedKey(const SetNotLoadedKey &)
Helper that protects access to setNotStored() to only RestartableDataWriter.
SetNotStoredKey(const SetNotStoredKey &)
Abstract definition of a RestartableData value.
void store(std::ostream &stream)
Stores the value into the stream stream and sets it as stored.
virtual void loadInternal(std::istream &stream)=0
Internal method that loads the value from the stream stream in the specialized class.
const std::string _name
The full (unique) name of this particular piece of data.
bool stored() const
Whether or not this data has been loaded.
void setNotLoaded(const SetNotLoadedKey)
Sets that this restartable value has been loaded.
void * context()
A context pointer for helping with load / store.
virtual const std::type_info & typeId() const =0
The type ID of the underlying data.
virtual ~RestartableDataValue()=default
Destructor.
virtual void storeJSONValue(nlohmann::json &json) const =0
Internal method for storing the underlying JSON value.
bool declared() const
Whether or not this data has been declared.
const std::string & name() const
The full (unique) name of this particular piece of data.
void setDeclared(const SetDeclaredKey)
Sets that this restartable value has been declared.
void load(std::istream &stream)
Loads the value from the stream stream and sets it as loaded.
virtual std::string type() const =0
String identifying the type of parameter stored.
virtual bool hasStoreJSON() const =0
void *const _context
A context pointer for helping with load and store.
bool _stored
Whether or not this has value has been stored.
bool _declared
Whether or not this data has been declared (true) or only retreived (false)
bool loaded() const
Whether or not this data has been loaded.
virtual void storeInternal(std::ostream &stream)=0
Internal method that stores the value into the stream stream in the specialized class.
void setNotStored(const SetNotStoredKey)
Sets that this restartable value has been loaded.
bool _loaded
Whether or not this has value has been loaded.
Writer for restartable data, to be read by the RestartableDataReader.
Concrete definition of a parameter value for a specified type.
virtual void loadInternal(std::istream &stream) override
Load the RestartableData from a binary stream.
static constexpr bool has_store_json
Whether or not this type has a JSON store method implemented.
std::unique_ptr< T > _value
Stored value.
virtual void storeJSONValue(nlohmann::json &json) const override final
Internal method for storing the underlying JSON value.
virtual bool hasStoreJSON() const override final
virtual void storeInternal(std::ostream &stream) override
Store the RestartableData into a binary stream.
const T & get() const
RestartableData(const std::string &name, void *const context, Params &&... args)
Constructor.
void reset()
Resets (destructs) the underlying data.
virtual const std::type_info & typeId() const override final
The type ID of the underlying data.
virtual std::string type() const override final
String identifying the type of parameter stored.
Struct that represents parameters for how to store the JSON value via store.