|
- #pragma once
-
- #include <cppcore/conversion/convert_cast.h>
-
- #include "vector_streambuf.h"
-
- namespace cppcore
- {
-
- template<typename T_data, typename T_char>
- basic_vector_streambuf<T_data, T_char>
- ::basic_vector_streambuf(size_t p_grow)
- : _buffer ()
- , _grow (p_grow)
- { }
-
- template<typename T_data, typename T_char>
- typename basic_vector_streambuf<T_data, T_char>::vector_type
- basic_vector_streambuf<T_data, T_char>
- ::get() const
- {
- auto vec = _buffer;
- vec.resize(convert_cast<size_t>(this->pptr() - this->pbase()));
- return vec;
- }
-
-
- template<typename T_data, typename T_char>
- template<typename T_vector>
- void basic_vector_streambuf<T_data, T_char>
- ::set(T_vector&& v)
- {
- _buffer = std::forward<T_vector>(v);
- auto beg = reinterpret_cast<char_type*>(&_buffer.front());
- auto end = beg + _buffer.size();
- this->setp(end, end);
- this->setg(beg, beg, end);
- }
-
- template<typename T_data, typename T_char>
- typename basic_vector_streambuf<T_data, T_char>::vector_type
- basic_vector_streambuf<T_data, T_char>
- ::extract()
- {
- _buffer.resize(convert_cast<size_t>(this->pptr() - this->pbase()));
- auto ret = std::move(_buffer);
- this->setp(nullptr, nullptr);
- this->setg(nullptr, nullptr, nullptr);
- return ret;
- }
-
- template<typename T_data, typename T_char>
- typename basic_vector_streambuf<T_data, T_char>::int_type
- basic_vector_streambuf<T_data, T_char>
- ::underflow()
- {
- auto gpos = convert_cast<size_t>(this->gptr() - this->eback());
- this->setg(this->eback(), this->eback() + gpos, this->pptr());
- if (this->gptr() == this->egptr())
- return traits_type::eof();
- auto ret = convert_cast<int_type>(*this->gptr());
- return ret;
- }
-
- template<typename T_data, typename T_char>
- typename basic_vector_streambuf<T_data, T_char>::int_type
- basic_vector_streambuf<T_data, T_char>
- ::overflow(int_type ch)
- {
- if (ch != traits_type::eof())
- {
- size_t new_cap;
- if (_grow > 0)
- {
- new_cap = _buffer.size() + _grow;
- }
- else
- {
- new_cap = std::min(max_grow, std::max(2 * _buffer.size(), min_grow));
- }
- _buffer.resize(new_cap);
-
- auto beg = reinterpret_cast<char_type*>(&_buffer.front());
- auto end = beg + _buffer.size();
- auto ppos = convert_cast<int>(this->pptr() - this->pbase());
- auto gpos = convert_cast<size_t>(this->gptr() - this->eback());
- this->setp(beg, end);
- this->setg(beg, beg + gpos, beg + ppos);
- this->pbump(ppos);
-
- auto p = this->pptr();
- assert(this->pbase() <= p && p < this->epptr());
- *p = convert_cast<char_type>(ch);
- this->pbump(1);
- }
- return ch;
- }
-
- }
|