1 /* 2 Copyright 2019 Glen Joseph Fernandes 3 (glenjofe@gmail.com) 4 5 Distributed under the Boost Software License, Version 1.0. 6 (http://www.boost.org/LICENSE_1_0.txt) 7 */ 8 #include <boost/config.hpp> 9 #if !defined(BOOST_NO_CXX11_ALLOCATOR) 10 #include <boost/core/alloc_construct.hpp> 11 #include <boost/core/lightweight_test.hpp> 12 13 class type { 14 public: type(int x)15 explicit type(int x) 16 : value_(x) { } 17 value() const18 int value() const { 19 return value_; 20 } 21 22 static int count; 23 24 private: 25 type(const type&); 26 type& operator=(const type&); 27 28 int value_; 29 }; 30 31 int type::count = 0; 32 33 template<class T> 34 struct creator { 35 typedef T value_type; 36 creatorcreator37 creator() { } 38 39 template<class U> creatorcreator40 creator(const creator<U>&) { } 41 allocatecreator42 T* allocate(std::size_t size) { 43 return static_cast<T*>(::operator new(sizeof(T) * size)); 44 } 45 deallocatecreator46 void deallocate(T* ptr, std::size_t) { 47 ::operator delete(ptr); 48 } 49 50 template<class V> constructcreator51 void construct(type* ptr, const V& value) { 52 ::new(static_cast<void*>(ptr)) type(value + 1); 53 ++type::count; 54 } 55 destroycreator56 void destroy(type* ptr) { 57 ptr->~type(); 58 --type::count; 59 } 60 }; 61 main()62int main() 63 { 64 creator<type> a; 65 type* p = a.allocate(1); 66 boost::alloc_construct(a, p, 1); 67 BOOST_TEST_EQ(type::count, 1); 68 BOOST_TEST_EQ(p->value(), 2); 69 boost::alloc_destroy(a, p); 70 BOOST_TEST_EQ(type::count, 0); 71 return boost::report_errors(); 72 } 73 #else main()74int main() 75 { 76 return 0; 77 } 78 #endif 79