/** This file is a part of rexy's general purpose library Copyright (C) 2020 rexy712 This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ #ifndef REXY_BINARY_HPP #define REXY_BINARY_HPP #include //size_t #include //move #include //memcpy #include #include "cx/utility.hpp" //max #include "detail/default_allocator.hpp" #include "steal.hpp" #include "expression.hpp" #include "traits.hpp" #include "detail/string_appender.hpp" namespace rexy{ class binary_base { protected: char* m_data = nullptr; size_t m_size = 0; size_t m_cap = 0; public: protected: constexpr binary_base(void)noexcept = default; constexpr binary_base(char* data, size_t size)noexcept; constexpr binary_base(char* data, size_t size, size_t cap)noexcept; constexpr binary_base(const binary_base& b)noexcept; ~binary_base(void)noexcept = default; public: constexpr char* 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 operator bool(void)const; constexpr char& operator[](size_t i)noexcept; constexpr const char& operator[](size_t i)const noexcept; }; template> class binary_data : public binary_base { public: using allocator_type = Allocator; public: constexpr binary_data(void)noexcept; constexpr binary_data(rexy::steal data, size_t size)noexcept; constexpr binary_data(rexy::steal data, size_t cap, size_t size)noexcept; constexpr binary_data(rexy::steal data)noexcept; binary_data(const char* data, size_t size)noexcept(noexcept(Allocator::copy(data, size))); binary_data(const char* data)noexcept(noexcept(Allocator::copy(data, 0))); binary_data(const char* data, size_t size, size_t cap)noexcept(noexcept(Allocator::copy(data, size))); explicit binary_data(size_t size)noexcept(noexcept(Allocator::allocate(size))); binary_data(size_t size, size_t cap)noexcept(noexcept(Allocator::allocate(size))); binary_data(const binary_data& b)noexcept(noexcept(Allocator::copy(b.m_data, b.m_cap))); constexpr binary_data(binary_data&& b)noexcept; binary_data(const binary_base& b)noexcept(noexcept(Allocator::copy(b.get(),b.size()))); ~binary_data(void)noexcept(noexcept(Allocator::free(m_data))); binary_data& operator=(const binary_data& b)noexcept(noexcept(Allocator::copy(b.m_data, b.m_size))); constexpr binary_data& operator=(binary_data&& b)noexcept; binary_data& operator=(const char* c)noexcept(noexcept(Allocator::copy(c, 0))); binary_data& operator=(const binary_base& b)noexcept(noexcept(Allocator::copy(b.get(), b.size()))); void reset(void) noexcept(noexcept(Allocator::free(m_data))); void reset(char* val, size_t cap, size_t size = 0) noexcept(noexcept(Allocator::free(m_data))); bool resize(size_t newsize) noexcept(noexcept(Allocator::allocate(0)) && noexcept(Allocator::free(nullptr))); void append(const char* data, size_t len) noexcept(noexcept(Allocator::allocate(0)) && noexcept(Allocator::free(nullptr))); private: binary_data& _copy_data(const char* data, size_t len) noexcept(noexcept(Allocator::copy(nullptr,0)) && noexcept(Allocator::free(nullptr))); }; using binary = binary_data<>; template class binary_cat_expr : public rexy::binary_expression { 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; template operator binary_data(void) noexcept(std::is_nothrow_constructible, size_t>::value && std::is_nothrow_invocable>,decltype(*this)>::value); }; class static_binary : public binary_base { 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 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 static_binary& str)noexcept; constexpr static_binary& operator=(static_binary&& str)noexcept; }; template binary_cat_expr(Left&&, Right&&) -> binary_cat_expr; template struct is_binary{ static constexpr bool value = rexy::is_type::value || rexy::is_template_type::value; }; template struct is_concrete_binary{ static constexpr bool value = rexy::is_type::value; }; namespace detail{ template using enable_if_binary = std::enable_if_t<(is_binary::value && ...),int>; template using enable_if_concrete_binary = std::enable_if_t<(is_concrete_binary::value && ...),int>; } template = 0> bool operator==(Left&& l, Right&& r)noexcept{ return l && r && l.size() == r.size() && l.capacity() == r.capacity() && !memcmp(l.get(), r.get(), l.size()); } template = 0> bool operator!=(Left&& l, Right&& r)noexcept{ return !(std::forward(l) == std::forward(r)); } template = 0> auto operator+(Left&& l, Right&& r) noexcept(noexcept(::new (nullptr) binary_cat_expr(std::forward(l), std::forward(r)))) { return binary_cat_expr(std::forward(l), std::forward(r)); } template = 0> auto operator+(Left&& l, const char* c) noexcept(noexcept(::new (nullptr) binary_cat_expr(std::forward(l), rexy::static_binary(c)))) { return binary_cat_expr(std::forward(l), rexy::static_binary(c)); } template = 0> auto operator+(const char* c, Right&& r) noexcept(noexcept(::new (nullptr) binary_cat_expr(rexy::static_binary(c), std::forward(r)))) { return binary_cat_expr(rexy::static_binary(c), std::forward(r)); } template = 0, detail::enable_if_binary = 0> decltype(auto) operator+=(Left& l, Right&& r) noexcept(noexcept(l + std::forward(r)) && std::is_nothrow_assignable(r))>::value) { return l = (l + std::forward(r)); } template = 0> decltype(auto) operator+=(Left& l, const char* c) noexcept(noexcept(l + c) && std::is_nothrow_assignable::value) { return l = (l + c); } } //namespace rexy #include "binary.tpp" namespace{ constexpr inline rexy::static_binary operator"" _sb(const char* str, size_t len)noexcept{ return rexy::static_binary(str, len); } } #ifdef REXY_STRING_BASE_HPP #include "detail/binary_string_conv.hpp" #endif #endif