2021-12-10 19:15:48 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// A multi-platform support c++11 library with focus on asynchronous socket I/O for any
|
|
|
|
// client application.
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2022-01-24 16:39:18 +08:00
|
|
|
Copyright (c) 2012-2022 HALX99
|
2021-12-10 19:15:48 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
|
2021-12-11 13:02:56 +08:00
|
|
|
The byte_buffer concepts:
|
2021-12-10 19:15:48 +08:00
|
|
|
a. The memory model is similar to to std::vector<char>, std::string
|
2021-12-11 13:02:56 +08:00
|
|
|
b. Use c realloc to manage memory
|
|
|
|
c. Implemented operations: resize(without fill), resize_fit, attach/detach(stl not support)
|
2021-12-10 19:15:48 +08:00
|
|
|
*/
|
2021-12-11 13:02:56 +08:00
|
|
|
#ifndef YASIO__BYTE_BUFFER_HPP
|
|
|
|
#define YASIO__BYTE_BUFFER_HPP
|
2021-12-10 19:15:48 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
2021-12-12 10:20:34 +08:00
|
|
|
#include <utility>
|
2021-12-10 19:15:48 +08:00
|
|
|
#include <memory>
|
2021-12-12 10:20:34 +08:00
|
|
|
#include <iterator>
|
2021-12-18 15:17:46 +08:00
|
|
|
#include <limits>
|
2021-12-12 10:20:34 +08:00
|
|
|
#include <algorithm>
|
2021-12-11 13:02:56 +08:00
|
|
|
#include <type_traits>
|
2021-12-11 20:15:29 +08:00
|
|
|
#include <stdexcept>
|
2022-07-03 17:24:10 +08:00
|
|
|
#include <initializer_list>
|
2022-07-07 14:49:26 +08:00
|
|
|
#include <type_traits>
|
2021-12-11 13:02:56 +08:00
|
|
|
#include "yasio/compiler/feature_test.hpp"
|
2021-12-10 19:15:48 +08:00
|
|
|
|
|
|
|
namespace yasio
|
|
|
|
{
|
2022-07-07 09:31:34 +08:00
|
|
|
#define _YASIO_VERIFY_RANGE(cond, mesg) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (cond) \
|
|
|
|
; /* contextually convertible to bool paranoia */ \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
throw std::out_of_range(mesg); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
} while (false)
|
|
|
|
|
2022-07-07 14:49:26 +08:00
|
|
|
template <bool _Test, class _Ty = void>
|
|
|
|
using enable_if_t = typename ::std::enable_if<_Test, _Ty>::type;
|
|
|
|
|
2021-12-11 20:15:29 +08:00
|
|
|
struct default_allocator {
|
2022-07-07 09:31:34 +08:00
|
|
|
static void* reallocate(void* old_block, size_t /*old_size*/, size_t new_size)
|
|
|
|
{
|
|
|
|
return ::realloc(old_block, new_size);
|
|
|
|
}
|
2021-12-11 20:15:29 +08:00
|
|
|
};
|
2022-07-04 09:13:06 +08:00
|
|
|
template <typename _Elem, typename _Alloc = default_allocator>
|
|
|
|
class basic_byte_buffer {
|
2021-12-12 10:20:34 +08:00
|
|
|
static_assert(std::is_same<_Elem, char>::value || std::is_same<_Elem, unsigned char>::value,
|
|
|
|
"The basic_byte_buffer only accept type which is char or unsigned char!");
|
2022-07-03 17:24:10 +08:00
|
|
|
|
2021-12-10 19:15:48 +08:00
|
|
|
public:
|
2022-07-07 14:49:26 +08:00
|
|
|
using pointer = _Elem*;
|
|
|
|
using const_pointer = const _Elem*;
|
|
|
|
using reference = _Elem&;
|
|
|
|
using const_reference = const _Elem&;
|
|
|
|
using size_type = size_t;
|
|
|
|
using value_type = _Elem;
|
|
|
|
using iterator = _Elem*; // byte_buffer only needs transparent iterator
|
|
|
|
using const_iterator = const _Elem*;
|
|
|
|
using allocator_type = _Alloc;
|
2021-12-10 19:15:48 +08:00
|
|
|
basic_byte_buffer() {}
|
2021-12-11 20:15:29 +08:00
|
|
|
explicit basic_byte_buffer(size_t count) { resize(count); }
|
2021-12-12 10:20:34 +08:00
|
|
|
basic_byte_buffer(size_t count, std::true_type /*fit*/) { resize_fit(count); }
|
2022-07-07 14:49:26 +08:00
|
|
|
basic_byte_buffer(size_t count, const_reference val) { resize(count, val); }
|
|
|
|
basic_byte_buffer(size_t count, const_reference val, std::true_type /*fit*/)
|
|
|
|
{
|
|
|
|
resize_fit(count, val);
|
|
|
|
}
|
2022-07-04 09:13:06 +08:00
|
|
|
template <typename _Iter>
|
|
|
|
basic_byte_buffer(_Iter first, _Iter last)
|
|
|
|
{
|
|
|
|
assign(first, last);
|
|
|
|
}
|
|
|
|
template <typename _Iter>
|
|
|
|
basic_byte_buffer(_Iter first, _Iter last, std::true_type /*fit*/)
|
|
|
|
{
|
|
|
|
assign(first, last, std::true_type{});
|
|
|
|
}
|
2022-07-03 17:24:10 +08:00
|
|
|
basic_byte_buffer(const basic_byte_buffer& rhs) { assign(rhs); };
|
2022-07-07 09:31:34 +08:00
|
|
|
basic_byte_buffer(const basic_byte_buffer& rhs, std::true_type /*fit*/)
|
|
|
|
{
|
|
|
|
assign(rhs, std::true_type{});
|
2022-07-07 14:49:26 +08:00
|
|
|
}
|
2022-07-03 17:24:10 +08:00
|
|
|
basic_byte_buffer(basic_byte_buffer&& rhs) noexcept { assign(std::move(rhs)); }
|
2022-07-07 14:49:26 +08:00
|
|
|
template <typename _Ty, enable_if_t<std::is_integral<_Ty>::value, int> = 0>
|
2022-07-04 09:13:06 +08:00
|
|
|
basic_byte_buffer(std::initializer_list<_Ty> rhs)
|
|
|
|
{
|
|
|
|
assign(rhs);
|
|
|
|
}
|
2022-07-07 14:49:26 +08:00
|
|
|
template <typename _Ty, enable_if_t<std::is_integral<_Ty>::value, int> = 0>
|
2022-07-04 09:13:06 +08:00
|
|
|
basic_byte_buffer(std::initializer_list<_Ty> rhs, std::true_type /*fit*/)
|
|
|
|
{
|
|
|
|
assign(rhs, std::true_type{});
|
|
|
|
}
|
2021-12-11 13:02:56 +08:00
|
|
|
~basic_byte_buffer() { shrink_to_fit(0); }
|
2022-07-03 17:24:10 +08:00
|
|
|
basic_byte_buffer& operator=(const basic_byte_buffer& rhs)
|
2021-12-10 19:15:48 +08:00
|
|
|
{
|
2022-07-03 17:24:10 +08:00
|
|
|
assign(rhs);
|
2021-12-10 19:15:48 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2022-07-04 09:13:06 +08:00
|
|
|
basic_byte_buffer& operator=(basic_byte_buffer&& rhs) noexcept
|
|
|
|
{
|
|
|
|
this->swap(rhs);
|
|
|
|
return *this;
|
|
|
|
}
|
2022-07-06 16:56:57 +08:00
|
|
|
template <typename _Cont>
|
|
|
|
basic_byte_buffer& operator+=(const _Cont& rhs)
|
|
|
|
{
|
|
|
|
return this->append(std::begin(rhs), std::end(rhs));
|
|
|
|
}
|
2022-07-07 14:49:26 +08:00
|
|
|
basic_byte_buffer& operator+=(const_reference rhs)
|
2022-07-06 16:56:57 +08:00
|
|
|
{
|
|
|
|
this->push_back(rhs);
|
|
|
|
return *this;
|
|
|
|
}
|
2022-07-04 09:13:06 +08:00
|
|
|
template <typename _Iter>
|
|
|
|
void assign(const _Iter first, const _Iter last)
|
|
|
|
{
|
|
|
|
_Assign_range(first, last);
|
|
|
|
}
|
|
|
|
template <typename _Iter>
|
|
|
|
void assign(const _Iter first, const _Iter last, std::true_type /*fit*/)
|
|
|
|
{
|
|
|
|
_Assign_range(first, last, std::true_type{});
|
|
|
|
}
|
2022-07-03 17:24:10 +08:00
|
|
|
void assign(const basic_byte_buffer& rhs) { _Assign_range(rhs.begin(), rhs.end()); }
|
2022-07-07 09:31:34 +08:00
|
|
|
void assign(const basic_byte_buffer& rhs, std::true_type)
|
|
|
|
{
|
|
|
|
_Assign_range(rhs.begin(), rhs.end(), std::true_type{});
|
|
|
|
}
|
2022-07-03 17:24:10 +08:00
|
|
|
void assign(basic_byte_buffer&& rhs) { _Assign_rv(std::move(rhs)); }
|
2022-07-07 14:49:26 +08:00
|
|
|
template <typename _Ty, enable_if_t<std::is_integral<_Ty>::value, int> = 0>
|
2022-07-04 09:13:06 +08:00
|
|
|
void assign(std::initializer_list<_Ty> rhs)
|
|
|
|
{
|
2022-07-07 14:49:26 +08:00
|
|
|
_Assign_range((iterator)rhs.begin(), (iterator)rhs.end());
|
2022-07-04 09:13:06 +08:00
|
|
|
}
|
2022-07-07 14:49:26 +08:00
|
|
|
template <typename _Ty, enable_if_t<std::is_integral<_Ty>::value, int> = 0>
|
2022-07-04 09:13:06 +08:00
|
|
|
void assign(std::initializer_list<_Ty> rhs, std::true_type /*fit*/)
|
2022-07-03 17:24:10 +08:00
|
|
|
{
|
2022-07-07 14:49:26 +08:00
|
|
|
_Assign_range((iterator)rhs.begin(), (iterator)rhs.end(), std::true_type{});
|
2022-07-03 17:24:10 +08:00
|
|
|
}
|
2022-07-04 09:13:06 +08:00
|
|
|
void swap(basic_byte_buffer& rhs) noexcept
|
2021-12-10 19:15:48 +08:00
|
|
|
{
|
2021-12-11 13:02:56 +08:00
|
|
|
char _Tmp[sizeof(rhs)];
|
|
|
|
memcpy(_Tmp, &rhs, sizeof(rhs));
|
|
|
|
memcpy(&rhs, this, sizeof(rhs));
|
|
|
|
memcpy(this, _Tmp, sizeof(_Tmp));
|
2021-12-10 19:15:48 +08:00
|
|
|
}
|
2022-07-04 09:13:06 +08:00
|
|
|
template <typename _Iter>
|
2022-07-07 09:31:34 +08:00
|
|
|
iterator insert(iterator _Where, _Iter first, const _Iter last)
|
2021-12-10 19:15:48 +08:00
|
|
|
{
|
2022-07-07 09:31:34 +08:00
|
|
|
_YASIO_VERIFY_RANGE(_Where >= _Myfirst && _Where <= _Mylast && first <= last,
|
|
|
|
"byte_buffer: out of range!");
|
|
|
|
auto insertion_pos = std::distance(_Myfirst, _Where);
|
|
|
|
if (_Where == _Mylast)
|
2021-12-12 10:20:34 +08:00
|
|
|
append(first, last);
|
|
|
|
else
|
2021-12-10 19:15:48 +08:00
|
|
|
{
|
2022-07-07 09:31:34 +08:00
|
|
|
auto count = std::distance(first, last);
|
2021-12-12 10:20:34 +08:00
|
|
|
if (count > 0 && insertion_pos >= 0)
|
2021-12-10 19:15:48 +08:00
|
|
|
{
|
2021-12-12 10:20:34 +08:00
|
|
|
auto old_size = _Mylast - _Myfirst;
|
2022-07-07 09:31:34 +08:00
|
|
|
_Where = resize(old_size + count) + insertion_pos;
|
|
|
|
auto move_to = _Where + count;
|
|
|
|
std::copy_n(_Where, _Mylast - move_to, move_to);
|
|
|
|
std::copy_n(first, count, _Where);
|
2021-12-10 19:15:48 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-07 09:31:34 +08:00
|
|
|
return _Myfirst + insertion_pos;
|
2021-12-10 19:15:48 +08:00
|
|
|
}
|
2022-07-04 09:13:06 +08:00
|
|
|
template <typename _Iter>
|
2022-07-06 16:56:57 +08:00
|
|
|
basic_byte_buffer& append(_Iter first, const _Iter last)
|
2022-07-04 09:13:06 +08:00
|
|
|
{
|
2022-07-06 16:56:57 +08:00
|
|
|
return append_n(first, std::distance(first, last));
|
2022-07-04 09:13:06 +08:00
|
|
|
}
|
|
|
|
template <typename _Iter>
|
2022-07-06 16:56:57 +08:00
|
|
|
basic_byte_buffer& append_n(_Iter first, ptrdiff_t count)
|
2021-12-12 10:20:34 +08:00
|
|
|
{
|
2022-07-06 16:56:57 +08:00
|
|
|
if (count > 1)
|
2021-12-12 10:20:34 +08:00
|
|
|
{
|
|
|
|
auto old_size = _Mylast - _Myfirst;
|
|
|
|
resize(old_size + count);
|
|
|
|
std::copy_n(first, count, _Myfirst + old_size);
|
|
|
|
}
|
2022-07-06 16:56:57 +08:00
|
|
|
else if (count == 1)
|
2022-07-07 14:49:26 +08:00
|
|
|
push_back(static_cast<value_type>(*first));
|
2022-07-06 16:56:57 +08:00
|
|
|
return *this;
|
2021-12-12 10:20:34 +08:00
|
|
|
}
|
2022-07-07 14:49:26 +08:00
|
|
|
void push_back(value_type v)
|
2021-12-10 19:15:48 +08:00
|
|
|
{
|
2021-12-11 13:02:56 +08:00
|
|
|
resize(this->size() + 1);
|
|
|
|
*(_Mylast - 1) = v;
|
2021-12-10 19:15:48 +08:00
|
|
|
}
|
2022-07-07 09:31:34 +08:00
|
|
|
iterator erase(iterator _Where)
|
|
|
|
{
|
|
|
|
_YASIO_VERIFY_RANGE(_Where >= _Myfirst && _Where < _Mylast, "byte_buffer: out of range!");
|
|
|
|
_Mylast = std::move(_Where + 1, _Mylast, _Where);
|
|
|
|
return _Where;
|
|
|
|
}
|
|
|
|
iterator erase(iterator first, iterator last)
|
2022-07-06 16:56:57 +08:00
|
|
|
{
|
2022-07-07 09:31:34 +08:00
|
|
|
_YASIO_VERIFY_RANGE((first <= last) && first >= _Myfirst && last <= _Mylast,
|
|
|
|
"byte_buffer: out of range!");
|
2022-07-06 16:56:57 +08:00
|
|
|
_Mylast = std::move(last, _Mylast, first);
|
2022-07-07 09:31:34 +08:00
|
|
|
return first;
|
2022-07-06 16:56:57 +08:00
|
|
|
}
|
2022-07-07 14:49:26 +08:00
|
|
|
value_type& front()
|
2021-12-10 19:15:48 +08:00
|
|
|
{
|
2022-07-07 09:31:34 +08:00
|
|
|
_YASIO_VERIFY_RANGE(_Myfirst < _Mylast, "byte_buffer: out of range!");
|
|
|
|
return *_Myfirst;
|
2021-12-10 19:15:48 +08:00
|
|
|
}
|
2022-07-07 14:49:26 +08:00
|
|
|
value_type& back()
|
2021-12-11 13:02:56 +08:00
|
|
|
{
|
2022-07-07 09:31:34 +08:00
|
|
|
_YASIO_VERIFY_RANGE(_Myfirst < _Mylast, "byte_buffer: out of range!");
|
|
|
|
return *(_Mylast - 1);
|
2021-12-11 13:02:56 +08:00
|
|
|
}
|
2021-12-18 15:17:46 +08:00
|
|
|
static constexpr size_t max_size() noexcept { return (std::numeric_limits<ptrdiff_t>::max)(); }
|
2022-07-06 16:56:57 +08:00
|
|
|
iterator begin() noexcept { return _Myfirst; }
|
|
|
|
iterator end() noexcept { return _Mylast; }
|
|
|
|
const_iterator begin() const noexcept { return _Myfirst; }
|
|
|
|
const_iterator end() const noexcept { return _Mylast; }
|
2021-12-10 19:15:48 +08:00
|
|
|
pointer data() noexcept { return _Myfirst; }
|
|
|
|
const_pointer data() const noexcept { return _Myfirst; }
|
2021-12-11 13:02:56 +08:00
|
|
|
size_t capacity() const noexcept { return _Myend - _Myfirst; }
|
|
|
|
size_t size() const noexcept { return _Mylast - _Myfirst; }
|
|
|
|
void clear() noexcept { _Mylast = _Myfirst; }
|
|
|
|
void shrink_to_fit() { shrink_to_fit(this->size()); }
|
|
|
|
bool empty() const noexcept { return _Mylast == _Myfirst; }
|
2022-07-07 14:49:26 +08:00
|
|
|
void resize(size_t new_size, const_reference val)
|
2021-12-10 19:15:48 +08:00
|
|
|
{
|
2021-12-11 13:02:56 +08:00
|
|
|
auto old_size = this->size();
|
|
|
|
resize(new_size);
|
|
|
|
if (old_size < new_size)
|
|
|
|
memset(_Myfirst + old_size, val, new_size - old_size);
|
2021-12-10 19:15:48 +08:00
|
|
|
}
|
2022-07-07 14:49:26 +08:00
|
|
|
pointer resize(size_t new_size)
|
2021-12-10 19:15:48 +08:00
|
|
|
{
|
2021-12-12 10:20:34 +08:00
|
|
|
auto old_cap = this->capacity();
|
|
|
|
if (old_cap < new_size)
|
|
|
|
_Reallocate_exactly((std::max)(old_cap + old_cap / 2, new_size));
|
2021-12-11 13:02:56 +08:00
|
|
|
_Mylast = _Myfirst + new_size;
|
2021-12-12 10:20:34 +08:00
|
|
|
return _Myfirst;
|
2021-12-10 19:15:48 +08:00
|
|
|
}
|
2022-07-07 14:49:26 +08:00
|
|
|
void resize_fit(size_t new_size, const_reference val)
|
2021-12-11 20:15:29 +08:00
|
|
|
{
|
|
|
|
auto old_size = this->size();
|
|
|
|
resize_fit(new_size);
|
|
|
|
if (old_size < new_size)
|
|
|
|
memset(_Myfirst + old_size, val, new_size - old_size);
|
|
|
|
}
|
2022-07-07 14:49:26 +08:00
|
|
|
pointer resize_fit(size_t new_size)
|
2021-12-10 19:15:48 +08:00
|
|
|
{
|
2021-12-11 13:02:56 +08:00
|
|
|
if (this->capacity() < new_size)
|
|
|
|
_Reallocate_exactly(new_size);
|
2021-12-10 19:15:48 +08:00
|
|
|
_Mylast = _Myfirst + new_size;
|
2021-12-12 10:20:34 +08:00
|
|
|
return _Myfirst;
|
2021-12-10 19:15:48 +08:00
|
|
|
}
|
2021-12-11 13:02:56 +08:00
|
|
|
void shrink_to_fit(size_t new_size)
|
2021-12-10 19:15:48 +08:00
|
|
|
{
|
2021-12-11 13:02:56 +08:00
|
|
|
if (this->capacity() != new_size)
|
|
|
|
_Reallocate_exactly(new_size);
|
2021-12-10 19:15:48 +08:00
|
|
|
_Mylast = _Myfirst + new_size;
|
|
|
|
}
|
2021-12-11 13:02:56 +08:00
|
|
|
void reserve(size_t new_cap)
|
|
|
|
{
|
|
|
|
if (this->capacity() < new_cap)
|
|
|
|
{
|
|
|
|
auto cur_size = this->size();
|
|
|
|
_Reallocate_exactly(new_cap);
|
|
|
|
_Mylast = _Myfirst + cur_size;
|
|
|
|
}
|
|
|
|
}
|
2022-07-07 14:49:26 +08:00
|
|
|
const_reference operator[](size_t index) const { return this->at(index); }
|
|
|
|
reference operator[](size_t index) { return this->at(index); }
|
|
|
|
const_reference at(size_t index) const
|
2022-07-06 16:56:57 +08:00
|
|
|
{
|
2022-07-07 09:31:34 +08:00
|
|
|
_YASIO_VERIFY_RANGE(index < this->size(), "byte_buffer: out of range!");
|
|
|
|
return _Myfirst[index];
|
2022-07-06 16:56:57 +08:00
|
|
|
}
|
2022-07-07 14:49:26 +08:00
|
|
|
reference at(size_t index)
|
2022-07-06 16:56:57 +08:00
|
|
|
{
|
2022-07-07 09:31:34 +08:00
|
|
|
_YASIO_VERIFY_RANGE(index < this->size(), "byte_buffer: out of range!");
|
|
|
|
return _Myfirst[index];
|
2022-07-06 16:56:57 +08:00
|
|
|
}
|
2021-12-10 19:15:48 +08:00
|
|
|
void attach(void* ptr, size_t len) noexcept
|
|
|
|
{
|
|
|
|
if (ptr)
|
|
|
|
{
|
2021-12-11 13:02:56 +08:00
|
|
|
shrink_to_fit(0);
|
2022-07-07 14:49:26 +08:00
|
|
|
_Myfirst = (pointer)ptr;
|
2021-12-10 19:15:48 +08:00
|
|
|
_Myend = _Mylast = _Myfirst + len;
|
|
|
|
}
|
|
|
|
}
|
2022-07-04 09:13:06 +08:00
|
|
|
template <typename _TSIZE>
|
2022-07-07 14:49:26 +08:00
|
|
|
pointer detach(_TSIZE& len) noexcept
|
2021-12-10 19:15:48 +08:00
|
|
|
{
|
|
|
|
auto ptr = _Myfirst;
|
|
|
|
len = static_cast<_TSIZE>(this->size());
|
|
|
|
memset(this, 0, sizeof(*this));
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-07-04 09:13:06 +08:00
|
|
|
template <typename _Iter>
|
|
|
|
void _Assign_range(_Iter first, _Iter last)
|
2021-12-12 10:20:34 +08:00
|
|
|
{
|
2022-07-03 17:24:10 +08:00
|
|
|
_Mylast = _Myfirst;
|
2021-12-12 10:20:34 +08:00
|
|
|
if (last > first)
|
|
|
|
std::copy(first, last, resize(std::distance(first, last)));
|
|
|
|
}
|
2022-07-04 09:13:06 +08:00
|
|
|
template <typename _Iter>
|
|
|
|
void _Assign_range(_Iter first, _Iter last, std::true_type)
|
2021-12-12 10:20:34 +08:00
|
|
|
{
|
2022-07-03 17:24:10 +08:00
|
|
|
_Mylast = _Myfirst;
|
2021-12-12 10:20:34 +08:00
|
|
|
if (last > first)
|
|
|
|
std::copy(first, last, resize_fit(std::distance(first, last)));
|
|
|
|
}
|
|
|
|
void _Assign_rv(basic_byte_buffer&& rhs)
|
|
|
|
{
|
|
|
|
memcpy(this, &rhs, sizeof(rhs));
|
|
|
|
memset(&rhs, 0, sizeof(rhs));
|
|
|
|
}
|
2021-12-11 13:02:56 +08:00
|
|
|
void _Reallocate_exactly(size_t new_cap)
|
2021-12-10 19:15:48 +08:00
|
|
|
{
|
2022-07-07 14:49:26 +08:00
|
|
|
auto new_block = (pointer)_Alloc::reallocate(_Myfirst, _Myend - _Myfirst, new_cap);
|
2021-12-11 13:02:56 +08:00
|
|
|
if (new_block || 0 == new_cap)
|
2021-12-12 10:20:34 +08:00
|
|
|
{
|
2021-12-11 13:02:56 +08:00
|
|
|
_Myfirst = new_block;
|
2021-12-12 10:20:34 +08:00
|
|
|
_Myend = _Myfirst + new_cap;
|
|
|
|
}
|
2021-12-10 19:15:48 +08:00
|
|
|
else
|
2021-12-11 13:02:56 +08:00
|
|
|
throw std::bad_alloc{};
|
2021-12-10 19:15:48 +08:00
|
|
|
}
|
2022-07-07 14:49:26 +08:00
|
|
|
pointer _Myfirst = nullptr;
|
|
|
|
pointer _Mylast = nullptr;
|
|
|
|
pointer _Myend = nullptr;
|
2021-12-10 19:15:48 +08:00
|
|
|
};
|
|
|
|
using sbyte_buffer = basic_byte_buffer<char>;
|
2021-12-12 10:20:34 +08:00
|
|
|
using byte_buffer = basic_byte_buffer<unsigned char>;
|
2021-12-10 19:15:48 +08:00
|
|
|
} // namespace yasio
|
2021-12-11 13:02:56 +08:00
|
|
|
#endif
|