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