fast_oss.h 1.78 KB
Newer Older
gab's avatar
gab committed
1 2 3 4
#pragma once

#include<streambuf>
#include<string>
gabime's avatar
gabime committed
5 6
namespace c11log {
namespace details {
gab's avatar
gab committed
7

gabime's avatar
gabime committed
8
class str_devicebuf:public std::streambuf {
gab's avatar
gab committed
9
public:
gabime's avatar
gabime committed
10 11 12
    str_devicebuf() = default;
    ~str_devicebuf() = default;
    str_devicebuf(const str_devicebuf& other):std::streambuf(),_str(other._str) {}
13 14 15

    str_devicebuf(str_devicebuf&& other) :std::streambuf(), _str(std::move(other._str)) {
        other._str.clear();
gabime's avatar
gabime committed
16
    }
gabi's avatar
gabi committed
17

18 19 20 21
    str_devicebuf& operator=(const str_devicebuf&) = delete;
    str_devicebuf& operator=(str_devicebuf&&) = delete;


gabime's avatar
gabime committed
22 23
    const std::string& str_ref() const {
        return _str;
gabi's avatar
gabi committed
24
        std::ostringstream oss;
gabime's avatar
gabime committed
25
    }
gab's avatar
gab committed
26

gabime's avatar
gabime committed
27 28 29
    void clear() {
        _str.clear();
    }
gab's avatar
gab committed
30 31

protected:
gabime's avatar
gabime committed
32 33 34
    virtual int sync() override {
        return 0;
    }
gab's avatar
gab committed
35

gabime's avatar
gabime committed
36 37 38 39
    virtual std::streamsize xsputn(const char_type* s, std::streamsize count) override {
        _str.append(s, static_cast<unsigned int>(count));
        return count;
    }
gab's avatar
gab committed
40

gabime's avatar
gabime committed
41 42 43 44 45
    virtual int_type overflow(int_type ch) override {
        if (ch != traits_type::eof())
            _str.append((char*)&ch, 1);
        return 1;
    }
gab's avatar
gab committed
46
private:
gabime's avatar
gabime committed
47
    std::string _str;
gab's avatar
gab committed
48 49
};

gabime's avatar
gabime committed
50
class fast_oss:public std::ostream {
gab's avatar
gab committed
51
public:
gabime's avatar
gabime committed
52 53
    fast_oss():std::ostream(&_dev) {}
    ~fast_oss() = default;
gabi's avatar
gabi committed
54

gabime's avatar
fixes  
gabime committed
55
    fast_oss(const fast_oss& other) :std::basic_ios<char>(), std::ostream(&_dev), _dev(other._dev) {}
56 57 58 59 60

    fast_oss(fast_oss&& other) :std::basic_ios<char>(), std::ostream(&_dev), _dev(std::move(other._dev)) {}

    fast_oss& operator=(const fast_oss& other) = delete;

gabi's avatar
gabi committed
61

gabime's avatar
gabime committed
62 63 64
    const std::string& str_ref() const {
        return _dev.str_ref();
    }
gab's avatar
gab committed
65

gabime's avatar
gabime committed
66 67 68
    const std::string str() const {
        return _dev.str_ref();
    }
gab's avatar
gab committed
69

gabime's avatar
gabime committed
70 71 72
    void clear() {
        _dev.clear();
    }
gab's avatar
gab committed
73
private:
gabime's avatar
gabime committed
74
    str_devicebuf _dev;
gab's avatar
gab committed
75 76
};
}
gabime's avatar
fixes  
gabime committed
77
}