factory_allocator_throws.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright 2019 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <boost/functional/factory.hpp>
  8. #include <boost/core/lightweight_test.hpp>
  9. #include <boost/smart_ptr/shared_ptr.hpp>
  10. struct type {
  11. explicit type(bool b) {
  12. if (b) {
  13. throw true;
  14. }
  15. }
  16. };
  17. template<class T>
  18. class creator {
  19. public:
  20. static int count;
  21. typedef T value_type;
  22. typedef T* pointer;
  23. template<class U>
  24. struct rebind {
  25. typedef creator<U> other;
  26. };
  27. creator() { }
  28. template<class U>
  29. creator(const creator<U>&) { }
  30. T* allocate(std::size_t size) {
  31. ++count;
  32. return static_cast<T*>(::operator new(sizeof(T) * size));
  33. }
  34. void deallocate(T* ptr, std::size_t) {
  35. --count;
  36. ::operator delete(ptr);
  37. }
  38. };
  39. template<class T>
  40. int creator<T>::count = 0;
  41. template<class T, class U>
  42. inline bool
  43. operator==(const creator<T>&, const creator<U>&)
  44. {
  45. return true;
  46. }
  47. template<class T, class U>
  48. inline bool
  49. operator!=(const creator<T>&, const creator<U>&)
  50. {
  51. return false;
  52. }
  53. int main()
  54. {
  55. bool b = true;
  56. try {
  57. boost::shared_ptr<type> s(boost::factory<boost::shared_ptr<type>,
  58. creator<void>,
  59. boost::factory_alloc_for_pointee_and_deleter>()(b));
  60. } catch (...) {
  61. BOOST_TEST(creator<type>::count == 0);
  62. }
  63. try {
  64. boost::shared_ptr<type> s(boost::factory<boost::shared_ptr<type>,
  65. creator<void>,
  66. boost::factory_passes_alloc_to_smart_pointer>()(b));
  67. } catch (...) {
  68. BOOST_TEST(creator<type>::count == 0);
  69. }
  70. return boost::report_errors();
  71. }