spdlog/include/c11log/details/fast_oss.h

78 lines
1.8 KiB
C
Raw Normal View History

2014-01-25 17:09:04 +08:00
#pragma once
#include<streambuf>
#include<string>
2014-02-22 04:51:54 +08:00
namespace c11log {
namespace details {
2014-01-25 17:09:04 +08:00
2014-02-22 04:51:54 +08:00
class str_devicebuf:public std::streambuf {
2014-01-25 17:09:04 +08:00
public:
2014-02-22 04:51:54 +08:00
str_devicebuf() = default;
~str_devicebuf() = default;
str_devicebuf(const str_devicebuf& other):std::streambuf(),_str(other._str) {}
str_devicebuf(str_devicebuf&& other) :std::streambuf(), _str(std::move(other._str)) {
other._str.clear();
2014-02-22 04:51:54 +08:00
}
2014-03-04 07:23:38 +08:00
str_devicebuf& operator=(const str_devicebuf&) = delete;
str_devicebuf& operator=(str_devicebuf&&) = delete;
2014-02-22 04:51:54 +08:00
const std::string& str_ref() const {
return _str;
2014-03-04 07:23:38 +08:00
std::ostringstream oss;
2014-02-22 04:51:54 +08:00
}
2014-01-25 17:09:04 +08:00
2014-02-22 04:51:54 +08:00
void clear() {
_str.clear();
}
2014-01-25 17:09:04 +08:00
protected:
2014-02-22 04:51:54 +08:00
virtual int sync() override {
return 0;
}
2014-01-25 17:09:04 +08:00
2014-02-22 04:51:54 +08:00
virtual std::streamsize xsputn(const char_type* s, std::streamsize count) override {
_str.append(s, static_cast<unsigned int>(count));
return count;
}
2014-01-25 17:09:04 +08:00
2014-02-22 04:51:54 +08:00
virtual int_type overflow(int_type ch) override {
if (ch != traits_type::eof())
_str.append((char*)&ch, 1);
return 1;
}
2014-01-25 17:09:04 +08:00
private:
2014-02-22 04:51:54 +08:00
std::string _str;
2014-01-25 17:09:04 +08:00
};
2014-02-22 04:51:54 +08:00
class fast_oss:public std::ostream {
2014-01-25 17:09:04 +08:00
public:
2014-02-22 04:51:54 +08:00
fast_oss():std::ostream(&_dev) {}
~fast_oss() = default;
2014-03-04 07:23:38 +08:00
2014-03-06 05:07:35 +08:00
fast_oss(const fast_oss& other) :std::basic_ios<char>(), std::ostream(&_dev), _dev(other._dev) {}
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;
2014-03-04 07:23:38 +08:00
2014-02-22 04:51:54 +08:00
const std::string& str_ref() const {
return _dev.str_ref();
}
2014-01-25 17:09:04 +08:00
2014-02-22 04:51:54 +08:00
const std::string str() const {
return _dev.str_ref();
}
2014-01-25 17:09:04 +08:00
2014-02-22 04:51:54 +08:00
void clear() {
_dev.clear();
}
2014-01-25 17:09:04 +08:00
private:
2014-02-22 04:51:54 +08:00
str_devicebuf _dev;
2014-01-25 17:09:04 +08:00
};
}
2014-02-04 02:28:19 +08:00
}