Skip to content

Commit

Permalink
Fix more allocator traits (boostorg#85)
Browse files Browse the repository at this point in the history
Finish the process of replacing allocator member types with allocator traits using an approach suggested by @glenfe.
  • Loading branch information
jefftrull authored Apr 10, 2020
1 parent e95a014 commit 016638e
Showing 1 changed file with 37 additions and 52 deletions.
89 changes: 37 additions & 52 deletions include/boost/wave/util/flex_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,29 +287,31 @@ inline bool operator!=(const mallocator<T>&,
return false;
}

template <class Allocator>
typename Allocator::pointer Reallocate(
Allocator& alloc,
typename Allocator::pointer p,
typename Allocator::size_type oldObjCount,
typename Allocator::size_type newObjCount,
void*)
{
// @@@ not implemented
return NULL;
}
#if defined(BOOST_NO_CXX11_ALLOCATOR)
template<class A>
struct allocator_traits {
typedef typename A::value_type value_type;
typedef typename A::pointer pointer;
typedef typename A::const_pointer const_pointer;
typedef typename A::size_type size_type;
typedef typename A::reference reference;
typedef typename A::const_reference const_reference;

template <class Allocator>
typename Allocator::pointer Reallocate(
Allocator& alloc,
typename Allocator::pointer p,
typename Allocator::size_type oldObjCount,
typename Allocator::size_type newObjCount,
mallocator<void>*)
{
// @@@ not implemented
return NULL;
}
static pointer allocate( A& a, size_type n, const_pointer hint )
{
return a.allocate(n, hint);
}

static void deallocate( A& a, pointer p, size_type n )
{
a.deallocate(p, n);
}

static size_type max_size( A const& a ) { return a.max_size(); }
};
#else
using std::allocator_traits;
#endif

////////////////////////////////////////////////////////////////////////////////
// class template SimpleStringStorage
Expand All @@ -331,7 +333,7 @@ class SimpleStringStorage
};
static const Data emptyString_;

typedef typename A::size_type size_type;
typedef typename allocator_traits<A>::size_type size_type;

private:
Data* pData_;
Expand Down Expand Up @@ -544,18 +546,13 @@ SimpleStringStorage<E, A>::emptyString_ =
template <typename E, class A = std::allocator<E> >
class AllocatorStringStorage : public A
{
typedef typename A::size_type size_type;
typedef typename allocator_traits<A>::size_type size_type;
typedef typename SimpleStringStorage<E, A>::Data Data;

void* Alloc(size_type sz, const void* p = 0)
{
#if defined(BOOST_NO_CXX11_ALLOCATOR)
return A::allocate(1 + (sz - 1) / sizeof(E),
static_cast<const char*>(p));
#else
return std::allocator_traits<A>::allocate(*this, 1 + (sz - 1) / sizeof(E),
return allocator_traits<A>::allocate(*this, 1 + (sz - 1) / sizeof(E),
static_cast<const char*>(p));
#endif
}

void* Realloc(void* p, size_type oldSz, size_type newSz)
Expand All @@ -568,7 +565,7 @@ class AllocatorStringStorage : public A

void Free(void* p, size_type sz)
{
A::deallocate(static_cast<E*>(p), sz);
allocator_traits<A>::deallocate(*this, static_cast<E*>(p), sz);
}

Data* pData_;
Expand Down Expand Up @@ -671,11 +668,7 @@ class AllocatorStringStorage : public A
{ return size_type(end() - begin()); }

size_type max_size() const
#if defined(BOOST_NO_CXX11_ALLOCATOR)
{ return A::max_size(); }
#else
{ return std::allocator_traits<A>::max_size(*this); }
#endif
{ return allocator_traits<A>::max_size(*this); }

size_type capacity() const
{ return size_type(pData_->pEndOfMem_ - pData_->buffer_); }
Expand Down Expand Up @@ -766,7 +759,7 @@ class VectorStringStorage : protected std::vector<E, A>
typedef typename base::iterator iterator;
typedef typename base::const_iterator const_iterator;
typedef A allocator_type;
typedef typename A::size_type size_type;
typedef typename allocator_traits<A>::size_type size_type;

VectorStringStorage(const VectorStringStorage& s) : base(s)
{ }
Expand Down Expand Up @@ -883,7 +876,7 @@ class SmallStringOpt
typedef value_type* iterator;
typedef const value_type* const_iterator;
typedef typename Storage::allocator_type allocator_type;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_traits<allocator_type>::size_type size_type;

private:
enum { temp1 = threshold * sizeof(value_type) > sizeof(Storage)
Expand Down Expand Up @@ -1210,7 +1203,7 @@ class CowString
typedef typename Storage::iterator iterator;
typedef typename Storage::const_iterator const_iterator;
typedef typename Storage::allocator_type allocator_type;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_traits<allocator_type>::size_type size_type;
typedef typename Storage::value_type& reference;

private:
Expand Down Expand Up @@ -1470,20 +1463,12 @@ class flex_string : private Storage
typedef T traits_type;
typedef typename traits_type::char_type value_type;
typedef A allocator_type;
typedef typename A::size_type size_type;
typedef typename A::difference_type difference_type;

#if defined(BOOST_NO_CXX11_ALLOCATOR)
typedef typename A::reference reference;
typedef typename A::const_reference const_reference;
typedef typename A::pointer pointer;
typedef typename A::const_pointer const_pointer;
#else
typedef typename std::allocator_traits<A>::value_type& reference;
typedef typename std::allocator_traits<A>::value_type const& const_reference;
typedef typename std::allocator_traits<A>::pointer pointer;
typedef typename std::allocator_traits<A>::const_pointer const_pointer;
#endif
typedef typename allocator_traits<A>::value_type& reference;
typedef typename allocator_traits<A>::value_type const& const_reference;
typedef typename allocator_traits<A>::pointer pointer;
typedef typename allocator_traits<A>::const_pointer const_pointer;
typedef typename allocator_traits<A>::size_type size_type;

typedef typename Storage::iterator iterator;
typedef typename Storage::const_iterator const_iterator;
Expand Down

0 comments on commit 016638e

Please sign in to comment.