diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a96874..7ed5501 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/include/rexy/binary.hpp b/include/rexy/binary.hpp index 4cb81f1..58e4f6f 100644 --- a/include/rexy/binary.hpp +++ b/include/rexy/binary.hpp @@ -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> @@ -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(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>::value && - std::is_nothrow_move_assignable>::value) + noexcept(noexcept(Allocator::copy(b.m_data, b.m_size))) { binary_data 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,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 decltype(auto) operator+=(rexy::binary_data& l, const rexy::binary_data& 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()); diff --git a/include/rexy/detail/binary_string_conv.hpp b/include/rexy/detail/binary_string_conv.hpp index 89c0146..6780515 100644 --- a/include/rexy/detail/binary_string_conv.hpp +++ b/include/rexy/detail/binary_string_conv.hpp @@ -29,44 +29,30 @@ namespace rexy{ noexcept(std::is_nothrow_constructible::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 = 0, std::enable_if_t,typename Str::allocator_type>::value,int> = 0> - auto binary_to_string(binary_data&& b) - noexcept(std::is_nothrow_default_constructible::value && - noexcept(std::declval().reset(nullptr, 0)) && - noexcept(b.release())) - { - Str s; - s.reset(b.get(), b.size()); - b.release(); - return s; + auto binary_to_string(binary_data&& b)noexcept{ + return Str(rexy::steal(b.release()), b.size()); } template = 0> auto string_to_binary(const string_base& s) noexcept(std::is_nothrow_constructible::value && - noexcept(std::declval().append(nullptr, 0))) + noexcept(std::decay_t::allocator_type::allocate(0))) { Bin b(s.length()+1); b.append(s.get(), s.length()+1); return b; } template = 0, std::enable_if_t,typename Bin::allocator_type>::value,int> = 0> - auto string_to_binary(string_intermediary&& s) - noexcept(std::is_nothrow_default_constructible::value && - noexcept(std::declval().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&& s)noexcept{ + return Bin(rexy::steal(s.release()), s.length()); } template = 0, detail::enable_if_concrete_string = 0> decltype(auto) operator+=(L& l, R&& r) - noexcept(noexcept(l.append(nullptr, 0))) + noexcept(noexcept(std::decay_t::allocator_type::allocate(0)) && + noexcept(std::decay_t::allocator_type::free(nullptr))) { l.append(r.get(), r.length()+1); return l; @@ -74,17 +60,17 @@ namespace rexy{ template = 0, detail::enable_if_string = 0, std::enable_if_t::value,int> = 0> decltype(auto) operator+=(L& l, R&& r) noexcept(std::is_nothrow_constructible::value && - std::is_nothrow_assignable::value) + (noexcept(std::decay_t::allocator_type::allocate(0)))) { rexy::string concrete = r; - return (l = concrete); + return (l += concrete); } template = 0, detail::enable_if_binary = 0> decltype(auto) operator+=(L& l, R&& r) - noexcept(noexcept(l.resize(0, 0))) + noexcept(noexcept(std::decay_t::allocator_type::allocate(0)) && + noexcept(std::decay_t::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 diff --git a/src/binary.cpp b/src/binary.cpp deleted file mode 100644 index 45296eb..0000000 --- a/src/binary.cpp +++ /dev/null @@ -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 . -*/ - -#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]; - } -}