2014-05-09 21:33:55 +08:00
|
|
|
#pragma once
|
|
|
|
|
2014-05-11 08:33:33 +08:00
|
|
|
// A faster-than-ostringstream class
|
|
|
|
// uses stack_buf as the underlying buffer (upto 192 bytes before using the heap)
|
|
|
|
|
2014-05-09 21:33:55 +08:00
|
|
|
#include <ostream>
|
2014-05-13 06:41:04 +08:00
|
|
|
#include <iomanip>
|
2014-10-14 08:44:40 +08:00
|
|
|
#include "fast_istostr.h"
|
2014-05-09 21:33:55 +08:00
|
|
|
#include "stack_buf.h"
|
|
|
|
|
2014-10-14 08:44:40 +08:00
|
|
|
|
2014-05-09 21:33:55 +08:00
|
|
|
namespace c11log
|
|
|
|
{
|
|
|
|
namespace details
|
|
|
|
{
|
|
|
|
|
|
|
|
class stack_devicebuf :public std::streambuf
|
|
|
|
{
|
2014-05-13 06:41:04 +08:00
|
|
|
public:
|
2014-10-14 08:44:40 +08:00
|
|
|
static const unsigned short stack_size = 256;
|
2014-05-11 08:33:33 +08:00
|
|
|
using stackbuf_t = stack_buf<stack_size>;
|
|
|
|
|
2014-05-09 21:33:55 +08:00
|
|
|
stack_devicebuf() = default;
|
|
|
|
~stack_devicebuf() = default;
|
|
|
|
stack_devicebuf& operator=(const stack_devicebuf&) = delete;
|
|
|
|
|
|
|
|
stack_devicebuf(const stack_devicebuf& other) :std::basic_streambuf<char>(), _stackbuf(other._stackbuf)
|
|
|
|
{}
|
|
|
|
|
2014-05-09 23:00:10 +08:00
|
|
|
stack_devicebuf(stack_devicebuf&& other):
|
|
|
|
std::basic_streambuf<char>(),
|
|
|
|
_stackbuf(std::move(other._stackbuf))
|
2014-05-09 21:33:55 +08:00
|
|
|
{
|
|
|
|
other.clear();
|
|
|
|
}
|
|
|
|
|
2014-05-11 08:33:33 +08:00
|
|
|
stackbuf_t::bufpair_t buf() const
|
2014-05-09 21:33:55 +08:00
|
|
|
{
|
|
|
|
return _stackbuf.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t size() const
|
|
|
|
{
|
|
|
|
return _stackbuf.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
_stackbuf.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// copy the give buffer into the accumulated fast buffer
|
|
|
|
std::streamsize xsputn(const char_type* s, std::streamsize count) override
|
|
|
|
{
|
|
|
|
_stackbuf.append(s, static_cast<unsigned int>(count));
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int_type overflow(int_type ch) override
|
|
|
|
{
|
|
|
|
if (traits_type::not_eof(ch))
|
|
|
|
{
|
|
|
|
char c = traits_type::to_char_type(ch);
|
|
|
|
xsputn(&c, 1);
|
|
|
|
}
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
private:
|
2014-05-11 08:33:33 +08:00
|
|
|
stackbuf_t _stackbuf;
|
2014-05-09 21:33:55 +08:00
|
|
|
};
|
|
|
|
|
2014-05-11 08:33:33 +08:00
|
|
|
|
2014-05-09 21:33:55 +08:00
|
|
|
class fast_oss :public std::ostream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
fast_oss() :std::ostream(&_dev) {}
|
|
|
|
~fast_oss() = default;
|
|
|
|
|
|
|
|
fast_oss& operator=(const fast_oss& other) = delete;
|
|
|
|
fast_oss& operator=(const fast_oss&& other) = delete;
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
other.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string str()
|
|
|
|
{
|
|
|
|
auto buf = _dev.buf();
|
|
|
|
return std::string(buf.first, buf.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::size_t size() const
|
|
|
|
{
|
|
|
|
return _dev.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
_dev.clear();
|
|
|
|
}
|
|
|
|
|
2014-10-14 08:44:40 +08:00
|
|
|
// The following were added because they add significant boost to perfromance
|
|
|
|
void putc(char c)
|
|
|
|
{
|
|
|
|
_dev.sputc(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// put int and pad with zeroes if smalled than min_width
|
|
|
|
void put_int(int n, int min_width)
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
details::fast_itostr(n, s, min_width);
|
|
|
|
_dev.sputn(s.data(), s.size());
|
|
|
|
//sprintf_s(buf, "%d", n);
|
|
|
|
//_dev.sputn(buf, width);
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:56:10 +08:00
|
|
|
void put_str(const std::string& s)
|
2014-10-14 08:44:40 +08:00
|
|
|
{
|
2014-10-14 09:56:10 +08:00
|
|
|
_dev.sputn(s.data(), s.size());
|
2014-10-14 08:44:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-09 21:33:55 +08:00
|
|
|
private:
|
|
|
|
stack_devicebuf _dev;
|
|
|
|
};
|
|
|
|
}
|
2014-05-09 23:00:10 +08:00
|
|
|
}
|