diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/boost/libs/signals2/example | |
parent | Initial commit. (diff) | |
download | ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.tar.xz ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/signals2/example')
17 files changed, 1159 insertions, 0 deletions
diff --git a/src/boost/libs/signals2/example/CMakeLists.txt b/src/boost/libs/signals2/example/CMakeLists.txt new file mode 100644 index 000000000..3b42e58d8 --- /dev/null +++ b/src/boost/libs/signals2/example/CMakeLists.txt @@ -0,0 +1,24 @@ +# Copyright 2019 Mike Dev +# Distributed under the Boost Software License, Version 1.0. +# See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt +# +# NOTE: CMake support for Boost.Signals2 is currently experimental at best +# and the interface is likely to change in the future + +file( GLOB example_src_files *.cpp ) + +find_package(Threads REQUIRED) + +foreach( file IN LISTS example_src_files ) + + get_filename_component( example_name ${file} NAME_WE ) + + add_executable( boost_signals2_example_${example_name} ${file} ) + target_link_libraries( boost_signals2_example_${example_name} + PUBLIC + Boost::signals2 + Boost::io + Threads::Threads + ) + +endforeach() diff --git a/src/boost/libs/signals2/example/Jamfile.v2 b/src/boost/libs/signals2/example/Jamfile.v2 new file mode 100644 index 000000000..079ad4432 --- /dev/null +++ b/src/boost/libs/signals2/example/Jamfile.v2 @@ -0,0 +1,42 @@ +# Boost.Signals2 example programs + +# Copyright Michael Caisse 2010 +# 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 + +project : requirements <implicit-dependency>/boost//headers ; + +exe hello_world_slot : hello_world_slot.cpp ; + +exe hello_world_multi_slot : hello_world_multi_slot.cpp ; + +exe ordering_slots : ordering_slots.cpp ; + +exe passing_slots : passing_slots.cpp ; + +exe extended_slot : extended_slot.cpp ; + +exe custom_combiners : custom_combiners.cpp ; + +exe disconnect_and_block : disconnect_and_block.cpp ; + +exe doc_view : doc_view.cpp ; + +exe doc_view_acm : doc_view_acm.cpp ; + +exe doc_view_acm_deconstruct : doc_view_acm_deconstruct.cpp ; + +exe postconstructor_ex1 : postconstructor_ex1.cpp ; + +exe postconstructor_ex2 : postconstructor_ex2.cpp ; + +exe predestructor_example : predestructor_example.cpp ; + +exe signal_return_value : signal_return_value.cpp ; + +exe slot_arguments : slot_arguments.cpp ; + diff --git a/src/boost/libs/signals2/example/custom_combiners.cpp b/src/boost/libs/signals2/example/custom_combiners.cpp new file mode 100644 index 000000000..72e43b760 --- /dev/null +++ b/src/boost/libs/signals2/example/custom_combiners.cpp @@ -0,0 +1,109 @@ +// Example program showing signals with custom combiners. +// +// Copyright Douglas Gregor 2001-2004. +// 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 <boost/signals2/signal.hpp> +#include <vector> + +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; } + +//[ custom_combiners_maximum_def_code_snippet +// 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; + } +}; +//] + +void maximum_combiner_example() +{ + // signal which uses our custom "maximum" combiner + boost::signals2::signal<float (float x, float y), maximum<float> > sig; + +//[ custom_combiners_maximum_usage_code_snippet + 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; +//] +} + +//[ custom_combiners_aggregate_values_def_code_snippet +// 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; + } +}; +//] + +void aggregate_values_example() +{ + // signal which uses aggregate_values as its combiner + boost::signals2::signal<float (float, float), + aggregate_values<std::vector<float> > > sig; + +//[ custom_combiners_aggregate_values_usage_code_snippet + 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"; +//] +} + +int main() +{ + maximum_combiner_example(); + aggregate_values_example(); +} diff --git a/src/boost/libs/signals2/example/disconnect_and_block.cpp b/src/boost/libs/signals2/example/disconnect_and_block.cpp new file mode 100644 index 000000000..4478be005 --- /dev/null +++ b/src/boost/libs/signals2/example/disconnect_and_block.cpp @@ -0,0 +1,114 @@ +// Various simple examples involving disconnecting and blocking slots. +// Copyright Douglas Gregor 2001-2004. +// 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 <boost/signals2/signal.hpp> +#include <boost/signals2/shared_connection_block.hpp> + +struct HelloWorld +{ + void operator()() const + { + std::cout << "Hello, World!" << std::endl; + } +}; + +void disconnect_example() +{ + boost::signals2::signal<void ()> sig; + +//[ disconnect_code_snippet + boost::signals2::connection c = sig.connect(HelloWorld()); + 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 +//] +} + +void block_example() +{ + boost::signals2::signal<void ()> sig; + +//[ block_code_snippet + boost::signals2::connection c = sig.connect(HelloWorld()); + 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!"} +//] +} + +struct ShortLived +{ + void operator()() const + { + std::cout << "Life is short!" << std::endl; + } +}; + +void scoped_connection_example() +{ + boost::signals2::signal<void ()> sig; + +//[ scoped_connection_code_snippet + { + 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 +//] +} + +//[ disconnect_by_slot_def_code_snippet +void foo() { std::cout << "foo"; } +void bar() { std::cout << "bar\n"; } +//] + +void disconnect_by_slot_example() +{ + boost::signals2::signal<void()> sig; + +//[ disconnect_by_slot_usage_code_snippet + sig.connect(&foo); + sig.connect(&bar); + sig(); + + // disconnects foo, but not bar + sig.disconnect(&foo); + sig(); +//] +} + +int main() +{ + std::cout << "Disconnect example:\n"; + disconnect_example(); + + std::cout << "\nBlock example:\n"; + block_example(); + + std::cout << "\nScoped connection example:\n"; + scoped_connection_example(); + + std::cout << "\nDisconnect by slot example:\n"; + disconnect_by_slot_example(); + + return 0; +} diff --git a/src/boost/libs/signals2/example/doc_view.cpp b/src/boost/libs/signals2/example/doc_view.cpp new file mode 100644 index 000000000..7ff1990e9 --- /dev/null +++ b/src/boost/libs/signals2/example/doc_view.cpp @@ -0,0 +1,115 @@ +// Document/View sample for Boost.Signals +// 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/signals2/signal.hpp> +#include <boost/bind.hpp> + +//[ document_def_code_snippet +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; +}; +//] + +//[ text_view_def_code_snippet +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; +}; +//] + +//[ hex_view_def_code_snippet +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; +}; +//] + +//[ document_view_main_code_snippet +int main(int argc, char* argv[]) +{ + Document doc; + TextView v1(doc); + HexView v2(doc); + + doc.append(argc == 2 ? argv[1] : "Hello world!"); + return 0; +} +//] diff --git a/src/boost/libs/signals2/example/doc_view_acm.cpp b/src/boost/libs/signals2/example/doc_view_acm.cpp new file mode 100644 index 000000000..ffc790826 --- /dev/null +++ b/src/boost/libs/signals2/example/doc_view_acm.cpp @@ -0,0 +1,127 @@ +// Document/View sample for Boost.Signals2. +// 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/io/ios_state.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 + { + boost::io::ios_flags_saver ifs(std::cout); + 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; +} diff --git a/src/boost/libs/signals2/example/doc_view_acm_deconstruct.cpp b/src/boost/libs/signals2/example/doc_view_acm_deconstruct.cpp new file mode 100644 index 000000000..1855c7806 --- /dev/null +++ b/src/boost/libs/signals2/example/doc_view_acm_deconstruct.cpp @@ -0,0 +1,135 @@ +// Document/View sample for Boost.Signals2. +// 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() : m_document(0) + {} + + 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(): m_document(0) + {} + + 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; +} diff --git a/src/boost/libs/signals2/example/extended_slot.cpp b/src/boost/libs/signals2/example/extended_slot.cpp new file mode 100644 index 000000000..40699e53c --- /dev/null +++ b/src/boost/libs/signals2/example/extended_slot.cpp @@ -0,0 +1,42 @@ +// Example program for connecting an extended slot, +// using a signal's connect_extended and extended_slot_type. +// +// 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/signals2/signal.hpp> +#include <iostream> +#include <string> + +namespace bs2 = boost::signals2; + +void single_shot_slot(const bs2::connection &conn, const std::string &message) +{ + conn.disconnect(); + std::cout << message; +} + +int main() +{ + typedef bs2::signal<void (void)> sig_type; + sig_type sig; + { + sig_type::extended_slot_type hello(&single_shot_slot, _1, "Hello"); + sig.connect_extended(hello); + } + sig(); // prints "Hello" + { + sig_type::extended_slot_type world(&single_shot_slot, _1, ", World!\n"); + sig.connect_extended(world); + } + sig(); // only prints ", World!\n" since hello slot has disconnected itself + sig(); // prints nothing, world slot has disconnected itself + + return 0; +} + diff --git a/src/boost/libs/signals2/example/hello_world_multi_slot.cpp b/src/boost/libs/signals2/example/hello_world_multi_slot.cpp new file mode 100644 index 000000000..f47835148 --- /dev/null +++ b/src/boost/libs/signals2/example/hello_world_multi_slot.cpp @@ -0,0 +1,47 @@ +// Multiple slot hello world example for Boost.Signals2 +// Copyright Douglas Gregor 2001-2004. +// 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 <boost/signals2/signal.hpp> + +//[ hello_def_code_snippet +struct Hello +{ + void operator()() const + { + std::cout << "Hello"; + } +}; +//] + +//[ world_def_code_snippet +struct World +{ + void operator()() const + { + std::cout << ", World!" << std::endl; + } +}; +//] + +int main() +{ +//[ hello_world_multi_code_snippet + boost::signals2::signal<void ()> sig; + + sig.connect(Hello()); + sig.connect(World()); + + sig(); +//] + + return 0; +} + diff --git a/src/boost/libs/signals2/example/hello_world_slot.cpp b/src/boost/libs/signals2/example/hello_world_slot.cpp new file mode 100644 index 000000000..18e3c6a38 --- /dev/null +++ b/src/boost/libs/signals2/example/hello_world_slot.cpp @@ -0,0 +1,40 @@ +// Beginner hello world example for Boost.Signals2 +// Copyright Douglas Gregor 2001-2004. +// 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 <boost/signals2/signal.hpp> + +//[ hello_world_def_code_snippet +struct HelloWorld +{ + void operator()() const + { + std::cout << "Hello, World!" << std::endl; + } +}; +//] + +int main() +{ +//[ hello_world_single_code_snippet + // Signal with no arguments and a void return value + boost::signals2::signal<void ()> sig; + + // Connect a HelloWorld slot + HelloWorld hello; + sig.connect(hello); + + // Call all of the slots + sig(); +//] + + return 0; +} + diff --git a/src/boost/libs/signals2/example/ordering_slots.cpp b/src/boost/libs/signals2/example/ordering_slots.cpp new file mode 100644 index 000000000..f69bc182c --- /dev/null +++ b/src/boost/libs/signals2/example/ordering_slots.cpp @@ -0,0 +1,62 @@ +// Ordered slots hello world example for Boost.Signals2 +// Copyright Douglas Gregor 2001-2004. +// 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 <boost/signals2/signal.hpp> + +struct Hello +{ + void operator()() const + { + std::cout << "Hello"; + } +}; + +struct World +{ + void operator()() const + { + std::cout << ", World!" << std::endl; + } +}; + +//[ good_morning_def_code_snippet +struct GoodMorning +{ + void operator()() const + { + std::cout << "... and good morning!" << std::endl; + } +}; +//] + +int main() +{ +//[ hello_world_ordered_code_snippet + boost::signals2::signal<void ()> sig; + + sig.connect(1, World()); // connect with group 1 + sig.connect(0, Hello()); // connect with group 0 +//] + +//[ hello_world_ordered_invoke_code_snippet + // by default slots are connected at the end of the slot list + sig.connect(GoodMorning()); + + // slots are invoked this order: + // 1) ungrouped slots connected with boost::signals2::at_front + // 2) grouped slots according to ordering of their groups + // 3) ungrouped slots connected with boost::signals2::at_back + sig(); +//] + + return 0; +} + diff --git a/src/boost/libs/signals2/example/passing_slots.cpp b/src/boost/libs/signals2/example/passing_slots.cpp new file mode 100644 index 000000000..d2c098cb3 --- /dev/null +++ b/src/boost/libs/signals2/example/passing_slots.cpp @@ -0,0 +1,55 @@ +// Example program showing passing of slots through an interface. +// +// Copyright Douglas Gregor 2001-2004. +// 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 <boost/signals2/signal.hpp> + +//[ passing_slots_defs_code_snippet +// 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"; +} +//] + +int main() +{ +//[ passing_slots_usage_code_snippet + Button button; + button.doOnClick(&printCoordinates); + button.simulateClick(); +//] + return 0; +} diff --git a/src/boost/libs/signals2/example/postconstructor_ex1.cpp b/src/boost/libs/signals2/example/postconstructor_ex1.cpp new file mode 100644 index 000000000..89f684bba --- /dev/null +++ b/src/boost/libs/signals2/example/postconstructor_ex1.cpp @@ -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; +} diff --git a/src/boost/libs/signals2/example/postconstructor_ex2.cpp b/src/boost/libs/signals2/example/postconstructor_ex2.cpp new file mode 100644 index 000000000..fdd1bd4ec --- /dev/null +++ b/src/boost/libs/signals2/example/postconstructor_ex2.cpp @@ -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; +} diff --git a/src/boost/libs/signals2/example/predestructor_example.cpp b/src/boost/libs/signals2/example/predestructor_example.cpp new file mode 100644 index 000000000..6f10195e5 --- /dev/null +++ b/src/boost/libs/signals2/example/predestructor_example.cpp @@ -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; +} diff --git a/src/boost/libs/signals2/example/signal_return_value.cpp b/src/boost/libs/signals2/example/signal_return_value.cpp new file mode 100644 index 000000000..163f29abf --- /dev/null +++ b/src/boost/libs/signals2/example/signal_return_value.cpp @@ -0,0 +1,38 @@ +// Example program for returning a value from slots to signal invocation. +// +// Copyright Douglas Gregor 2001-2004. +// 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 <boost/signals2/signal.hpp> + +//[ signal_return_value_slot_defs_code_snippet +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; } +//] + +int main() +{ + boost::signals2::signal<float (float x, float y)> sig; + +//[ signal_return_value_main_code_snippet + 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; +//] +} + diff --git a/src/boost/libs/signals2/example/slot_arguments.cpp b/src/boost/libs/signals2/example/slot_arguments.cpp new file mode 100644 index 000000000..86c49cb6f --- /dev/null +++ b/src/boost/libs/signals2/example/slot_arguments.cpp @@ -0,0 +1,57 @@ +// Example program for passing arguments from signal invocations to slots. +// +// Copyright Douglas Gregor 2001-2004. +// 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 <boost/signals2/signal.hpp> + +//[ slot_arguments_slot_defs_code_snippet +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; +} +//] + +int main() +{ +//[ slot_arguments_main_code_snippet + 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.); +//] + return 0; +} + |