From 9f881f83f0710aaf51c93ae379e0c6a55d1dd9d7 Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Mon, 12 Mar 2018 07:07:30 +0100 Subject: [PATCH] Move the slideshow code to examples * Remove the rest from doc --- CMakeLists.txt | 2 - doc/CMakeLists.txt | 1 - doc/code/CMakeLists.txt | 2 - doc/code/documentation/CMakeLists.txt | 5 - doc/code/documentation/doc-documentation.cpp | 152 ------------------ doc/code/slideshow/CMakeLists.txt | 5 - examples/CMakeLists.txt | 1 + examples/example-slideshow/CMakeLists.txt | 5 + .../example-slideshow/example-slideshow.cpp | 44 ++--- 9 files changed, 18 insertions(+), 199 deletions(-) delete mode 100644 doc/CMakeLists.txt delete mode 100644 doc/code/CMakeLists.txt delete mode 100644 doc/code/documentation/CMakeLists.txt delete mode 100644 doc/code/documentation/doc-documentation.cpp delete mode 100644 doc/code/slideshow/CMakeLists.txt create mode 100644 examples/example-slideshow/CMakeLists.txt rename doc/code/slideshow/doc-slideshow.cpp => examples/example-slideshow/example-slideshow.cpp (76%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1584fa5..d796995 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,8 +130,6 @@ if (CTI_CONTINUABLE_WITH_TESTS OR CTI_CONTINUABLE_WITH_EXAMPLES) enable_testing() - add_subdirectory(doc) - if (CTI_CONTINUABLE_WITH_TESTS) add_subdirectory(test) endif() diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt deleted file mode 100644 index 9c71105..0000000 --- a/doc/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_subdirectory(code) diff --git a/doc/code/CMakeLists.txt b/doc/code/CMakeLists.txt deleted file mode 100644 index 4fc9aff..0000000 --- a/doc/code/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_subdirectory(documentation) -add_subdirectory(slideshow) diff --git a/doc/code/documentation/CMakeLists.txt b/doc/code/documentation/CMakeLists.txt deleted file mode 100644 index a75ba5e..0000000 --- a/doc/code/documentation/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_executable(doc-documentation - ${CMAKE_CURRENT_LIST_DIR}/doc-documentation.cpp) -target_link_libraries(doc-documentation - PRIVATE - continuable) diff --git a/doc/code/documentation/doc-documentation.cpp b/doc/code/documentation/doc-documentation.cpp deleted file mode 100644 index ca988c4..0000000 --- a/doc/code/documentation/doc-documentation.cpp +++ /dev/null @@ -1,152 +0,0 @@ -/* - Copyright(c) 2015 - 2018 Denis Blank - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files(the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and / or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions : - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -**/ - -#include - -#include -#include - -using cti::detail::util::unused; - -void creating_continuables() { - auto void_continuable = cti::make_continuable([](auto&& callback) { - // ^^^^ - - // Call the promise later when you have finished your work - callback.set_value(); - }); - - auto str_continuable = - cti::make_continuable([](auto&& callback) { - // ^^^^^^^^^^^ - callback.set_value("Hello, World!"); - }); -} - -struct ResultSet {}; -template -void mysql_handle_async_query(Args&&...) { -} - -auto mysql_query(std::string query) { - return cti::make_continuable([query = std::move(query)]( - auto&& callback) mutable { - // Pass the callback to the handler which calls the callback when finished. - // Every function accepting callbacks works with continuables. - mysql_handle_async_query(std::move(query), - std::forward(callback)); - }); -} - -void providing_helper_functions() { - // You may use the helper function like you would normally do, - // without using the support methods of the continuable. - mysql_query("DELETE FROM `users` WHERE `id` = 27361"); - - // Or using chaining to handle the result which is covered in the - // documentation. - mysql_query("SELECT `id`, `name` FROM users").then([](ResultSet result) { - // ... - unused(result); - }); -} - -void chaining_continuables() { - mysql_query("SELECT `id`, `name` FROM `users`") - .then([](ResultSet users) { - (void)users; - // Return the next continuable to process ... - return mysql_query("SELECT `id` name FROM `sessions`"); - }) - .then([](ResultSet sessions) { - // ... or pass multiple values to the next callback using tuples or - // pairs ... - return std::make_tuple(std::move(sessions), true); - }) - .then([](ResultSet sessions, bool is_ok) { - (void)sessions; - (void)is_ok; - // ... or pass a single value to the next callback ... - return 10; - }) - .then([](auto value) { - // ^^^^ Templated callbacks are possible too - (void)value; - }) - // ... you may even pass continuables to the `then` method directly: - .then(mysql_query("SELECT * `statistics`")) - .then([](ResultSet result) { - // ... - (void)result; - }); -} - -auto http_request(std::string /*url*/) { - return cti::make_continuable([](auto&& callback) { - // ... - callback.set_value("..."); - }); -} - -void connecting_continuables() { - // `all` of connections: - (http_request("github.com") && http_request("travis-ci.org") && - http_request("atom.io")) - .then([](std::string github, std::string travis, std::string atom) { - // The callback is called with the response of github, travis and atom. - unused(github, travis, atom); - }); - - // `any` of connections: - (http_request("github.com") || http_request("travis-ci.org") || - http_request("atom.io")) - .then([](std::string github_or_travis_or_atom) { - // The callback is called with the first response of either github, - // travis or atom. - unused(github_or_travis_or_atom); - }); - - // mixed logical connections: - (http_request("github.com") && - (http_request("travis-ci.org") || http_request("atom.io"))) - .then([](std::string github, std::string travis_or_atom) { - // The callback is called with the response of github for sure - // and the second parameter represents the response of travis or atom. - unused(github, travis_or_atom); - }); - - // There are helper functions for connecting continuables: - auto all = - cti::when_all(http_request("github.com"), http_request("travis-ci.org")); - auto any = - cti::when_any(http_request("github.com"), http_request("travis-ci.org")); -} - -int main() { - creating_continuables(); - - providing_helper_functions(); - - chaining_continuables(); - - connecting_continuables(); -} diff --git a/doc/code/slideshow/CMakeLists.txt b/doc/code/slideshow/CMakeLists.txt deleted file mode 100644 index 43354f8..0000000 --- a/doc/code/slideshow/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_executable(doc-slideshow - ${CMAKE_CURRENT_LIST_DIR}/doc-slideshow.cpp) -target_link_libraries(doc-slideshow - PRIVATE - continuable) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 64741a9..34cc628 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(example-asio) add_subdirectory(example-ai) +add_subdirectory(example-slideshow) diff --git a/examples/example-slideshow/CMakeLists.txt b/examples/example-slideshow/CMakeLists.txt new file mode 100644 index 0000000..6df8a8a --- /dev/null +++ b/examples/example-slideshow/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable(example-slideshow + ${CMAKE_CURRENT_LIST_DIR}/example-slideshow.cpp) +target_link_libraries(example-slideshow + PRIVATE + continuable) diff --git a/doc/code/slideshow/doc-slideshow.cpp b/examples/example-slideshow/example-slideshow.cpp similarity index 76% rename from doc/code/slideshow/doc-slideshow.cpp rename to examples/example-slideshow/example-slideshow.cpp index bc53ab6..d7dfa59 100644 --- a/doc/code/slideshow/doc-slideshow.cpp +++ b/examples/example-slideshow/example-slideshow.cpp @@ -25,43 +25,32 @@ #include "continuable/continuable.hpp" cti::continuable http_request(std::string /*url*/) { - return [](auto&& callback) { - // ... - callback.set_value("..."); - }; + return cti::make_ready_continuable("..."); } struct ResultSet {}; struct Buffer {}; cti::continuable mysql_query(std::string /*url*/) { - return [](auto&& callback) { - // ... - callback.set_value(ResultSet{}); - }; + return cti::make_ready_continuable(ResultSet{}); } cti::continuable read_file(std::string /*url*/) { - return [](auto&& callback) { - // ... - callback.set_value(Buffer{}); - }; + return cti::make_ready_continuable(Buffer{}); } -struct a { +struct functional_executor { auto post() const { return [](auto&&) {}; } }; int main(int, char**) { - a e; + functional_executor e; auto executor = &e; + // clang-format off - - // ---------- - (http_request("github.com") && http_request("atom.io")) .then([] (std::string /*github*/, std::string /*atom*/) { // ... @@ -71,23 +60,15 @@ int main(int, char**) { // ... }, executor->post()); - // ---------- + // clang-format on - auto c1 = http_request("github.com") && http_request("atom.io") ; + http_request("github.com") && http_request("atom.io"); + http_request("github.com") || http_request("atom.io"); + http_request("github.com") >> http_request("atom.io"); - auto c2 = http_request("github.com") || http_request("atom.io") ; - - - - auto c3 = http_request("github.com") >> http_request("atom.io") ; - - (void)c1; - (void)c2; - (void)c3; - - // ---------- + // clang-format off read_file("entries.csv") .then([] (Buffer /*buffer*/) { @@ -98,8 +79,7 @@ int main(int, char**) { // ... }); - // ---------- - // clang-format on + return 0; }