spdlog/include/c11log/details/stack_oss.h

90 lines
1.7 KiB
C
Raw Normal View History

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