Improve binary constexpr-ness and exception specs

This commit is contained in:
rexy712 2020-05-07 11:54:28 -07:00
parent daac14dddf
commit 981e57b34c
4 changed files with 24 additions and 75 deletions

View File

@ -14,7 +14,7 @@ option(ENABLE_SHARED "Build shared library" ON)
option(ENABLE_PROFILING "Enable asan" OFF)
mark_as_advanced(ENABLE_PROFILING)
set(SOURCE_LIST "src/binary.cpp" "src/filerd.cpp")
set(SOURCE_LIST "src/filerd.cpp")
add_library(ensure OBJECT "src/ensure.cpp")
target_compile_options(ensure PRIVATE -Wall -Wextra -pedantic -std=c++17)
if(ENABLE_SHARED)

View File

@ -49,7 +49,7 @@ namespace rexy{
~binary_base(void)noexcept = default;
public:
char* release(void);
constexpr char* release(void)noexcept{return cx::exchange(m_data, nullptr);}
constexpr size_t size(void)const{return m_size;}
constexpr size_t capacity(void)const{return m_cap;}
@ -57,8 +57,8 @@ namespace rexy{
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;
constexpr char& operator[](size_t i)noexcept{return m_data[i];}
constexpr const char& operator[](size_t i)const noexcept{return m_data[i];}
};
template<class Allocator = detail::default_allocator<>>
@ -68,10 +68,6 @@ namespace rexy{
using allocator_type = Allocator;
public:
constexpr binary_data(void)noexcept = default;
[[deprecated]] binary_data(char* data, size_t size)noexcept:
binary_base(data, size){}
[[deprecated]] binary_data(char* data, size_t cap, size_t size)noexcept:
binary_base(data, cap, size){}
binary_data(const char* data, size_t size)
noexcept(noexcept(Allocator::copy(data, size))):
binary_base(reinterpret_cast<char*>(Allocator::copy(data, size)), size){}
@ -99,15 +95,12 @@ namespace rexy{
Allocator::free(m_data);
}
binary_data& operator=(const binary_data& b)
noexcept(std::is_nothrow_copy_constructible<binary_data<Allocator>>::value &&
std::is_nothrow_move_assignable<binary_data<Allocator>>::value)
noexcept(noexcept(Allocator::copy(b.m_data, b.m_size)))
{
binary_data<allocator_type> tmp(b);
return (*this = std::move(tmp));
}
constexpr binary_data& operator=(binary_data&& b)
noexcept(noexcept(cx::swap(m_data, b.m_data)))
{
constexpr binary_data& operator=(binary_data&& b)noexcept{
m_size = b.m_size;
m_cap = b.m_cap;
cx::swap(m_data, b.m_data);
@ -129,7 +122,8 @@ namespace rexy{
m_size = size;
}
bool resize(size_t newsize)
noexcept(std::is_nothrow_constructible<binary_data<Allocator>,size_t>::value)
noexcept(noexcept(Allocator::allocate(0)) &&
noexcept(Allocator::free(nullptr)))
{
if(newsize < m_cap)
return false;
@ -142,7 +136,8 @@ namespace rexy{
return true;
}
void append(const char* data, size_t len)
noexcept(noexcept(resize(len)))
noexcept(noexcept(Allocator::allocate(0)) &&
noexcept(Allocator::free(nullptr)))
{
if(m_size + len > m_cap)
resize(cx::max(m_cap*2, m_size+len));
@ -184,7 +179,7 @@ namespace rexy{
}
template<class All, class Alr>
decltype(auto) operator+=(rexy::binary_data<All>& l, const rexy::binary_data<Alr>& r)
noexcept(noexcept(l.resize(0)))
noexcept(noexcept(All::allocate(0)))
{
l.resize(l.size() + r.size());
memcpy(l.get()+l.size(), r.get(), r.size());

View File

@ -29,44 +29,30 @@ namespace rexy{
noexcept(std::is_nothrow_constructible<Str, decltype(b.size())>::value)
{
Str s(b.size()+1);
memcpy(s.get(), b.get(), b.size());
s[b.size()] = 0;
s.append(b.get(), b.size());
return s;
}
template<class Al, class Str = rexy::string, detail::enable_if_concrete_string<Str> = 0, std::enable_if_t<std::is_same<std::decay_t<Al>,typename Str::allocator_type>::value,int> = 0>
auto binary_to_string(binary_data<Al>&& b)
noexcept(std::is_nothrow_default_constructible<Str>::value &&
noexcept(std::declval<Str>().reset(nullptr, 0)) &&
noexcept(b.release()))
{
Str s;
s.reset(b.get(), b.size());
b.release();
return s;
auto binary_to_string(binary_data<Al>&& b)noexcept{
return Str(rexy::steal(b.release()), b.size());
}
template<class Bin = rexy::binary, detail::enable_if_binary<Bin> = 0>
auto string_to_binary(const string_base& s)
noexcept(std::is_nothrow_constructible<Bin, decltype(s.length())>::value &&
noexcept(std::declval<Bin>().append(nullptr, 0)))
noexcept(std::decay_t<Bin>::allocator_type::allocate(0)))
{
Bin b(s.length()+1);
b.append(s.get(), s.length()+1);
return b;
}
template<class Al, class Bin = rexy::binary, detail::enable_if_binary<Bin> = 0, std::enable_if_t<std::is_same<std::decay_t<Al>,typename Bin::allocator_type>::value,int> = 0>
auto string_to_binary(string_intermediary<Al>&& s)
noexcept(std::is_nothrow_default_constructible<Bin>::value &&
noexcept(std::declval<Bin>().reset(nullptr, 0)) &&
noexcept(s.release()))
{
Bin b;
b.reset(s.get(), s.length()+1);
s.release();
return b;
auto string_to_binary(string_intermediary<Al>&& s)noexcept{
return Bin(rexy::steal(s.release()), s.length());
}
template<class L, class R, detail::enable_if_binary<L> = 0, detail::enable_if_concrete_string<R> = 0>
decltype(auto) operator+=(L& l, R&& r)
noexcept(noexcept(l.append(nullptr, 0)))
noexcept(noexcept(std::decay_t<L>::allocator_type::allocate(0)) &&
noexcept(std::decay_t<L>::allocator_type::free(nullptr)))
{
l.append(r.get(), r.length()+1);
return l;
@ -74,17 +60,17 @@ namespace rexy{
template<class L, class R, detail::enable_if_binary<L> = 0, detail::enable_if_string<R> = 0, std::enable_if_t<!detail::is_concrete_string<R>::value,int> = 0>
decltype(auto) operator+=(L& l, R&& r)
noexcept(std::is_nothrow_constructible<rexy::string,R&&>::value &&
std::is_nothrow_assignable<L,rexy::string>::value)
(noexcept(std::decay_t<L>::allocator_type::allocate(0))))
{
rexy::string concrete = r;
return (l = concrete);
return (l += concrete);
}
template<class L, class R, detail::enable_if_concrete_string<L> = 0, detail::enable_if_binary<R> = 0>
decltype(auto) operator+=(L& l, R&& r)
noexcept(noexcept(l.resize(0, 0)))
noexcept(noexcept(std::decay_t<L>::allocator_type::allocate(0)) &&
noexcept(std::decay_t<L>::allocator_type::free(nullptr)))
{
l.resize(l.length(), r.size() + 1);
memcpy(l.get()+l.length()+1, r.get(), r.size());
l.append(r.get(), r.size());
return l;
}
} //namespace rexy

View File

@ -1,32 +0,0 @@
/**
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/>.
*/
#include "rexy/binary.hpp"
namespace rexy{
char* binary_base::release(void){
return std::exchange(m_data, nullptr);
}
char& binary_base::operator[](size_t i){
return m_data[i];
}
const char& binary_base::operator[](size_t i)const{
return m_data[i];
}
}