From bba582964bded940f9dc280fd15ed84aa2db2d39 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 17 Sep 2025 09:59:28 +0200 Subject: feat! lib: reorganize library (OCL v1.0.44) Signed-off-by: Amlal El Mahrouss --- dev/lib/core/allocator_system.hpp | 75 -------------------------- dev/lib/core/chunk_string.hpp | 102 ++++++++++++++++++++++++++++++++++++ dev/lib/logic/opt.hpp | 17 +++++- dev/lib/memory/allocator_system.hpp | 75 ++++++++++++++++++++++++++ dev/lib/utility/chunk_string.hpp | 102 ------------------------------------ 5 files changed, 192 insertions(+), 179 deletions(-) delete mode 100644 dev/lib/core/allocator_system.hpp create mode 100644 dev/lib/core/chunk_string.hpp create mode 100644 dev/lib/memory/allocator_system.hpp delete mode 100644 dev/lib/utility/chunk_string.hpp (limited to 'dev/lib') diff --git a/dev/lib/core/allocator_system.hpp b/dev/lib/core/allocator_system.hpp deleted file mode 100644 index 1243ed5..0000000 --- a/dev/lib/core/allocator_system.hpp +++ /dev/null @@ -1,75 +0,0 @@ -/* - * File: core/allocator_system.hpp - * Purpose: Allocator System container. - * Author: Amlal El Mahrouss (amlal@nekernel.org) - * Copyright 2025, Amlal El Mahrouss. Licensed under the BSL 1.0 license - */ - -#ifndef _OCL_ALLOCATOR_SYSTEM_HPP -#define _OCL_ALLOCATOR_SYSTEM_HPP - -#include -#include -#include - -namespace ocl -{ - template - struct new_op final - { - inline auto operator()() -> type* - { - return new type; - } - - template - inline auto var_alloc(var_type... args) -> type* - { - return new type{args...}; - } - }; - - template - struct delete_op final - { - inline auto operator()(type* t) -> void - { - delete t; - } - }; - - template - class allocator_system - { - allocator_new alloc_; - allocator_delete del_; - - public: - allocator_system() = default; - ~allocator_system() = default; - - allocator_system& operator=(const allocator_system&) = delete; - allocator_system(const allocator_system&) = delete; - - ret_type* claim() noexcept - { - return alloc_(); - } - - template - auto construct(var_type... args) -> std::shared_ptr - { - return std::shared_ptr(alloc_.template var_alloc(args...), allocator_delete{}); - } - - void unclaim(ret_type* ptr) - { - del_(ptr); - } - }; - - template - using standard_allocator_type = allocator_system, delete_op>; -} // namespace ocl - -#endif // ifndef _OCL_ALLOCATOR_SYSTEM_HPP diff --git a/dev/lib/core/chunk_string.hpp b/dev/lib/core/chunk_string.hpp new file mode 100644 index 0000000..4fe5cc2 --- /dev/null +++ b/dev/lib/core/chunk_string.hpp @@ -0,0 +1,102 @@ +/* +* File: core/chunk_string.hpp + * Purpose: String implementation for the OCL C++ library. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss + */ + +#ifndef OCL_UTILITY_CHUNK_STRING_HPP +#define OCL_UTILITY_CHUNK_STRING_HPP + +#include + +namespace ocl +{ + template + class basic_chunk_string; + + template + class basic_chunk_string final + { + private: + char_type packed_chunks_[max_chunk_size] = {0}; + std::size_t chunk_total_{}; + + bool bad_{false}; + + public: + const bool& bad{bad_}; + + basic_chunk_string() = default; + + basic_chunk_string(const char_type* in) + { + this->operator+=(in); + } + + basic_chunk_string(const std::basic_string& in) + { + this->operator+=(in); + } + + ~basic_chunk_string() = default; + + basic_chunk_string& operator=(const basic_chunk_string&) = delete; + basic_chunk_string(const basic_chunk_string&) = delete; + + public: + basic_chunk_string& operator+=(const std::basic_string& in) + { + if (in.empty() || bad_) + return *this; + + if (in.size() > max_chunk_size) + { + bad_ = true; + return *this; + } + + if (chunk_total_ > max_chunk_size) + { + bad_ = true; + return *this; + } + + const auto& sz = in.size(); + const static auto size_max_chunk = max_chunk_size; + const auto& ptr = in.data(); + + if (chunk_total_ < size_max_chunk) + { + std::memcpy(packed_chunks_ + chunk_total_, ptr, sz); + chunk_total_ += sz; + } + + return *this; + } + + std::basic_string str() const noexcept + { + static std::basic_string ret; + + if (ret.size() > 0) + ret.clear(); + + ret += packed_chunks_; + + return ret; + } + + void print() noexcept + { + ocl::io::print(packed_chunks_); + } + }; + + template + inline void print(basic_chunk_string& fmt) noexcept + { + fmt.print(); + } +} // namespace ocl +#endif // ifndef OCL_UTILITY_CHUNK_STRING_HPP diff --git a/dev/lib/logic/opt.hpp b/dev/lib/logic/opt.hpp index 442756c..eff08eb 100644 --- a/dev/lib/logic/opt.hpp +++ b/dev/lib/logic/opt.hpp @@ -20,6 +20,7 @@ namespace ocl count = err - okay + 1, }; + template struct opt final { explicit opt(const return_type& return_type) @@ -27,16 +28,28 @@ namespace ocl { } - opt& expect(const char* input) + opt& expect(const char_type* input) { if (m_ret == return_type::err) { - throw std::runtime_error(input); + throw std::runtime_error(input ? input : "opt::error"); } return *this; } + template + opt& expect_or_handle(const char_type* input) + { + if (m_ret == return_type::err) + { + ErrorHandler err_handler; + err_handler(input ? input : "opt::error"); + } + + return *this; + } + private: return_type m_ret; }; diff --git a/dev/lib/memory/allocator_system.hpp b/dev/lib/memory/allocator_system.hpp new file mode 100644 index 0000000..1243ed5 --- /dev/null +++ b/dev/lib/memory/allocator_system.hpp @@ -0,0 +1,75 @@ +/* + * File: core/allocator_system.hpp + * Purpose: Allocator System container. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss. Licensed under the BSL 1.0 license + */ + +#ifndef _OCL_ALLOCATOR_SYSTEM_HPP +#define _OCL_ALLOCATOR_SYSTEM_HPP + +#include +#include +#include + +namespace ocl +{ + template + struct new_op final + { + inline auto operator()() -> type* + { + return new type; + } + + template + inline auto var_alloc(var_type... args) -> type* + { + return new type{args...}; + } + }; + + template + struct delete_op final + { + inline auto operator()(type* t) -> void + { + delete t; + } + }; + + template + class allocator_system + { + allocator_new alloc_; + allocator_delete del_; + + public: + allocator_system() = default; + ~allocator_system() = default; + + allocator_system& operator=(const allocator_system&) = delete; + allocator_system(const allocator_system&) = delete; + + ret_type* claim() noexcept + { + return alloc_(); + } + + template + auto construct(var_type... args) -> std::shared_ptr + { + return std::shared_ptr(alloc_.template var_alloc(args...), allocator_delete{}); + } + + void unclaim(ret_type* ptr) + { + del_(ptr); + } + }; + + template + using standard_allocator_type = allocator_system, delete_op>; +} // namespace ocl + +#endif // ifndef _OCL_ALLOCATOR_SYSTEM_HPP diff --git a/dev/lib/utility/chunk_string.hpp b/dev/lib/utility/chunk_string.hpp deleted file mode 100644 index 4fe5cc2..0000000 --- a/dev/lib/utility/chunk_string.hpp +++ /dev/null @@ -1,102 +0,0 @@ -/* -* File: core/chunk_string.hpp - * Purpose: String implementation for the OCL C++ library. - * Author: Amlal El Mahrouss (amlal@nekernel.org) - * Copyright 2025, Amlal El Mahrouss - */ - -#ifndef OCL_UTILITY_CHUNK_STRING_HPP -#define OCL_UTILITY_CHUNK_STRING_HPP - -#include - -namespace ocl -{ - template - class basic_chunk_string; - - template - class basic_chunk_string final - { - private: - char_type packed_chunks_[max_chunk_size] = {0}; - std::size_t chunk_total_{}; - - bool bad_{false}; - - public: - const bool& bad{bad_}; - - basic_chunk_string() = default; - - basic_chunk_string(const char_type* in) - { - this->operator+=(in); - } - - basic_chunk_string(const std::basic_string& in) - { - this->operator+=(in); - } - - ~basic_chunk_string() = default; - - basic_chunk_string& operator=(const basic_chunk_string&) = delete; - basic_chunk_string(const basic_chunk_string&) = delete; - - public: - basic_chunk_string& operator+=(const std::basic_string& in) - { - if (in.empty() || bad_) - return *this; - - if (in.size() > max_chunk_size) - { - bad_ = true; - return *this; - } - - if (chunk_total_ > max_chunk_size) - { - bad_ = true; - return *this; - } - - const auto& sz = in.size(); - const static auto size_max_chunk = max_chunk_size; - const auto& ptr = in.data(); - - if (chunk_total_ < size_max_chunk) - { - std::memcpy(packed_chunks_ + chunk_total_, ptr, sz); - chunk_total_ += sz; - } - - return *this; - } - - std::basic_string str() const noexcept - { - static std::basic_string ret; - - if (ret.size() > 0) - ret.clear(); - - ret += packed_chunks_; - - return ret; - } - - void print() noexcept - { - ocl::io::print(packed_chunks_); - } - }; - - template - inline void print(basic_chunk_string& fmt) noexcept - { - fmt.print(); - } -} // namespace ocl -#endif // ifndef OCL_UTILITY_CHUNK_STRING_HPP -- cgit v1.2.3