From 1648d96232851d582fbd7ba0aec1092d33a0b05c Mon Sep 17 00:00:00 2001 From: rexy712 Date: Tue, 4 Aug 2020 15:39:35 -0700 Subject: [PATCH] Add member typedefs to rexy::binary --- include/rexy/binary_base.hpp | 90 +++++++++++++++++++++++------------- include/rexy/binary_base.tpp | 62 ++++++++++++------------- 2 files changed, 89 insertions(+), 63 deletions(-) diff --git a/include/rexy/binary_base.hpp b/include/rexy/binary_base.hpp index e580f64..3866841 100644 --- a/include/rexy/binary_base.hpp +++ b/include/rexy/binary_base.hpp @@ -22,6 +22,7 @@ #include //size_t #include //move #include //memcpy +#include //ptrdiff_t #include #include "cx/utility.hpp" //max #include "steal.hpp" @@ -34,30 +35,40 @@ namespace rexy{ class binary_base { - protected: - char* m_data = nullptr; - size_t m_size = 0; - size_t m_cap = 0; 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: constexpr binary_base(void)noexcept = default; - constexpr binary_base(size_t len)noexcept; - constexpr binary_base(char* data, size_t size)noexcept; - constexpr binary_base(char* data, size_t size, size_t cap)noexcept; + constexpr binary_base(size_type len)noexcept; + constexpr binary_base(pointer data, size_type size)noexcept; + constexpr binary_base(pointer data, size_type size, size_type cap)noexcept; constexpr binary_base(const binary_base& b)noexcept; ~binary_base(void)noexcept = default; public: - constexpr char* release(void)noexcept; + constexpr pointer release(void)noexcept; - constexpr size_t size(void)const; - constexpr size_t capacity(void)const; - constexpr char* get(void); - constexpr const char* get(void)const; + constexpr size_type size(void)const; + constexpr size_type capacity(void)const; + constexpr pointer get(void); + constexpr const_pointer get(void)const; constexpr operator bool(void)const; - constexpr char& operator[](size_t i)noexcept; - constexpr const char& operator[](size_t i)const noexcept; + constexpr reference operator[](size_type i)noexcept; + constexpr const_reference operator[](size_type i)const noexcept; }; template @@ -68,14 +79,14 @@ namespace rexy{ public: constexpr basic_binary(void)noexcept; - constexpr basic_binary(rexy::steal data, size_t size)noexcept; - constexpr basic_binary(rexy::steal data, size_t cap, size_t size)noexcept; - constexpr basic_binary(rexy::steal data)noexcept; - basic_binary(const char* data, size_t size)noexcept(noexcept(this->allocate(0))); - basic_binary(const char* data)noexcept(noexcept(this->allocate(0))); - basic_binary(const char* data, size_t size, size_t cap)noexcept(noexcept(this->allocate(0))); - explicit basic_binary(size_t size)noexcept(noexcept(this->allocate(0))); - basic_binary(size_t size, size_t cap)noexcept(noexcept(this->allocate(0))); + constexpr basic_binary(rexy::steal data, size_type size)noexcept; + constexpr basic_binary(rexy::steal data, size_type cap, size_type size)noexcept; + constexpr basic_binary(rexy::steal data)noexcept; + basic_binary(const_pointer data, size_type size)noexcept(noexcept(this->allocate(0))); + basic_binary(const_pointer data)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_type size)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))); 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))); 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))); void reset(void) 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))); - bool resize(size_t newsize) + bool resize(size_type newsize) noexcept(noexcept(this->allocate(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(this->deallocate(nullptr,0))); using detail::hasallocator::allocator; 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(this->deallocate(nullptr,0))); }; @@ -110,17 +121,32 @@ namespace rexy{ template class binary_cat_expr : public rexy::binary_expression { + private: + using left_t = std::decay_t; + using right_t = std::decay_t; + static_assert(std::is_same::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: using binary_expression::binary_expression; constexpr binary_cat_expr(const 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 operator basic_binary(void) - noexcept(std::is_nothrow_constructible, size_t>::value && + noexcept(std::is_nothrow_constructible, typename basic_binary::size_type>::value && std::is_nothrow_invocable>,decltype(*this)>::value); }; @@ -128,13 +154,13 @@ namespace rexy{ { public: constexpr static_binary(void)noexcept = default; - constexpr static_binary(const char* str, size_t len)noexcept; - constexpr static_binary(const char* str)noexcept; + constexpr static_binary(const_pointer str, size_type len)noexcept; + constexpr static_binary(const_pointer str)noexcept; constexpr static_binary(const static_binary&)noexcept; constexpr static_binary(static_binary&&)noexcept; ~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=(static_binary&& str)noexcept; }; diff --git a/include/rexy/binary_base.tpp b/include/rexy/binary_base.tpp index d36e860..a89584d 100644 --- a/include/rexy/binary_base.tpp +++ b/include/rexy/binary_base.tpp @@ -33,59 +33,59 @@ namespace rexy{ - constexpr binary_base::binary_base(size_t len)noexcept: + constexpr binary_base::binary_base(size_type len)noexcept: 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){} - 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){} 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); } - constexpr size_t binary_base::size(void)const{ + constexpr auto binary_base::size(void)const -> size_type{ return m_size; } - constexpr size_t binary_base::capacity(void)const{ + constexpr auto binary_base::capacity(void)const -> size_type{ return m_cap; } - constexpr char* binary_base::get(void){ + constexpr auto binary_base::get(void) -> pointer{ return m_data; } - constexpr const char* binary_base::get(void)const{ + constexpr auto binary_base::get(void)const -> const_pointer{ return m_data; } constexpr binary_base::operator bool(void)const{ 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]; } - 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]; } template constexpr basic_binary::basic_binary(void)noexcept{} template - constexpr basic_binary::basic_binary(rexy::steal data)noexcept: + constexpr basic_binary::basic_binary(rexy::steal data)noexcept: binary_base(data.value() ? cx::strlen(data.value()) : 0) { m_data = data.value(); m_size = m_cap; } template - constexpr basic_binary::basic_binary(rexy::steal data, size_t size)noexcept: + constexpr basic_binary::basic_binary(rexy::steal data, size_type size)noexcept: binary_base(data.value(), size){} template - constexpr basic_binary::basic_binary(rexy::steal data, size_t cap, size_t size)noexcept: + constexpr basic_binary::basic_binary(rexy::steal data, size_type cap, size_type size)noexcept: binary_base(data.value(), cap, size){} template - basic_binary::basic_binary(const char* data, size_t size) + basic_binary::basic_binary(const_pointer data, size_type size) noexcept(noexcept(this->allocate(0))): binary_base(size ? this->allocate(size) : nullptr, size) { @@ -93,7 +93,7 @@ namespace rexy{ memcpy(m_data, data, size); } template - basic_binary::basic_binary(const char* data) + basic_binary::basic_binary(const_pointer data) noexcept(noexcept(this->allocate(0))): basic_binary(data ? this->allocate(cx::strlen(data)) : nullptr, cx::strlen(data)) { @@ -101,7 +101,7 @@ namespace rexy{ memcpy(m_data, data, m_cap); } template - basic_binary::basic_binary(const char* data, size_t size, size_t cap) + basic_binary::basic_binary(const_pointer data, size_type size, size_type cap) noexcept(noexcept(this->allocate(0))): binary_base(size ? this->allocate(size) : nullptr, size, cap) { @@ -109,11 +109,11 @@ namespace rexy{ memcpy(m_data, data, size); } template - basic_binary::basic_binary(size_t size) + basic_binary::basic_binary(size_type size) noexcept(noexcept(this->allocate(0))): binary_base(this->allocate(size), size){} template - basic_binary::basic_binary(size_t size, size_t cap) + basic_binary::basic_binary(size_type size, size_type cap) noexcept(noexcept(this->allocate(0))): binary_base(size ? this->allocate(size) : nullptr, size, cap){} template @@ -155,7 +155,7 @@ namespace rexy{ return *this; } template - basic_binary& basic_binary::operator=(const char* c) + basic_binary& basic_binary::operator=(const_pointer c) noexcept(noexcept(this->allocate(0))) { return _copy_data(c, strlen(c)); @@ -175,7 +175,7 @@ namespace rexy{ m_cap = m_size = 0; } template - void basic_binary::reset(char* val, size_t cap, size_t size) + void basic_binary::reset(pointer val, size_type cap, size_type size) noexcept(noexcept(this->deallocate(nullptr,0))) { this->deallocate(m_data, m_cap); @@ -184,7 +184,7 @@ namespace rexy{ m_size = size; } template - bool basic_binary::resize(size_t newsize) + bool basic_binary::resize(size_type newsize) noexcept(noexcept(this->allocate(0)) && noexcept(this->deallocate(nullptr,0))) { @@ -199,7 +199,7 @@ namespace rexy{ return true; } template - void basic_binary::append(const char* data, size_t len) + void basic_binary::append(const_pointer data, size_type len) noexcept(noexcept(this->allocate(0)) && noexcept(this->deallocate(nullptr,0))) { @@ -209,12 +209,12 @@ namespace rexy{ m_size += len; } template - basic_binary& basic_binary::_copy_data(const char* data, size_t len) + basic_binary& basic_binary::_copy_data(const_pointer data, size_type len) noexcept(noexcept(this->allocate(0)) && noexcept(this->deallocate(nullptr,0))) { if(!len) - return (*this = basic_binary(rexy::steal(nullptr), 0, 0)); + return (*this = basic_binary(rexy::steal(nullptr), 0, 0)); if(len <= m_size){ m_size = len; memcpy(m_data, data, len); @@ -223,17 +223,17 @@ namespace rexy{ return (*this = basic_binary(data, len)); } - constexpr static_binary::static_binary(const char* str, size_t len)noexcept: - binary_base(const_cast(str), len, len){} - constexpr static_binary::static_binary(const char* str)noexcept: + constexpr static_binary::static_binary(const_pointer str, size_type len)noexcept: + binary_base(const_cast(str), len, len){} + constexpr static_binary::static_binary(const_pointer str)noexcept: static_binary(str, cx::strlen(str)){} constexpr static_binary::static_binary(const static_binary& s)noexcept: static_binary(s.get(), s.size()){} constexpr static_binary::static_binary(static_binary&& s)noexcept: static_binary(s.get(), s.size()){} - constexpr static_binary& static_binary::operator=(const char* str)noexcept{ - m_data = const_cast(str); + constexpr static_binary& static_binary::operator=(const_pointer str)noexcept{ + m_data = const_cast(str); m_size = cx::strlen(str); m_cap = m_size; return *this; @@ -252,14 +252,14 @@ namespace rexy{ } template - constexpr size_t binary_cat_expr::size(void)const noexcept{ + constexpr auto binary_cat_expr::size(void)const noexcept -> size_type{ return this->m_l.size() + this->m_r.size(); } template template binary_cat_expr::operator basic_binary(void) - noexcept(std::is_nothrow_constructible, size_t>::value && + noexcept(std::is_nothrow_constructible, typename basic_binary::size_type>::value && std::is_nothrow_invocable>,decltype(*this)>::value) { auto sz = size();