diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-23 20:16:02 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-23 20:16:02 -0500 |
| commit | 85f89ee4bb137100cbeffcbc10168eb8ea52e6cc (patch) | |
| tree | f6e2063319ceaaa02f523fb5c289e4f37411a2df /examples | |
| parent | 9a70f32ddaec0eef99efbf7ff5597c2adf08f45a (diff) | |
| parent | 65a8349aa5526d071b18cd4d42586c46faaa3823 (diff) | |
Merge pull request #18 from amlel-el-mahrouss/developv1.0.48
OCL v1.0.48
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/allocator_system/CMakeLists.txt | 16 | ||||
| -rw-r--r-- | examples/allocator_system/allocator_system.cc | 53 | ||||
| -rw-r--r-- | examples/cgi/CMakeLists.txt | 16 | ||||
| -rw-r--r-- | examples/cgi/cgi.cc | 75 | ||||
| -rw-r--r-- | examples/equiv/CMakeLists.txt | 12 | ||||
| -rw-r--r-- | examples/equiv/equiv.cc | 19 | ||||
| -rw-r--r-- | examples/fix/CMakeLists.txt | 15 | ||||
| -rw-r--r-- | examples/fix/fix.cc | 37 | ||||
| -rw-r--r-- | examples/opt/CMakeLists.txt | 12 | ||||
| -rw-r--r-- | examples/opt/opt.cc | 45 | ||||
| -rw-r--r-- | examples/tracked_ptr/CMakeLists.txt | 12 | ||||
| -rw-r--r-- | examples/tracked_ptr/tracked_ptr.cc | 52 |
12 files changed, 364 insertions, 0 deletions
diff --git a/examples/allocator_system/CMakeLists.txt b/examples/allocator_system/CMakeLists.txt new file mode 100644 index 0000000..a4bb69d --- /dev/null +++ b/examples/allocator_system/CMakeLists.txt @@ -0,0 +1,16 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + Alloc + VERSION 1.0 + LANGUAGES CXX) + +find_package(Boost REQUIRED COMPONENTS container) + +add_executable(Alloc allocator_system.cc) + +target_link_libraries(Alloc PRIVATE Boost::container) + +set_property(TARGET Alloc PROPERTY CXX_STANDARD 20) +target_include_directories(Alloc PUBLIC ../../include/ocl) diff --git a/examples/allocator_system/allocator_system.cc b/examples/allocator_system/allocator_system.cc new file mode 100644 index 0000000..fd7ae56 --- /dev/null +++ b/examples/allocator_system/allocator_system.cc @@ -0,0 +1,53 @@ +/* + * File: allocator_system.cc + * Purpose: Allocator System container. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss, Licensed under the Boost Software License. Licensed under the BSL 1.0 license + */ + +#include <memory/allocator_system.hpp> +#include <iostream> + +class MyClass final +{ +public: + int a{}; + std::string b{}; + + MyClass() : a(0), b("default") + { + std::cout << "MyClass() constructed\n"; + } + + MyClass(int x, std::string y) : a(x), b(std::move(y)) + { + std::cout << "MyClass(int, string) constructed\n"; + } + + ~MyClass() + { + std::cout << "~MyClass() destroyed\n"; + } +}; + +int main() +{ + ocl::standard_allocator_type<MyClass> allocator; + + // Test 1: claim() + unclaim() + std::cout << "=== Test 1: claim/unclaim ===\n"; + + MyClass* raw = allocator.claim(); + + std::cout << "raw->a = " << raw->a << ", raw->b = " << raw->b << "\n"; + allocator.unclaim(raw); // Manual delete + + // Test 2: construct() → shared_ptr + std::cout << "\n=== Test 2: construct (shared_ptr) ===\n"; + + auto ptr = allocator.construct<int, std::string>(42, "hello"); + std::cout << "ptr->a = " << ptr->a << ", ptr->b = " << ptr->b << "\n"; + + // `shared_ptr` will automatically delete the object + std::cout << "\n=== End of main ===\n"; +} diff --git a/examples/cgi/CMakeLists.txt b/examples/cgi/CMakeLists.txt new file mode 100644 index 0000000..e30707c --- /dev/null +++ b/examples/cgi/CMakeLists.txt @@ -0,0 +1,16 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + CGI + VERSION 1.0 + LANGUAGES CXX) + +find_package(Boost REQUIRED COMPONENTS container) + +add_executable(CGI cgi.cc) + +target_link_libraries(CGI PRIVATE Boost::container) + +set_property(TARGET CGI PROPERTY CXX_STANDARD 20) +target_include_directories(CGI PUBLIC ../../include/ocl) diff --git a/examples/cgi/cgi.cc b/examples/cgi/cgi.cc new file mode 100644 index 0000000..b837433 --- /dev/null +++ b/examples/cgi/cgi.cc @@ -0,0 +1,75 @@ +/* + cgi example + written by Amlal El Mahrouss. + licensed under the Boost Software License + */ + +#include <utility/cgi.hpp> + +static ocl::basic_chunk_string<char> text_sample = R"( +<!DOCTYPE html> +<html> +<head> + <meta charset="UTF-8"> + <title>Error | OCL</title> + <style> + body { + font-family: monospace; + background: white; + color: black; + margin: 2em; + } + h1 { + font-size: 1.5em; + } + hr { + border: none; + border-top: 1px solid #ccc; + } + table { + width: 100%; + border-collapse: collapse; + } + td { + padding: 0.2em 0.5em; + } + td:first-child { + white-space: nowrap; + } + a { + color: blue; + text-decoration: none; + } + a:hover { + text-decoration: underline; + } + </style> +</head> +<body> + <h1>Uh Oh!</h1> + <address>No index file was found in this directory.</address> + <hr> + <table> + <tr><th>Name</th></tr> + <tr><td colspan="3"><hr></td></tr> + + <tr> + <td><a href="javascript:window.location.reload();">Refresh</a></td> + </tr> + + <tr><td colspan="3"><hr></td></tr> + </table> + <address>OCL's Common Gateway Server.</address> +</body> +</html> +)"; + +/* finally test it */ +/* @brief this stub loads a 'index.html' or returns an error message if not found. */ +int main(int argc, char** argv) +{ + ocl::cgi::basic_writer<> writer; + writer << text_sample; + + return 0; +} diff --git a/examples/equiv/CMakeLists.txt b/examples/equiv/CMakeLists.txt new file mode 100644 index 0000000..4a374ce --- /dev/null +++ b/examples/equiv/CMakeLists.txt @@ -0,0 +1,12 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + Equiv + VERSION 1.0 + LANGUAGES CXX) + +add_executable(Equiv equiv.cc) + +set_property(TARGET Equiv PROPERTY CXX_STANDARD 20) +target_include_directories(Equiv PUBLIC ../../include/ocl) diff --git a/examples/equiv/equiv.cc b/examples/equiv/equiv.cc new file mode 100644 index 0000000..0c68e21 --- /dev/null +++ b/examples/equiv/equiv.cc @@ -0,0 +1,19 @@ +/* + string checksum example + written by Amlal El Mahrouss. + licensed under the Boost Software License + */ + +#include <logic/equiv.hpp> +#include <iostream> + +/* finally test it */ +int main(int argc, char** argv) +{ + std::cout << std::boolalpha; + std::cout << ocl::equiv::is_same<bool, int>::value << std::endl; + std::cout << ocl::equiv::is_same<bool, bool>::value << std::endl; + std::cout << ocl::equiv::is_same<int, int>::value << std::endl; + + return 0; +} diff --git a/examples/fix/CMakeLists.txt b/examples/fix/CMakeLists.txt new file mode 100644 index 0000000..4c0a432 --- /dev/null +++ b/examples/fix/CMakeLists.txt @@ -0,0 +1,15 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + Fix + VERSION 1.0 + LANGUAGES CXX) + +find_package(Boost REQUIRED COMPONENTS container) + +add_executable(Fix fix.cc) + +target_link_libraries(Fix PRIVATE Boost::container) +set_property(TARGET Fix PROPERTY CXX_STANDARD 20) +target_include_directories(Fix PUBLIC ../../include/ocl) diff --git a/examples/fix/fix.cc b/examples/fix/fix.cc new file mode 100644 index 0000000..eabe3ac --- /dev/null +++ b/examples/fix/fix.cc @@ -0,0 +1,37 @@ +/* + fix example + written by Amlal El Mahrouss. + licensed under the Boost Software License + */ + +#include <core/error_handler.hpp> +#include <net/modem.hpp> +#include <fix/fix.hpp> +#include <iostream> +#include <unistd.h> +#include <io/print.hpp> +#include <sys/socket.h> + +/* finally test it */ +int main(int argc, char** argv) +{ + constexpr auto default_fix = "8=FIX.4.2|9=65|35=A|49=SERVER|56=CLIENT|34=177|52=20090107-18:15:16|98=0|108=30|10=062|"; + + ocl::fix::basic_visitor<char> basic_visitor; + ocl::fix::basic_range_data<char> fix = basic_visitor.visit(default_fix); + + std::cout << "magic=" << fix.magic_ << std::endl; + std::cout << "magic_len=" << fix.magic_len_ << std::endl; + std::cout << "is_valid=" << std::boolalpha << fix.is_valid() << std::endl; + + ocl::basic_error_handler handler; + ocl::fix::must_pass<char, ocl::basic_error_handler>(fix, handler); + + for (auto fields : fix.body_) + { + ocl::io::print("key=", fields.first); + ocl::io::print(", value=", fields.second); + } + + return 0; +} diff --git a/examples/opt/CMakeLists.txt b/examples/opt/CMakeLists.txt new file mode 100644 index 0000000..e537cec --- /dev/null +++ b/examples/opt/CMakeLists.txt @@ -0,0 +1,12 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + Opt + VERSION 1.0 + LANGUAGES CXX) + +add_executable(Opt opt.cc) + +set_property(TARGET Opt PROPERTY CXX_STANDARD 20) +target_include_directories(Opt PUBLIC ../../include/ocl) diff --git a/examples/opt/opt.cc b/examples/opt/opt.cc new file mode 100644 index 0000000..75abed4 --- /dev/null +++ b/examples/opt/opt.cc @@ -0,0 +1,45 @@ +/* + String checksum example + Written by Amlal El Mahrouss. + Licensed under the Boost Software License + */ + +#include <logic/opt.hpp> +#include <io/print.hpp> +#include <utility/crc32.hpp> +#include <string> + +static const char do_hash(const std::string& in) +{ + int hash = 0; + + for (long index = 0; index < in.size(); ++index) + { + hash += in[index]; + } + + return hash; +} + +static auto do_some(const std::string recv_data, const std::string check_data) +{ + const int hash_to_check = do_hash(check_data); /* here we assume this should match opt_hash */ + const int opt_hash = do_hash(recv_data); /* we assume that the hash is correct */ + + auto opt = ocl::opt(ocl::eval_eq(hash_to_check, opt_hash)); /* do the compute */ + return opt; +} + +/* finally test it */ +int main(int argc, char** argv) +{ + // ... let's assume we fetch data from network... + + ocl::io::println("Testing data..."); + + auto opt = do_some("Ohio", "Ohio"); + opt.expect("Checksum failed, Ohio isn't Ohio!"); + + + return 0; +} diff --git a/examples/tracked_ptr/CMakeLists.txt b/examples/tracked_ptr/CMakeLists.txt new file mode 100644 index 0000000..ea1312a --- /dev/null +++ b/examples/tracked_ptr/CMakeLists.txt @@ -0,0 +1,12 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + Fix + VERSION 1.0 + LANGUAGES CXX) + +add_executable(TrackedPtr tracked_ptr.cc) + +set_property(TARGET TrackedPtr PROPERTY CXX_STANDARD 20) +target_include_directories(TrackedPtr PUBLIC ../../include/ocl) diff --git a/examples/tracked_ptr/tracked_ptr.cc b/examples/tracked_ptr/tracked_ptr.cc new file mode 100644 index 0000000..0f7c43d --- /dev/null +++ b/examples/tracked_ptr/tracked_ptr.cc @@ -0,0 +1,52 @@ +/* + tracked_ptr example + written by Amlal El Mahrouss. + licensed under the Boost Software License + */ + +#include <memory/tracked_ptr.hpp> +#include <io/print.hpp> + +static void summon_tracked_ptr() +{ + ocl::memory::tracked_ptr<int> ptr = ocl::memory::make_tracked<int>(42); + std::cout << ptr.data() << "=" << ptr.manager().allocator().allocated_count_ << std::endl; +} + +static void summon_leak_tracked_ptr() +{ + ocl::memory::tracked_ptr<int>* ptr = new ocl::memory::tracked_ptr<int>(42); + std::cout << ptr->data() << "=" << ptr->manager().allocator().allocated_count_ << std::endl; +} + +/* finally test it */ +int main(int argc, char** argv) +{ + summon_tracked_ptr(); + summon_tracked_ptr(); + summon_tracked_ptr(); + summon_tracked_ptr(); + + ocl::memory::tracked_ptr<int> ptr; + + std::cout << ptr.data() << "=" << ptr.manager().allocator().allocated_count_ << std::endl; + + std::cout << "total=" << ptr.manager().allocator().deallocated_count_ << std::endl; + std::cout << "leak-detected=" << std::boolalpha << (ptr.manager().allocator().allocated_count_ > ptr.manager().allocator().deallocated_count_) << std::endl; + + summon_leak_tracked_ptr(); + summon_leak_tracked_ptr(); + summon_leak_tracked_ptr(); + summon_leak_tracked_ptr(); + summon_leak_tracked_ptr(); + summon_leak_tracked_ptr(); + summon_leak_tracked_ptr(); + summon_leak_tracked_ptr(); + + std::cout << "total=" << ptr.manager().allocator().deallocated_count_ << std::endl; + std::cout << "leak-detected=" << std::boolalpha << (ptr.manager().allocator().allocated_count_ > ptr.manager().allocator().deallocated_count_) << std::endl; + + ocl::memory::must_pass(ptr); + + return EXIT_SUCCESS; +} |
