1 // Document/View sample for Boost.Signals
2 // Copyright Keith MacDonald 2005. Use, modification and
3 // distribution is subject to the Boost Software License, Version
4 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 // For more information, see http://www.boost.org
10 #include <boost/signal.hpp>
11 #include <boost/bind.hpp>
16 typedef boost::signal
<void (bool)> signal_t
;
17 typedef boost::signals::connection connection_t
;
23 connection_t
connect(signal_t::slot_function_type subscriber
)
25 return m_sig
.connect(subscriber
);
28 void disconnect(connection_t subscriber
)
30 subscriber
.disconnect();
33 void append(const char* s
)
39 const std::string
& getText() const
55 m_connection
= m_document
.connect(boost::bind(&View::refresh
, this, _1
));
60 m_document
.disconnect(m_connection
);
63 virtual void refresh(bool bExtended
) const = 0;
69 Document::connection_t m_connection
;
72 class TextView
: public View
75 TextView(Document
& doc
)
79 virtual void refresh(bool bExtended
) const
81 std::cout
<< "TextView: " << m_document
.getText() << std::endl
;
85 class HexView
: public View
88 HexView(Document
& doc
)
92 virtual void refresh(bool bExtended
) const
94 const std::string
& s
= m_document
.getText();
96 std::cout
<< "HexView:";
98 for (std::string::const_iterator it
= s
.begin(); it
!= s
.end(); ++it
)
99 std::cout
<< ' ' << std::hex
<< static_cast<int>(*it
);
101 std::cout
<< std::endl
;
105 int main(int argc
, char* argv
[])
111 doc
.append(argc
== 2 ? argv
[1] : "Hello world!");