1. Synopsis
namespace strf {
template <typename T, unsigned Log2BufferSpace>
class output_buffer;
constexpr std::size_t min_destination_buffer_size = …; // >= 64
template <typename CharT>
using destination = output_buffer<CharT, …>;
template <typename CharT>
class basic_cstr_destination;
template <typename CharT>
class array_destination;
template <typename CharT>
class discarder;
template <typename CharT>
CharT* garbage_buff();
template <typename CharT>
CharT* garbage_buff_end();
using u8cstr_destination = basic_cstr_destination<char8_t>;
using cstr_destination = basic_cstr_destination<char>;
using u16cstr_destination = basic_cstr_destination<char16_t>;
using u32cstr_destination = basic_cstr_destination<char32_t>;
using wcstr_destination = basic_cstr_destination<wchar_t>;
} // namespace strf
2. Class template output_buffer
2.1. Synopsis
namespace strf {
template <typename T, unsigned Log2BufferSpace>
class output_buffer: output_buffer<T, Log2BufferSpace - 1>
{
public:
static constexpr std::size_t min_space_after_recycle = 1 << Log2BufferSpace;
static constexpr std::ptrdiff_t min_sspace_after_recycle = 1 << Log2BufferSpace;
protected:
using output_buffer<T, Log2BufferSpace - 1>::output_buffer;
};
template <typename T>
class output_buffer<T, 0>
{
public:
static constexpr std::size_t min_space_after_recycle = 1;
static constexpr std::ptrdiff_t min_sspace_after_recycle = 1;
using value_type = T;
output_buffer(const output_buffer&) = delete;
output_buffer(output_buffer&&) = delete;
output_buffer& operator=(const output_buffer&) = delete;
output_buffer& operator=(output_buffer&&) = delete;
virtual ~output_buffer() = default;
T* buffer_ptr() const noexcept;
T* ptr() const noexcept;
T* buffer_end() const noexcept;
T* end() const noexcept;
std::size_t buffer_space() const noexcept;
std::size_t space() const noexcept;
std::ptrdiff_t buffer_sspace() const noexcept;
std::ptrdiff_t sspace() const noexcept;
bool good() const noexcept;
void advance_to(T* p);
void advance();
void advance_count(std::integral auto n);
void ensure(std::integral auto s);
void flush();
void write(const T* data, std::integral auto count);
virtual void recycle() = 0;
protected:
output_buffer(T* buff, T* buff_end);
output_buffer(T* buff, std::ptrdiff_t buff_size);
void set_buffer_ptr(T* p) noexcept;
void set_buffer_ptr(T* p) noexcept;
void set_buffer_end(T* e) noexcept;
void set_buffer_end(T* e) noexcept;
void set_good(bool g) noexcept;
virtual void do_write(const T* data, std::size_t count);
};
// global functions
template <typename T>
void put(output_buffer<T, 0>& dest, T value);
} // namespace strf
2.2. Member types
using value_type = T;
2.3. Public member functions
T* buffer_ptr() const noxcept;
T* ptr() const noxcept;
Return |
The memory position where the content shall be written. |
T* buffer_end() const noxcept;
T* end() const noxcept;
Return |
The end of memory position where the content shall be written.
Dereferencing |
std::size_t buffer_space() const noexcept;
std::size_t space() const noexcept;
std::ptrdiff_t_t buffer_sspace() const noexcept;
std::ptrdiff_t_t sspace() const noexcept;
Return |
|
void flush();
Effect |
Calls |
virtual void recycle() = 0;
Posconditions |
|
void ensure(std::integral auto s)
Effect |
Calls |
Precondition |
|
Postcondition |
|
void advance_to(T* p)
Effect |
Advance the buffer’s pointer to |
Precondition |
|
Postcondition |
|
void advance()
Effect |
Equivalent to |
Precondition |
|
void advance(std::integral auto n)
Effect |
Equivalent to |
Precondition |
|
bool good() const;
Return |
The state of this object. |
Semantincs |
|
Note |
The range [ |
void write(const T* data, std::integral auto count);
Effect |
None if |
2.4. Protected Member functions
output_buffer(T* buff_, T* buff_end_)
Preconditions |
|
Posconditions |
|
output_buffer(T* buff_, std::ptrdiff_t buff_size_)
Preconditions |
|
Posconditions |
|
void set_buffer_ptr(T* p) noexcept;
void set_ptr(T* p) noexcept;
Postconditions |
|
void set_buffer_end(T* e) noexcept;
void set_end(T* e) noexcept;
Postconditions |
|
void set_good(bool g) noexcept;
Postconditions |
|
virtual void do_write(const T* data, std::size_t count);
Effect |
Writes the first |
Note |
This function is made virtual so that any derived classes can override it with an optimized version. |
2.5. Global functions
template <typename T>
void put(output_buffer<T, 0>& dest, T value);
Effect |
|
3. Type alias template destination
namespace strf {
constexpr unsigned log2_min_destination_buffer_size = …; // >= 6
constexpr std::size_t min_destination_buffer_size = …; // >= 64
template <typename CharT>
using destination = output_buffer<CharT, log2_min_destination_buffer_size>;
} // namespace strf
-
log2_min_destination_buffer_size
is an implementation-defined value that is greater than or equal to6
. -
min_destination_buffer_size
is equal to(std::size_t)1 << log2_min_destination_buffer_size
4. Class template basic_cstr_destination
namespace strf {
template <typename CharT>
class basic_cstr_destination final: public output_buffer<CharT, log2_garbage_buff_size> {
public:
basic_cstr_destination(CharT* dest, CharT* dest_end) noexcept;
basic_cstr_destination(CharT* dest, std::integral auto len) noexcept;
template <std::size_t N>
basic_cstr_destination(CharT (&dest)[N]) noexcept;
basic_cstr_destination(const basic_cstr_destination&) = delete;
basic_cstr_destination(basic_cstr_destination&&) = delete;
basic_cstr_destination& operator=(const basic_cstr_destination&) = delete;
basic_cstr_destination& operator=(basic_cstr_destination&&) = delete;
basic_cstr_destination() override = default;
void recycle() noexcept override;
struct result {
CharT* ptr;
bool truncated;
};
result finish() noexcept;
};
} // namespace strf
4.1. Public member functions
basic_cstr_destination(CharT* dest, CharT* dest_end) noexcept;
Precondition |
|
Postconditions |
|
basic_cstr_destination(CharT* dest, std::integral auto dest_size) noexcept;
Precondition |
|
Postconditions |
|
template <std::size_t N>
basic_cstr_destination(CharT (&dest)[N]) noexcept;
Postconditions |
|
void recycle() noexcept;
Postconditions |
|
result finish() noexcept;
Effects |
|
Return value |
|
Postconditions |
|
5. Class template array_destination
namespace strf {
template <typename CharT>
class array_destination final : public output_buffer<CharT, log2_garbage_buff_size> {
public:
template <std::size_t N>
array_destination(CharT (&dest)[N]) noexcept;
array_destination(CharT* dest, CharT* dest_end) noexcept;
array_destination(CharT* dest, std::integral auto dest_size) noexcept;
array_destination(const array_destination&) = delete;
array_destination(array_destination&&) = delete;
array_destination& operator=(const array_destination&) = delete;
array_destination& operator=(array_destination&&) = delete;
~array_destination() override = default;
void recycle() noexcept override;
struct result {
CharT* ptr;
bool truncated;
};
result finish() noexcept;
};
} // namespace strf
5.1. Public member functions
template <std::size_t N>
array_destination(CharT (&dest)[N]) noexcept;
- Postconditions
-
-
good() == true
-
buffer_ptr() == dest
-
buffer_end() == dest + N
-
array_destination(CharT* dest, CharT* dest_end) noexcept;
Precondition |
|
Postconditions |
|
array_destination(CharT* dest, std::integral auto dest_size) noexcept;
Precondition |
|
Postconditions |
|
void recycle() noexcept;
Postconditions |
|
result finish() noexcept;
- Return value
-
-
result.truncated
istrue
whenrecycle()
ordo_write(…)
has been previously called in this object, which means that the the range which with it was initialized is too small. -
result::ptr
is the one-past-the-end pointer of the characters written. However, whenresult.truncated
istrue
, the number of characters written is unspecified.
-
6. Class template discarder
discarder
it’s the library’s analogous to /dev/null
.
A discarder
object ignores anything written to it.
namespace strf {
template <typename CharT>
class discarder final: public output_buffer<CharT, {log2_garbage_buff_size}>
{
public:
discarder() noexcept;
void recycle() noexcept override;
};
} // namespace strf
discarder() noexcept;
Postconditions |
|
void recycle() noexcept;
Postconditions |
|
7. Garbage buffer
These function templates return the begin and the end of a memory area that is never supposed to be read. It can be used when implementing a class that derives from output_buffer
to set the buffer when the state is "bad".
constexpr unsigned log2_garbage_buff_size = …;
Implementation-defined type that is greater than or equal to
log2_min_destination_buffer_size
;
constexpr std::size_t garbage_buff_size = (std::size_t)1 << log2_garbage_buff_size;
template <typename CharT>
CharT* garbage_buff() noexcept;
Returns the begin a memory area of garbage_buff_size
elements that are never supposed to be read.
template <typename CharT>
CharT* garbage_buff_end() noexcept;
Returns garbage_buff() + garbage_buff_size