spdlog/include/c11log/details/fast_oss.h

77 lines
1.5 KiB
C
Raw Normal View History

2014-01-25 04:09:04 -05:00
#pragma once
2014-03-06 17:52:50 -05:00
// Fast ostringstream like supprt which return its string by ref and nothing more
2014-01-25 04:09:04 -05:00
#include<streambuf>
#include<string>
2014-03-06 17:52:50 -05:00
namespace c11log
{
namespace details
{
class str_devicebuf:public std::streambuf
{
2014-01-25 04:09:04 -05:00
public:
2014-02-21 15:51:54 -05:00
str_devicebuf() = default;
~str_devicebuf() = default;
2014-03-03 18:23:38 -05:00
2014-03-06 17:52:50 -05:00
str_devicebuf(const str_devicebuf& other) = delete;
str_devicebuf(str_devicebuf&& other) = delete;
str_devicebuf& operator=(const str_devicebuf&) = delete;
str_devicebuf& operator=(str_devicebuf&&) = delete;
const std::string& str_ref() const
{
2014-02-21 15:51:54 -05:00
return _str;
}
2014-01-25 04:09:04 -05:00
void clear()
{
2014-02-21 15:51:54 -05:00
_str.clear();
}
2014-01-25 04:09:04 -05:00
protected:
virtual int sync() override
{
2014-02-21 15:51:54 -05:00
return 0;
}
2014-01-25 04:09:04 -05:00
virtual std::streamsize xsputn(const char_type* s, std::streamsize count) override
{
2014-02-21 15:51:54 -05:00
_str.append(s, static_cast<unsigned int>(count));
return count;
}
2014-01-25 04:09:04 -05:00
virtual int_type overflow(int_type ch) override
{
2014-02-21 15:51:54 -05:00
if (ch != traits_type::eof())
_str.append((char*)&ch, 1);
return 1;
}
2014-01-25 04:09:04 -05:00
private:
2014-02-21 15:51:54 -05:00
std::string _str;
2014-01-25 04:09:04 -05:00
};
2014-03-06 17:52:50 -05:00
class fast_oss:public std::ostream
{
2014-01-25 04:09:04 -05:00
public:
2014-02-21 15:51:54 -05:00
fast_oss():std::ostream(&_dev) {}
~fast_oss() = default;
2014-03-03 18:23:38 -05:00
2014-03-06 17:52:50 -05:00
fast_oss(const fast_oss& other) = delete;
fast_oss(fast_oss&& other) = delete;
fast_oss& operator=(const fast_oss& other) = delete;
const std::string& str_ref() const
{
2014-02-21 15:51:54 -05:00
return _dev.str_ref();
}
2014-01-25 04:09:04 -05:00
private:
2014-02-21 15:51:54 -05:00
str_devicebuf _dev;
2014-01-25 04:09:04 -05:00
};
}
2014-02-03 13:28:19 -05:00
}