Make buffer actually constexpr-able

This commit is contained in:
rexy712 2022-07-15 16:36:54 -07:00
parent c054600b05
commit 26b7723f19
2 changed files with 19 additions and 10 deletions

View File

@ -24,6 +24,8 @@
#include <cstddef> //size_t, ptrdiff_t #include <cstddef> //size_t, ptrdiff_t
#include <iterator> //reverse_iterator #include <iterator> //reverse_iterator
#include "storage_for.hpp"
#include "compat/standard.hpp" #include "compat/standard.hpp"
namespace rexy{ namespace rexy{
@ -92,6 +94,8 @@ namespace rexy{
constexpr const_reverse_iterator crbegin(void)const; constexpr const_reverse_iterator crbegin(void)const;
constexpr const_reverse_iterator crend(void)const; constexpr const_reverse_iterator crend(void)const;
REXY_CPP20_CONSTEXPR void clear(void);
REXY_CPP20_CONSTEXPR void append(const_pointer p, size_type len); REXY_CPP20_CONSTEXPR void append(const_pointer p, size_type len);
}; };

View File

@ -21,6 +21,7 @@
#include <utility> //exchange, swap #include <utility> //exchange, swap
#include <algorithm> //max #include <algorithm> //max
#include <memory> //construct_at
namespace rexy{ namespace rexy{
@ -33,7 +34,7 @@ namespace rexy{
m_size(length) m_size(length)
{ {
for(size_type i = 0;i < length;++i){ for(size_type i = 0;i < length;++i){
new (m_data + i) T(data[i]); std::construct_at(m_data + i, data[i]);
} }
} }
template<class T, class Allocator> template<class T, class Allocator>
@ -48,7 +49,7 @@ namespace rexy{
count = 0; count = 0;
for(auto it = start;it != last;++it){ for(auto it = start;it != last;++it){
new (m_data + count) T(*it); std::construct_at(m_data + count, *it);
++count; ++count;
} }
m_size = count; m_size = count;
@ -65,7 +66,7 @@ namespace rexy{
m_size(b.m_size) m_size(b.m_size)
{ {
for(size_type i = 0;i < b.m_size;++i){ for(size_type i = 0;i < b.m_size;++i){
new (m_data + i) T(b.m_data[i]); std::construct_at(m_data + i, b.m_data[i]);
} }
} }
template<class T, class Allocator> template<class T, class Allocator>
@ -76,7 +77,7 @@ namespace rexy{
template<class T, class Allocator> template<class T, class Allocator>
REXY_CPP20_CONSTEXPR buffer<T,Allocator>::~buffer(void)noexcept(is_nothrow_allocator_v<Allocator>){ REXY_CPP20_CONSTEXPR buffer<T,Allocator>::~buffer(void)noexcept(is_nothrow_allocator_v<Allocator>){
for(size_type i = 0;i < m_size;++i){ for(size_type i = 0;i < m_size;++i){
m_data[i].~T(); std::destroy_at(m_data + i);
} }
this->deallocate(m_data, m_cap * sizeof(value_type)); this->deallocate(m_data, m_cap * sizeof(value_type));
} }
@ -106,7 +107,7 @@ namespace rexy{
if(new_cap > m_cap){ if(new_cap > m_cap){
buffer tmp(new_cap); buffer tmp(new_cap);
for(size_type i = 0;i < m_size;++i){ for(size_type i = 0;i < m_size;++i){
new (tmp.m_data + i) T(std::move(m_data[i])); std::construct_at(tmp.m_data + i, std::move(m_data[i]));
} }
std::swap(tmp.m_data, m_data); std::swap(tmp.m_data, m_data);
} }
@ -202,13 +203,17 @@ namespace rexy{
template<class T, class Allocator> template<class T, class Allocator>
REXY_CPP20_CONSTEXPR void buffer<T,Allocator>::append(const_pointer p, size_type len){ REXY_CPP20_CONSTEXPR void buffer<T,Allocator>::append(const_pointer p, size_type len){
if(len + m_size > m_cap){ if(len + m_size > m_cap){
resize(std::max(m_cap * 2, len + m_size)); buffer b(std::max(m_cap * 2, len + m_size));
} b.append(m_data, m_size);
b.append(p, len);
*this = std::move(b);
}else{
for(size_type i = 0;i < len;++i){ for(size_type i = 0;i < len;++i){
new (m_data + (m_size + i)) T(p[i]); std::construct_at(m_data + (m_size + i), p[i]);
} }
m_size += len; m_size += len;
} }
}
} }