Boost logo

Boost-Commit :

From: fmhess_at_[hidden]
Date: 2007-09-21 16:35:45


Author: fmhess
Date: 2007-09-21 16:35:45 EDT (Fri, 21 Sep 2007)
New Revision: 39460
URL: http://svn.boost.org/trac/boost/changeset/39460

Log:
Added an example program referenced in the tutorial.

Added:
   sandbox/thread_safe_signals/libs/thread_safe_signals/example/
   sandbox/thread_safe_signals/libs/thread_safe_signals/example/doc_view.cpp (contents, props changed)

Added: sandbox/thread_safe_signals/libs/thread_safe_signals/example/doc_view.cpp
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/libs/thread_safe_signals/example/doc_view.cpp 2007-09-21 16:35:45 EDT (Fri, 21 Sep 2007)
@@ -0,0 +1,113 @@
+// Document/View sample for Boost.Signals
+// Copyright Keith MacDonald 2005. Use, modification and
+// distribution is subject to the Boost Software License, Version
+// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+// For more information, see http://www.boost.org
+
+#include <iostream>
+#include <string>
+#include <boost/signal.hpp>
+#include <boost/bind.hpp>
+
+class Document
+{
+public:
+ typedef boost::signal<void (bool)> signal_t;
+ typedef boost::signals::connection connection_t;
+
+public:
+ Document()
+ {}
+
+ connection_t connect(const signal_t::slot_type &subscriber)
+ {
+ return m_sig.connect(subscriber);
+ }
+
+ void disconnect(connection_t subscriber)
+ {
+ subscriber.disconnect();
+ }
+
+ void append(const char* s)
+ {
+ m_text += s;
+ m_sig(true);
+ }
+
+ const std::string& getText() const
+ {
+ return m_text;
+ }
+
+private:
+ signal_t m_sig;
+ std::string m_text;
+};
+
+class View
+{
+public:
+ View(Document& m)
+ : m_document(m)
+ {
+ m_connection = m_document.connect(boost::bind(&View::refresh, this, _1));
+ }
+
+ virtual ~View()
+ {
+ m_document.disconnect(m_connection);
+ }
+
+ virtual void refresh(bool bExtended) const = 0;
+
+protected:
+ Document& m_document;
+
+private:
+ Document::connection_t m_connection;
+};
+
+class TextView : public View
+{
+public:
+ TextView(Document& doc)
+ : View(doc)
+ {}
+
+ virtual void refresh(bool bExtended) const
+ {
+ std::cout << "TextView: " << m_document.getText() << std::endl;
+ }
+};
+
+class HexView : public View
+{
+public:
+ HexView(Document& doc)
+ : View(doc)
+ {}
+
+ virtual void refresh(bool bExtended) const
+ {
+ const std::string& s = m_document.getText();
+
+ std::cout << "HexView:";
+
+ for (std::string::const_iterator it = s.begin(); it != s.end(); ++it)
+ std::cout << ' ' << std::hex << static_cast<int>(*it);
+
+ std::cout << std::endl;
+ }
+};
+
+int main(int argc, char* argv[])
+{
+ Document doc;
+ TextView v1(doc);
+ HexView v2(doc);
+
+ doc.append(argc == 2 ? argv[1] : "Hello world!");
+ return 0;
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk