]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/signals2/example/doc_view_acm.cpp
1 // Document/View sample for Boost.Signals2.
2 // Expands on doc_view.cpp example by using automatic
3 // connection management.
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/io/ios_state.hpp>
18 #include <boost/signals2/signal.hpp>
19 #include <boost/shared_ptr.hpp>
24 typedef boost::signals2::signal
<void ()> signal_t
;
30 /* connect a slot to the signal which will be emitted whenever
31 text is appended to the document. */
32 boost::signals2::connection
connect(const signal_t::slot_type
&subscriber
)
34 return m_sig
.connect(subscriber
);
37 void append(const char* s
)
43 const std::string
& getText() const
56 // static factory function that sets up automatic connection tracking
57 static boost::shared_ptr
<TextView
> create(Document
& doc
)
59 boost::shared_ptr
<TextView
> new_view(new TextView(doc
));
61 typedef Document::signal_t::slot_type slot_type
;
62 slot_type
myslot(&TextView::refresh
, new_view
.get());
63 doc
.connect(myslot
.track(new_view
));
70 std::cout
<< "TextView: " << m_document
.getText() << std::endl
;
73 // private constructor to force use of static factory function
74 TextView(Document
&doc
): m_document(doc
)
83 // static factory function that sets up automatic connection tracking
84 static boost::shared_ptr
<HexView
> create(Document
& doc
)
86 boost::shared_ptr
<HexView
> new_view(new HexView(doc
));
88 typedef Document::signal_t::slot_type slot_type
;
89 slot_type
myslot(&HexView::refresh
, new_view
.get());
90 doc
.connect(myslot
.track(new_view
));
97 boost::io::ios_flags_saver
ifs(std::cout
);
98 const std::string
& s
= m_document
.getText();
100 std::cout
<< "HexView:";
102 for (std::string::const_iterator it
= s
.begin(); it
!= s
.end(); ++it
)
103 std::cout
<< ' ' << std::hex
<< static_cast<int>(*it
);
105 std::cout
<< std::endl
;
108 // private constructor to force use of static factory function
109 HexView(Document
& doc
): m_document(doc
)
112 Document
& m_document
;
115 int main(int argc
, char* argv
[])
118 boost::shared_ptr
<TextView
> v1
= TextView::create(doc
);
119 boost::shared_ptr
<HexView
> v2
= HexView::create(doc
);
121 doc
.append(argc
>= 2 ? argv
[1] : "Hello world!");
123 v2
.reset(); // destroy the HexView, automatically disconnecting
124 doc
.append(" HexView should no longer be connected.");