-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable exporting and importing subsimulator state #769
Open
kyllingstad
wants to merge
2
commits into
dev/state-persistence
Choose a base branch
from
feature/756-subsim-state-export
base: dev/state-persistence
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,132 @@ | ||
/** | ||
* \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 <boost/property_tree/ptree.hpp> | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#include <ostream> | ||
#include <string> | ||
#include <variant> | ||
#include <vector> | ||
|
||
|
||
namespace cosim | ||
{ | ||
/// Supporting functionality for serialization and persistence of simulation state. | ||
namespace serialization | ||
{ | ||
|
||
|
||
/** | ||
* The internal data type for `cosim::serialization::node`. | ||
* | ||
* This is a `std::variant` of the types that can be stored in a node's data | ||
* field. This includes a variety of primitive types, plus the aggregate | ||
* types `std::string` (for convenience) and `std::vector<std::byte>` (for | ||
* convenience *and* memory-efficient storage of binary blobs). | ||
* | ||
* A `nullptr` is used as the "empty" state, i.e., to indicate that the node | ||
* stores no data. This is the default if no other value has been set. | ||
*/ | ||
using node_data = std::variant< | ||
std::nullptr_t, // represents "empty" | ||
bool, | ||
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, | ||
char, | ||
std::string, | ||
std::byte, | ||
std::vector<std::byte> | ||
>; | ||
|
||
|
||
/** | ||
* A type for storing data intermediately before serialization. | ||
* | ||
* This is a recursive, dynamic data type that can be used to store virtually | ||
* any data structure in a generic yet type-safe manner. | ||
* | ||
* It is just an alias for `boost::property_tree::basic_ptree<std::string, cosim::serialization::node_data>`. | ||
* For guidance on its use, see the [Boost.PropertyTree](https://www.boost.org/doc/libs/release/doc/html/property_tree.html) | ||
* documentation. | ||
* | ||
* \note | ||
* To make convenience functions like `node::get()` and `node::put()` work | ||
* seamlessly, `cosim::serialization::node_data_translator` has been | ||
* declared as the default translator for | ||
* `cosim::serialization::node_data`. However, we only recommend using | ||
* these functions for primitive types, because they return by value | ||
* rather than by reference. In other words, a copy of the node data is | ||
* made. This could significantly impact performance in some cases if the | ||
* node stores a large string or byte array. | ||
* \code | ||
* // Convenient, but always makes a copy | ||
* auto thisIsACopy = myNode.get<std::vector<std::byte>>("huge_binary_blob"); | ||
* | ||
* // Uglier, but always gets a reference | ||
* const auto& thisIsARef = std::get<std::vector<std::byte>>(myNode.get_child("huge_binary_blob").data()); | ||
* \endcode | ||
*/ | ||
using node = boost::property_tree::basic_ptree<std::string, node_data>; | ||
|
||
|
||
/// An implementation of Boost.PropertyTree's _Translator_ concept. | ||
template<typename T> | ||
struct node_data_translator | ||
{ | ||
using internal_type = node_data; | ||
using external_type = T; | ||
T get_value(const node_data& value) { return std::get<T>(value); } | ||
node_data put_value(const T& value) { return node_data(value); } | ||
}; | ||
|
||
}} // namespace cosim::serialization | ||
|
||
|
||
// Ordinarily, the following function would be in the `cosim::serialization` | ||
// namespace and found by the compiler via ADL. This doesn't work here, | ||
// because `cosim::serialization::node` is just an alias for a type in the | ||
// `boost::property_tree` namespace. | ||
/** | ||
* 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 cosim::serialization::node& data); | ||
|
||
|
||
// Make node_translator the default translator for property trees whose data | ||
// type is node_data. | ||
namespace boost | ||
{ | ||
namespace property_tree | ||
{ | ||
template<typename T> | ||
struct translator_between<cosim::serialization::node_data, T> | ||
{ | ||
using type = cosim::serialization::node_data_translator<T>; | ||
}; | ||
}} // namespace boost::property_tree | ||
|
||
#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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This and the removal on lines 304–305 was just something I missed in #765.