1 // An example of defining a postconstructor for a class which 2 // uses boost::signals2::deconstruct as its factory function. 3 // This example expands on the basic postconstructor_ex1.cpp example 4 // by passing arguments to the constructor and postconstructor. 5 // 6 // Copyright Frank Mori Hess 2009. 7 8 // Use, modification and 9 // distribution is subject to the Boost Software License, Version 10 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at 11 // http://www.boost.org/LICENSE_1_0.txt) 12 // For more information, see http://www.boost.org 13 14 #include <boost/shared_ptr.hpp> 15 #include <boost/signals2/deconstruct.hpp> 16 #include <iostream> 17 #include <sstream> 18 #include <string> 19 20 namespace bs2 = boost::signals2; 21 22 namespace mynamespace 23 { 24 class Y 25 { 26 public: 27 /* This adl_postconstruct function will be found 28 via argument-dependent lookup when using boost::signals2::deconstruct. */ 29 template<typename T> friend adl_postconstruct(const boost::shared_ptr<T> &,Y * y,const std::string & text)30 void adl_postconstruct(const boost::shared_ptr<T> &, Y *y, const std::string &text) 31 { 32 y->_text_stream << text; 33 } print() const34 void print() const 35 { 36 std::cout << _text_stream.str() << std::endl; 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. Y(const std::string & text)41 Y(const std::string &text) 42 { 43 _text_stream << text; 44 } 45 46 std::ostringstream _text_stream; 47 }; 48 } 49 main()50int main() 51 { 52 boost::shared_ptr<mynamespace::Y> y = bs2::deconstruct<mynamespace::Y>("Hello, ").postconstruct("world!"); 53 y->print(); 54 return 0; 55 } 56