From 9eec329ebdf4cf079619edb58dbcd78ce42b8626 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 18 Nov 2025 07:44:48 +0100 Subject: feat: lib: error_handler improvements, new method. FIX module improvements. Signed-off-by: Amlal El Mahrouss --- dev/examples/fix/CMakeLists.txt | 3 +++ dev/examples/fix/fix.cc | 9 ++++++--- dev/lib/core/error_handler.hpp | 13 ++++++++++++- dev/lib/core/includes.hpp | 9 +++++++++ dev/lib/fix/fix.hpp | 10 +++++----- dev/lib/io/print.hpp | 24 +++++++++++++++--------- dev/lib/net/modem.hpp | 6 ++++++ 7 files changed, 56 insertions(+), 18 deletions(-) (limited to 'dev') diff --git a/dev/examples/fix/CMakeLists.txt b/dev/examples/fix/CMakeLists.txt index db8921b..5ed31e7 100644 --- a/dev/examples/fix/CMakeLists.txt +++ b/dev/examples/fix/CMakeLists.txt @@ -6,7 +6,10 @@ project( 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 ../../) diff --git a/dev/examples/fix/fix.cc b/dev/examples/fix/fix.cc index ec6668a..4015a67 100644 --- a/dev/examples/fix/fix.cc +++ b/dev/examples/fix/fix.cc @@ -4,10 +4,12 @@ licensed under the MIT license */ +#include #include #include #include #include +#include #include /* finally test it */ @@ -22,12 +24,13 @@ int main(int argc, char** argv) std::cout << "magic_len=" << fix.magic_len_ << std::endl; std::cout << "is_valid=" << std::boolalpha << fix.is_valid() << std::endl; - ocl::fix::must_pass(fix); + ocl::basic_error_handler handler; + ocl::fix::must_pass(fix, handler); for (auto fields : fix.body_) { - std::cout << "key=" << fields.first; - std::cout << ":value=" << fields.second << std::endl; + ocl::io::print("key=", fields.first); + ocl::io::print(", value=", fields.second); } return 0; diff --git a/dev/lib/core/error_handler.hpp b/dev/lib/core/error_handler.hpp index 67bf7b4..4234721 100644 --- a/dev/lib/core/error_handler.hpp +++ b/dev/lib/core/error_handler.hpp @@ -23,7 +23,18 @@ namespace ocl basic_error_handler& operator=(const basic_error_handler&) = delete; basic_error_handler(const basic_error_handler&) = delete; - virtual void operator()(const std::basic_string& msg) + template + void error(const std::basic_string& msg) + { + this->operator()(msg); + + if constexpr (throw_too) + { + throw std::runtime_error(msg); + } + } + + void operator()(const std::basic_string& msg) { ocl::io::print(msg); } diff --git a/dev/lib/core/includes.hpp b/dev/lib/core/includes.hpp index ff59535..78eccd3 100644 --- a/dev/lib/core/includes.hpp +++ b/dev/lib/core/includes.hpp @@ -12,3 +12,12 @@ #include #include #include + +namespace ocl +{ +#ifdef OCL_USE_UTF8 + using char_type = char8_t; +#else + using char_type = char; +#endif +} // namespace ocl \ No newline at end of file diff --git a/dev/lib/fix/fix.hpp b/dev/lib/fix/fix.hpp index 9490004..67376e3 100644 --- a/dev/lib/fix/fix.hpp +++ b/dev/lib/fix/fix.hpp @@ -89,10 +89,10 @@ namespace ocl::fix class basic_range_data final { public: - std::size_t magic_len_; - std::basic_string magic_; - std::size_t body_len_; - std::vector, std::basic_string>> body_; + std::size_t magic_len_{}; + std::basic_string magic_{}; + std::size_t body_len_{}; + std::vector, std::basic_string>> body_{}; static inline const char_type* begin = detail::begin_fix(); @@ -205,7 +205,7 @@ namespace ocl::fix { if (!basic_range.is_valid()) { - handler("Invalid FIX packet"); + handler.template error("Invalid FIX packet"); } } diff --git a/dev/lib/io/print.hpp b/dev/lib/io/print.hpp index 307c06f..466b698 100644 --- a/dev/lib/io/print.hpp +++ b/dev/lib/io/print.hpp @@ -13,13 +13,6 @@ namespace ocl::io { - template - inline void print(T fmt, Args... other) noexcept - { - std::cout << fmt; - print(other...); - } - template inline void print(T fmt) noexcept { @@ -28,14 +21,27 @@ namespace ocl::io inline void print() noexcept { - std::cout << std::endl; + } + + template + inline void print(Args... fmt) noexcept + { + print(fmt...); + print(); + } + + template + inline void print(T fmt, Args... other) noexcept + { + std::cout << fmt; + print(other...); } template inline void println(T... fmt) noexcept { print(fmt...); - print(); + print("\n"); } } // namespace ocl::io diff --git a/dev/lib/net/modem.hpp b/dev/lib/net/modem.hpp index 80b3b7a..7c07191 100644 --- a/dev/lib/net/modem.hpp +++ b/dev/lib/net/modem.hpp @@ -8,6 +8,8 @@ #ifndef _OCL_NET_NETWORK_HPP #define _OCL_NET_NETWORK_HPP +#include + #include #include #include @@ -110,6 +112,10 @@ namespace ocl::net static_assert(af != 0, "Address family is zero"); static_assert(kind != 0, "Kind is zero"); + must_pass(); + must_pass(); + must_pass<(port > 0) && (port < 65536)>(); + fd_ = ::socket(af, kind, 0); server_fd_ = is_server; -- cgit v1.2.3