diff options
author | Kirk Shoop <kirk.shoop@microsoft.com> | 2015-06-03 13:07:59 -0700 |
---|---|---|
committer | Kirk Shoop <kirk.shoop@microsoft.com> | 2015-06-03 13:07:59 -0700 |
commit | 80848009b743645173950ff0249c69bd193020c9 (patch) | |
tree | ab55a319389831492616a2eeb53f5b6273b0be75 /Rx/v2/examples | |
parent | 9c21b7c3b1250ccab0dc543e1eb6de5c9cfd4ced (diff) | |
download | RxCpp-80848009b743645173950ff0249c69bd193020c9.tar.gz |
add stop example
Diffstat (limited to 'Rx/v2/examples')
-rw-r--r-- | Rx/v2/examples/stop/CMakeLists.txt | 34 | ||||
-rw-r--r-- | Rx/v2/examples/stop/main.cpp | 54 |
2 files changed, 88 insertions, 0 deletions
diff --git a/Rx/v2/examples/stop/CMakeLists.txt b/Rx/v2/examples/stop/CMakeLists.txt new file mode 100644 index 0000000..b1791ad --- /dev/null +++ b/Rx/v2/examples/stop/CMakeLists.txt @@ -0,0 +1,34 @@ +cmake_minimum_required(VERSION 3.2 FATAL_ERROR) + +get_filename_component(SAMPLE_PROJECT "${CMAKE_CURRENT_SOURCE_DIR}" NAME) + +project(${SAMPLE_PROJECT} LANGUAGES CXX) + +# define some folders +get_filename_component(RXCPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}" PATH) +get_filename_component(RXCPP_DIR "${RXCPP_DIR}" PATH) +get_filename_component(RXCPP_DIR "${RXCPP_DIR}" PATH) +get_filename_component(RXCPP_DIR "${RXCPP_DIR}" PATH) + +MESSAGE( STATUS "RXCPP_DIR: " ${RXCPP_DIR} ) + +include(${RXCPP_DIR}/projects/CMake/shared.cmake) + +# define the sources +set(SAMPLE_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp +) +add_executable(${SAMPLE_PROJECT} ${SAMPLE_SOURCES}) +add_executable(rxcpp::examples::${SAMPLE_PROJECT} ALIAS ${SAMPLE_PROJECT}) +target_compile_options(${SAMPLE_PROJECT} PUBLIC ${RX_COMPILE_OPTIONS}) +target_compile_features(${SAMPLE_PROJECT} PUBLIC ${RX_COMPILE_FEATURES}) +target_include_directories(${SAMPLE_PROJECT} PUBLIC ${RX_SRC_DIR}) +target_link_libraries(${SAMPLE_PROJECT} ${CMAKE_THREAD_LIBS_INIT}) + +# configure unit tests via CTest +enable_testing() +set(CTEST_CONFIGURATION_TYPE "${JOB_BUILD_CONFIGURATION}") + +add_test(NAME RunTests + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" + COMMAND ${SAMPLE_PROJECT} ${TEST_ARGS}) diff --git a/Rx/v2/examples/stop/main.cpp b/Rx/v2/examples/stop/main.cpp new file mode 100644 index 0000000..62bd5d7 --- /dev/null +++ b/Rx/v2/examples/stop/main.cpp @@ -0,0 +1,54 @@ + +#include "rxcpp/rx.hpp" +// create alias' to simplify code +// these are owned by the user so that +// conflicts can be managed by the user. +namespace rx=rxcpp; +namespace rxsub=rxcpp::subjects; +namespace rxu=rxcpp::util; + +// At this time, RxCpp will fail to compile if the contents +// of the std namespace are merged into the global namespace +// DO NOT USE: 'using namespace std;' + +int main() +{ + // works + { + auto published_observable = + rx::observable<>::range(1) + .filter([](int i) + { + std::cout << i << std::endl; + std::this_thread::sleep_for(std::chrono::milliseconds(300)); + return true; + }) + .subscribe_on(rx::observe_on_new_thread()) + .publish(); + + auto subscription = published_observable.connect(); + std::this_thread::sleep_for(std::chrono::seconds(1)); + subscription.unsubscribe(); + std::cout << "unsubscribed" << std::endl << std::endl; + } + + // idiomatic (prefer operators) + { + auto published_observable = + rx::observable<>::interval(std::chrono::milliseconds(300)) + .subscribe_on(rx::observe_on_new_thread()) + .publish(); + + published_observable. + ref_count(). + take_until(rx::observable<>::timer(std::chrono::seconds(1))). + finally([](){ + std::cout << "unsubscribed" << std::endl << std::endl; + }). + subscribe([](int i){ + std::cout << i << std::endl; + }); + } + + return 0; +} |