/* Copyright 2021 Glen Joseph Fernandes (glenjofe@gmail.com) Distributed under the Boost Software License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt) */ #include #include #include template class point { public: point() : state_() , ptr_() { } point(int count, T* value) : state_(count) , ptr_(value) { } operator bool() const { return static_cast(ptr_); } T* operator->() const { return ptr_; } int state() const { return state_; } private: int state_; T* ptr_; }; template class creator { public: typedef T value_type; typedef point pointer; typedef std::ptrdiff_t difference_type; creator() : state_() { } explicit creator(int value) : state_(value) { } template creator(const creator& other) : state_(other.state()) { } pointer allocate(std::size_t size) { return pointer(state_, static_cast(::operator new(sizeof(T) * size))); } void deallocate(pointer ptr, std::size_t) { ::operator delete(ptr.operator->()); } int state() const { return state_; } private: int state_; }; template inline bool operator==(const creator&, const creator&) { return true; } template inline bool operator!=(const creator&, const creator&) { return false; } int main() { { std::unique_ptr > > result = boost::allocate_unique(creator<>(1)); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 1); } { std::unique_ptr > > result = boost::allocate_unique(creator<>(2)); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 2); } { std::unique_ptr > > result = boost::allocate_unique(creator<>(3), 3); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 3); } { std::unique_ptr > > result = boost::allocate_unique(creator<>(4)); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 4); } { std::unique_ptr > > result = boost::allocate_unique(creator<>(5), 2); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 5); } { std::unique_ptr > > result = boost::allocate_unique(creator<>(6)); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 6); } { std::unique_ptr > > result = boost::allocate_unique(creator<>(7), 3); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 7); } { std::unique_ptr > > result = boost::allocate_unique(creator<>(8)); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 8); } { std::unique_ptr > > result = boost::allocate_unique(creator<>(9), 2); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 9); } { std::unique_ptr > > result = boost::allocate_unique(creator<>(10)); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 10); } { std::unique_ptr > > > result = boost::allocate_unique_noinit(creator<>(11)); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 11); } { std::unique_ptr > > > result = boost::allocate_unique_noinit(creator<>(12)); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 12); } { std::unique_ptr > > > result = boost::allocate_unique_noinit(creator<>(13), 3); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 13); } { std::unique_ptr > > > result = boost::allocate_unique_noinit(creator<>(14)); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 14); } { std::unique_ptr > > > result = boost::allocate_unique_noinit(creator<>(15), 2); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 15); } { std::unique_ptr > > > result = boost::allocate_unique_noinit(creator<>(16)); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 16); } { std::unique_ptr > > > result = boost::allocate_unique_noinit(creator<>(17), 3); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 17); } { std::unique_ptr > > > result = boost::allocate_unique_noinit(creator<>(18)); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 18); } { std::unique_ptr > > > result = boost::allocate_unique_noinit(creator<>(19), 2); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 19); } { std::unique_ptr > > > result = boost::allocate_unique_noinit(creator<>(20)); point ptr = boost::get_allocator_pointer(result); BOOST_TEST_EQ(ptr.state(), 20); } return boost::report_errors(); }