212 lines
7.8 KiB
C++
212 lines
7.8 KiB
C++
/**
|
|
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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef REXY_BINARY_HPP
|
|
#define REXY_BINARY_HPP
|
|
|
|
#include <cstdlib> //size_t
|
|
#include <utility> //move
|
|
#include <cstring> //memcpy
|
|
#include <type_traits>
|
|
#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(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(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 Allocator = detail::default_allocator<>>
|
|
class binary_data : public binary_base
|
|
{
|
|
public:
|
|
using allocator_type = Allocator;
|
|
public:
|
|
constexpr binary_data(void)noexcept;
|
|
constexpr binary_data(rexy::steal<char*> data, size_t size)noexcept;
|
|
constexpr binary_data(rexy::steal<char*> data, size_t cap, size_t size)noexcept;
|
|
constexpr binary_data(rexy::steal<char*> 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 Left, class Right>
|
|
class binary_cat_expr : public rexy::binary_expression<Left,Right>
|
|
{
|
|
public:
|
|
using binary_expression<Left,Right>::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<class Alloc>
|
|
operator binary_data<Alloc>(void)
|
|
noexcept(std::is_nothrow_constructible<binary_data<Alloc>, size_t>::value &&
|
|
std::is_nothrow_invocable<detail::string_appender<binary_data<Alloc>>,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<class Left, class Right>
|
|
binary_cat_expr(Left&&, Right&&) -> binary_cat_expr<Left&&,Right&&>;
|
|
|
|
template<class T>
|
|
struct is_binary{
|
|
static constexpr bool value = rexy::is_type<T,binary_base>::value || rexy::is_template_type<T,binary_cat_expr>::value;
|
|
};
|
|
template<class T>
|
|
struct is_concrete_binary{
|
|
static constexpr bool value = rexy::is_type<T,binary_base>::value;
|
|
};
|
|
namespace detail{
|
|
template<class... Ts>
|
|
using enable_if_binary = std::enable_if_t<(is_binary<Ts>::value && ...),int>;
|
|
template<class... Ts>
|
|
using enable_if_concrete_binary = std::enable_if_t<(is_concrete_binary<Ts>::value && ...),int>;
|
|
|
|
}
|
|
|
|
template<class Left, class Right, detail::enable_if_concrete_binary<Left,Right> = 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<class Left, class Right, detail::enable_if_concrete_binary<Left,Right> = 0>
|
|
bool operator!=(Left&& l, Right&& r)noexcept{
|
|
return !(std::forward<Left>(l) == std::forward<Right>(r));
|
|
}
|
|
template<class Left, class Right, detail::enable_if_binary<Left,Right> = 0>
|
|
auto operator+(Left&& l, Right&& r)
|
|
noexcept(noexcept(::new (nullptr) binary_cat_expr(std::forward<Left>(l), std::forward<Right>(r))))
|
|
{
|
|
return binary_cat_expr(std::forward<Left>(l), std::forward<Right>(r));
|
|
}
|
|
template<class Left, detail::enable_if_binary<Left> = 0>
|
|
auto operator+(Left&& l, const char* c)
|
|
noexcept(noexcept(::new (nullptr) binary_cat_expr(std::forward<Left>(l), rexy::static_binary(c))))
|
|
{
|
|
return binary_cat_expr(std::forward<Left>(l), rexy::static_binary(c));
|
|
}
|
|
template<class Right, detail::enable_if_binary<Right> = 0>
|
|
auto operator+(const char* c, Right&& r)
|
|
noexcept(noexcept(::new (nullptr) binary_cat_expr(rexy::static_binary(c), std::forward<Right>(r))))
|
|
{
|
|
return binary_cat_expr(rexy::static_binary(c), std::forward<Right>(r));
|
|
}
|
|
template<class Left, class Right, detail::enable_if_concrete_binary<Left> = 0, detail::enable_if_binary<Right> = 0>
|
|
decltype(auto) operator+=(Left& l, Right&& r)
|
|
noexcept(noexcept(l + std::forward<Right>(r)) && std::is_nothrow_assignable<Left,decltype(l + std::forward<Right>(r))>::value)
|
|
{
|
|
return l = (l + std::forward<Right>(r));
|
|
}
|
|
template<class Left, detail::enable_if_concrete_binary<Left> = 0>
|
|
decltype(auto) operator+=(Left& l, const char* c)
|
|
noexcept(noexcept(l + c) && std::is_nothrow_assignable<Left, decltype(l + c)>::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
|