Skip to content

Commit

Permalink
GH-1062 Add new datastream_mirror which keeps a copy of the stream da…
Browse files Browse the repository at this point in the history
…ta as it reads.
  • Loading branch information
heifner committed Feb 5, 2025
1 parent b56562c commit 4fa1edf
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions libraries/libfc/include/fc/io/datastream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,38 @@ class datastream<Container, typename std::enable_if_t<(std::is_same_v<std::vecto
const Container& storage() const { return _container; }
};

/**
* Datastream wrapper that creates a copy of the datastream data read. After reading
* the data is available via extract_mirror()
* @tparam DataStream datastream to wrap
*/
template <typename DataStream>
class datastream_mirror {
public:
explicit datastream_mirror( DataStream& ds, size_t reserve = 0 ) : ds(ds) {
mirror.reserve(reserve);
}

void skip( size_t s ) { ds.skip(s); }
bool read( char* d, size_t s ) {
if (ds.read(d, s)) {
auto size = mirror.size();
mirror.resize(size + s);
memcpy(mirror.data() + size, d, s);
return true;
}
return false;
}

bool get( unsigned char& c ) { return read(&c, 1); }
bool get( char& c ) { return read(&c, 1); }

std::vector<char> extract_mirror() { return std::move(mirror); }

private:
DataStream& ds;
std::vector<char> mirror;
};


template<typename ST>
Expand Down

0 comments on commit 4fa1edf

Please sign in to comment.