diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-18 07:56:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-18 07:56:32 +0100 |
| commit | 4fd646501b75cb1d94a6e17d2387372d72ce797b (patch) | |
| tree | b9b6d442f8a92fea4c8943a1504dba105b133596 | |
| parent | d6d127aefb6819fa97ef11aef0998c03a3aad88e (diff) | |
| parent | 9eec329ebdf4cf079619edb58dbcd78ce42b8626 (diff) | |
Merge pull request #10 from amlel-el-mahrouss/develop
fix: IO and FIX modules.
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | dev/examples/fix/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | dev/examples/fix/fix.cc | 9 | ||||
| -rw-r--r-- | dev/lib/core/error_handler.hpp | 13 | ||||
| -rw-r--r-- | dev/lib/core/includes.hpp | 9 | ||||
| -rw-r--r-- | dev/lib/fix/fix.hpp | 10 | ||||
| -rw-r--r-- | dev/lib/io/print.hpp | 24 | ||||
| -rw-r--r-- | dev/lib/net/modem.hpp | 6 | ||||
| -rw-r--r-- | dev/lib/utility/cgi_writer.hpp | 18 |
9 files changed, 66 insertions, 27 deletions
@@ -10,6 +10,7 @@ docs/html docs/latex +build*/ build/ .idea 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 <lib/core/error_handler.hpp> #include <lib/net/modem.hpp> #include <lib/fix/fix.hpp> #include <iostream> #include <unistd.h> +#include <lib/io/print.hpp> #include <sys/socket.h> /* 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<char, ocl::basic_error_handler>(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<char>& msg) + template <bool throw_too = false> + void error(const std::basic_string<char>& msg) + { + this->operator()(msg); + + if constexpr (throw_too) + { + throw std::runtime_error(msg); + } + } + + void operator()(const std::basic_string<char>& 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 <boost/core/demangle.hpp> #include <boost/core/null_deleter.hpp> #include <boost/container/allocator.hpp> + +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<char_type> magic_; - std::size_t body_len_; - std::vector<std::pair<std::basic_string<char_type>, std::basic_string<char_type>>> body_; + std::size_t magic_len_{}; + std::basic_string<char_type> magic_{}; + std::size_t body_len_{}; + std::vector<std::pair<std::basic_string<char_type>, std::basic_string<char_type>>> body_{}; static inline const char_type* begin = detail::begin_fix<char_type>(); @@ -205,7 +205,7 @@ namespace ocl::fix { if (!basic_range.is_valid()) { - handler("Invalid FIX packet"); + handler.template error<true>("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 <typename T, typename... Args> - inline void print(T fmt, Args... other) noexcept - { - std::cout << fmt; - print(other...); - } - template <typename T> inline void print(T fmt) noexcept { @@ -28,14 +21,27 @@ namespace ocl::io inline void print() noexcept { - std::cout << std::endl; + } + + template <typename... Args> + inline void print(Args... fmt) noexcept + { + print(fmt...); + print(); + } + + template <typename T, typename... Args> + inline void print(T fmt, Args... other) noexcept + { + std::cout << fmt; + print(other...); } template <typename... T> 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 <lib/tests/hpptest.hpp> + #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> @@ -110,6 +112,10 @@ namespace ocl::net static_assert(af != 0, "Address family is zero"); static_assert(kind != 0, "Kind is zero"); + must_pass<af == AF_INET || af == AF_INET6>(); + must_pass<kind == SOCK_STREAM || kind == SOCK_DGRAM>(); + must_pass<(port > 0) && (port < 65536)>(); + fd_ = ::socket(af, kind, 0); server_fd_ = is_server; diff --git a/dev/lib/utility/cgi_writer.hpp b/dev/lib/utility/cgi_writer.hpp index d54d9de..6f59fa5 100644 --- a/dev/lib/utility/cgi_writer.hpp +++ b/dev/lib/utility/cgi_writer.hpp @@ -8,7 +8,7 @@ #define _OCL_CGI_WRITER_HPP #include <lib/io/print.hpp> -#include <lib/utility/chunk_string.hpp> +#include <lib/core/chunk_string.hpp> #include <sstream> #include <format> @@ -21,7 +21,7 @@ namespace ocl class basic_writer { private: - basic_writer& eval_(const ocl::basic_chunk_string<char_type>& mime, const ocl::basic_chunk_string<char_type>& ss) noexcept + basic_writer& eval_(const basic_chunk_string<char_type>& mime, const basic_chunk_string<char_type>& ss) noexcept { std::basic_stringstream<char_type> ss_out; @@ -30,7 +30,7 @@ namespace ocl ss_out << std::format("Content-Length: {}\r\n\r\n", ss.str().size()); ss_out << ss.str(); - ocl::io::print(ss_out.str()); + io::print(ss_out.str()); return *this; } @@ -43,32 +43,32 @@ namespace ocl basic_writer(const basic_writer&) = default; public: - friend void operator<<(basic_writer& self, const ocl::basic_chunk_string<char_type>& ss_in) + friend void operator<<(basic_writer& self, const basic_chunk_string<char_type>& ss_in) { self = self.eval_("text/plain", ss_in); } - basic_writer& binary(const ocl::basic_chunk_string<char_type>& ss_in) + basic_writer& binary(const basic_chunk_string<char_type>& ss_in) { return this->eval_("application/octet-stream", ss_in); } - basic_writer& html(const ocl::basic_chunk_string<char_type>& ss_in) + basic_writer& html(const basic_chunk_string<char_type>& ss_in) { return this->eval_("text/html", ss_in); } - basic_writer& xml(const ocl::basic_chunk_string<char_type>& ss_in) + basic_writer& xml(const basic_chunk_string<char_type>& ss_in) { return this->eval_("application/xml", ss_in); } - basic_writer& json(const ocl::basic_chunk_string<char_type>& ss_in) + basic_writer& json(const basic_chunk_string<char_type>& ss_in) { return this->eval_("application/json", ss_in); } - basic_writer& js(const ocl::basic_chunk_string<char_type>& ss_in) + basic_writer& js(const basic_chunk_string<char_type>& ss_in) { return this->eval_("text/javascript", ss_in); } |
