Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/signals/example/doc_view.cpp @ 35

Last change on this file since 35 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 2.2 KB
Line 
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
13class Document
14{
15public:
16    typedef boost::signal<void (bool)>  signal_t;
17    typedef boost::signals::connection  connection_t;
18
19public:
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
44private:
45    signal_t    m_sig;
46    std::string m_text;
47};
48
49class View
50{
51public:
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
65protected:
66    Document&               m_document;
67
68private:
69    Document::connection_t  m_connection;
70};
71
72class TextView : public View
73{
74public:
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
85class HexView : public View
86{
87public:
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
105int 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}
Note: See TracBrowser for help on using the repository browser.