|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r51123 - sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets
From: fmhess_at_[hidden]
Date: 2009-02-08 22:07:40
Author: fmhess
Date: 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
New Revision: 51123
URL: http://svn.boost.org/trac/boost/changeset/51123
Log:
Added new snippets extracted from examples.
Added:
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/block_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/button_declaration_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/custom_combiners_aggregate_values_def_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/custom_combiners_aggregate_values_usage_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/custom_combiners_maximum_def_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/custom_combiners_maximum_usage_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/disconnect_by_slot_def_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/disconnect_by_slot_usage_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/disconnect_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/passing_slots_defs_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/passing_slots_usage_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/scoped_connection_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/signal_return_value_main_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/signal_return_value_slot_defs_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/slot_arguments_main_code_snippet.txt (contents, props changed)
sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/slot_arguments_slot_defs_code_snippet.txt (contents, props changed)
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/block_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/block_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,12 @@
+ boost::signals2::connection c = sig.connect(HelloWorld());
+ // connection is not blocked
+ std::cout << "c is not blocked.\n";
+ sig(); // Prints "Hello, World!"
+
+ {
+ boost::signals2::shared_connection_block block(c); // block the slot
+ std::cout << "c is blocked.\n";
+ sig(); // No output: the slot is blocked
+ } // shared_connection_block going out of scope unblocks the slot
+ std::cout << "c is not blocked.\n";
+ sig(); // Prints "Hello, World!"}
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/button_declaration_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/button_declaration_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,14 @@
+// a pretend GUI button
+class Button
+{
+ typedef boost::signals2::signal<void (int x, int y)> OnClick;
+public:
+ typedef OnClick::slot_type OnClickSlotType;
+ // forward slots through Button interface to its private signal
+ boost::signals2::connection doOnClick(const OnClickSlotType & slot);
+
+ // simulate user clicking on GUI button at coordinates 52, 38
+ void simulateClick();
+private:
+ OnClick onClick;
+};
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/custom_combiners_aggregate_values_def_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/custom_combiners_aggregate_values_def_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,19 @@
+// aggregate_values is a combiner which places all the values returned
+// from slots into a container
+template<typename Container>
+struct aggregate_values
+{
+ typedef Container result_type;
+
+ template<typename InputIterator>
+ Container operator()(InputIterator first, InputIterator last) const
+ {
+ Container values;
+
+ while(first != last) {
+ values.push_back(*first);
+ ++first;
+ }
+ return values;
+ }
+};
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/custom_combiners_aggregate_values_usage_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/custom_combiners_aggregate_values_usage_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,10 @@
+ sig.connect("ient);
+ sig.connect(&product);
+ sig.connect(&sum);
+ sig.connect(&difference);
+
+ std::vector<float> results = sig(5, 3);
+ std::cout << "aggregate values: ";
+ std::copy(results.begin(), results.end(),
+ std::ostream_iterator<float>(std::cout, " "));
+ std::cout << "\n";
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/custom_combiners_maximum_def_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/custom_combiners_maximum_def_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,22 @@
+// combiner which returns the maximum value returned by all slots
+template<typename T>
+struct maximum
+{
+ typedef T result_type;
+
+ template<typename InputIterator>
+ T operator()(InputIterator first, InputIterator last) const
+ {
+ // If there are no slots to call, just return the
+ // default-constructed value
+ if(first == last ) return T();
+ T max_value = *first++;
+ while (first != last) {
+ if (max_value < *first)
+ max_value = *first;
+ ++first;
+ }
+
+ return max_value;
+ }
+};
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/custom_combiners_maximum_usage_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/custom_combiners_maximum_usage_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,8 @@
+ sig.connect(&product);
+ sig.connect("ient);
+ sig.connect(&sum);
+ sig.connect(&difference);
+
+ // Outputs the maximum value returned by the connected slots, in this case
+ // 15 from the product function.
+ std::cout << "maximum: " << sig(5, 3) << std::endl;
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/disconnect_by_slot_def_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/disconnect_by_slot_def_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,2 @@
+void foo() { std::cout << "foo"; }
+void bar() { std::cout << "bar\n"; }
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/disconnect_by_slot_usage_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/disconnect_by_slot_usage_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,7 @@
+ sig.connect(&foo);
+ sig.connect(&bar);
+ sig();
+
+ // disconnects foo, but not bar
+ sig.disconnect(&foo);
+ sig();
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/disconnect_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/disconnect_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,8 @@
+ boost::signals2::connection c = sig.connect(HelloWorld());
+ // c is connected
+ std::cout << "c is connected\n";
+ sig(); // Prints "Hello, World!"
+
+ c.disconnect(); // Disconnect the HelloWorld object
+ std::cout << "c is disconnected\n";
+ sig(); // Does nothing: there are no connected slots
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/passing_slots_defs_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/passing_slots_defs_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,29 @@
+// a pretend GUI button
+class Button
+{
+ typedef boost::signals2::signal<void (int x, int y)> OnClick;
+public:
+ typedef OnClick::slot_type OnClickSlotType;
+ // forward slots through Button interface to its private signal
+ boost::signals2::connection doOnClick(const OnClickSlotType & slot);
+
+ // simulate user clicking on GUI button at coordinates 52, 38
+ void simulateClick();
+private:
+ OnClick onClick;
+};
+
+boost::signals2::connection Button::doOnClick(const OnClickSlotType & slot)
+{
+ return onClick.connect(slot);
+}
+
+void Button::simulateClick()
+{
+ onClick(52, 38);
+}
+
+void printCoordinates(long x, long y)
+{
+ std::cout << "(" << x << ", " << y << ")\n";
+}
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/passing_slots_usage_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/passing_slots_usage_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,3 @@
+ Button button;
+ button.doOnClick(&printCoordinates);
+ button.simulateClick();
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/scoped_connection_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/scoped_connection_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,6 @@
+ {
+ boost::signals2::scoped_connection c(sig.connect(ShortLived()));
+ sig(); // will call ShortLived function object
+ } // scoped_connection goes out of scope and disconnects
+
+ sig(); // ShortLived function object no longer connected to sig
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/signal_return_value_main_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/signal_return_value_main_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,9 @@
+ sig.connect(&product);
+ sig.connect("ient);
+ sig.connect(&sum);
+ sig.connect(&difference);
+
+ // The default combiner returns a boost::optional containing the return
+ // value of the last slot in the slot list, in this case the
+ // difference function.
+ std::cout << *sig(5, 3) << std::endl;
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/signal_return_value_slot_defs_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/signal_return_value_slot_defs_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,4 @@
+float product(float x, float y) { return x * y; }
+float quotient(float x, float y) { return x / y; }
+float sum(float x, float y) { return x + y; }
+float difference(float x, float y) { return x - y; }
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/slot_arguments_main_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/slot_arguments_main_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,9 @@
+ boost::signals2::signal<void (float, float)> sig;
+
+ sig.connect(&print_args);
+ sig.connect(&print_sum);
+ sig.connect(&print_product);
+ sig.connect(&print_difference);
+ sig.connect(&print_quotient);
+
+ sig(5., 3.);
Added: sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/slot_arguments_slot_defs_code_snippet.txt
==============================================================================
--- (empty file)
+++ sandbox/thread_safe_signals/trunk/libs/signals2/doc/snippets/slot_arguments_slot_defs_code_snippet.txt 2009-02-08 22:07:39 EST (Sun, 08 Feb 2009)
@@ -0,0 +1,24 @@
+void print_args(float x, float y)
+{
+ std::cout << "The arguments are " << x << " and " << y << std::endl;
+}
+
+void print_sum(float x, float y)
+{
+ std::cout << "The sum is " << x + y << std::endl;
+}
+
+void print_product(float x, float y)
+{
+ std::cout << "The product is " << x * y << std::endl;
+}
+
+void print_difference(float x, float y)
+{
+ std::cout << "The difference is " << x - y << std::endl;
+}
+
+void print_quotient(float x, float y)
+{
+ std::cout << "The quotient is " << x / y << std::endl;
+}
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