Add member typedefs to rexy::binary

This commit is contained in:
rexy712 2020-08-04 15:39:35 -07:00
parent 710ea2a8bf
commit 1648d96232
2 changed files with 89 additions and 63 deletions

View File

@ -22,6 +22,7 @@
#include <cstdlib> //size_t #include <cstdlib> //size_t
#include <utility> //move #include <utility> //move
#include <cstring> //memcpy #include <cstring> //memcpy
#include <cstddef> //ptrdiff_t
#include <type_traits> #include <type_traits>
#include "cx/utility.hpp" //max #include "cx/utility.hpp" //max
#include "steal.hpp" #include "steal.hpp"
@ -34,30 +35,40 @@ namespace rexy{
class binary_base class binary_base
{ {
protected:
char* m_data = nullptr;
size_t m_size = 0;
size_t m_cap = 0;
public: public:
using value_type = char;
using size_type = size_t;
using difference_type = ptrdiff_t;
using pointer = value_type*;
using const_pointer = const value_type*;
using reference = value_type&;
using const_reference = const value_type&;
using iterator = pointer;
using const_iterator = const_pointer;
protected:
pointer m_data = nullptr;
size_type m_size = 0;
size_type m_cap = 0;
protected: protected:
constexpr binary_base(void)noexcept = default; constexpr binary_base(void)noexcept = default;
constexpr binary_base(size_t len)noexcept; constexpr binary_base(size_type len)noexcept;
constexpr binary_base(char* data, size_t size)noexcept; constexpr binary_base(pointer data, size_type size)noexcept;
constexpr binary_base(char* data, size_t size, size_t cap)noexcept; constexpr binary_base(pointer data, size_type size, size_type cap)noexcept;
constexpr binary_base(const binary_base& b)noexcept; constexpr binary_base(const binary_base& b)noexcept;
~binary_base(void)noexcept = default; ~binary_base(void)noexcept = default;
public: public:
constexpr char* release(void)noexcept; constexpr pointer release(void)noexcept;
constexpr size_t size(void)const; constexpr size_type size(void)const;
constexpr size_t capacity(void)const; constexpr size_type capacity(void)const;
constexpr char* get(void); constexpr pointer get(void);
constexpr const char* get(void)const; constexpr const_pointer get(void)const;
constexpr operator bool(void)const; constexpr operator bool(void)const;
constexpr char& operator[](size_t i)noexcept; constexpr reference operator[](size_type i)noexcept;
constexpr const char& operator[](size_t i)const noexcept; constexpr const_reference operator[](size_type i)const noexcept;
}; };
template<class Allocator> template<class Allocator>
@ -68,14 +79,14 @@ namespace rexy{
public: public:
constexpr basic_binary(void)noexcept; constexpr basic_binary(void)noexcept;
constexpr basic_binary(rexy::steal<char*> data, size_t size)noexcept; constexpr basic_binary(rexy::steal<pointer> data, size_type size)noexcept;
constexpr basic_binary(rexy::steal<char*> data, size_t cap, size_t size)noexcept; constexpr basic_binary(rexy::steal<pointer> data, size_type cap, size_type size)noexcept;
constexpr basic_binary(rexy::steal<char*> data)noexcept; constexpr basic_binary(rexy::steal<pointer> data)noexcept;
basic_binary(const char* data, size_t size)noexcept(noexcept(this->allocate(0))); basic_binary(const_pointer data, size_type size)noexcept(noexcept(this->allocate(0)));
basic_binary(const char* data)noexcept(noexcept(this->allocate(0))); basic_binary(const_pointer data)noexcept(noexcept(this->allocate(0)));
basic_binary(const char* data, size_t size, size_t cap)noexcept(noexcept(this->allocate(0))); basic_binary(const_pointer data, size_type size, size_type cap)noexcept(noexcept(this->allocate(0)));
explicit basic_binary(size_t size)noexcept(noexcept(this->allocate(0))); explicit basic_binary(size_type size)noexcept(noexcept(this->allocate(0)));
basic_binary(size_t size, size_t cap)noexcept(noexcept(this->allocate(0))); basic_binary(size_type size, size_type cap)noexcept(noexcept(this->allocate(0)));
basic_binary(const basic_binary& b)noexcept(noexcept(this->allocate(0))); basic_binary(const basic_binary& b)noexcept(noexcept(this->allocate(0)));
constexpr basic_binary(basic_binary&& b)noexcept; constexpr basic_binary(basic_binary&& b)noexcept;
@ -85,24 +96,24 @@ namespace rexy{
basic_binary& operator=(const basic_binary& b)noexcept(noexcept(this->allocate(0))); basic_binary& operator=(const basic_binary& b)noexcept(noexcept(this->allocate(0)));
constexpr basic_binary& operator=(basic_binary&& b)noexcept; constexpr basic_binary& operator=(basic_binary&& b)noexcept;
basic_binary& operator=(const char* c)noexcept(noexcept(this->allocate(0))); basic_binary& operator=(const_pointer c)noexcept(noexcept(this->allocate(0)));
basic_binary& operator=(const binary_base& b)noexcept(noexcept(this->allocate(0))); basic_binary& operator=(const binary_base& b)noexcept(noexcept(this->allocate(0)));
void reset(void) void reset(void)
noexcept(noexcept(this->deallocate(nullptr,0))); noexcept(noexcept(this->deallocate(nullptr,0)));
void reset(char* val, size_t cap, size_t size = 0) void reset(pointer val, size_type cap, size_type size = 0)
noexcept(noexcept(this->deallocate(nullptr,0))); noexcept(noexcept(this->deallocate(nullptr,0)));
bool resize(size_t newsize) bool resize(size_type newsize)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))); noexcept(this->deallocate(nullptr,0)));
void append(const char* data, size_t len) void append(const_pointer data, size_type len)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))); noexcept(this->deallocate(nullptr,0)));
using detail::hasallocator<Allocator>::allocator; using detail::hasallocator<Allocator>::allocator;
private: private:
basic_binary& _copy_data(const char* data, size_t len) basic_binary& _copy_data(const_pointer data, size_type len)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))); noexcept(this->deallocate(nullptr,0)));
}; };
@ -110,17 +121,32 @@ namespace rexy{
template<class Left, class Right> template<class Left, class Right>
class binary_cat_expr : public rexy::binary_expression<Left,Right> class binary_cat_expr : public rexy::binary_expression<Left,Right>
{ {
private:
using left_t = std::decay_t<Left>;
using right_t = std::decay_t<Right>;
static_assert(std::is_same<typename left_t::value_type,typename right_t::value_type>::value);
public:
using value_type = typename left_t::value_type;
using size_type = decltype(typename left_t::size_type{0} + typename right_t::size_type{0});
using difference_type = decltype(typename left_t::difference_type{0} - typename right_t::difference_type{0});
using pointer = value_type*;
using const_pointer = const value_type*;
using reference = value_type&;
using const_reference = const value_type&;
using iterator = pointer;
using const_iterator = const_pointer;
public: public:
using binary_expression<Left,Right>::binary_expression; using binary_expression<Left,Right>::binary_expression;
constexpr binary_cat_expr(const binary_cat_expr&) = default; constexpr binary_cat_expr(const binary_cat_expr&) = default;
constexpr binary_cat_expr(binary_cat_expr&&) = default; constexpr binary_cat_expr(binary_cat_expr&&) = default;
constexpr size_t size(void)const noexcept; constexpr size_type size(void)const noexcept;
template<class Alloc> template<class Alloc>
operator basic_binary<Alloc>(void) operator basic_binary<Alloc>(void)
noexcept(std::is_nothrow_constructible<basic_binary<Alloc>, size_t>::value && noexcept(std::is_nothrow_constructible<basic_binary<Alloc>, typename basic_binary<Alloc>::size_type>::value &&
std::is_nothrow_invocable<detail::string_appender<basic_binary<Alloc>>,decltype(*this)>::value); std::is_nothrow_invocable<detail::string_appender<basic_binary<Alloc>>,decltype(*this)>::value);
}; };
@ -128,13 +154,13 @@ namespace rexy{
{ {
public: public:
constexpr static_binary(void)noexcept = default; constexpr static_binary(void)noexcept = default;
constexpr static_binary(const char* str, size_t len)noexcept; constexpr static_binary(const_pointer str, size_type len)noexcept;
constexpr static_binary(const char* str)noexcept; constexpr static_binary(const_pointer str)noexcept;
constexpr static_binary(const static_binary&)noexcept; constexpr static_binary(const static_binary&)noexcept;
constexpr static_binary(static_binary&&)noexcept; constexpr static_binary(static_binary&&)noexcept;
~static_binary(void)noexcept = default; ~static_binary(void)noexcept = default;
constexpr static_binary& operator=(const char* str)noexcept; constexpr static_binary& operator=(const_pointer str)noexcept;
constexpr static_binary& operator=(const static_binary& str)noexcept; constexpr static_binary& operator=(const static_binary& str)noexcept;
constexpr static_binary& operator=(static_binary&& str)noexcept; constexpr static_binary& operator=(static_binary&& str)noexcept;
}; };

View File

@ -33,59 +33,59 @@
namespace rexy{ namespace rexy{
constexpr binary_base::binary_base(size_t len)noexcept: constexpr binary_base::binary_base(size_type len)noexcept:
m_cap(len){} m_cap(len){}
constexpr binary_base::binary_base(char* data, size_t size)noexcept: constexpr binary_base::binary_base(pointer data, size_type size)noexcept:
m_data(data), m_cap(size){} m_data(data), m_cap(size){}
constexpr binary_base::binary_base(char* data, size_t size, size_t cap)noexcept: constexpr binary_base::binary_base(pointer data, size_type size, size_type cap)noexcept:
m_data(data), m_size(size), m_cap(cap){} m_data(data), m_size(size), m_cap(cap){}
constexpr binary_base::binary_base(const binary_base&)noexcept{} constexpr binary_base::binary_base(const binary_base&)noexcept{}
constexpr char* binary_base::release(void)noexcept{ constexpr auto binary_base::release(void)noexcept -> pointer{
return cx::exchange(m_data, nullptr); return cx::exchange(m_data, nullptr);
} }
constexpr size_t binary_base::size(void)const{ constexpr auto binary_base::size(void)const -> size_type{
return m_size; return m_size;
} }
constexpr size_t binary_base::capacity(void)const{ constexpr auto binary_base::capacity(void)const -> size_type{
return m_cap; return m_cap;
} }
constexpr char* binary_base::get(void){ constexpr auto binary_base::get(void) -> pointer{
return m_data; return m_data;
} }
constexpr const char* binary_base::get(void)const{ constexpr auto binary_base::get(void)const -> const_pointer{
return m_data; return m_data;
} }
constexpr binary_base::operator bool(void)const{ constexpr binary_base::operator bool(void)const{
return m_data; return m_data;
} }
constexpr char& binary_base::operator[](size_t i)noexcept{ constexpr auto binary_base::operator[](size_type i)noexcept -> reference{
return m_data[i]; return m_data[i];
} }
constexpr const char& binary_base::operator[](size_t i)const noexcept{ constexpr auto binary_base::operator[](size_type i)const noexcept -> const_reference{
return m_data[i]; return m_data[i];
} }
template<class Allocator> template<class Allocator>
constexpr basic_binary<Allocator>::basic_binary(void)noexcept{} constexpr basic_binary<Allocator>::basic_binary(void)noexcept{}
template<class Allocator> template<class Allocator>
constexpr basic_binary<Allocator>::basic_binary(rexy::steal<char*> data)noexcept: constexpr basic_binary<Allocator>::basic_binary(rexy::steal<pointer> data)noexcept:
binary_base(data.value() ? cx::strlen(data.value()) : 0) binary_base(data.value() ? cx::strlen(data.value()) : 0)
{ {
m_data = data.value(); m_data = data.value();
m_size = m_cap; m_size = m_cap;
} }
template<class Allocator> template<class Allocator>
constexpr basic_binary<Allocator>::basic_binary(rexy::steal<char*> data, size_t size)noexcept: constexpr basic_binary<Allocator>::basic_binary(rexy::steal<pointer> data, size_type size)noexcept:
binary_base(data.value(), size){} binary_base(data.value(), size){}
template<class Allocator> template<class Allocator>
constexpr basic_binary<Allocator>::basic_binary(rexy::steal<char*> data, size_t cap, size_t size)noexcept: constexpr basic_binary<Allocator>::basic_binary(rexy::steal<pointer> data, size_type cap, size_type size)noexcept:
binary_base(data.value(), cap, size){} binary_base(data.value(), cap, size){}
template<class Allocator> template<class Allocator>
basic_binary<Allocator>::basic_binary(const char* data, size_t size) basic_binary<Allocator>::basic_binary(const_pointer data, size_type size)
noexcept(noexcept(this->allocate(0))): noexcept(noexcept(this->allocate(0))):
binary_base(size ? this->allocate(size) : nullptr, size) binary_base(size ? this->allocate(size) : nullptr, size)
{ {
@ -93,7 +93,7 @@ namespace rexy{
memcpy(m_data, data, size); memcpy(m_data, data, size);
} }
template<class Allocator> template<class Allocator>
basic_binary<Allocator>::basic_binary(const char* data) basic_binary<Allocator>::basic_binary(const_pointer data)
noexcept(noexcept(this->allocate(0))): noexcept(noexcept(this->allocate(0))):
basic_binary(data ? this->allocate(cx::strlen(data)) : nullptr, cx::strlen(data)) basic_binary(data ? this->allocate(cx::strlen(data)) : nullptr, cx::strlen(data))
{ {
@ -101,7 +101,7 @@ namespace rexy{
memcpy(m_data, data, m_cap); memcpy(m_data, data, m_cap);
} }
template<class Allocator> template<class Allocator>
basic_binary<Allocator>::basic_binary(const char* data, size_t size, size_t cap) basic_binary<Allocator>::basic_binary(const_pointer data, size_type size, size_type cap)
noexcept(noexcept(this->allocate(0))): noexcept(noexcept(this->allocate(0))):
binary_base(size ? this->allocate(size) : nullptr, size, cap) binary_base(size ? this->allocate(size) : nullptr, size, cap)
{ {
@ -109,11 +109,11 @@ namespace rexy{
memcpy(m_data, data, size); memcpy(m_data, data, size);
} }
template<class Allocator> template<class Allocator>
basic_binary<Allocator>::basic_binary(size_t size) basic_binary<Allocator>::basic_binary(size_type size)
noexcept(noexcept(this->allocate(0))): noexcept(noexcept(this->allocate(0))):
binary_base(this->allocate(size), size){} binary_base(this->allocate(size), size){}
template<class Allocator> template<class Allocator>
basic_binary<Allocator>::basic_binary(size_t size, size_t cap) basic_binary<Allocator>::basic_binary(size_type size, size_type cap)
noexcept(noexcept(this->allocate(0))): noexcept(noexcept(this->allocate(0))):
binary_base(size ? this->allocate(size) : nullptr, size, cap){} binary_base(size ? this->allocate(size) : nullptr, size, cap){}
template<class Allocator> template<class Allocator>
@ -155,7 +155,7 @@ namespace rexy{
return *this; return *this;
} }
template<class Allocator> template<class Allocator>
basic_binary<Allocator>& basic_binary<Allocator>::operator=(const char* c) basic_binary<Allocator>& basic_binary<Allocator>::operator=(const_pointer c)
noexcept(noexcept(this->allocate(0))) noexcept(noexcept(this->allocate(0)))
{ {
return _copy_data(c, strlen(c)); return _copy_data(c, strlen(c));
@ -175,7 +175,7 @@ namespace rexy{
m_cap = m_size = 0; m_cap = m_size = 0;
} }
template<class Allocator> template<class Allocator>
void basic_binary<Allocator>::reset(char* val, size_t cap, size_t size) void basic_binary<Allocator>::reset(pointer val, size_type cap, size_type size)
noexcept(noexcept(this->deallocate(nullptr,0))) noexcept(noexcept(this->deallocate(nullptr,0)))
{ {
this->deallocate(m_data, m_cap); this->deallocate(m_data, m_cap);
@ -184,7 +184,7 @@ namespace rexy{
m_size = size; m_size = size;
} }
template<class Allocator> template<class Allocator>
bool basic_binary<Allocator>::resize(size_t newsize) bool basic_binary<Allocator>::resize(size_type newsize)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))) noexcept(this->deallocate(nullptr,0)))
{ {
@ -199,7 +199,7 @@ namespace rexy{
return true; return true;
} }
template<class Allocator> template<class Allocator>
void basic_binary<Allocator>::append(const char* data, size_t len) void basic_binary<Allocator>::append(const_pointer data, size_type len)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))) noexcept(this->deallocate(nullptr,0)))
{ {
@ -209,12 +209,12 @@ namespace rexy{
m_size += len; m_size += len;
} }
template<class Allocator> template<class Allocator>
basic_binary<Allocator>& basic_binary<Allocator>::_copy_data(const char* data, size_t len) basic_binary<Allocator>& basic_binary<Allocator>::_copy_data(const_pointer data, size_type len)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))) noexcept(this->deallocate(nullptr,0)))
{ {
if(!len) if(!len)
return (*this = basic_binary(rexy::steal<char*>(nullptr), 0, 0)); return (*this = basic_binary(rexy::steal<pointer>(nullptr), 0, 0));
if(len <= m_size){ if(len <= m_size){
m_size = len; m_size = len;
memcpy(m_data, data, len); memcpy(m_data, data, len);
@ -223,17 +223,17 @@ namespace rexy{
return (*this = basic_binary(data, len)); return (*this = basic_binary(data, len));
} }
constexpr static_binary::static_binary(const char* str, size_t len)noexcept: constexpr static_binary::static_binary(const_pointer str, size_type len)noexcept:
binary_base(const_cast<char*>(str), len, len){} binary_base(const_cast<pointer>(str), len, len){}
constexpr static_binary::static_binary(const char* str)noexcept: constexpr static_binary::static_binary(const_pointer str)noexcept:
static_binary(str, cx::strlen(str)){} static_binary(str, cx::strlen(str)){}
constexpr static_binary::static_binary(const static_binary& s)noexcept: constexpr static_binary::static_binary(const static_binary& s)noexcept:
static_binary(s.get(), s.size()){} static_binary(s.get(), s.size()){}
constexpr static_binary::static_binary(static_binary&& s)noexcept: constexpr static_binary::static_binary(static_binary&& s)noexcept:
static_binary(s.get(), s.size()){} static_binary(s.get(), s.size()){}
constexpr static_binary& static_binary::operator=(const char* str)noexcept{ constexpr static_binary& static_binary::operator=(const_pointer str)noexcept{
m_data = const_cast<char*>(str); m_data = const_cast<pointer>(str);
m_size = cx::strlen(str); m_size = cx::strlen(str);
m_cap = m_size; m_cap = m_size;
return *this; return *this;
@ -252,14 +252,14 @@ namespace rexy{
} }
template<class Left, class Right> template<class Left, class Right>
constexpr size_t binary_cat_expr<Left,Right>::size(void)const noexcept{ constexpr auto binary_cat_expr<Left,Right>::size(void)const noexcept -> size_type{
return this->m_l.size() + this->m_r.size(); return this->m_l.size() + this->m_r.size();
} }
template<class Left, class Right> template<class Left, class Right>
template<class Alloc> template<class Alloc>
binary_cat_expr<Left,Right>::operator basic_binary<Alloc>(void) binary_cat_expr<Left,Right>::operator basic_binary<Alloc>(void)
noexcept(std::is_nothrow_constructible<basic_binary<Alloc>, size_t>::value && noexcept(std::is_nothrow_constructible<basic_binary<Alloc>, typename basic_binary<Alloc>::size_type>::value &&
std::is_nothrow_invocable<detail::string_appender<basic_binary<Alloc>>,decltype(*this)>::value) std::is_nothrow_invocable<detail::string_appender<basic_binary<Alloc>>,decltype(*this)>::value)
{ {
auto sz = size(); auto sz = size();