Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r50973 - sandbox/thread_safe_signals/trunk/libs/signals2/example
From: fmhess_at_[hidden]
Date: 2009-02-02 14:48:55


Author: fmhess
Date: 2009-02-02 14:48:54 EST (Mon, 02 Feb 2009)
New Revision: 50973
URL: http://svn.boost.org/trac/boost/changeset/50973

Log:
Added new examples showing the use of postconstructors, predestructors,
and automatic connection management.

Added:
   sandbox/thread_safe_signals/trunk/libs/signals2/example/doc_view_acm.cpp (contents, props changed)
   sandbox/thread_safe_signals/trunk/libs/signals2/example/doc_view_acm_deconstruct.cpp (contents, props changed)
   sandbox/thread_safe_signals/trunk/libs/signals2/example/postconstructor_ex1.cpp (contents, props changed)
   sandbox/thread_safe_signals/trunk/libs/signals2/example/postconstructor_ex2.cpp (contents, props changed)
   sandbox/thread_safe_signals/trunk/libs/signals2/example/predestructor_example.cpp (contents, props changed)

Added: sandbox/thread_safe_signals/trunk/libs/signals2/example/doc_view_acm.cpp
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/example/doc_view_acm.cpp 2009-02-02 14:48:54 EST (Mon, 02 Feb 2009)
@@ -0,0 +1,125 @@
+// Document/View sample for Boost.Signals.
+// Expands on doc_view.cpp example by using automatic
+// connection management.
+//
+// Copyright Keith MacDonald 2005.
+// Copyright Frank Mori Hess 2009.
+//
+// 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/bind.hpp>
+#include <boost/signals2/signal.hpp>
+#include <boost/shared_ptr.hpp>
+
+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;
+};
+
+class TextView
+{
+public:
+ // static factory function that sets up automatic connection tracking
+ static boost::shared_ptr<TextView> create(Document& doc)
+ {
+ boost::shared_ptr<TextView> new_view(new TextView(doc));
+ {
+ typedef Document::signal_t::slot_type slot_type;
+ slot_type myslot(&TextView::refresh, new_view.get());
+ doc.connect(myslot.track(new_view));
+ }
+ return new_view;
+ }
+
+ void refresh() const
+ {
+ std::cout << "TextView: " << m_document.getText() << std::endl;
+ }
+private:
+ // private constructor to force use of static factory function
+ TextView(Document &doc): m_document(doc)
+ {}
+
+ Document& m_document;
+};
+
+class HexView
+{
+public:
+ // static factory function that sets up automatic connection tracking
+ static boost::shared_ptr<HexView> create(Document& doc)
+ {
+ boost::shared_ptr<HexView> new_view(new HexView(doc));
+ {
+ typedef Document::signal_t::slot_type slot_type;
+ slot_type myslot(&HexView::refresh, new_view.get());
+ doc.connect(myslot.track(new_view));
+ }
+ return new_view;
+ }
+
+ 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:
+ // private constructor to force use of static factory function
+ HexView(Document& doc): m_document(doc)
+ {}
+
+ Document& m_document;
+};
+
+int main(int argc, char* argv[])
+{
+ Document doc;
+ boost::shared_ptr<TextView> v1 = TextView::create(doc);
+ boost::shared_ptr<HexView> v2 = HexView::create(doc);
+
+ doc.append(argc >= 2 ? argv[1] : "Hello world!");
+
+ v2.reset(); // destroy the HexView, automatically disconnecting
+ doc.append(" HexView should no longer be connected.");
+
+ return 0;
+}

Added: sandbox/thread_safe_signals/trunk/libs/signals2/example/doc_view_acm_deconstruct.cpp
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/example/doc_view_acm_deconstruct.cpp 2009-02-02 14:48:54 EST (Mon, 02 Feb 2009)
@@ -0,0 +1,135 @@
+// Document/View sample for Boost.Signals.
+// Expands on doc_view_acm.cpp example by using boost::signals2::deconstruct
+// as a post-constructing factory function.
+//
+// Copyright Keith MacDonald 2005.
+// Copyright Frank Mori Hess 2009.
+//
+// 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/bind.hpp>
+#include <boost/ref.hpp>
+#include <boost/signals2/deconstruct.hpp>
+#include <boost/signals2/signal.hpp>
+#include <boost/shared_ptr.hpp>
+
+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;
+};
+
+class TextView
+{
+public:
+ /* This adl_postconstruct function will be found
+ via argument-dependent lookup when using boost::signals2::deconstruct. */
+ template<typename T>
+ friend void adl_postconstruct(const boost::shared_ptr<T> &view_sp, TextView *view, Document& doc)
+ {
+ view->m_document = &doc;
+ {
+ typedef Document::signal_t::slot_type slot_type;
+ slot_type myslot(&TextView::refresh, view);
+ doc.connect(myslot.track(view_sp));
+ }
+ }
+
+ void refresh() const
+ {
+ std::cout << "TextView: " << m_document->getText() << std::endl;
+ }
+private:
+ // give boost::signals2::deconstruct access to private constructor
+ friend class boost::signals2::deconstruct_access;
+ // private constructor to force use of deconstruct
+ TextView()
+ {}
+
+ Document* m_document;
+};
+
+class HexView
+{
+public:
+ /* This adl_postconstruct function will be found
+ via argument-dependent lookup when using boost::signals2::deconstruct. */
+ template<typename T>
+ friend void adl_postconstruct(const boost::shared_ptr<T> &view_sp, HexView *view, Document& doc)
+ {
+ view->m_document = &doc;
+ {
+ typedef Document::signal_t::slot_type slot_type;
+ slot_type myslot(&HexView::refresh, view);
+ doc.connect(myslot.track(view_sp));
+ }
+ }
+
+ 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:
+ // give boost::signals2::deconstruct access to private constructor
+ friend class boost::signals2::deconstruct_access;
+ // private constructor to force use of deconstruct
+ HexView()
+ {}
+
+ Document* m_document;
+};
+
+namespace bs2 = boost::signals2;
+
+int main(int argc, char* argv[])
+{
+ Document doc;
+ boost::shared_ptr<TextView> v1 = bs2::deconstruct<TextView>().postconstruct(boost::ref(doc));
+ boost::shared_ptr<HexView> v2 = bs2::deconstruct<HexView>().postconstruct(boost::ref(doc));
+
+ doc.append(argc >= 2 ? argv[1] : "Hello world!");
+
+ v2.reset(); // destroy the HexView, automatically disconnecting
+ doc.append(" HexView should no longer be connected.");
+
+ return 0;
+}

Added: sandbox/thread_safe_signals/trunk/libs/signals2/example/postconstructor_ex1.cpp
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/example/postconstructor_ex1.cpp 2009-02-02 14:48:54 EST (Mon, 02 Feb 2009)
@@ -0,0 +1,45 @@
+// Minimal example of defining a postconstructor for a class which
+// uses boost::signals2::deconstruct as its factory function.
+//
+// Copyright Frank Mori Hess 2009.
+
+// 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 <boost/shared_ptr.hpp>
+#include <boost/signals2/deconstruct.hpp>
+#include <iostream>
+
+namespace bs2 = boost::signals2;
+
+namespace mynamespace
+{
+ class X
+ {
+ public:
+ /* This adl_postconstruct function will be found
+ via argument-dependent lookup when using boost::signals2::deconstruct. */
+ template<typename T> friend
+ void adl_postconstruct(const boost::shared_ptr<T> &, X *)
+ {
+ std::cout << "world!" << std::endl;
+ }
+ private:
+ friend class bs2::deconstruct_access; // give boost::signals2::deconstruct access to private constructor
+ // private constructor forces use of boost::signals2::deconstruct to create objects.
+ X()
+ {
+ std::cout << "Hello, ";
+ }
+ };
+}
+
+int main()
+{
+ // adl_postconstruct will be called during implicit conversion of return value to shared_ptr
+ boost::shared_ptr<mynamespace::X> x = bs2::deconstruct<mynamespace::X>();
+ return 0;
+}

Added: sandbox/thread_safe_signals/trunk/libs/signals2/example/postconstructor_ex2.cpp
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/example/postconstructor_ex2.cpp 2009-02-02 14:48:54 EST (Mon, 02 Feb 2009)
@@ -0,0 +1,55 @@
+// An example of defining a postconstructor for a class which
+// uses boost::signals2::deconstruct as its factory function.
+// This example expands on the basic postconstructor_ex1.cpp example
+// by passing arguments to the constructor and postconstructor.
+//
+// Copyright Frank Mori Hess 2009.
+
+// 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 <boost/shared_ptr.hpp>
+#include <boost/signals2/deconstruct.hpp>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+namespace bs2 = boost::signals2;
+
+namespace mynamespace
+{
+ class Y
+ {
+ public:
+ /* This adl_postconstruct function will be found
+ via argument-dependent lookup when using boost::signals2::deconstruct. */
+ template<typename T> friend
+ void adl_postconstruct(const boost::shared_ptr<T> &, Y *y, const std::string &text)
+ {
+ y->_text_stream << text;
+ }
+ void print() const
+ {
+ std::cout << _text_stream.str() << std::endl;
+ }
+ private:
+ friend class bs2::deconstruct_access; // give boost::signals2::deconstruct access to private constructor
+ // private constructor forces use of boost::signals2::deconstruct to create objects.
+ Y(const std::string &text)
+ {
+ _text_stream << text;
+ }
+
+ std::ostringstream _text_stream;
+ };
+}
+
+int main()
+{
+ boost::shared_ptr<mynamespace::Y> y = bs2::deconstruct<mynamespace::Y>("Hello, ").postconstruct("world!");
+ y->print();
+ return 0;
+}

Added: sandbox/thread_safe_signals/trunk/libs/signals2/example/predestructor_example.cpp
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/example/predestructor_example.cpp 2009-02-02 14:48:54 EST (Mon, 02 Feb 2009)
@@ -0,0 +1,52 @@
+// Minimal example of defining a predestructor for a class which
+// uses boost::signals2::deconstruct as its factory function.
+//
+// Copyright Frank Mori Hess 2009.
+
+// 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 <boost/shared_ptr.hpp>
+#include <boost/signals2/deconstruct.hpp>
+#include <iostream>
+
+namespace bs2 = boost::signals2;
+
+namespace mynamespace
+{
+ class X
+ {
+ public:
+ ~X()
+ {
+ std::cout << "cruel world!" << std::endl;
+ }
+ /* This adl_predestruct friend function will be found by
+ via argument-dependent lookup when using boost::signals2::deconstruct. */
+ friend void adl_predestruct(X *)
+ {
+ std::cout << "Goodbye, ";
+ }
+ /* boost::signals2::deconstruct always requires an adl_postconstruct function
+ which can be found via argument-dependent, so we define one which does nothing. */
+ template<typename T> friend
+ void adl_postconstruct(const boost::shared_ptr<T> &, X *)
+ {}
+ private:
+ friend class bs2::deconstruct_access; // give boost::signals2::deconstruct access to private constructor
+ // private constructor forces use of boost::signals2::deconstruct to create objects.
+ X()
+ {}
+ };
+}
+
+int main()
+{
+ {
+ boost::shared_ptr<mynamespace::X> x = bs2::deconstruct<mynamespace::X>();
+ }
+ 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