Skip to content

Instantly share code, notes, and snippets.

@bkietz
Created July 7, 2023 00:45
Show Gist options
  • Save bkietz/03b24e5e206c85f825ab0bda0a476d61 to your computer and use it in GitHub Desktop.
Save bkietz/03b24e5e206c85f825ab0bda0a476d61 to your computer and use it in GitHub Desktop.
Minimal std::ostream which outputs to a fixed span of `char`
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
#include <ostream>
#include <streambuf>
#include <cassert>
// Minimal implementation std::streambuf, loosely cadged from std::span_streambuf
class SpanStream final : public std::streambuf, public std::ostream {
public:
SpanStream(char *buffer, size_t size) : std::ostream{this} {
setbuf(buffer, static_cast<std::streamsize>(size));
}
template <typename ContiguousRange>
explicit SpanStream(ContiguousRange&& r) : SpanStream(std::data(r), std::size(r)) {}
private:
std::streambuf *setbuf(char *buffer, std::streamsize size) final {
setp(buffer, buffer + size);
return this;
}
using std::streambuf::off_type;
using std::streambuf::pos_type;
pos_type seekoff(off_type offset, ios_base::seekdir seekdir,
ios_base::openmode mode) final {
assert(mode == ios_base::out);
auto pos = static_cast<off_type>(pptr() - pbase());
auto new_pos = seekdir == ios_base::beg
? offset // offset was provided relative to beginning; ~absolute
: pos + offset;
if (new_pos < 0 or new_pos >= epptr() - pbase()) {
return pos_type{off_type{-1}}; // return invalid stream position to signal failure
}
pbump(static_cast<int>(new_pos - pos));
return pos_type{new_pos};
}
pos_type seekpos(pos_type pos, ios_base::openmode mode) final {
return seekoff(off_type{pos}, ios_base::beg, mode);
}
};
void usage() {
char storage[16];
SpanStream ss(storage);
ss << 1024 << std::endl;
assert(ss.tellp() == 5);
assert(std::string_view(storage, 5) == "1024\n");
char lorem[] = R"(
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
)";
ss << lorem;
assert(not ss); // SpanStream just becomes falsy on overflow
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment