Files
boost_beast/extras/beast/test/test_allocator.hpp
T

167 lines
3.5 KiB
C++
Raw Normal View History

2017-04-30 11:29:33 -07:00
//
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
//
#ifndef BEAST_TEST_TEST_ALLOCATOR_HPP
#define BEAST_TEST_TEST_ALLOCATOR_HPP
#include <atomic>
#include <cstddef>
#include <memory>
namespace beast {
namespace test {
struct test_allocator_info
{
2017-06-09 22:36:57 -07:00
std::size_t id;
2017-04-30 11:29:33 -07:00
std::size_t ncopy = 0;
std::size_t nmove = 0;
2017-06-09 22:36:57 -07:00
std::size_t nmassign = 0;
std::size_t ncpassign = 0;
2017-04-30 11:29:33 -07:00
std::size_t nselect = 0;
2017-06-09 22:36:57 -07:00
test_allocator_info()
: id([]
{
static std::atomic<std::size_t> sid(0);
return ++sid;
}())
{
}
2017-04-30 11:29:33 -07:00
};
2017-06-09 22:36:57 -07:00
template<class T, bool Equal, bool Assign, bool Move, bool Swap, bool Select>
2017-04-30 11:29:33 -07:00
class test_allocator;
2017-06-09 22:36:57 -07:00
template<class T, bool Equal, bool Assign, bool Move, bool Swap, bool Select>
2017-04-30 11:29:33 -07:00
struct test_allocator_base
{
};
2017-06-09 22:36:57 -07:00
template<class T, bool Equal, bool Assign, bool Move, bool Swap>
struct test_allocator_base<T, Equal, Assign, Move, Swap, true>
2017-04-30 11:29:33 -07:00
{
static
2017-04-30 14:36:16 -07:00
test_allocator<T, Equal, Assign, Move, Swap, true>
select_on_container_copy_construction(test_allocator<
T, Equal, Assign, Move, Swap, true> const& a)
2017-04-30 11:29:33 -07:00
{
2017-06-09 22:36:57 -07:00
return test_allocator<T, Equal, Assign, Move, Swap, true>{};
2017-04-30 11:29:33 -07:00
}
};
2017-06-09 22:36:57 -07:00
template<class T, bool Equal, bool Assign, bool Move, bool Swap, bool Select>
2017-04-30 11:29:33 -07:00
class test_allocator : public test_allocator_base<
2017-04-30 14:36:16 -07:00
T, Equal, Assign, Move, Swap, Select>
2017-04-30 11:29:33 -07:00
{
std::shared_ptr<test_allocator_info> info_;
2017-04-30 14:36:16 -07:00
template<class, bool, bool, bool, bool, bool>
2017-04-30 11:29:33 -07:00
friend class test_allocator;
public:
using value_type = T;
2017-04-30 14:36:16 -07:00
2017-04-30 11:29:33 -07:00
using propagate_on_container_copy_assignment =
std::integral_constant<bool, Assign>;
2017-04-30 14:36:16 -07:00
2017-04-30 11:29:33 -07:00
using propagate_on_container_move_assignment =
std::integral_constant<bool, Move>;
2017-04-30 14:36:16 -07:00
2017-04-30 11:29:33 -07:00
using propagate_on_container_swap =
std::integral_constant<bool, Swap>;
template<class U>
struct rebind
{
2017-06-09 22:36:57 -07:00
using other = test_allocator<U, Equal, Assign, Move, Swap, Select>;
2017-04-30 11:29:33 -07:00
};
test_allocator()
2017-06-09 22:36:57 -07:00
: info_(std::make_shared<test_allocator_info>())
2017-04-30 11:29:33 -07:00
{
}
test_allocator(test_allocator const& u) noexcept
2017-06-09 22:36:57 -07:00
: info_(u.info_)
2017-04-30 11:29:33 -07:00
{
++info_->ncopy;
}
template<class U>
2017-04-30 14:36:16 -07:00
test_allocator(test_allocator<U,
Equal, Assign, Move, Swap, Select> const& u) noexcept
2017-06-09 22:36:57 -07:00
: info_(u.info_)
2017-04-30 11:29:33 -07:00
{
++info_->ncopy;
}
test_allocator(test_allocator&& t)
2017-06-09 22:36:57 -07:00
: info_(t.info_)
2017-04-30 11:29:33 -07:00
{
++info_->nmove;
}
2017-06-09 22:36:57 -07:00
test_allocator&
operator=(test_allocator const& u) noexcept
{
info_ = u.info_;
++info_->ncpassign;
return *this;
}
test_allocator&
operator=(test_allocator&& u) noexcept
{
info_ = u.info_;
++info_->nmassign;
return *this;
}
2017-04-30 11:29:33 -07:00
value_type*
allocate(std::size_t n)
{
return static_cast<value_type*>(
::operator new (n*sizeof(value_type)));
}
void
deallocate(value_type* p, std::size_t) noexcept
{
::operator delete(p);
}
2017-04-30 14:36:16 -07:00
bool
operator==(test_allocator const& other) const
{
2017-06-09 22:36:57 -07:00
return id() == other.id() || Equal;
2017-04-30 14:36:16 -07:00
}
bool
operator!=(test_allocator const& other) const
{
return ! this->operator==(other);
}
2017-04-30 11:29:33 -07:00
std::size_t
id() const
{
2017-06-09 22:36:57 -07:00
return info_->id;
2017-04-30 11:29:33 -07:00
}
test_allocator_info const*
operator->() const
{
return info_.get();
}
};
} // test
} // beast
#endif