Skip to content

Instantly share code, notes, and snippets.

@Lyoko-Jeremie
Last active May 4, 2023 16:53
Show Gist options
  • Save Lyoko-Jeremie/da3160d6fbea3ee49d72e177cb3a8a5e to your computer and use it in GitHub Desktop.
Save Lyoko-Jeremie/da3160d6fbea3ee49d72e177cb3a8a5e to your computer and use it in GitHub Desktop.
Binary Memory Stream
// from https://cplusplus.com/forum/general/226786/
#include <istream>
#include <ostream>
#include <streambuf>
struct IMemBuf: std::streambuf
{
IMemBuf(const char* base, size_t size)
{
char* p(const_cast<char*>(base));
this->setg(p, p, p + size);
}
};
struct IMemStream: virtual IMemBuf, std::istream
{
IMemStream(const char* mem, size_t size) :
IMemBuf(mem, size),
std::istream(static_cast<std::streambuf*>(this))
{
}
};
struct OMemBuf: std::streambuf
{
OMemBuf(char* base, size_t size)
{
this->setp(base, base + size);
}
};
struct OMemStream: virtual OMemBuf, std::ostream
{
OMemStream(char* base, size_t size) :
OMemBuf(mem, size),
std::ostream(static_cast<std::streambuf*>(this))
{
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment