| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2017-03-06 01:18:16 -05:00
										 |  |  | Copyright 2012-2015 Glen Joseph Fernandes | 
					
						
							|  |  |  | (glenjofe@gmail.com) | 
					
						
							| 
									
										
										
										
											2015-11-09 22:35:34 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 01:18:16 -05:00
										 |  |  | Distributed under the Boost Software License, Version 1.0. | 
					
						
							|  |  |  | (http://www.boost.org/LICENSE_1_0.txt)
 | 
					
						
							| 
									
										
										
										
											2015-11-09 22:35:34 -05:00
										 |  |  | */ | 
					
						
							|  |  |  | #include <boost/core/lightweight_test.hpp>
 | 
					
						
							| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  | #include <boost/smart_ptr/enable_shared_from_this.hpp>
 | 
					
						
							| 
									
										
										
										
											2015-11-09 22:35:34 -05:00
										 |  |  | #include <boost/smart_ptr/make_shared.hpp>
 | 
					
						
							| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-20 20:03:52 -04:00
										 |  |  | template<class T = void> | 
					
						
							| 
									
										
										
										
											2017-03-06 01:18:16 -05:00
										 |  |  | struct creator { | 
					
						
							|  |  |  |     typedef T value_type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<class U> | 
					
						
							|  |  |  |     struct rebind { | 
					
						
							|  |  |  |         typedef creator<U> other; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     creator() { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<class U> | 
					
						
							|  |  |  |     creator(const creator<U>&) { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     T* allocate(std::size_t size) { | 
					
						
							|  |  |  |         return static_cast<T*>(::operator new(sizeof(T) * size)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void deallocate(T* ptr, std::size_t) { | 
					
						
							|  |  |  |         ::operator delete(ptr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<class T, class U> | 
					
						
							|  |  |  | inline bool | 
					
						
							|  |  |  | operator==(const creator<T>&, const creator<U>&) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<class T, class U> | 
					
						
							|  |  |  | inline bool | 
					
						
							|  |  |  | operator!=(const creator<T>&, const creator<U>&) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class type | 
					
						
							| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  |     : public boost::enable_shared_from_this<type> { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2017-03-06 01:18:16 -05:00
										 |  |  |     static unsigned instances; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     type() { | 
					
						
							|  |  |  |         ++instances; | 
					
						
							| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-03-06 01:18:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  |     ~type() { | 
					
						
							| 
									
										
										
										
											2017-03-06 01:18:16 -05:00
										 |  |  |         --instances; | 
					
						
							| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-03-06 01:18:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  | private: | 
					
						
							|  |  |  |     type(const type&); | 
					
						
							|  |  |  |     type& operator=(const type&); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 01:18:16 -05:00
										 |  |  | unsigned type::instances = 0; | 
					
						
							| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-09 22:35:34 -05:00
										 |  |  | int main() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  |     BOOST_TEST(type::instances == 0); | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-03-06 01:18:16 -05:00
										 |  |  |         boost::shared_ptr<type[]> result = | 
					
						
							|  |  |  |             boost::allocate_shared<type[]>(creator<type>(), 3); | 
					
						
							| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2017-03-06 01:18:16 -05:00
										 |  |  |             result[0].shared_from_this(); | 
					
						
							| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  |             BOOST_ERROR("shared_from_this did not throw"); | 
					
						
							| 
									
										
										
										
											2012-11-11 19:14:50 +00:00
										 |  |  |         } catch (...) { | 
					
						
							| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  |             BOOST_TEST(type::instances == 3); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-12-11 17:42:47 +00:00
										 |  |  |     BOOST_TEST(type::instances == 0); | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-03-06 01:18:16 -05:00
										 |  |  |         boost::shared_ptr<type[]> result = | 
					
						
							| 
									
										
										
										
											2017-06-20 20:03:52 -04:00
										 |  |  |             boost::allocate_shared_noinit<type[]>(creator<>(), 3); | 
					
						
							| 
									
										
										
										
											2012-12-11 17:42:47 +00:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2017-03-06 01:18:16 -05:00
										 |  |  |             result[0].shared_from_this(); | 
					
						
							| 
									
										
										
										
											2012-12-11 17:42:47 +00:00
										 |  |  |             BOOST_ERROR("shared_from_this did not throw"); | 
					
						
							|  |  |  |         } catch (...) { | 
					
						
							|  |  |  |             BOOST_TEST(type::instances == 3); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-11-06 14:17:32 +00:00
										 |  |  |     return boost::report_errors(); | 
					
						
							|  |  |  | } |