From 1a8b227c502454fb05cd619e208ff460304b9c33 Mon Sep 17 00:00:00 2001 From: Darius Neatu Date: Wed, 12 Jun 2024 11:41:14 +0300 Subject: [PATCH] move/add examples to /examples --- CMakeLists.txt | 1 + examples/CMakeLists.txt | 32 ++++++++++++++ examples/README.md | 42 +++++++++++++++++++ .../optional_ref.cpp | 0 examples/range_loop.cpp | 17 ++++++++ examples/sample.cpp | 16 +++++++ src/CMakeLists.txt | 1 - src/examples/CMakeLists.txt | 29 ------------- src/examples/main.cpp | 4 -- 9 files changed, 108 insertions(+), 34 deletions(-) create mode 100644 examples/CMakeLists.txt create mode 100644 examples/README.md rename src/examples/before_after.cpp => examples/optional_ref.cpp (100%) create mode 100644 examples/range_loop.cpp create mode 100644 examples/sample.cpp delete mode 100644 src/examples/CMakeLists.txt delete mode 100644 src/examples/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 452671fd..ebc7bc12 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ set(TARGETS_EXPORT_NAME ${CMAKE_PROJECT_NAME}Targets) add_subdirectory(extern) add_subdirectory(src) +add_subdirectory(examples) include(GNUInstallDirs) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 00000000..fac02b85 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,32 @@ +SET(BEMAN_OPTIONAL26_LIBRARY "beman_optional26") + +include(GNUInstallDirs) + +# List of all buildable examples. +set(EXAMPLES + sample + range_loop + optional_ref +) + +foreach(EXAMPLE ${EXAMPLES}) + # Add example executable. + add_executable(${EXAMPLE} "") + + # Add example source file. + target_sources( + ${EXAMPLE} + PRIVATE + ${EXAMPLE}.cpp + ) + + # Link example with the library. + target_link_libraries(${EXAMPLE} "${BEMAN_OPTIONAL26_LIBRARY}") + + # Install . + install( + TARGETS ${EXAMPLE} + EXPORT ${TARGETS_EXPORT_NAME} + DESTINATION ${CMAKE_INSTALL_BINDIR} + ) +endforeach() diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..b43db0be --- /dev/null +++ b/examples/README.md @@ -0,0 +1,42 @@ +# Beman.Optional26 Examples + +List of usage examples for `Beman.Optional26`. + +## Sample + +Check [sample](sample.cpp) for basic `Beman.Optional26` library usage. + +Build and run instructions: +```shell +# build +$ cmake --workflow --preset gcc-14 + +# run sample +$ .build/gcc-14/examples/RelWithDebInfo/sample +empty_opt is empty! +opt = 26 +``` + +## Range Support (P3168R1) + +Range support added in [*Give std::optional Range Support* (P3168R1)](https://wg21.link/P3168R1) examples: +* [./range_loop.cpp](./range_loop.cpp) + + +Build and run instructions: + +```shell +# build +$ cmake --workflow --preset gcc-14 + +# run range_loop +$ .build/gcc-14/examples/RelWithDebInfo/range_loop + # note: 1st log (empty_opt) is missing from console! +"for each loop" over C++26 optional: opt = 26 # 2nd log (non empty opt) +``` + +## Optional Reference (P2988R5) + +Reference support added in [*std::optional*(P2988R5)](https://wg21.link/P2988R5) examples: + +* [./optional_ref.cpp](./optional_ref.cpp) diff --git a/src/examples/before_after.cpp b/examples/optional_ref.cpp similarity index 100% rename from src/examples/before_after.cpp rename to examples/optional_ref.cpp diff --git a/examples/range_loop.cpp b/examples/range_loop.cpp new file mode 100644 index 00000000..dd29d055 --- /dev/null +++ b/examples/range_loop.cpp @@ -0,0 +1,17 @@ +#include +#include + +int main() { + beman::optional::optional empty_opt{}; + for ([[maybe_unused]] const auto& i : empty_opt) { + // Should not see this in console. + std::cout << "\"for each loop\" over C++26 optional: empty_opt\n"; + } + + beman::optional::optional opt{26}; + for (const auto& i : opt) { + // Should see this in console. + std::cout << "\"for each loop\" over C++26 optional: opt = " << i << "\n"; + } + return 0; +} diff --git a/examples/sample.cpp b/examples/sample.cpp new file mode 100644 index 00000000..508eb049 --- /dev/null +++ b/examples/sample.cpp @@ -0,0 +1,16 @@ +#include +#include + +int main() { + beman::optional::optional empty_opt{}; + if (!empty_opt) { + std::println("empty_opt is empty!"); + } + + beman::optional::optional opt{26}; + if (opt) { + std::println("opt = {}", *opt); + } + + return 0; +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7b654f63..066cc47b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,2 +1 @@ add_subdirectory(beman) -add_subdirectory(examples) diff --git a/src/examples/CMakeLists.txt b/src/examples/CMakeLists.txt deleted file mode 100644 index 67966991..00000000 --- a/src/examples/CMakeLists.txt +++ /dev/null @@ -1,29 +0,0 @@ -SET(BEMAN_OPTIONAL26_LIBRARY "beman_optional26") - -include(GNUInstallDirs) - -add_executable(main "") - -target_sources( - main - PRIVATE - main.cpp -) - -target_link_libraries(main "${BEMAN_OPTIONAL26_LIBRARY}") - -add_executable(before_after "") - -target_sources( - before_after - PRIVATE - before_after.cpp -) - -target_link_libraries(before_after "${BEMAN_OPTIONAL26_LIBRARY}") - -install( - TARGETS main - EXPORT ${TARGETS_EXPORT_NAME} - DESTINATION ${CMAKE_INSTALL_BINDIR} -) diff --git a/src/examples/main.cpp b/src/examples/main.cpp deleted file mode 100644 index 5a5e7fd4..00000000 --- a/src/examples/main.cpp +++ /dev/null @@ -1,4 +0,0 @@ -#include - -int main() { -}