Last active
May 4, 2023 16:53
-
-
Save Lyoko-Jeremie/da3160d6fbea3ee49d72e177cb3a8a5e to your computer and use it in GitHub Desktop.
Binary Memory Stream
This file contains hidden or 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
// 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