Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r51139 - in sandbox/thread_safe_signals/trunk/libs/signals2: doc doc/snippets example
From: fmhess_at_[hidden]
Date: 2009-02-09 10:00:03


Author: fmhess
Date: 2009-02-09 10:00:02 EST (Mon, 09 Feb 2009)
New Revision: 51139
URL: http://svn.boost.org/trac/boost/changeset/51139

Log:
Converted document-view section of tutorial to use extracted
code snippets.

Added:
   sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/document_def_code_snippet.txt (contents, props changed)
   sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/document_view_main_code_snippet.txt (contents, props changed)
   sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/hex_view_def_code_snippet.txt (contents, props changed)
   sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/text_view_def_code_snippet.txt (contents, props changed)
Text files modified:
   sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/block_code_snippet.txt | 1
   sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/disconnect_code_snippet.txt | 1
   sandbox/thread_safe_signals/trunk/libs/signals2/doc/tutorial.xml | 140 ++++++---------------------------------
   sandbox/thread_safe_signals/trunk/libs/signals2/example/disconnect_and_block.cpp | 2
   sandbox/thread_safe_signals/trunk/libs/signals2/example/doc_view.cpp | 8 ++
   5 files changed, 32 insertions(+), 120 deletions(-)

Modified: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/block_code_snippet.txt
==============================================================================
--- sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/block_code_snippet.txt (original)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/block_code_snippet.txt 2009-02-09 10:00:02 EST (Mon, 09 Feb 2009)
@@ -1,5 +1,4 @@
   boost::signals2::connection c = sig.connect(HelloWorld());
- // connection is not blocked
   std::cout << "c is not blocked.\n";
   sig(); // Prints "Hello, World!"
 

Modified: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/disconnect_code_snippet.txt
==============================================================================
--- sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/disconnect_code_snippet.txt (original)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/disconnect_code_snippet.txt 2009-02-09 10:00:02 EST (Mon, 09 Feb 2009)
@@ -1,5 +1,4 @@
   boost::signals2::connection c = sig.connect(HelloWorld());
- // c is connected
   std::cout << "c is connected\n";
   sig(); // Prints "Hello, World!"
 

Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/document_def_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/document_def_code_snippet.txt 2009-02-09 10:00:02 EST (Mon, 09 Feb 2009)
@@ -0,0 +1,31 @@
+class Document
+{
+public:
+ typedef boost::signals2::signal<void ()> signal_t;
+
+public:
+ Document()
+ {}
+
+ /* Connect a slot to the signal which will be emitted whenever
+ text is appended to the document. */
+ boost::signals2::connection connect(const signal_t::slot_type &subscriber)
+ {
+ return m_sig.connect(subscriber);
+ }
+
+ void append(const char* s)
+ {
+ m_text += s;
+ m_sig();
+ }
+
+ const std::string& getText() const
+ {
+ return m_text;
+ }
+
+private:
+ signal_t m_sig;
+ std::string m_text;
+};

Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/document_view_main_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/document_view_main_code_snippet.txt 2009-02-09 10:00:02 EST (Mon, 09 Feb 2009)
@@ -0,0 +1,9 @@
+int main(int argc, char* argv[])
+{
+ Document doc;
+ TextView v1(doc);
+ HexView v2(doc);
+
+ doc.append(argc == 2 ? argv[1] : "Hello world!");
+ return 0;
+}

Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/hex_view_def_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/hex_view_def_code_snippet.txt 2009-02-09 10:00:02 EST (Mon, 09 Feb 2009)
@@ -0,0 +1,28 @@
+class HexView
+{
+public:
+ HexView(Document& doc): m_document(doc)
+ {
+ m_connection = m_document.connect(boost::bind(&HexView::refresh, this));
+ }
+
+ ~HexView()
+ {
+ m_connection.disconnect();
+ }
+
+ void refresh() 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;
+ }
+private:
+ Document& m_document;
+ boost::signals2::connection m_connection;
+};

Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/text_view_def_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/text_view_def_code_snippet.txt 2009-02-09 10:00:02 EST (Mon, 09 Feb 2009)
@@ -0,0 +1,21 @@
+class TextView
+{
+public:
+ TextView(Document& doc): m_document(doc)
+ {
+ m_connection = m_document.connect(boost::bind(&TextView::refresh, this));
+ }
+
+ ~TextView()
+ {
+ m_connection.disconnect();
+ }
+
+ void refresh() const
+ {
+ std::cout << "TextView: " << m_document.getText() << std::endl;
+ }
+private:
+ Document& m_document;
+ boost::signals2::connection m_connection;
+};

Modified: sandbox/thread_safe_signals/trunk/libs/signals2/doc/tutorial.xml
==============================================================================
--- sandbox/thread_safe_signals/trunk/libs/signals2/doc/tutorial.xml (original)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/tutorial.xml 2009-02-09 10:00:02 EST (Mon, 09 Feb 2009)
@@ -733,130 +733,38 @@
   that it stores a single signal to which all of the views will be
   connected.</para>
 
- <programlisting>class Document
-{
-public:
- typedef boost::signals2::signal&lt;void (bool)&gt; signal_t;
- typedef boost::signals2::connection connection_t;
-
-public:
- Document()
- {}
-
- connection_t connect(const signal_t::slot_type &amp;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&amp; getText() const
- {
- return m_text;
- }
-
-private:
- signal_t m_sig;
- std::string m_text;
-};</programlisting>
-
- <para>Next, we can define a <code>View</code> base class from which
- views can derive. This isn't strictly required, but it keeps the
- Document-View logic separate from the logic itself. Note that the
- constructor just connects the view to the document and the
- destructor disconnects the view.</para>
-
- <programlisting>
-class View
-{
-public:
- View(Document&amp; m)
- : m_document(m)
- {
- m_connection = m_document.connect(boost::bind(&amp;View::refresh, this, _1));
- }
-
- virtual ~View()
- {
- m_document.disconnect(m_connection);
- }
-
- virtual void refresh(bool bExtended) const = 0;
-
-protected:
- Document&amp; m_document;
-
-private:
- Document::connection_t m_connection;
-};
- </programlisting>
-
- <para>Finally, we can begin to define views. The
- following <code>TextView</code> class provides a simple view of the
- document text.</para>
-
- <programlisting>class TextView : public View
-{
-public:
- TextView(Document&amp; doc)
- : View(doc)
- {}
-
- virtual void refresh(bool bExtended) const
- {
- std::cout &lt;&lt; "TextView: " &lt;&lt; m_document.getText() &lt;&lt; std::endl;
- }
-};</programlisting>
+ <programlisting><xi:include href="./snippets/document_def_code_snippet.txt"
+ xmlns:xi="http://www.w3.org/2001/XInclude" parse="text"/></programlisting>
+
+ <para>
+ Next, we can begin to define views. The
+ following <code>TextView</code> class provides a simple view of the
+ document text.
+ </para>
+
+ <programlisting><xi:include href="./snippets/text_view_def_code_snippet.txt"
+ xmlns:xi="http://www.w3.org/2001/XInclude" parse="text"/></programlisting>
 
   <para>Alternatively, we can provide a view of the document
     translated into hex values using the <code>HexView</code>
     view:</para>
 
- <programlisting>class HexView : public View
-{
-public:
- HexView(Document&amp; doc)
- : View(doc)
- {}
-
- virtual void refresh(bool bExtended) const
- {
- const std::string&amp; s = m_document.getText();
-
- std::cout &lt;&lt; "HexView:";
-
- for (std::string::const_iterator it = s.begin(); it != s.end(); ++it)
- std::cout &lt;&lt; ' ' &lt;&lt; std::hex &lt;&lt; static_cast&lt;int&gt;(*it);
-
- std::cout &lt;&lt; std::endl;
- }
-};</programlisting>
-
- <para>To tie the example together, here is a
- simple <code>main</code> function that sets up two views and then
- modifies the document:</para>
-
- <programlisting>int main(int argc, char* argv[])
-{
- Document doc;
- TextView v1(doc);
- HexView v2(doc);
-
- doc.append(argc == 2 ? argv[1] : "Hello world!");
- return 0;
-}</programlisting>
+ <programlisting><xi:include href="./snippets/hex_view_def_code_snippet.txt"
+ xmlns:xi="http://www.w3.org/2001/XInclude" parse="text"/></programlisting>
+
+ <para>
+ To tie the example together, here is a
+ simple <code>main</code> function that sets up two views and then
+ modifies the document:
+ </para>
+
+ <programlisting><xi:include href="./snippets/document_view_main_code_snippet.txt"
+ xmlns:xi="http://www.w3.org/2001/XInclude" parse="text"/></programlisting>
 
   <para>The complete example source, contributed by Keith MacDonald,
     is available in the <link linkend="signals2.examples.document-view">examples</link> section.
+ We also provide variations on the program which employ automatic connection management
+ to disconnect views on their destruction.
   </para>
 </section>
 

Modified: sandbox/thread_safe_signals/trunk/libs/signals2/example/disconnect_and_block.cpp
==============================================================================
--- sandbox/thread_safe_signals/trunk/libs/signals2/example/disconnect_and_block.cpp (original)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/example/disconnect_and_block.cpp 2009-02-09 10:00:02 EST (Mon, 09 Feb 2009)
@@ -25,7 +25,6 @@
 
 //[ disconnect_code_snippet
   boost::signals2::connection c = sig.connect(HelloWorld());
- // c is connected
   std::cout << "c is connected\n";
   sig(); // Prints "Hello, World!"
 
@@ -41,7 +40,6 @@
 
 //[ block_code_snippet
   boost::signals2::connection c = sig.connect(HelloWorld());
- // connection is not blocked
   std::cout << "c is not blocked.\n";
   sig(); // Prints "Hello, World!"
 

Modified: sandbox/thread_safe_signals/trunk/libs/signals2/example/doc_view.cpp
==============================================================================
--- sandbox/thread_safe_signals/trunk/libs/signals2/example/doc_view.cpp (original)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/example/doc_view.cpp 2009-02-09 10:00:02 EST (Mon, 09 Feb 2009)
@@ -13,6 +13,7 @@
 #include <boost/signals2/signal.hpp>
 #include <boost/bind.hpp>
 
+//[ document_def_code_snippet
 class Document
 {
 public:
@@ -44,7 +45,9 @@
     signal_t m_sig;
     std::string m_text;
 };
+//]
 
+//[ text_view_def_code_snippet
 class TextView
 {
 public:
@@ -66,7 +69,9 @@
     Document& m_document;
     boost::signals2::connection m_connection;
 };
+//]
 
+//[ hex_view_def_code_snippet
 class HexView
 {
 public:
@@ -95,7 +100,9 @@
     Document& m_document;
     boost::signals2::connection m_connection;
 };
+//]
 
+//[ document_view_main_code_snippet
 int main(int argc, char* argv[])
 {
     Document doc;
@@ -105,3 +112,4 @@
     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