]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/signals2/example/doc_view_acm_deconstruct.cpp
1 // Document/View sample for Boost.Signals2.
2 // Expands on doc_view_acm.cpp example by using boost::signals2::deconstruct
3 // as a post-constructing factory function.
5 // Copyright Keith MacDonald 2005.
6 // Copyright Frank Mori Hess 2009.
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
16 #include <boost/bind.hpp>
17 #include <boost/ref.hpp>
18 #include <boost/signals2/deconstruct.hpp>
19 #include <boost/signals2/signal.hpp>
20 #include <boost/shared_ptr.hpp>
25 typedef boost::signals2::signal
<void ()> signal_t
;
31 /* Connect a slot to the signal which will be emitted whenever
32 text is appended to the document. */
33 boost::signals2::connection
connect(const signal_t::slot_type
&subscriber
)
35 return m_sig
.connect(subscriber
);
38 void append(const char* s
)
44 const std::string
& getText() const
57 /* This adl_postconstruct function will be found
58 via argument-dependent lookup when using boost::signals2::deconstruct. */
60 friend void adl_postconstruct(const boost::shared_ptr
<T
> &view_sp
, TextView
*view
, Document
& doc
)
62 view
->m_document
= &doc
;
64 typedef Document::signal_t::slot_type slot_type
;
65 slot_type
myslot(&TextView::refresh
, view
);
66 doc
.connect(myslot
.track(view_sp
));
72 std::cout
<< "TextView: " << m_document
->getText() << std::endl
;
75 // give boost::signals2::deconstruct access to private constructor
76 friend class boost::signals2::deconstruct_access
;
77 // private constructor to force use of deconstruct
78 TextView() : m_document(0)
87 /* This adl_postconstruct function will be found
88 via argument-dependent lookup when using boost::signals2::deconstruct. */
90 friend void adl_postconstruct(const boost::shared_ptr
<T
> &view_sp
, HexView
*view
, Document
& doc
)
92 view
->m_document
= &doc
;
94 typedef Document::signal_t::slot_type slot_type
;
95 slot_type
myslot(&HexView::refresh
, view
);
96 doc
.connect(myslot
.track(view_sp
));
102 const std::string
& s
= m_document
->getText();
104 std::cout
<< "HexView:";
106 for (std::string::const_iterator it
= s
.begin(); it
!= s
.end(); ++it
)
107 std::cout
<< ' ' << std::hex
<< static_cast<int>(*it
);
109 std::cout
<< std::endl
;
112 // give boost::signals2::deconstruct access to private constructor
113 friend class boost::signals2::deconstruct_access
;
114 // private constructor to force use of deconstruct
115 HexView(): m_document(0)
118 Document
* m_document
;
121 namespace bs2
= boost::signals2
;
123 int main(int argc
, char* argv
[])
126 boost::shared_ptr
<TextView
> v1
= bs2::deconstruct
<TextView
>().postconstruct(boost::ref(doc
));
127 boost::shared_ptr
<HexView
> v2
= bs2::deconstruct
<HexView
>().postconstruct(boost::ref(doc
));
129 doc
.append(argc
>= 2 ? argv
[1] : "Hello world!");
131 v2
.reset(); // destroy the HexView, automatically disconnecting
132 doc
.append(" HexView should no longer be connected.");