www.mooseframework.org
RestartableData.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
26 class MooseApp;
27 
32 {
33 public:
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 
104  {
105  friend class RestartableDataReader;
108  };
109 
113  void setNotLoaded(const SetNotLoadedKey) { _loaded = false; }
114 
121  bool stored() const { return _stored; }
122 
127  {
128  friend class RestartableDataWriter;
131  };
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 
187 protected:
191  virtual void storeJSONValue(nlohmann::json & json) const = 0;
192 
194  const std::string _name;
195 
197  void * const _context;
198 
199 private:
201  bool _declared;
202 
204  bool _loaded;
205 
207  bool _stored;
208 };
209 
214 template <typename T>
216 {
217 public:
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 
258 protected:
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 
271 private:
273  std::unique_ptr<T> _value;
274 };
275 
276 // ------------------------------------------------------------
277 // RestartableData<> class inline methods
278 template <typename T>
279 inline const T &
281 {
282  mooseAssert(_value, "Not valid");
283  return *_value;
284 }
285 
286 template <typename T>
287 inline T &
289 {
290  mooseAssert(_value, "Not valid");
291  return *_value;
292 }
293 
294 template <typename T>
295 inline void
297 {
298  mooseAssert(_value, "Not valid"); // shouldn't really call this twice
299  _value.reset();
300 }
301 
302 template <typename T>
303 inline std::string
305 {
306  return MooseUtils::prettyCppType<T>();
307 }
308 
309 template <typename T>
310 inline void
311 RestartableData<T>::storeInternal(std::ostream & stream)
312 {
313  storeHelper(stream, set(), _context);
314 }
315 
316 template <typename T>
317 inline void
318 RestartableData<T>::loadInternal(std::istream & stream)
319 {
320  loadHelper(stream, set(), _context);
321 }
322 
323 template <typename T>
324 inline void
325 RestartableData<T>::storeJSONValue(nlohmann::json & json) const
326 {
328  nlohmann::to_json(json, get());
329  else
330  mooseAssert(false, "Should not be called");
331 }
332 
333 using DataNames = std::unordered_set<std::string>;
void setNotLoaded(const SetNotLoadedKey)
Sets that this restartable value has been loaded.
SetNotLoadedKey(const SetNotLoadedKey &)
Reader for restartable data written by the RestartableDataWriter.
void *const _context
A context pointer for helping with load and store.
SetDeclaredKey(const SetDeclaredKey &)
virtual void storeInternal(std::ostream &stream) override
Store the RestartableData into a binary stream.
const T & get() const
bool stored() const
Whether or not this data has been loaded.
virtual bool hasStoreJSON() const override final
virtual const std::type_info & typeId() const =0
The type ID of the underlying data.
virtual void storeJSONValue(nlohmann::json &json) const override final
Internal method for storing the underlying JSON value.
std::unique_ptr< T > _value
Stored value.
Writer for restartable data, to be read by the RestartableDataReader.
virtual bool hasStoreJSON() const =0
Base class for MOOSE-based applications.
Definition: MooseApp.h:69
bool _loaded
Whether or not this has value has been loaded.
virtual void storeInternal(std::ostream &stream)=0
Internal method that stores the value into the stream stream in the specialized class.
virtual void loadInternal(std::istream &stream) override
Load the RestartableData from a binary stream.
virtual std::string type() const override final
String identifying the type of parameter stored.
SetNotStoredKey(const SetNotStoredKey &)
RestartableDataValue(const std::string &name, void *const context)
Constructor.
Helper that protects access to setDeclared() to only MooseApp.
bool hasContext() const
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:809
virtual void storeJSONValue(nlohmann::json &json) const =0
Internal method for storing the underlying JSON value.
bool _stored
Whether or not this has value has been stored.
RestartableData(const std::string &name, void *const context, Params &&... args)
Constructor.
static constexpr bool has_store_json
Whether or not this type has a JSON store method implemented.
virtual void loadInternal(std::istream &stream)=0
Internal method that loads the value from the stream stream in the specialized class.
Helper that protects access to setNotStored() to only RestartableDataWriter.
virtual std::string type() const =0
String identifying the type of parameter stored.
bool loaded() const
Whether or not this data has been loaded.
bool declared() const
Whether or not this data has been declared.
void load(std::istream &stream)
Loads the value from the stream stream and sets it as loaded.
void store(std::ostream &stream)
Stores the value into the stream stream and sets it as stored.
virtual const std::type_info & typeId() const override final
The type ID of the underlying data.
Struct that represents parameters for how to store the JSON value via store.
Concrete definition of a parameter value for a specified type.
Helper that protects access to setNotLoaded() to only RestartableDataReader.
std::unordered_set< std::string > DataNames
void to_json(nlohmann::json &json, const Moose::LibtorchArtificialNeuralNet *const &network)
void * context()
A context pointer for helping with load / store.
bool _declared
Whether or not this data has been declared (true) or only retreived (false)
void reset()
Resets (destructs) the underlying data.
const std::string _name
The full (unique) name of this particular piece of data.
void setDeclared(const SetDeclaredKey)
Sets that this restartable value has been declared.
void setNotStored(const SetNotStoredKey)
Sets that this restartable value has been loaded.
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:881
Abstract definition of a RestartableData value.
const std::string & name() const
The full (unique) name of this particular piece of data.
virtual ~RestartableDataValue()=default
Destructor.