spdlog/include/c11log/details/stack_oss.h

90 lines
1.7 KiB
C
Raw Normal View History

2014-03-22 20:11:17 +08:00
#pragma once
// Faster than ostringstream--returns its string by ref
2014-03-22 22:26:08 +08:00
#include <ostream>
2014-03-28 21:05:09 +08:00
#include "c11log/details/stack_buf.h"
2014-03-22 20:11:17 +08:00
namespace c11log
{
namespace details
{
class str_devicebuf:public std::streambuf
{
public:
2014-03-28 21:05:09 +08:00
using Base = std::streambuf;
2014-03-22 20:11:17 +08:00
str_devicebuf() = default;
~str_devicebuf() = default;
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;
2014-03-22 22:37:48 +08:00
2014-03-22 20:11:17 +08:00
bufpair_t buf()
{
2014-03-28 21:05:09 +08:00
return _stackbuf.get();
}
std::size_t size()
{
return _stackbuf.size();
2014-03-22 20:11:17 +08:00
}
2014-03-22 22:37:48 +08:00
void clear()
{
2014-03-28 21:05:09 +08:00
_stackbuf.clear();
2014-03-22 20:11:17 +08:00
}
protected:
2014-03-23 07:48:37 +08:00
// copy the give buffer into the accumulated fast buffer
2014-03-22 20:11:17 +08:00
std::streamsize xsputn(const char_type* s, std::streamsize count) override
2014-03-22 22:37:48 +08:00
{
2014-03-28 21:05:09 +08:00
_stackbuf.append(s, static_cast<unsigned int>(count));
2014-03-22 20:11:17 +08:00
return count;
}
int_type overflow(int_type ch) override
2014-03-28 21:05:09 +08:00
{
2014-03-23 07:48:37 +08:00
if (traits_type::not_eof(ch))
2014-03-22 20:11:17 +08:00
{
char c = traits_type::to_char_type(ch);
xsputn(&c, 1);
}
2014-03-23 07:48:37 +08:00
return ch;
2014-03-22 20:11:17 +08:00
}
private:
2014-03-28 21:05:09 +08:00
stack_buf<192> _stackbuf;
2014-03-22 20:11:17 +08:00
};
2014-03-28 21:05:09 +08:00
class stack_oss:public std::ostream
2014-03-22 20:11:17 +08:00
{
public:
2014-03-28 21:05:09 +08:00
stack_oss():std::ostream(&_dev) {}
~stack_oss() = default;
2014-03-22 20:11:17 +08:00
2014-03-28 21:05:09 +08:00
stack_oss(const stack_oss& other) = delete;
stack_oss(stack_oss&& other) = delete;
stack_oss& operator=(const stack_oss& other) = delete;
2014-03-22 22:26:08 +08:00
2014-03-22 20:11:17 +08:00
bufpair_t buf()
{
return _dev.buf();
}
2014-03-28 21:05:09 +08:00
std::size_t size()
{
return _dev.size();
}
2014-03-22 22:37:48 +08:00
void clear()
2014-03-22 20:11:17 +08:00
{
2014-03-22 22:37:48 +08:00
_dev.clear();
2014-03-22 20:11:17 +08:00
}
private:
str_devicebuf _dev;
};
}
}