diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2026-03-05 15:31:57 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2026-03-05 15:31:57 +0100 |
| commit | 97d846f41e2bccf7957983c03d1fe9867b789efb (patch) | |
| tree | 9d2193612d3f9b3976bea40243d63e60d31fe5d9 | |
| parent | fcc9da9f2f3b5c3002349e43faad065716606751 (diff) | |
[FEAT] New Option 2 example, updated README, fixed crc_hash.hpp.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | README.md | 19 | ||||
| -rw-r--r-- | example/asio_example/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | example/option_2_example/CMakeLists.txt | 18 | ||||
| -rw-r--r-- | example/option_2_example/example.cpp | 29 | ||||
| -rw-r--r-- | include/ocl/asio.hpp | 4 | ||||
| -rw-r--r-- | include/ocl/crc_hash.hpp | 32 | ||||
| -rw-r--r-- | include/ocl/option.hpp | 7 |
7 files changed, 92 insertions, 22 deletions
@@ -17,6 +17,25 @@ The OCL requires: The OCL aims to be easily installable and to get up and running. +## Core by Examples: + +You can have a look at the examples in order to get familiar with the library. + +```cpp +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 0; +} +``` + +This one implements an `ocl::option` container, which is used for error handling and other related tasks. + ## Freestanding Status: The Freestanding Status is a concept where a module is evaluated on whether it has or is fully freestanding or not. diff --git a/example/asio_example/CMakeLists.txt b/example/asio_example/CMakeLists.txt index 2efd470..64494b9 100644 --- a/example/asio_example/CMakeLists.txt +++ b/example/asio_example/CMakeLists.txt @@ -12,4 +12,7 @@ add_executable(AsioExample example.cpp) set_property(TARGET AsioExample PROPERTY CXX_STANDARD 20) target_include_directories(AsioExample PUBLIC ../../include/) -# target_link_libraries(AsioExample PRIVATE Boost Core Asio) + +if (NOT DEFINED LINUX) +target_link_libraries(AsioExample PRIVATE Boost Core Asio) +endif () 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; +} diff --git a/include/ocl/asio.hpp b/include/ocl/asio.hpp index 43ebd71..bcab94f 100644 --- a/include/ocl/asio.hpp +++ b/include/ocl/asio.hpp @@ -25,7 +25,7 @@ namespace ocl::asio { using io_context_type = boost::asio::io_context; - using run_pred_type = void(*)(); + using run_pred_type = void (*)(); template <run_pred_type IOCPred> inline void run(io_context_type& ioc) @@ -40,6 +40,6 @@ namespace ocl::asio } } -} // namespace ocl +} // namespace ocl::asio #endif diff --git a/include/ocl/crc_hash.hpp b/include/ocl/crc_hash.hpp index 050bb92..11a9ed7 100644 --- a/include/ocl/crc_hash.hpp +++ b/include/ocl/crc_hash.hpp @@ -20,9 +20,9 @@ namespace ocl /// @brief implements a trait object for crc hashing. struct crc_hash_trait { - static constexpr const std::uint16_t crc_sz_ = 256U; + static constexpr const uint16_t crc_sz_ = 256U; - static constexpr std::uint32_t crc_array_[crc_sz_] = { + static constexpr uint32_t crc_array_[crc_sz_] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, @@ -74,11 +74,11 @@ namespace ocl return ~crc; } - template <std::uint8_t algorithm> - constexpr std::uint8_t crc(const char* in, std::size_t len) = delete; + template <uint8_t algorithm> + constexpr uint8_t crc(const char* in, std::size_t len) = delete; - template <std::uint64_t algorithm> - constexpr std::uint64_t crc(const char* in, std::size_t len) = delete; + template <uint64_t algorithm> + constexpr uint64_t crc(const char* in, std::size_t len) = delete; }; template <typename V> @@ -89,16 +89,16 @@ namespace ocl } // namespace ocl -inline std::uint32_t +inline uint32_t operator""_crc32(const char* in, std::size_t len) noexcept { - return ocl::crc_hash_trait::crc<std::uint32_t>(static_cast<const char*>(in), len); + return ocl::crc_hash_trait::crc<uint32_t>(static_cast<const char*>(in), len); } -inline std::uint32_t +inline uint32_t operator""_crc32(const wchar_t* in, std::size_t len) noexcept { - return ocl::crc_hash_trait::crc<std::uint32_t, wchar_t>(static_cast<const wchar_t*>(in), len); + return ocl::crc_hash_trait::crc<uint32_t, wchar_t>(static_cast<const wchar_t*>(in), len); } namespace std @@ -110,21 +110,21 @@ namespace std ~hash() = default; template <typename T> - inline std::size_t operator()(T* in_) const + inline size_t operator()(T* in_) const { - return ocl::crc_hash_trait::crc<std::uint32_t>(reinterpret_cast<const char*>(in_), sizeof(T)); + return ocl::crc_hash_trait::crc<uint32_t>(reinterpret_cast<const char*>(in_), sizeof(T)); } template <bool is_mem = true> - inline std::size_t operator()(const char* in_) const + inline size_t operator()(const char* in_) const { - return ocl::crc_hash_trait::crc<std::uint32_t>(in_, strlen(in_)); + return ocl::crc_hash_trait::crc<uint32_t>(in_, strlen(in_)); } template <bool is_me = true, bool string_overload = true> - inline std::size_t operator()(const std::string& in_) const + inline size_t operator()(const string& in_) const { - return ocl::crc_hash_trait::crc<std::uint32_t>(in_.c_str(), in_.size()); + return ocl::crc_hash_trait::crc<uint32_t>(in_.c_str(), in_.size()); } }; diff --git a/include/ocl/option.hpp b/include/ocl/option.hpp index 2c387a7..74300a3 100644 --- a/include/ocl/option.hpp +++ b/include/ocl/option.hpp @@ -1,4 +1,4 @@ -// Copyright 2025, Amlal El Mahrouss (amlal@nekernel.org) +// 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 @@ -8,6 +8,7 @@ #include <ocl/detail/config.hpp> #include <ocl/print.hpp> +#include <cstdint> namespace ocl { @@ -43,8 +44,8 @@ namespace ocl ~option() = default; - option& operator=(const option&) = delete; - option(const option&) = delete; + option& operator=(const option&) = default; + option(const option&) = default; option& expect(const char* input) { |
