2014-03-19 22:00:26 -04:00
|
|
|
#pragma once
|
|
|
|
|
2014-03-22 10:29:43 -04:00
|
|
|
#include <array>
|
2014-03-19 22:00:26 -04:00
|
|
|
#include <vector>
|
2014-03-22 10:29:43 -04:00
|
|
|
#include <algorithm>
|
2014-03-28 12:03:24 -04:00
|
|
|
#include <cstring>
|
2014-03-19 22:00:26 -04:00
|
|
|
|
2014-03-22 10:26:08 -04:00
|
|
|
// Fast memory storage
|
2014-03-19 22:00:26 -04:00
|
|
|
// stores its contents on the stack when possible, in vector<char> otherwise
|
|
|
|
// NOTE: User should be remember that returned buffer might be on the stack!!
|
|
|
|
|
2014-03-22 10:37:48 -04:00
|
|
|
namespace c11log
|
|
|
|
{
|
|
|
|
namespace details
|
|
|
|
{
|
2014-03-19 22:00:26 -04:00
|
|
|
|
|
|
|
template<std::size_t STACK_SIZE=128>
|
2014-03-28 09:05:09 -04:00
|
|
|
class stack_buf
|
2014-03-19 22:00:26 -04:00
|
|
|
{
|
|
|
|
public:
|
2014-03-28 09:05:09 -04:00
|
|
|
stack_buf():_stack_size(0) {}
|
|
|
|
~stack_buf() {};
|
2014-03-19 22:00:26 -04:00
|
|
|
|
2014-03-28 09:05:09 -04:00
|
|
|
stack_buf(const bufpair_t& buf_to_copy):stack_buf()
|
2014-03-22 08:11:17 -04:00
|
|
|
{
|
|
|
|
append(buf_to_copy);
|
|
|
|
}
|
2014-03-19 22:00:26 -04:00
|
|
|
|
2014-03-28 09:05:09 -04:00
|
|
|
stack_buf(const stack_buf& other)
|
2014-03-19 22:00:26 -04:00
|
|
|
{
|
|
|
|
_stack_size = other._stack_size;
|
|
|
|
if(!other._v.empty())
|
|
|
|
_v = other._v;
|
|
|
|
else if(_stack_size)
|
|
|
|
std::copy(other._stack_buf.begin(), other._stack_buf.begin()+_stack_size, _stack_buf.begin());
|
|
|
|
}
|
|
|
|
|
2014-03-28 09:05:09 -04:00
|
|
|
stack_buf(stack_buf&& other)
|
2014-03-19 22:00:26 -04:00
|
|
|
{
|
|
|
|
_stack_size = other._stack_size;
|
|
|
|
if(!other._v.empty())
|
|
|
|
_v = other._v;
|
|
|
|
else if(_stack_size)
|
|
|
|
std::copy(other._stack_buf.begin(), other._stack_buf.begin()+_stack_size, _stack_buf.begin());
|
|
|
|
other.clear();
|
|
|
|
}
|
|
|
|
|
2014-03-28 09:05:09 -04:00
|
|
|
stack_buf& operator=(const stack_buf& other) = delete;
|
|
|
|
stack_buf& operator=(stack_buf&& other) = delete;
|
2014-03-19 22:00:26 -04:00
|
|
|
|
2014-03-28 09:05:09 -04:00
|
|
|
void append(const char* buf, std::size_t buf_size)
|
2014-03-19 22:00:26 -04:00
|
|
|
{
|
|
|
|
//If we are aleady using _v, forget about the stack
|
|
|
|
if(!_v.empty())
|
|
|
|
{
|
2014-03-28 09:05:09 -04:00
|
|
|
_v.insert(_v.end(), buf, buf+ buf_size);
|
2014-03-19 22:00:26 -04:00
|
|
|
}
|
|
|
|
//Try use the stack
|
|
|
|
else
|
|
|
|
{
|
2014-03-28 09:05:09 -04:00
|
|
|
if(_stack_size+buf_size <= STACK_SIZE)
|
2014-03-19 22:00:26 -04:00
|
|
|
{
|
2014-03-28 09:05:09 -04:00
|
|
|
std::memcpy(&_stack_buf[_stack_size], buf, buf_size);
|
|
|
|
_stack_size+=buf_size;
|
2014-03-19 22:00:26 -04:00
|
|
|
}
|
|
|
|
//Not enough stack space. Copy all to _v
|
|
|
|
else
|
|
|
|
{
|
2014-03-28 09:05:09 -04:00
|
|
|
_v.reserve(_stack_size+buf_size);
|
2014-03-19 22:00:26 -04:00
|
|
|
if(_stack_size)
|
|
|
|
_v.insert(_v.end(), _stack_buf.begin(), _stack_buf.begin() +_stack_size);
|
2014-03-28 09:05:09 -04:00
|
|
|
_v.insert(_v.end(), buf, buf+buf_size);
|
2014-03-19 22:00:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 08:11:17 -04:00
|
|
|
void append(const bufpair_t &buf)
|
|
|
|
{
|
|
|
|
append(buf.first, buf.second);
|
|
|
|
}
|
2014-03-19 22:00:26 -04:00
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
_stack_size = 0;
|
|
|
|
_v.clear();
|
|
|
|
}
|
|
|
|
|
2014-03-30 18:06:46 -04:00
|
|
|
bufpair_t get() const
|
2014-03-19 22:00:26 -04:00
|
|
|
{
|
|
|
|
if(!_v.empty())
|
|
|
|
return bufpair_t(_v.data(), _v.size());
|
|
|
|
else
|
|
|
|
return bufpair_t(_stack_buf.data(), _stack_size);
|
|
|
|
}
|
|
|
|
|
2014-03-30 18:06:46 -04:00
|
|
|
std::size_t size() const
|
2014-03-28 09:05:09 -04:00
|
|
|
{
|
|
|
|
if(!_v.empty())
|
|
|
|
return _v.size();
|
|
|
|
else
|
|
|
|
return _stack_size;
|
|
|
|
}
|
|
|
|
|
2014-03-19 22:00:26 -04:00
|
|
|
private:
|
|
|
|
std::vector<char> _v;
|
|
|
|
std::array<char, STACK_SIZE> _stack_buf;
|
2014-03-22 08:11:17 -04:00
|
|
|
std::size_t _stack_size;
|
2014-03-19 22:00:26 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
} //namespace c11log { namespace details {
|