Skip to content

Commit

Permalink
add any_allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Jan 18, 2022
1 parent a511da2 commit 432f0e1
Show file tree
Hide file tree
Showing 9 changed files with 1,250 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -64,6 +64,7 @@ function(boost_url_setup_properties target)
Boost::align
Boost::assert
Boost::config
Boost::core
Boost::exception
Boost::mp11
Boost::system
Expand Down
1 change: 1 addition & 0 deletions doc/qbk/quickref.xml
Expand Up @@ -25,6 +25,7 @@
<entry valign="top">
<bridgehead renderas="sect3">Classes</bridgehead>
<simplelist type="vert" columns="1">
<member><link linkend="url.ref.boost__urls__any_allocator">any_allocator</link></member>
<member><link linkend="url.ref.boost__urls__authority_view">authority_view</link></member>
<member><link linkend="url.ref.boost__urls__ipv4_address">ipv4_address</link></member>
<member><link linkend="url.ref.boost__urls__ipv6_address">ipv6_address</link></member>
Expand Down
124 changes: 124 additions & 0 deletions include/boost/url/any_allocator.hpp
@@ -0,0 +1,124 @@
//
// Copyright (c) 2022 Alan Freitas (alandefreitas@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/CPPAlliance/url
//

#ifndef BOOST_URL_ANY_ALLOCATOR_HPP
#define BOOST_URL_ANY_ALLOCATOR_HPP

#include <boost/url/detail/any_allocator.hpp>
#include <boost/url/detail/config.hpp>

#include <memory>
#include <type_traits>

namespace boost {
namespace urls {

/** A type-erased allocator with shared ownership.
This type satisfies the requirements for
<em>Allocator</em>
@par Specification
@li <a href="https://en.cppreference.com/w/cpp/named_req/Allocator">Allocator (cppreference.com)</a>
*/
#ifdef BOOST_URL_DOCS
template <class T>
using any_allocator = __see_below__;
#else
template <class T>
class any_allocator
: private detail::any_allocator_base
{
std::shared_ptr<base> p_;

template <class U>
friend class any_allocator;

public:
using value_type = T;

using size_type = std::size_t;

using difference_type = std::ptrdiff_t;

using reference = value_type&;

using const_reference = value_type const&;

using pointer = value_type*;

using const_pointer = value_type const*;

template <class U>
struct rebind
{
using other = any_allocator<U>;
};

using is_always_equal = std::false_type;

#ifndef BOOST_URL_NO_GCC_4_2_WORKAROUND
any_allocator() = default;
#endif

any_allocator(any_allocator const&) = default;

template <class U>
explicit any_allocator(
any_allocator<U> const& other) noexcept;

template <class Allocator>
explicit any_allocator(Allocator const& a);

any_allocator&
operator=(any_allocator const&)
= default;

T*
allocate(std::size_t n) const;

void
deallocate(T* p, std::size_t n);

template <class U, class... Args>
void
construct(U* p, Args&&... args);

template <class U>
void
destroy(U* p);

friend bool
operator==(const any_allocator& a,
const any_allocator& b) noexcept
{
return (a.p_ == b.p_)
|| (a.p_ != nullptr
&& b.p_ != nullptr
&& (a.p_->type_id
== b.p_->type_id
&& a.p_->is_equal(*b.p_)));
}

friend bool
operator!=(const any_allocator& a,
const any_allocator& b) noexcept
{
return !(a == b);
}
};
#endif

} // namespace urls
} // namespace boost

#include <boost/url/impl/any_allocator.hpp>

#endif

0 comments on commit 432f0e1

Please sign in to comment.