summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/signals2/example/postconstructor_ex1.cpp
blob: 89f684bbae51ab2b7747dc203524e3134c34681a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Minimal example of defining a postconstructor for a class which
// uses boost::signals2::deconstruct as its factory function.
//
// Copyright Frank Mori Hess 2009.

// Use, modification and
// distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// For more information, see http://www.boost.org

#include <boost/shared_ptr.hpp>
#include <boost/signals2/deconstruct.hpp>
#include <iostream>

namespace bs2 = boost::signals2;

namespace mynamespace
{
  class X
  {
  public:
    /* This adl_postconstruct function will be found
    via argument-dependent lookup when using boost::signals2::deconstruct. */
    template<typename T> friend
      void adl_postconstruct(const boost::shared_ptr<T> &, X *)
    {
      std::cout << "world!" << std::endl;
    }
  private:
    friend class bs2::deconstruct_access;  // give boost::signals2::deconstruct access to private constructor
    // private constructor forces use of boost::signals2::deconstruct to create objects.
    X()
    {
      std::cout << "Hello, ";
    }
  };
}

int main()
{
  // adl_postconstruct will be called during implicit conversion of return value to shared_ptr
  boost::shared_ptr<mynamespace::X> x = bs2::deconstruct<mynamespace::X>();
  return 0;
}