173 lines
5.2 KiB
C++
173 lines
5.2 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 <new>
|
|
#include <rexy/detail/util.hpp> //max
|
|
#include <rexy/detail/default_allocator.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) = default;
|
|
constexpr binary_base(char* data, size_t size):
|
|
m_data(data), m_cap(size){}
|
|
constexpr binary_base(char* data, size_t cap, size_t size):
|
|
m_data(data), m_size(size), m_cap(cap){}
|
|
template<class Allocator>
|
|
binary_base(const binary_base& b):
|
|
m_data(Allocator::copy(b.m_data, b.m_cap)),
|
|
m_size(b.m_size),
|
|
m_cap(b.m_cap){}
|
|
binary_base(binary_base&&);
|
|
~binary_base(void) = default;
|
|
|
|
public:
|
|
char* release(void);
|
|
|
|
constexpr size_t size(void)const{return m_size;}
|
|
constexpr size_t capacity(void)const{return m_cap;}
|
|
constexpr char* get(void){return m_data;}
|
|
constexpr const char* get(void)const{return m_data;}
|
|
constexpr operator bool(void)const{return m_data;}
|
|
|
|
char& operator[](size_t i);
|
|
const char& operator[](size_t i)const;
|
|
|
|
};
|
|
template<class Allocator = detail::default_allocator<>>
|
|
class binary_data : public binary_base
|
|
{
|
|
public:
|
|
using allocator_type = Allocator;
|
|
public:
|
|
constexpr binary_data(void) = default;
|
|
binary_data(char* data, size_t size):
|
|
binary_base(data, size){}
|
|
binary_data(const char* data, size_t size):
|
|
binary_base(reinterpret_cast<char*>(Allocator::copy(data, size)), size){}
|
|
binary_data(char* data, size_t cap, size_t size):
|
|
binary_base(data, cap, size){}
|
|
binary_data(const char* data, size_t cap, size_t size):
|
|
binary_base(reinterpret_cast<char*>(Allocator::copy(data, size)), cap, size){}
|
|
binary_data(size_t size):
|
|
binary_base(reinterpret_cast<char*>(Allocator::allocate(size)), size){}
|
|
binary_data(const binary_data& b):
|
|
binary_base(b)
|
|
{
|
|
m_data = Allocator::copy(b.m_data, b.m_cap);
|
|
}
|
|
binary_data(binary_data&& b):
|
|
binary_base(std::move(b)){}
|
|
~binary_data(void){
|
|
Allocator::free(m_data);
|
|
}
|
|
binary_data& operator=(const binary_data& b){
|
|
binary_data tmp(b);
|
|
return (*this = std::move(tmp));
|
|
}
|
|
binary_data& operator=(binary_data&& b){
|
|
m_size = b.m_size;
|
|
m_cap = b.m_cap;
|
|
std::swap(m_data, b.m_data);
|
|
return *this;
|
|
}
|
|
void reset(void){
|
|
Allocator::free(m_data);
|
|
m_data = nullptr;
|
|
m_cap = m_size = 0;
|
|
}
|
|
void reset(char* val, size_t cap, size_t size = 0){
|
|
Allocator::free(m_data);
|
|
m_data = val;
|
|
m_cap = cap;
|
|
m_size = size;
|
|
}
|
|
bool resize(size_t newsize){
|
|
if(newsize < m_cap)
|
|
return false;
|
|
binary_data tmp(newsize);
|
|
if(!tmp)
|
|
return false;
|
|
memcpy(tmp.m_data, m_data, m_size);
|
|
std::swap(m_data, tmp.m_data);
|
|
m_cap = tmp.m_cap;
|
|
return true;
|
|
}
|
|
void append(const char* data, size_t len){
|
|
if(m_size + len > m_cap)
|
|
resize(detail::max(m_cap*2, m_size+len));
|
|
memcpy(m_data+m_size, data, len);
|
|
m_size += len;
|
|
}
|
|
};
|
|
using binary = binary_data<>;
|
|
|
|
namespace detail{
|
|
std::true_type is_binary_type_helper(binary_base);
|
|
std::false_type is_binary_type_helper(...);
|
|
|
|
template<class T>
|
|
struct is_binary_type{
|
|
constexpr static bool value = std::is_same<decltype(is_binary_type_helper(std::declval<typename std::decay<T>::type>())),std::true_type>::value;
|
|
};
|
|
}
|
|
|
|
}
|
|
|
|
template<class Left, class Right, typename std::enable_if<rexy::detail::is_binary_type<Left>::value && rexy::detail::is_binary_type<Right>::value,void>::type* = nullptr>
|
|
bool operator==(Left&& l, Right&& r){
|
|
return l && r && l.size() == r.size() && l.capacity() == r.capacity() && !memcmp(l.get(), r.get(), l.size());
|
|
}
|
|
template<class Left, class Right, typename std::enable_if<rexy::detail::is_binary_type<Left>::value && rexy::detail::is_binary_type<Right>::value,void>::type* = nullptr>
|
|
bool operator!=(Left&& l, Right&& r){
|
|
return !(std::forward<Left>(l) == std::forward<Right>(r));
|
|
}
|
|
template<class All, class Alr>
|
|
auto operator+(const rexy::binary_data<All>& l, const rexy::binary_data<Alr>& r){
|
|
rexy::binary_data<All> retval(l.size() + r.size());
|
|
memcpy(retval.get(), l.get(), l.size());
|
|
memcpy(retval.get()+l.size(), r.get(), r.size());
|
|
return retval;
|
|
}
|
|
template<class All, class Alr>
|
|
decltype(auto) operator+=(rexy::binary_data<All>& l, const rexy::binary_data<Alr>& r){
|
|
l.resize(l.size() + r.size());
|
|
memcpy(l.get()+l.size(), r.get(), r.size());
|
|
return l;
|
|
}
|
|
|
|
#ifdef REXY_STRING_BASE_HPP
|
|
#include <rexy/detail/binary_string_conv.hpp>
|
|
#endif
|
|
|
|
#endif
|