The Sparta Modeling Framework
Loading...
Searching...
No Matches
StringStreamStorage.hpp
1// <StringStreamStorage> -*- C++ -*-
2
3#pragma once
4
5#include "sparta/functional/ArchData.hpp"
7
8namespace sparta::serialization::checkpoint::storage
9{
10
17{
18 std::stringstream ss_;
19
20public:
22 ss_.exceptions(std::ostream::eofbit | std::ostream::badbit |
23 std::ostream::failbit | std::ostream::goodbit);
24 }
25
26 void dump(std::ostream& o) const {
27 auto s = ss_.str();
28 auto itr = s.begin();
29 for(; itr != s.end(); itr++){
30 char chr = *itr;
31 if(chr == 'L'){
32 uint32_t off = 0;
34 strncpy((char*)&ln_idx, s.substr(itr-s.begin(), sizeof(ln_idx)).c_str(), sizeof(ln_idx));
35 std::cout << "\nLine: " << ln_idx << std::endl;
36 itr += sizeof(ArchData::line_idx_type);
37
38 for(uint16_t i=0; i<64; ++i){
39 chr = *itr;
40 if(off % 32 == 0){
41 o << std::setw(7) << std::hex << off;
42 }
43 if(chr == 0){
44 o << ' ' << "..";
45 }else{
46 o << ' ' << std::setfill('0') << std::setw(2) << std::hex << (0xff & (uint16_t)chr);
47 }
48 off++;
49 if(off % 32 == 0){
50 o << std::endl;
51 }
52 ++itr;
53 }
54 }
55 }
56 }
57
58 uint32_t getSize() const {
59 return ss_.str().size() + sizeof(decltype(*this));
60 }
61
62 void prepareForLoad() {
63 ss_.seekg(0); // Seek to start with get pointer before consuming
64 }
65
66 void beginLine(ArchData::line_idx_type idx) {
67 ss_ << 'L'; // Line start char
68
70 ss_.write((char*)&idx_repr, sizeof(ArchData::line_idx_type));
71 }
72
73 void writeLineBytes(const char* data, size_t size) {
74 ss_.write(data, size);
75 }
76
80 void endArchData() {
81 ss_ << "E"; // Indicates end of this checkpoint data
82
83 sparta_assert(ss_.good(),
84 "Ostream error while writing checkpoint data");
85 }
86
91 bool good() const {
92 return ss_.good();
93 }
94
100 char ctrl;
101 ss_ >> ctrl;
102 sparta_assert(ss_.good(),
103 "Encountered checkpoint data stream error or eof");
104 if(ctrl == 'L'){
105 ArchData::line_idx_type ln_idx = 0;
106 ss_.read((char*)&ln_idx, sizeof(ln_idx)); // Presumed LE encoding
107 return ln_idx;
108 }else if(ctrl == 'E'){
109 return ArchData::INVALID_LINE_IDX; // Done with restore
110 }else{
111 throw SpartaException("Failed to restore a checkpoint because a '")
112 << ctrl << "' control character was found where an 'L' or 'E' was found";
113 }
114 };
115
119 void copyLineBytes(char* buf, uint32_t size) {
120 ss_.read(buf, size);
121 }
122};
123
124} // namespace sparta::serialization::checkpoint::storage
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Exception class for all of Sparta.
static const line_idx_type INVALID_LINE_IDX
Invalid line index.
Definition ArchData.hpp:112
offset_type line_idx_type
Represents offsets into this ArchData.
Definition ArchData.hpp:59
Used to construct and throw a standard C++ exception. Inherits from std::exception.
void copyLineBytes(char *buf, uint32_t size)
Read bytes for the current line.
bool good() const
Is the reading state of this storage good? (i.e. haven't tried to read past the end of the data)
ArchData::line_idx_type getNextRestoreLine()
Restore next line. Return ArchData::INVALID_LINE_IDX on end of data.
std::enable_if< std::is_same< boost::mpl::int_< BO >, boost::mpl::int_< BE > >::value, T >::type reorder(const T &t)