summaryrefslogtreecommitdiff
path: root/Rx/v2/examples
diff options
context:
space:
mode:
authorKirk Shoop <kirk.shoop@microsoft.com>2015-04-24 16:44:05 -0700
committerKirk Shoop <kirk.shoop@microsoft.com>2015-04-24 16:44:05 -0700
commitb1dd5d5c18553a9f1704a8d7ae64bca5f3b85d10 (patch)
tree047563dffef47c5d5bac11fc17d7a2cd9820ae08 /Rx/v2/examples
parent9b2e07af319fefa1053f327ee07add4cf58dde19 (diff)
downloadRxCpp-b1dd5d5c18553a9f1704a8d7ae64bca5f3b85d10.tar.gz
adding docs in order to update readme.html code
Diffstat (limited to 'Rx/v2/examples')
-rw-r--r--Rx/v2/examples/doxygen/create.cpp51
-rw-r--r--Rx/v2/examples/doxygen/iterate.cpp16
-rw-r--r--Rx/v2/examples/doxygen/range.cpp112
3 files changed, 170 insertions, 9 deletions
diff --git a/Rx/v2/examples/doxygen/create.cpp b/Rx/v2/examples/doxygen/create.cpp
index a9fa347..f8d024f 100644
--- a/Rx/v2/examples/doxygen/create.cpp
+++ b/Rx/v2/examples/doxygen/create.cpp
@@ -5,15 +5,17 @@
SCENARIO("Create sample"){
printf("//! [Create sample]\n");
- auto subscriber = rxcpp::make_subscriber<int>(
- [&](int v){printf("OnNext: %d\n", v);},
- [](){printf("OnCompleted\n");});
- rxcpp::observable<>::create<int>(
- [](const rxcpp::subscriber<int>& s){
+ auto ints = rxcpp::observable<>::create<int>(
+ [](rxcpp::subscriber<int> s){
s.on_next(1);
s.on_next(2);
s.on_completed();
- }).subscribe(subscriber);
+ });
+
+ ints.
+ subscribe(
+ [](int v){printf("OnNext: %d\n", v);},
+ [](){printf("OnCompleted\n");});
printf("//! [Create sample]\n");
}
@@ -31,7 +33,7 @@ SCENARIO("Create bad code"){
printf("OnCompleted\n");
});
rxcpp::observable<>::create<int>(
- [](const rxcpp::subscriber<int>& s){
+ [](rxcpp::subscriber<int> s){
for (int i = 0; i < 5; ++i) {
s.on_next(i);
printf("Just sent: OnNext(%d)\n", i);
@@ -56,10 +58,10 @@ SCENARIO("Create good code"){
printf("OnCompleted\n");
});
rxcpp::observable<>::create<int>(
- [](const rxcpp::subscriber<int>& s){
+ [](rxcpp::subscriber<int> s){
for (int i = 0; i < 5; ++i) {
if (!s.is_subscribed()) // Stop emitting if nobody is listening
- return;
+ break;
s.on_next(i);
printf("Just sent: OnNext(%d)\n", i);
}
@@ -68,3 +70,34 @@ SCENARIO("Create good code"){
}).subscribe(subscriber);
printf("//! [Create good code]\n");
}
+
+SCENARIO("Create great code"){
+ printf("//! [Create great code]\n");
+ auto ints = rxcpp::observable<>::create<int>(
+ [](rxcpp::subscriber<int> s){
+ for (int i = 0; i < 5; ++i) {
+ if (!s.is_subscribed()) // Stop emitting if nobody is listening
+ break;
+ s.on_next(i);
+ printf("Just sent: OnNext(%d)\n", i);
+ }
+ s.on_completed();
+ printf("Just sent: OnCompleted()\n");
+ });
+ ints.
+ take(2).
+ subscribe(
+ [](int v){
+ printf("OnNext: %d\n", v);
+ },
+ [](std::exception_ptr ep){
+ try {std::rethrow_exception(ep);}
+ catch (const std::exception& ex) {
+ printf("OnError: %s\n", ex.what());
+ }
+ },
+ [](){
+ printf("OnCompleted\n");
+ });
+ printf("//! [Create great code]\n");
+}
diff --git a/Rx/v2/examples/doxygen/iterate.cpp b/Rx/v2/examples/doxygen/iterate.cpp
new file mode 100644
index 0000000..332fe59
--- /dev/null
+++ b/Rx/v2/examples/doxygen/iterate.cpp
@@ -0,0 +1,16 @@
+#include "rxcpp/rx.hpp"
+
+#include "rxcpp/rx-test.hpp"
+#include "catch.hpp"
+
+SCENARIO("iterate sample"){
+ printf("//! [iterate sample]\n");
+ std::array< int, 3 > a={{1, 2, 3}};
+ auto values1 = rxcpp::observable<>::iterate(a);
+ values1.
+ subscribe(
+ [](int v){printf("OnNext: %d\n", v);},
+ [](){printf("OnCompleted\n");});
+ printf("//! [iterate sample]\n");
+}
+
diff --git a/Rx/v2/examples/doxygen/range.cpp b/Rx/v2/examples/doxygen/range.cpp
new file mode 100644
index 0000000..af66359
--- /dev/null
+++ b/Rx/v2/examples/doxygen/range.cpp
@@ -0,0 +1,112 @@
+#include "rxcpp/rx.hpp"
+
+#include "rxcpp/rx-test.hpp"
+#include "catch.hpp"
+
+SCENARIO("range sample"){
+ printf("//! [range sample]\n");
+ auto values1 = rxcpp::observable<>::range(1, 5);
+ values1.
+ subscribe(
+ [](int v){printf("OnNext: %d\n", v);},
+ [](){printf("OnCompleted\n");});
+ printf("//! [range sample]\n");
+}
+
+SCENARIO("range concat sample"){
+ printf("//! [range concat sample]\n");
+
+ auto values = rxcpp::observable<>::range(1); // infinite (until overflow) stream of integers
+
+ auto s1 = values.
+ take(3).
+ map([](int prime) { return std::make_tuple("1:", prime);});
+
+ auto s2 = values.
+ take(3).
+ map([](int prime) { return std::make_tuple("2:", prime);});
+
+ s1.
+ concat(s2).
+ subscribe(rxcpp::util::apply_to(
+ [](const char* s, int p) {
+ printf("%s %d\n", s, p);
+ }));
+ printf("//! [range concat sample]\n");
+}
+
+SCENARIO("range merge sample"){
+ printf("//! [range merge sample]\n");
+
+ auto values = rxcpp::observable<>::range(1); // infinite (until overflow) stream of integers
+
+ auto s1 = values.
+ map([](int prime) { return std::make_tuple("1:", prime);});
+
+ auto s2 = values.
+ map([](int prime) { return std::make_tuple("2:", prime);});
+
+ s1.
+ merge(s2).
+ take(6).
+ as_blocking().
+ subscribe(rxcpp::util::apply_to(
+ [](const char* s, int p) {
+ printf("%s %d\n", s, p);
+ }));
+ printf("//! [range merge sample]\n");
+}
+
+SCENARIO("threaded range concat sample"){
+ printf("//! [threaded range concat sample]\n");
+ auto threads = rxcpp::observe_on_event_loop();
+
+ auto values = rxcpp::observable<>::range(1); // infinite (until overflow) stream of integers
+
+ auto s1 = values.
+ subscribe_on(threads).
+ take(3).
+ map([](int prime) { std::this_thread::yield(); return std::make_tuple("1:", prime);});
+
+ auto s2 = values.
+ subscribe_on(threads).
+ take(3).
+ map([](int prime) { std::this_thread::yield(); return std::make_tuple("2:", prime);});
+
+ s1.
+ concat(s2).
+ observe_on(threads).
+ as_blocking().
+ subscribe(rxcpp::util::apply_to(
+ [](const char* s, int p) {
+ printf("%s %d\n", s, p);
+ }));
+ printf("//! [threaded range concat sample]\n");
+}
+
+SCENARIO("threaded range merge sample"){
+ printf("//! [threaded range merge sample]\n");
+ auto threads = rxcpp::observe_on_event_loop();
+
+ auto values = rxcpp::observable<>::range(1); // infinite (until overflow) stream of integers
+
+ auto s1 = values.
+ subscribe_on(threads).
+ map([](int prime) { std::this_thread::yield(); return std::make_tuple("1:", prime);});
+
+ auto s2 = values.
+ subscribe_on(threads).
+ map([](int prime) { std::this_thread::yield(); return std::make_tuple("2:", prime);});
+
+ s1.
+ merge(s2).
+ take(6).
+ observe_on(threads).
+ as_blocking().
+ subscribe(rxcpp::util::apply_to(
+ [](const char* s, int p) {
+ printf("%s %d\n", s, p);
+ }));
+ printf("//! [threaded range merge sample]\n");
+}
+