Skip to content

Commit

Permalink
move/add examples to /examples
Browse files Browse the repository at this point in the history
  • Loading branch information
neatudarius committed Jun 12, 2024
1 parent d0c2d42 commit 1a8b227
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 34 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(TARGETS_EXPORT_NAME ${CMAKE_PROJECT_NAME}Targets)

add_subdirectory(extern)
add_subdirectory(src)
add_subdirectory(examples)

include(GNUInstallDirs)

Expand Down
32 changes: 32 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
42 changes: 42 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -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<T&>*(P2988R5)](https://wg21.link/P2988R5) examples:

* [./optional_ref.cpp](./optional_ref.cpp)
File renamed without changes.
17 changes: 17 additions & 0 deletions examples/range_loop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <Beman/Optional26/optional.hpp>
#include <iostream>

int main() {
beman::optional::optional<int> 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<int> 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;
}
16 changes: 16 additions & 0 deletions examples/sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <Beman/Optional26/optional.hpp>
#include <print>

int main() {
beman::optional::optional<int> empty_opt{};
if (!empty_opt) {
std::println("empty_opt is empty!");
}

beman::optional::optional<int> opt{26};
if (opt) {
std::println("opt = {}", *opt);
}

return 0;
}
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
add_subdirectory(beman)
add_subdirectory(examples)
29 changes: 0 additions & 29 deletions src/examples/CMakeLists.txt

This file was deleted.

4 changes: 0 additions & 4 deletions src/examples/main.cpp

This file was deleted.

0 comments on commit 1a8b227

Please sign in to comment.