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 |
---|
7 | |
---|
8 | #include <iostream> |
---|
9 | #include <string> |
---|
10 | #include <boost/signal.hpp> |
---|
11 | #include <boost/bind.hpp> |
---|
12 | |
---|
13 | class Document |
---|
14 | { |
---|
15 | public: |
---|
16 | typedef boost::signal<void (bool)> signal_t; |
---|
17 | typedef boost::signals::connection connection_t; |
---|
18 | |
---|
19 | public: |
---|
20 | Document() |
---|
21 | {} |
---|
22 | |
---|
23 | connection_t connect(signal_t::slot_function_type subscriber) |
---|
24 | { |
---|
25 | return m_sig.connect(subscriber); |
---|
26 | } |
---|
27 | |
---|
28 | void disconnect(connection_t subscriber) |
---|
29 | { |
---|
30 | subscriber.disconnect(); |
---|
31 | } |
---|
32 | |
---|
33 | void append(const char* s) |
---|
34 | { |
---|
35 | m_text += s; |
---|
36 | m_sig(true); |
---|
37 | } |
---|
38 | |
---|
39 | const std::string& getText() const |
---|
40 | { |
---|
41 | return m_text; |
---|
42 | } |
---|
43 | |
---|
44 | private: |
---|
45 | signal_t m_sig; |
---|
46 | std::string m_text; |
---|
47 | }; |
---|
48 | |
---|
49 | class View |
---|
50 | { |
---|
51 | public: |
---|
52 | View(Document& m) |
---|
53 | : m_document(m) |
---|
54 | { |
---|
55 | m_connection = m_document.connect(boost::bind(&View::refresh, this, _1)); |
---|
56 | } |
---|
57 | |
---|
58 | virtual ~View() |
---|
59 | { |
---|
60 | m_document.disconnect(m_connection); |
---|
61 | } |
---|
62 | |
---|
63 | virtual void refresh(bool bExtended) const = 0; |
---|
64 | |
---|
65 | protected: |
---|
66 | Document& m_document; |
---|
67 | |
---|
68 | private: |
---|
69 | Document::connection_t m_connection; |
---|
70 | }; |
---|
71 | |
---|
72 | class TextView : public View |
---|
73 | { |
---|
74 | public: |
---|
75 | TextView(Document& doc) |
---|
76 | : View(doc) |
---|
77 | {} |
---|
78 | |
---|
79 | virtual void refresh(bool bExtended) const |
---|
80 | { |
---|
81 | std::cout << "TextView: " << m_document.getText() << std::endl; |
---|
82 | } |
---|
83 | }; |
---|
84 | |
---|
85 | class HexView : public View |
---|
86 | { |
---|
87 | public: |
---|
88 | HexView(Document& doc) |
---|
89 | : View(doc) |
---|
90 | {} |
---|
91 | |
---|
92 | virtual void refresh(bool bExtended) const |
---|
93 | { |
---|
94 | const std::string& s = m_document.getText(); |
---|
95 | |
---|
96 | std::cout << "HexView:"; |
---|
97 | |
---|
98 | for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) |
---|
99 | std::cout << ' ' << std::hex << static_cast<int>(*it); |
---|
100 | |
---|
101 | std::cout << std::endl; |
---|
102 | } |
---|
103 | }; |
---|
104 | |
---|
105 | int main(int argc, char* argv[]) |
---|
106 | { |
---|
107 | Document doc; |
---|
108 | TextView v1(doc); |
---|
109 | HexView v2(doc); |
---|
110 | |
---|
111 | doc.append(argc == 2 ? argv[1] : "Hello world!"); |
---|
112 | return 0; |
---|
113 | } |
---|