]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/signals2/example/doc_view_acm.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / 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.
4 //
5 // Copyright Keith MacDonald 2005.
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 <iostream>
15 #include <string>
16 #include <boost/bind.hpp>
17 #include <boost/io/ios_state.hpp>
18 #include <boost/signals2/signal.hpp>
19 #include <boost/shared_ptr.hpp>
20
21 class Document
22 {
23 public:
24 typedef boost::signals2::signal<void ()> signal_t;
25
26 public:
27 Document()
28 {}
29
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)
33 {
34 return m_sig.connect(subscriber);
35 }
36
37 void append(const char* s)
38 {
39 m_text += s;
40 m_sig();
41 }
42
43 const std::string& getText() const
44 {
45 return m_text;
46 }
47
48 private:
49 signal_t m_sig;
50 std::string m_text;
51 };
52
53 class TextView
54 {
55 public:
56 // static factory function that sets up automatic connection tracking
57 static boost::shared_ptr<TextView> create(Document& doc)
58 {
59 boost::shared_ptr<TextView> new_view(new TextView(doc));
60 {
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));
64 }
65 return new_view;
66 }
67
68 void refresh() const
69 {
70 std::cout << "TextView: " << m_document.getText() << std::endl;
71 }
72 private:
73 // private constructor to force use of static factory function
74 TextView(Document &doc): m_document(doc)
75 {}
76
77 Document& m_document;
78 };
79
80 class HexView
81 {
82 public:
83 // static factory function that sets up automatic connection tracking
84 static boost::shared_ptr<HexView> create(Document& doc)
85 {
86 boost::shared_ptr<HexView> new_view(new HexView(doc));
87 {
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));
91 }
92 return new_view;
93 }
94
95 void refresh() const
96 {
97 boost::io::ios_flags_saver ifs(std::cout);
98 const std::string& s = m_document.getText();
99
100 std::cout << "HexView:";
101
102 for (std::string::const_iterator it = s.begin(); it != s.end(); ++it)
103 std::cout << ' ' << std::hex << static_cast<int>(*it);
104
105 std::cout << std::endl;
106 }
107 private:
108 // private constructor to force use of static factory function
109 HexView(Document& doc): m_document(doc)
110 {}
111
112 Document& m_document;
113 };
114
115 int main(int argc, char* argv[])
116 {
117 Document doc;
118 boost::shared_ptr<TextView> v1 = TextView::create(doc);
119 boost::shared_ptr<HexView> v2 = HexView::create(doc);
120
121 doc.append(argc >= 2 ? argv[1] : "Hello world!");
122
123 v2.reset(); // destroy the HexView, automatically disconnecting
124 doc.append(" HexView should no longer be connected.");
125
126 return 0;
127 }