diff options
Diffstat (limited to 'example/option_2_example')
| -rw-r--r-- | example/option_2_example/CMakeLists.txt | 18 | ||||
| -rw-r--r-- | example/option_2_example/example.cpp | 29 |
2 files changed, 47 insertions, 0 deletions
diff --git a/example/option_2_example/CMakeLists.txt b/example/option_2_example/CMakeLists.txt new file mode 100644 index 0000000..b3e7dbd --- /dev/null +++ b/example/option_2_example/CMakeLists.txt @@ -0,0 +1,18 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + OptionExample + VERSION 1.0 + LANGUAGES CXX) + +find_package(Boost REQUIRED) + +add_executable(OptionExample example.cpp) + +set_property(TARGET OptionExample PROPERTY CXX_STANDARD 20) +target_include_directories(OptionExample PUBLIC ../../include/) + +if (DEFINED LINUX) +target_link_libraries(OptionExample PRIVATE Boost) +endif () diff --git a/example/option_2_example/example.cpp b/example/option_2_example/example.cpp new file mode 100644 index 0000000..0ac6dbd --- /dev/null +++ b/example/option_2_example/example.cpp @@ -0,0 +1,29 @@ +// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// Official repository: https://github.com/ocl-foss-org/core + +#include <ocl/crc_hash.hpp> +#include <ocl/print.hpp> +#include <ocl/option.hpp> + +struct invalid_callable +{ + explicit invalid_callable() = default; + void operator()(const char* reason) + { + ocl::detail::throw_runtime_error(); + } +}; + +/// \brief Option Monad example +int main(int argc, char** argv) +{ + ocl::option opt{ocl::eval_eq(nullptr, nullptr)}; + opt.expect("option::incorrect"); + + opt = ocl::option{ocl::eval_eq(argv, nullptr)}; + opt.expect<invalid_callable>("option::incorrect"); + + return EXIT_SUCCESS; +} |
