Change allocator to conform to c++ standard library allocators

This commit is contained in:
rexy712 2020-07-28 09:07:42 -07:00
parent f4aa7f8564
commit a10c12c1cf
11 changed files with 273 additions and 135 deletions

View File

@ -24,9 +24,9 @@
namespace rexy{ namespace rexy{
using binary = basic_binary<detail::default_allocator<>>; using binary = basic_binary<detail::default_allocator<char>>;
extern template class basic_binary<detail::default_allocator<>>; extern template class basic_binary<detail::default_allocator<char>>;
} }

View File

@ -29,6 +29,7 @@
#include "expression.hpp" #include "expression.hpp"
#include "traits.hpp" #include "traits.hpp"
#include "detail/string_appender.hpp" #include "detail/string_appender.hpp"
#include "detail/hasallocator.hpp"
namespace rexy{ namespace rexy{
@ -60,47 +61,51 @@ namespace rexy{
constexpr const char& operator[](size_t i)const noexcept; constexpr const char& operator[](size_t i)const noexcept;
}; };
template<class Allocator = detail::default_allocator<>> template<class Allocator>
class basic_binary : public binary_base class basic_binary : private detail::hasallocator<Allocator>, public binary_base
{ {
public: public:
using allocator_type = Allocator; using allocator_type = Allocator;
public: public:
constexpr basic_binary(void)noexcept; constexpr basic_binary(void)noexcept;
constexpr basic_binary(rexy::steal<char*> data, size_t size)noexcept; constexpr basic_binary(rexy::steal<char*> data, size_t size)noexcept;
constexpr basic_binary(rexy::steal<char*> data, size_t cap, size_t size)noexcept; constexpr basic_binary(rexy::steal<char*> data, size_t cap, size_t size)noexcept;
constexpr basic_binary(rexy::steal<char*> data)noexcept; constexpr basic_binary(rexy::steal<char*> data)noexcept;
basic_binary(const char* data, size_t size)noexcept(noexcept(Allocator::copy(data, size))); basic_binary(const char* data, size_t size)noexcept(noexcept(this->allocate(0)));
basic_binary(const char* data)noexcept(noexcept(Allocator::copy(data, 0))); basic_binary(const char* data)noexcept(noexcept(this->allocate(0)));
basic_binary(const char* data, size_t size, size_t cap)noexcept(noexcept(Allocator::copy(data, size))); basic_binary(const char* data, size_t size, size_t cap)noexcept(noexcept(this->allocate(0)));
explicit basic_binary(size_t size)noexcept(noexcept(Allocator::allocate(size))); explicit basic_binary(size_t size)noexcept(noexcept(this->allocate(0)));
basic_binary(size_t size, size_t cap)noexcept(noexcept(Allocator::allocate(size))); basic_binary(size_t size, size_t cap)noexcept(noexcept(this->allocate(0)));
basic_binary(const basic_binary& b)noexcept(noexcept(Allocator::copy(b.m_data, b.m_cap))); basic_binary(const basic_binary& b)noexcept(noexcept(this->allocate(0)));
constexpr basic_binary(basic_binary&& b)noexcept; constexpr basic_binary(basic_binary&& b)noexcept;
basic_binary(const binary_base& b)noexcept(noexcept(Allocator::copy(b.get(),b.size()))); basic_binary(const binary_base& b)noexcept(noexcept(this->allocate(0)));
~basic_binary(void)noexcept(noexcept(Allocator::free(m_data))); ~basic_binary(void)noexcept(noexcept(this->deallocate(nullptr,0)));
basic_binary& operator=(const basic_binary& b)noexcept(noexcept(Allocator::copy(b.m_data, b.m_size))); basic_binary& operator=(const basic_binary& b)noexcept(noexcept(this->allocate(0)));
constexpr basic_binary& operator=(basic_binary&& b)noexcept; constexpr basic_binary& operator=(basic_binary&& b)noexcept;
basic_binary& operator=(const char* c)noexcept(noexcept(Allocator::copy(c, 0))); basic_binary& operator=(const char* c)noexcept(noexcept(this->allocate(0)));
basic_binary& operator=(const binary_base& b)noexcept(noexcept(Allocator::copy(b.get(), b.size()))); basic_binary& operator=(const binary_base& b)noexcept(noexcept(this->allocate(0)));
void reset(void) void reset(void)
noexcept(noexcept(Allocator::free(m_data))); noexcept(noexcept(this->deallocate(nullptr,0)));
void reset(char* val, size_t cap, size_t size = 0) void reset(char* val, size_t cap, size_t size = 0)
noexcept(noexcept(Allocator::free(m_data))); noexcept(noexcept(this->deallocate(nullptr,0)));
bool resize(size_t newsize) bool resize(size_t newsize)
noexcept(noexcept(Allocator::allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))); noexcept(this->deallocate(nullptr,0)));
void append(const char* data, size_t len) void append(const char* data, size_t len)
noexcept(noexcept(Allocator::allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))); noexcept(this->deallocate(nullptr,0)));
using detail::hasallocator<Allocator>::allocator;
private: private:
basic_binary& _copy_data(const char* data, size_t len) basic_binary& _copy_data(const char* data, size_t len)
noexcept(noexcept(Allocator::copy(nullptr,0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))); noexcept(this->deallocate(nullptr,0)));
}; };
template<class Left, class Right> template<class Left, class Right>

View File

@ -87,43 +87,64 @@ namespace rexy{
template<class Allocator> template<class Allocator>
basic_binary<Allocator>::basic_binary(const char* data, size_t size) basic_binary<Allocator>::basic_binary(const char* data, size_t size)
noexcept(noexcept(Allocator::copy(data, size))): noexcept(noexcept(this->allocate(0))):
binary_base(reinterpret_cast<char*>(Allocator::copy(data, size)), size){} binary_base(size ? this->allocate(size) : nullptr, size)
{
if(size)
memcpy(m_data, data, size);
}
template<class Allocator> template<class Allocator>
basic_binary<Allocator>::basic_binary(const char* data) basic_binary<Allocator>::basic_binary(const char* data)
noexcept(noexcept(Allocator::copy(data, 0))): noexcept(noexcept(this->allocate(0))):
basic_binary(data, cx::strlen(data)){} basic_binary(data ? this->allocate(cx::strlen(data)) : nullptr, cx::strlen(data))
{
if(data)
memcpy(m_data, data, m_cap);
}
template<class Allocator> template<class Allocator>
basic_binary<Allocator>::basic_binary(const char* data, size_t size, size_t cap) basic_binary<Allocator>::basic_binary(const char* data, size_t size, size_t cap)
noexcept(noexcept(Allocator::copy(data, size))): noexcept(noexcept(this->allocate(0))):
binary_base(reinterpret_cast<char*>(Allocator::copy(data, size)), size, cap){} binary_base(size ? this->allocate(size) : nullptr, size, cap)
{
if(size)
memcpy(m_data, data, size);
}
template<class Allocator> template<class Allocator>
basic_binary<Allocator>::basic_binary(size_t size) basic_binary<Allocator>::basic_binary(size_t size)
noexcept(noexcept(Allocator::allocate(size))): noexcept(noexcept(this->allocate(0))):
binary_base(reinterpret_cast<char*>(Allocator::allocate(size)), size){} binary_base(this->allocate(size), size){}
template<class Allocator> template<class Allocator>
basic_binary<Allocator>::basic_binary(size_t size, size_t cap) basic_binary<Allocator>::basic_binary(size_t size, size_t cap)
noexcept(noexcept(Allocator::allocate(size))): noexcept(noexcept(this->allocate(0))):
binary_base(reinterpret_cast<char*>(size ? Allocator::allocate(size) : nullptr), size, cap){} binary_base(size ? this->allocate(size) : nullptr, size, cap){}
template<class Allocator> template<class Allocator>
basic_binary<Allocator>::basic_binary(const basic_binary& b) basic_binary<Allocator>::basic_binary(const basic_binary& b)
noexcept(noexcept(Allocator::copy(b.m_data, b.m_cap))): noexcept(noexcept(this->allocate(0))):
binary_base(reinterpret_cast<char*>(b.m_size ? Allocator::copy(b.m_data, b.m_size) : nullptr), b.m_size, b.m_size){} binary_base(b.m_size ? this->allocate(b.m_size) : nullptr, b.m_size, b.m_size)
{
if(b.m_size)
memcpy(m_data, b.m_data, b.m_size);
}
template<class Allocator> template<class Allocator>
constexpr basic_binary<Allocator>::basic_binary(basic_binary&& b)noexcept: constexpr basic_binary<Allocator>::basic_binary(basic_binary&& b)noexcept:
binary_base(cx::exchange(b.m_data, nullptr), b.m_size, b.m_cap){} binary_base(cx::exchange(b.m_data, nullptr), b.m_size, b.m_cap){}
template<class Allocator> template<class Allocator>
basic_binary<Allocator>::basic_binary(const binary_base& b)noexcept(noexcept(Allocator::copy(b.get(),b.size()))): basic_binary<Allocator>::basic_binary(const binary_base& b)
binary_base(reinterpret_cast<char*>(b.size() ? Allocator::copy(b.get(), b.size()) : nullptr), b.size(), b.size()){} noexcept(noexcept(this->allocate(0))):
binary_base(b.size() ? this->allocate(b.size()) : nullptr, b.size(), b.size())
{
if(b.size())
memcpy(m_data, b.get(), b.size());
}
template<class Allocator> template<class Allocator>
basic_binary<Allocator>::~basic_binary(void) basic_binary<Allocator>::~basic_binary(void)
noexcept(noexcept(Allocator::free(m_data))) noexcept(noexcept(this->deallocate(nullptr,0)))
{ {
Allocator::free(m_data); this->deallocate(m_data, m_cap);
} }
template<class Allocator> template<class Allocator>
basic_binary<Allocator>& basic_binary<Allocator>::operator=(const basic_binary& b) basic_binary<Allocator>& basic_binary<Allocator>::operator=(const basic_binary& b)
noexcept(noexcept(Allocator::copy(b.m_data, b.m_size))) noexcept(noexcept(this->allocate(0)))
{ {
return _copy_data(b.get(), b.size()); return _copy_data(b.get(), b.size());
} }
@ -136,37 +157,37 @@ namespace rexy{
} }
template<class Allocator> template<class Allocator>
basic_binary<Allocator>& basic_binary<Allocator>::operator=(const char* c) basic_binary<Allocator>& basic_binary<Allocator>::operator=(const char* c)
noexcept(noexcept(Allocator::copy(c, 0))) noexcept(noexcept(this->allocate(0)))
{ {
return _copy_data(c, strlen(c)); return _copy_data(c, strlen(c));
} }
template<class Allocator> template<class Allocator>
basic_binary<Allocator>& basic_binary<Allocator>::operator=(const binary_base& b) basic_binary<Allocator>& basic_binary<Allocator>::operator=(const binary_base& b)
noexcept(noexcept(Allocator::copy(b.get(), b.size()))) noexcept(noexcept(this->allocate(0)))
{ {
return _copy_data(b.get(), b.size()); return _copy_data(b.get(), b.size());
} }
template<class Allocator> template<class Allocator>
void basic_binary<Allocator>::reset(void) void basic_binary<Allocator>::reset(void)
noexcept(noexcept(Allocator::free(m_data))) noexcept(noexcept(this->deallocate(nullptr,0)))
{ {
Allocator::free(m_data); this->deallocate(m_data, m_cap);
m_data = nullptr; m_data = nullptr;
m_cap = m_size = 0; m_cap = m_size = 0;
} }
template<class Allocator> template<class Allocator>
void basic_binary<Allocator>::reset(char* val, size_t cap, size_t size) void basic_binary<Allocator>::reset(char* val, size_t cap, size_t size)
noexcept(noexcept(Allocator::free(m_data))) noexcept(noexcept(this->deallocate(nullptr,0)))
{ {
Allocator::free(m_data); this->deallocate(m_data, m_cap);
m_data = val; m_data = val;
m_cap = cap; m_cap = cap;
m_size = size; m_size = size;
} }
template<class Allocator> template<class Allocator>
bool basic_binary<Allocator>::resize(size_t newsize) bool basic_binary<Allocator>::resize(size_t newsize)
noexcept(noexcept(Allocator::allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))) noexcept(this->deallocate(nullptr,0)))
{ {
if(newsize < m_cap) if(newsize < m_cap)
return false; return false;
@ -180,8 +201,8 @@ namespace rexy{
} }
template<class Allocator> template<class Allocator>
void basic_binary<Allocator>::append(const char* data, size_t len) void basic_binary<Allocator>::append(const char* data, size_t len)
noexcept(noexcept(Allocator::allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))) noexcept(this->deallocate(nullptr,0)))
{ {
if(m_size + len > m_cap) if(m_size + len > m_cap)
resize(cx::max(m_cap*2, m_size+len)); resize(cx::max(m_cap*2, m_size+len));
@ -190,8 +211,8 @@ namespace rexy{
} }
template<class Allocator> template<class Allocator>
basic_binary<Allocator>& basic_binary<Allocator>::_copy_data(const char* data, size_t len) basic_binary<Allocator>& basic_binary<Allocator>::_copy_data(const char* data, size_t len)
noexcept(noexcept(Allocator::copy(nullptr,0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))) noexcept(this->deallocate(nullptr,0)))
{ {
if(!len) if(!len)
return (*this = basic_binary(rexy::steal<char*>(nullptr), 0, 0)); return (*this = basic_binary(rexy::steal<char*>(nullptr), 0, 0));

View File

@ -20,28 +20,74 @@
#define REXY_DEFAULT_ALLOCATOR_HPP #define REXY_DEFAULT_ALLOCATOR_HPP
#include <cstdlib> //size_t #include <cstdlib> //size_t
#include <cstring> //memcpy #include <type_traits> //true_type, false_type
#include <new> #include <new>
namespace rexy{ namespace rexy{
namespace detail{ namespace detail{
template<size_t Alignment = __STDCPP_DEFAULT_NEW_ALIGNMENT__> template<class T>
struct default_allocator struct default_allocator
{ {
static void free(void* data){ using pointer = T*;
::operator delete(data, std::align_val_t{Alignment}); using const_pointer = const T*;
using void_pointer = void*;
using const_void_pointer = const void*;
using value_type = T;
using size_type = size_t;
using difference_type = ptrdiff_t;
using is_always_equal = std::true_type;
using propagate_on_container_copy_assignment = std::false_type;
using propagate_on_container_move_assignment = std::false_type;
using propagate_on_container_swap = std::false_type;
template<class U>
struct rebind{
using other = default_allocator<U>;
};
private:
constexpr bool has_overflow(size_type n)const{
return n > max_size();
} }
static void* allocate(size_t size){
return ::operator new(size, std::align_val_t{Alignment}); public:
default_allocator(void) = default;
default_allocator(const default_allocator&) = default;
default_allocator(default_allocator&&) = default;
template<class U>
constexpr default_allocator(const default_allocator<U>&)noexcept{}
~default_allocator(void) = default;
pointer allocate(size_type n){
size_type bytes = has_overflow(n) ? size_type{-1} : n*sizeof(T);
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
return reinterpret_cast<pointer>(::operator new(bytes));
}else{
return reinterpret_cast<pointer>(::operator new(bytes, static_cast<std::align_val_t>(alignof(T))));
}
} }
static void* copy(const void* c, size_t size){ void deallocate(pointer p, size_type n){
char* tmp = reinterpret_cast<char*>(allocate(size)); size_type bytes = has_overflow(n) ? size_type{-1} : n*sizeof(T);
memcpy(tmp, c, size); if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
return tmp; ::operator delete(p, bytes);
}else{
::operator delete(p, bytes, static_cast<std::align_val_t>(alignof(T)));
}
}
constexpr size_type max_size(void)const{
return size_type{-1}/sizeof(T);
} }
}; };
template<class T, class U>
constexpr bool operator==(const default_allocator<T>&, const default_allocator<U>&){
return true;
}
template<class T, class U>
constexpr bool operator!=(const default_allocator<T>&, const default_allocator<U>&){
return false;
}
} }
} }

View File

@ -0,0 +1,46 @@
/**
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_DETAIL_HASALLOCATOR_HPP
#define REXY_DETAIL_HASALLOCATOR_HPP
namespace rexy::detail{
template<class Alloc>
struct hasallocator
{
Alloc m_alloc;
auto allocate(typename Alloc::size_type bytes)noexcept(noexcept(m_alloc.allocate(0))){
return m_alloc.allocate(bytes);
}
void deallocate(typename Alloc::pointer p, typename Alloc::size_type bytes)noexcept(noexcept(m_alloc.deallocate(nullptr,0))){
m_alloc.deallocate(p, bytes);
}
Alloc& allocator(void){
return m_alloc;
}
const Alloc& allocator(void)const{
return m_alloc;
}
};
}
#endif

View File

@ -25,9 +25,9 @@
namespace rexy{ namespace rexy{
//new allocated string //new allocated string
using string = basic_string<detail::default_allocator<>>; using string = basic_string<detail::default_allocator<char>>;
extern template class basic_string<detail::default_allocator<>>; extern template class basic_string<detail::default_allocator<char>>;
} }

View File

@ -29,6 +29,7 @@
#include "traits.hpp" #include "traits.hpp"
#include "expression.hpp" #include "expression.hpp"
#include "detail/string_appender.hpp" #include "detail/string_appender.hpp"
#include "detail/hasallocator.hpp"
namespace rexy{ namespace rexy{
@ -36,8 +37,8 @@ namespace rexy{
class string_base class string_base
{ {
protected: protected:
size_t m_length = 0; size_t m_length = 0; //length of string not including null terminator
size_t m_cap = 0; size_t m_cap = 0; //size of current buffer not including null terminator
char* m_data = nullptr; char* m_data = nullptr;
protected: protected:
@ -77,66 +78,67 @@ namespace rexy{
//Supplies all functions that string_base can't implement //Supplies all functions that string_base can't implement
template<class Allocator> template<class Allocator>
class basic_string : public string_base class basic_string : private detail::hasallocator<Allocator>, public string_base
{ {
public: public:
using allocator_type = Allocator; using allocator_type = Allocator;
private: private:
basic_string& _copy_string(const char* s, size_t len) basic_string& _copy_string(const char* s, size_t len)
noexcept(noexcept(Allocator::copy(nullptr,0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))); noexcept(this->deallocate(nullptr,0)));
public: public:
constexpr basic_string(void)noexcept; constexpr basic_string(void)noexcept;
constexpr basic_string(rexy::steal<char*> data, size_t len)noexcept; constexpr basic_string(rexy::steal<char*> data, size_t len)noexcept;
constexpr basic_string(rexy::steal<char*> data, size_t len, size_t cap)noexcept; constexpr basic_string(rexy::steal<char*> data, size_t len, size_t cap)noexcept;
constexpr basic_string(rexy::steal<char*> data)noexcept; constexpr basic_string(rexy::steal<char*> data)noexcept;
basic_string(const char* data, size_t len)noexcept(noexcept(Allocator::copy(data,len))); basic_string(const char* data, size_t len)noexcept(noexcept(this->allocate(data,len)));
basic_string(const char* data)noexcept(noexcept(Allocator::copy(data, m_cap))); basic_string(const char* data)noexcept(noexcept(this->allocate(data, m_cap)));
explicit basic_string(size_t len)noexcept(noexcept(Allocator::allocate(len))); explicit basic_string(size_t len)noexcept(noexcept(this->allocate(len)));
basic_string(size_t len, size_t cap)noexcept(noexcept(Allocator::allocate(len))); basic_string(size_t len, size_t cap)noexcept(noexcept(this->allocate(len)));
//normal copy and move ctors //normal copy and move ctors
basic_string(const basic_string& b)noexcept(noexcept(Allocator::copy(b.m_data, b.m_length))); basic_string(const basic_string& b)noexcept(noexcept(this->allocate(b.m_data, b.m_length)));
constexpr basic_string(basic_string&& s)noexcept(noexcept(cx::exchange(s.m_data, nullptr))); constexpr basic_string(basic_string&& s)noexcept(noexcept(cx::exchange(s.m_data, nullptr)));
basic_string(const string_base& b)noexcept(noexcept(Allocator::copy(b.get(), b.length()))); basic_string(const string_base& b)noexcept(noexcept(this->allocate(b.get(), b.length())));
//dtor //dtor
~basic_string(void)noexcept(noexcept(Allocator::free(m_data))); ~basic_string(void)noexcept(noexcept(this->deallocate(nullptr, 0)));
basic_string& operator=(const basic_string& s) basic_string& operator=(const basic_string& s)
noexcept(noexcept(Allocator::copy(nullptr,0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))); noexcept(this->deallocate(nullptr,0)));
constexpr basic_string& operator=(basic_string&& s)noexcept; constexpr basic_string& operator=(basic_string&& s)noexcept;
//Copy from c string //Copy from c string
basic_string& operator=(const char* c) basic_string& operator=(const char* c)
noexcept(noexcept(Allocator::copy(nullptr,0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))); noexcept(this->deallocate(nullptr,0)));
//Copy from other string_base //Copy from other string_base
basic_string& operator=(const string_base& s) basic_string& operator=(const string_base& s)
noexcept(noexcept(Allocator::copy(nullptr,0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))); noexcept(this->deallocate(nullptr,0)));
//Replace managed pointer. Frees existing value //Replace managed pointer. Frees existing value
void reset(char* val = nullptr)noexcept(noexcept(Allocator::free(m_data))); void reset(char* val = nullptr)noexcept(noexcept(this->deallocate(m_data)));
void reset(char* val, size_t len)noexcept(noexcept(Allocator::free(m_data))); void reset(char* val, size_t len)noexcept(noexcept(this->deallocate(m_data)));
bool resize(size_t newsize) bool resize(size_t newsize)
noexcept(noexcept(Allocator::copy(nullptr,0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))); noexcept(this->deallocate(nullptr,0)));
void append(const char* data, size_t len) void append(const char* data, size_t len)
noexcept(noexcept(Allocator::allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))); noexcept(this->deallocate(nullptr,0)));
void append(const char* data) void append(const char* data)
noexcept(noexcept(Allocator::allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))); noexcept(this->deallocate(nullptr,0)));
void append(const string_base& s) void append(const string_base& s)
noexcept(noexcept(Allocator::allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))); noexcept(this->deallocate(nullptr,0)));
using detail::hasallocator<Allocator>::allocator;
}; };
//Like an expression template but not really //Like an expression template but not really

View File

@ -47,41 +47,51 @@ namespace rexy{
string_base(data.value(), len, cap){} string_base(data.value(), len, cap){}
template<class Allocator> template<class Allocator>
basic_string<Allocator>::basic_string(const char* data, size_t len) basic_string<Allocator>::basic_string(const char* data, size_t len)
noexcept(noexcept(Allocator::copy(data,len))): noexcept(noexcept(this->allocate(0))):
string_base(reinterpret_cast<char*>(len ? Allocator::copy(data, len+1) : nullptr), len, len) string_base(len ? this->allocate(len+1) : nullptr, len, len)
{ {
m_data[len] = 0; if(len){
memcpy(m_data, data, len);
m_data[len] = 0;
}
} }
template<class Allocator> template<class Allocator>
basic_string<Allocator>::basic_string(const char* data) basic_string<Allocator>::basic_string(const char* data)
noexcept(noexcept(Allocator::copy(data, m_cap))): noexcept(noexcept(this->allocate(0))):
string_base(data ? strlen(data) : 0) string_base(data ? strlen(data) : 0)
{ {
if(m_cap){ if(m_cap){
m_data = reinterpret_cast<char*>(Allocator::copy(data, m_cap+1)); m_data = this->allocate(m_cap+1);
memcpy(m_data, data, m_cap);
m_length = m_cap; m_length = m_cap;
} }
} }
template<class Allocator> template<class Allocator>
basic_string<Allocator>::basic_string(size_t len) basic_string<Allocator>::basic_string(size_t len)
noexcept(noexcept(Allocator::allocate(len))): noexcept(noexcept(this->allocate(0))):
string_base(reinterpret_cast<char*>(len ? Allocator::allocate(len+1) : nullptr), len) string_base(len ? this->allocate(len+1) : nullptr, len)
{ {
m_data[len] = 0; if(len)
m_data[len] = 0;
} }
template<class Allocator> template<class Allocator>
basic_string<Allocator>::basic_string(size_t len, size_t cap) basic_string<Allocator>::basic_string(size_t len, size_t cap)
noexcept(noexcept(Allocator::allocate(len))): noexcept(noexcept(this->allocate(len))):
string_base(reinterpret_cast<char*>(len ? Allocator::allocate(len+1) : nullptr), len, cap) string_base(len ? this->allocate(len+1) : nullptr, len, cap)
{ {
m_data[len] = 0; if(len)
m_data[len] = 0;
} }
//normal copy and move ctors //normal copy and move ctors
template<class Allocator> template<class Allocator>
basic_string<Allocator>::basic_string(const basic_string& b) basic_string<Allocator>::basic_string(const basic_string& b)
noexcept(noexcept(Allocator::copy(b.m_data, b.m_length))): noexcept(noexcept(this->allocate(0))):
string_base(reinterpret_cast<char*>(b.m_length ? Allocator::copy(b.m_data, b.m_length+1) : nullptr), b.m_length, b.m_length){} string_base(b.m_length ? this->allocate(b.m_length+1) : nullptr, b.m_length, b.m_length)
{
if(b.m_length)
memcpy(m_data, b.m_data, b.m_length+1);
}
template<class Allocator> template<class Allocator>
constexpr basic_string<Allocator>::basic_string(basic_string&& s) constexpr basic_string<Allocator>::basic_string(basic_string&& s)
noexcept(noexcept(cx::exchange(s.m_data, nullptr))): noexcept(noexcept(cx::exchange(s.m_data, nullptr))):
@ -89,21 +99,25 @@ namespace rexy{
template<class Allocator> template<class Allocator>
basic_string<Allocator>::basic_string(const string_base& b) basic_string<Allocator>::basic_string(const string_base& b)
noexcept(noexcept(Allocator::copy(b.get(), b.length()))): noexcept(noexcept(this->allocate(b.length()))):
string_base(reinterpret_cast<char*>(b.length() ? Allocator::copy(b.get(), b.length()+1) : nullptr), b.length(), b.length()){} string_base(b.length() ? this->allocate(b.length()+1) : nullptr, b.length(), b.length())
{
if(b.length())
memcpy(m_data, b.get(), b.length()+1);
}
//dtor //dtor
template<class Allocator> template<class Allocator>
basic_string<Allocator>::~basic_string(void) basic_string<Allocator>::~basic_string(void)
noexcept(noexcept(Allocator::free(m_data))) noexcept(noexcept(this->deallocate(nullptr, 0)))
{ {
Allocator::free(m_data); this->deallocate(m_data, m_cap+1);
} }
template<class Allocator> template<class Allocator>
basic_string<Allocator>& basic_string<Allocator>::operator=(const basic_string& s) basic_string<Allocator>& basic_string<Allocator>::operator=(const basic_string& s)
noexcept(noexcept(Allocator::copy(nullptr,0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))) noexcept(this->deallocate(nullptr, 0)))
{ {
if(s.m_length < m_cap){ if(s.m_length < m_cap){
memcpy(m_data, s.m_data, s.m_length+1); memcpy(m_data, s.m_data, s.m_length+1);
@ -123,16 +137,16 @@ namespace rexy{
//Copy from c string //Copy from c string
template<class Allocator> template<class Allocator>
basic_string<Allocator>& basic_string<Allocator>::operator=(const char* c) basic_string<Allocator>& basic_string<Allocator>::operator=(const char* c)
noexcept(noexcept(Allocator::copy(nullptr,0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))) noexcept(this->deallocate(nullptr,0)))
{ {
return _copy_string(c, strlen(c)); return _copy_string(c, strlen(c));
} }
//Copy from other string_base //Copy from other string_base
template<class Allocator> template<class Allocator>
basic_string<Allocator>& basic_string<Allocator>::operator=(const string_base& s) basic_string<Allocator>& basic_string<Allocator>::operator=(const string_base& s)
noexcept(noexcept(Allocator::copy(nullptr,0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))) noexcept(this->deallocate(nullptr,0)))
{ {
return _copy_string(s.get(), s.length()); return _copy_string(s.get(), s.length());
} }
@ -140,26 +154,26 @@ namespace rexy{
//Replace managed pointer. Frees existing value //Replace managed pointer. Frees existing value
template<class Allocator> template<class Allocator>
void basic_string<Allocator>::reset(char* val) void basic_string<Allocator>::reset(char* val)
noexcept(noexcept(Allocator::free(m_data))) noexcept(noexcept(this->deallocate(m_data,0)))
{ {
Allocator::free(m_data); this->deallocate(m_data,m_cap+1);
m_data = val; m_data = val;
m_length = val ? strlen(val) : 0; m_length = val ? strlen(val) : 0;
m_cap = m_length; m_cap = m_length;
} }
template<class Allocator> template<class Allocator>
void basic_string<Allocator>::reset(char* val, size_t len) void basic_string<Allocator>::reset(char* val, size_t len)
noexcept(noexcept(Allocator::free(m_data))) noexcept(noexcept(this->deallocate(m_data,0)))
{ {
Allocator::free(m_data); this->deallocate(m_data,m_cap+1);
m_data = val; m_data = val;
m_length = len; m_length = len;
m_cap = len; m_cap = len;
} }
template<class Allocator> template<class Allocator>
bool basic_string<Allocator>::resize(size_t newsize) bool basic_string<Allocator>::resize(size_t newsize)
noexcept(noexcept(Allocator::copy(nullptr,0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))) noexcept(this->deallocate(nullptr,0)))
{ {
if(newsize < m_cap) if(newsize < m_cap)
return false; return false;
@ -167,8 +181,8 @@ namespace rexy{
} }
template<class Allocator> template<class Allocator>
void basic_string<Allocator>::append(const char* data, size_t len) void basic_string<Allocator>::append(const char* data, size_t len)
noexcept(noexcept(Allocator::allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))) noexcept(this->deallocate(nullptr,0)))
{ {
if(len+m_length <= m_cap){ if(len+m_length <= m_cap){
memcpy(m_data+m_length, data, len); memcpy(m_data+m_length, data, len);
@ -188,24 +202,24 @@ namespace rexy{
} }
template<class Allocator> template<class Allocator>
void basic_string<Allocator>::append(const char* data) void basic_string<Allocator>::append(const char* data)
noexcept(noexcept(Allocator::allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))) noexcept(this->deallocate(nullptr,0)))
{ {
if(data) if(data)
append(data, strlen(data)); append(data, strlen(data));
} }
template<class Allocator> template<class Allocator>
void basic_string<Allocator>::append(const string_base& s) void basic_string<Allocator>::append(const string_base& s)
noexcept(noexcept(Allocator::allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))) noexcept(this->deallocate(nullptr,0)))
{ {
append(s.get(), s.length()); append(s.get(), s.length());
} }
template<class Allocator> template<class Allocator>
basic_string<Allocator>& basic_string<Allocator>::_copy_string(const char* s, size_t len) basic_string<Allocator>& basic_string<Allocator>::_copy_string(const char* s, size_t len)
noexcept(noexcept(Allocator::copy(nullptr,0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(Allocator::free(nullptr))) noexcept(this->deallocate(nullptr,0)))
{ {
if(!len) if(!len)
return (*this = basic_string(rexy::steal<char*>(nullptr), 0, 0)); return (*this = basic_string(rexy::steal<char*>(nullptr), 0, 0));

View File

@ -21,6 +21,6 @@
namespace rexy{ namespace rexy{
template class basic_binary<detail::default_allocator<>>; template class basic_binary<detail::default_allocator<char>>;
} }

View File

@ -76,9 +76,11 @@ namespace rexy{
return fread(dest, 1, bytes, m_fp); return fread(dest, 1, bytes, m_fp);
} }
rexy::string filerd::read(size_t bytes)noexcept{ rexy::string filerd::read(size_t bytes)noexcept{
char* tmp = reinterpret_cast<char*>(rexy::string::allocator_type::allocate(bytes)); rexy::string ret;
char* tmp = ret.allocator().allocate(bytes);
size_t written = read(tmp, bytes); size_t written = read(tmp, bytes);
return rexy::string(rexy::steal(tmp), written); ret.reset(tmp, written);
return ret;
} }
rexy::string filerd::readln(size_t max)noexcept{ rexy::string filerd::readln(size_t max)noexcept{
rexy::string ret; rexy::string ret;
@ -95,9 +97,11 @@ namespace rexy{
rexy::binary filerd::read_bin(size_t bytes) rexy::binary filerd::read_bin(size_t bytes)
noexcept(std::is_nothrow_constructible<rexy::binary, rexy::steal<char*>, size_t, size_t>::value) noexcept(std::is_nothrow_constructible<rexy::binary, rexy::steal<char*>, size_t, size_t>::value)
{ {
char* tmp = reinterpret_cast<char*>(rexy::binary::allocator_type::allocate(bytes)); rexy::binary ret;
char* tmp = ret.allocator().allocate(bytes);
size_t written = read(tmp, bytes); size_t written = read(tmp, bytes);
return rexy::binary(rexy::steal(tmp), written, written); ret.reset(tmp, written);
return ret;
} }
size_t filerd::write(const char* c, size_t bytes)noexcept{ size_t filerd::write(const char* c, size_t bytes)noexcept{
return fwrite(c, 1, bytes, m_fp); return fwrite(c, 1, bytes, m_fp);

View File

@ -21,6 +21,6 @@
namespace rexy{ namespace rexy{
template class basic_string<detail::default_allocator<>>; template class basic_string<detail::default_allocator<char>>;
} }