predestructor_example.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Minimal example of defining a predestructor for a class which
  2. // uses boost::signals2::deconstruct as its factory function.
  3. //
  4. // Copyright Frank Mori Hess 2009.
  5. // Use, modification and
  6. // distribution is subject to the Boost Software License, Version
  7. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. // For more information, see http://www.boost.org
  10. #include <boost/shared_ptr.hpp>
  11. #include <boost/signals2/deconstruct.hpp>
  12. #include <iostream>
  13. namespace bs2 = boost::signals2;
  14. namespace mynamespace
  15. {
  16. class X
  17. {
  18. public:
  19. ~X()
  20. {
  21. std::cout << "cruel world!" << std::endl;
  22. }
  23. /* This adl_predestruct friend function will be found by
  24. via argument-dependent lookup when using boost::signals2::deconstruct. */
  25. friend void adl_predestruct(X *)
  26. {
  27. std::cout << "Goodbye, ";
  28. }
  29. /* boost::signals2::deconstruct always requires an adl_postconstruct function
  30. which can be found via argument-dependent, so we define one which does nothing. */
  31. template<typename T> friend
  32. void adl_postconstruct(const boost::shared_ptr<T> &, X *)
  33. {}
  34. private:
  35. friend class bs2::deconstruct_access; // give boost::signals2::deconstruct access to private constructor
  36. // private constructor forces use of boost::signals2::deconstruct to create objects.
  37. X()
  38. {}
  39. };
  40. }
  41. int main()
  42. {
  43. {
  44. boost::shared_ptr<mynamespace::X> x = bs2::deconstruct<mynamespace::X>();
  45. }
  46. return 0;
  47. }