ZeroErr
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
1#pragma once
2#include <type_traits>
3#include "zeroerr/string.h"
4
5namespace zeroerr {
6
8public:
9 struct Format {
10 int base = 10;
11 int precision = 6;
12 int width = 0;
13 char fill = ' ';
14 int style = 0;
15 };
16 StringBuffer() = default;
17 template <class Stream>
18 void attach(Stream& stream) {
19 target_ = &stream;
20 write_ = [](void* p, const char* data, std::size_t size) {
21 static_cast<Stream*>(p)->write(data, size);
22 };
23 flush_ = [](void* p) { static_cast<Stream*>(p)->flush(); };
24 }
25 template <class Stream,
26 typename std::enable_if<!std::is_same<Stream, StringBuffer>::value, int>::type = 0>
27 explicit StringBuffer(Stream& stream)
28 : target_(&stream),
29 write_([](void* p, const char* data, std::size_t size) {
30 static_cast<Stream*>(p)->write(data, size);
31 }),
32 flush_([](void* p) { static_cast<Stream*>(p)->flush(); }) {}
33 StringBuffer& write(const char* data, std::size_t size);
34 void flush();
35 void clear() { text_.clear(); }
36 String str() const { return text_; }
37 const String& text() const { return text_; }
38 Format& format() { return format_; }
39 const Format& format() const { return format_; }
40 StringBuffer& operator<<(const String& text) { return padded(text.data(), text.size()); }
41 StringBuffer& operator<<(const char* text);
42 StringBuffer& operator<<(char value) { return write(&value, 1); }
43 StringBuffer& operator<<(const void* value);
44 StringBuffer& operator<<(std::nullptr_t) { return *this << static_cast<const void*>(nullptr); }
45 template <class T>
46 typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, char>::value,
48 operator<<(T value) {
49 unsigned long long bits = static_cast<unsigned long long>(value);
50 if (format_.base == 8 || format_.base == 16)
51 bits &= (~0ull >> ((sizeof(unsigned long long) - sizeof(T)) * 8));
52 return integer(bits, std::is_signed<T>::value && value < 0);
53 }
54 template <class T>
55 typename std::enable_if<std::is_floating_point<T>::value, StringBuffer&>::type operator<<(
56 T value) {
57 return floating(static_cast<long double>(value));
58 }
59 template <class T>
60 typename std::enable_if<std::is_enum<T>::value, StringBuffer&>::type operator<<(T value) {
61 return *this << static_cast<typename std::underlying_type<T>::type>(value);
62 }
63
64private:
65 StringBuffer& integer(unsigned long long value, bool negative);
66 StringBuffer& floating(long double value);
67 StringBuffer& padded(const char* text, std::size_t size);
68 String text_;
69 void* target_ = nullptr;
70 void (*write_)(void*, const char*, std::size_t) = nullptr;
71 void (*flush_)(void*) = nullptr;
72 Format format_;
73};
74
75} // namespace zeroerr
Definition buffer.h:7
void clear()
Definition buffer.h:35
std::enable_if< std::is_enum< T >::value, StringBuffer & >::type operator<<(T value)
Definition buffer.h:60
void attach(Stream &stream)
Definition buffer.h:18
StringBuffer(Stream &stream)
Definition buffer.h:27
StringBuffer & operator<<(std::nullptr_t)
Definition buffer.h:44
const String & text() const
Definition buffer.h:37
const Format & format() const
Definition buffer.h:39
Format & format()
Definition buffer.h:38
StringBuffer & write(const char *data, std::size_t size)
Definition buffer.cpp:5
void flush()
Definition buffer.cpp:12
StringBuffer & operator<<(char value)
Definition buffer.h:42
StringBuffer & operator<<(const String &text)
Definition buffer.h:40
String str() const
Definition buffer.h:36
std::enable_if< std::is_floating_point< T >::value, StringBuffer & >::type operator<<(T value)
Definition buffer.h:55
Definition string.h:41
const char * data() const noexcept
Definition string.h:76
void clear() noexcept
Definition string.cpp:50
size_type size() const noexcept
Definition string.h:78
STL namespace.
Definition assert_text.cpp:5
Definition buffer.h:9
int base
Definition buffer.h:10
char fill
Definition buffer.h:13
int precision
Definition buffer.h:11
int style
Definition buffer.h:14
int width
Definition buffer.h:12