fast_buf\!

This commit is contained in:
gabime
2014-03-20 03:47:57 +02:00
parent d0f3b05f90
commit ba2b6aea25
12 changed files with 75 additions and 55 deletions

View File

@@ -2,8 +2,7 @@
// Faster than ostringstream--returns its string by ref
#include<streambuf>
#include<string>
#include "c11log/details/fast_buf.h"
namespace c11log
{
@@ -12,7 +11,7 @@ namespace details
class str_devicebuf:public std::streambuf
{
public:
public:
str_devicebuf() = default;
~str_devicebuf() = default;
@@ -21,15 +20,21 @@ public:
str_devicebuf& operator=(const str_devicebuf&) = delete;
str_devicebuf& operator=(str_devicebuf&&) = delete;
/*
const std::string& str_ref() const
{
return _str;
}
*/
bufpair_t buf()
{
return _fastbuf.get();
}
void reset_str()
{
_str.clear();
//_str.clear();
_fastbuf.clear();
}
protected:
@@ -38,32 +43,35 @@ protected:
return 0;
}
// copy the give buffer into the accumulated string.
// reserve initially 128 bytes which should be enough for common log lines
std::streamsize xsputn(const char_type* s, std::streamsize count) override
{
if(_str.capacity() < k_initial_reserve)
{
_str.reserve(k_initial_reserve);
}
_str.append(s, static_cast<unsigned int>(count));
// copy the give buffer into the accumulated string.
// reserve initially 128 bytes which should be enough for common log lines
std::streamsize xsputn(const char_type* s, std::streamsize count) override
{
/*
if(_str.capacity() < k_initial_reserve)
{
_str.reserve(k_initial_reserve);
}
_str.append(s, static_cast<unsigned int>(count));
*/
_fastbuf.append(s, static_cast<unsigned int>(count));
return count;
}
int_type overflow(int_type ch) override
{
bool not_eofile = traits_type::not_eof(ch);
bool not_eofile = traits_type::not_eof(ch);
if (not_eofile)
{
char c = traits_type::to_char_type(ch);
{
char c = traits_type::to_char_type(ch);
xsputn(&c, 1);
}
return not_eofile;
}
return not_eofile;
}
private:
std::string _str;
static constexpr std::streamsize k_initial_reserve = 128;
//std::string _str;
fast_buf<128> _fastbuf;
};
class fast_oss:public std::ostream
@@ -75,15 +83,21 @@ public:
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
{
return _dev.str_ref();
}
void reset_str()
{
_dev.reset_str();
}
*/
bufpair_t buf()
{
return _dev.buf();
}
void reset_str()
{
_dev.reset_str();
}
private:
str_devicebuf _dev;