-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable exporting and importing subsimulator state
This is a follow-up to #765 and the second and final step to close #756. Here, I've implemented functionality to export the internal state of individual subsimulators in a generic, structured form, and to import them again later. This exported form is intended as an intermediate step before serialisation and disk storage. The idea was to create a type that can be inspected and serialised to almost any file format we'd like. The type is defined by `cosim::serialization::node` in `cosim/serialization.hpp`. It is a hierarchical, dynamic data type with support for a variety of primitive scalar types and a few aggregate types: strings, arrays of nodes, dictionaries of nodes, and binary blobs. (Think JSON, only with more types.)
- Loading branch information
1 parent
981236a
commit f5967bb
Showing
15 changed files
with
511 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/** | ||
* \file | ||
* Supporting functionality for serialization and persistence of simulation state. | ||
* | ||
* \copyright | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
#ifndef COSIM_SERIALIZATION_HPP | ||
#define COSIM_SERIALIZATION_HPP | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#include <ostream> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <variant> | ||
#include <vector> | ||
|
||
|
||
namespace cosim | ||
{ | ||
/// Supporting functionality for serialization and persistence of simulation state. | ||
namespace serialization | ||
{ | ||
|
||
// This is a small trick to enable nodes to contain other nodes. We | ||
// forward-declare the `node` struct here, which allows us to use it in the | ||
// declarations of the container types below. Thus, it can (indirectly) be | ||
// used in the `node_base` type, which is the "actual" node type. Finally, | ||
// we'll define `node` so that it inherits from `node_base`. | ||
struct node; | ||
|
||
/** | ||
* An array of `cosim::serialization::node` objects. | ||
* | ||
* This is used to enable a node to contain a sequence of other nodes. | ||
*/ | ||
using array = std::vector<node>; | ||
|
||
/** | ||
* An associative array which maps strings to `cosim::serialization::node` | ||
* objects. | ||
* | ||
* This is used to enable a node to contain a dictionary of other nodes. | ||
*/ | ||
using associative_array = std::unordered_map<std::string, node>; | ||
|
||
/** | ||
* An array of bytes. | ||
* | ||
* This is used to enable a `cosim::serialization::node` to contain arbitrary | ||
* binary data. | ||
*/ | ||
using binary_blob = std::vector<std::byte>; | ||
|
||
|
||
namespace detail | ||
{ | ||
// This is step 2 of the trick mentioned earlier, the type that actually | ||
// holds the node contents. | ||
using node_base = std::variant< | ||
bool, | ||
std::byte, | ||
std::uint8_t, | ||
std::int8_t, | ||
std::uint16_t, | ||
std::int16_t, | ||
std::uint32_t, | ||
std::int32_t, | ||
std::uint64_t, | ||
std::int64_t, | ||
float, | ||
double, | ||
std::string, | ||
array, | ||
associative_array, | ||
binary_blob | ||
>; | ||
} | ||
|
||
|
||
/** | ||
* A recursive, dynamic data type that can be used to store structured data in | ||
* a type-safe manner. | ||
* | ||
* A `node` is essentially an `std::variant` which can hold the following | ||
* types: | ||
* | ||
* - `bool` | ||
* - `std::byte` | ||
* - `std::[u]int{8,16,32,64}_t` | ||
* - `float` and `double` | ||
* - `std::string` | ||
* - `cosim::serialization::array` | ||
* - `cosim::serialization::associative_array` | ||
* - `cosim::serialization::binary_blob` | ||
* | ||
* Its purpose is to be a generic representation of virtually any data | ||
* structure, so that serialization to a variety of formats can be supported. | ||
*/ | ||
struct node : public detail::node_base | ||
{ | ||
using detail::node_base::node_base; | ||
}; | ||
|
||
|
||
/** | ||
* Writes the contents of `data` to the output stream `out` in a human-readable | ||
* format. | ||
* | ||
* This is meant for debugging purposes, not for serialization. There is no | ||
* corresponding "read" function, nor is the output format designed to support | ||
* round-trip information or type preservation. | ||
*/ | ||
std::ostream& operator<<(std::ostream& out, const node& data); | ||
|
||
|
||
}} // namespace cosim::serialization | ||
#endif // COSIM_SERIALIZATION_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.