diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-01 09:09:57 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-01 09:09:57 +0100 |
| commit | 284708d1e8a14867b5276a8e760588d8531835d8 (patch) | |
| tree | 9f651db6d8b51bf97f2fd2fb41efbf5e53498eea /dev/lib | |
| parent | 574d373163f7edade42e935b9e6957cdaf08d94b (diff) | |
feat: generic: new `except` module, network model for FIX, and tracked_ptr improvements.
feat: introduce the `must_pass` concept to validate a container.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/lib')
| -rw-r--r-- | dev/lib/except/error.hpp (renamed from dev/lib/detail/error.hpp) | 6 | ||||
| -rw-r--r-- | dev/lib/fix/network.hpp | 63 | ||||
| -rw-r--r-- | dev/lib/fix/parser.hpp | 19 | ||||
| -rw-r--r-- | dev/lib/io/print.hpp | 4 | ||||
| -rw-r--r-- | dev/lib/logic/opt.hpp | 6 | ||||
| -rw-r--r-- | dev/lib/memory/tracked_ptr.hpp | 23 |
6 files changed, 101 insertions, 20 deletions
diff --git a/dev/lib/detail/error.hpp b/dev/lib/except/error.hpp index 85a5e25..1a07ef0 100644 --- a/dev/lib/detail/error.hpp +++ b/dev/lib/except/error.hpp @@ -9,9 +9,11 @@ #include <stdexcept> -namespace snu +namespace snu::except { using runtime_error = std::runtime_error; -} + using fix_error = runtime_error; + using math_error = runtime_error; +} // namespace snu::except #endif // _SNU_ERR_HPP
\ No newline at end of file diff --git a/dev/lib/fix/network.hpp b/dev/lib/fix/network.hpp index 0ca0920..637559c 100644 --- a/dev/lib/fix/network.hpp +++ b/dev/lib/fix/network.hpp @@ -1,5 +1,5 @@ /* -* File: fix/netowrk.hpp + * File: fix/netowrk.hpp * Purpose: Financial Information Exchange in C++ * Author: Amlal El Mahrouss (founder@snu.systems) * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp all rights reserved. @@ -11,10 +11,67 @@ #include <arpa/inet.h> #include <sys/socket.h> +#include <cstddef> + namespace snu::fix { - class network_rx; - class network_tx; + class delivery_modem; + + typedef int delivery_type; + + /// @brief a delivery modem is a container which establishes a comm. channel between the FIX session and application. + class delivery_modem final + { + public: + explicit delivery_modem() = default; + ~delivery_modem() = default; + + delivery_modem& operator=(const delivery_modem&) = default; + delivery_modem(const delivery_modem&) = default; + + public: + delivery_type fd_{}; + + template <typename T> + bool receive(T& out, std::size_t len) noexcept + { + if (!len) + return false; + + auto ret = ::recv(fd_, &out, len, 0); + return ret > 0; + } + + template <typename T> + bool transmit(T& out, std::size_t len) noexcept + { + if (!out) + return false; + if (!len) + return false; + + auto ret = ::send(fd_, out, len, 0); + return ret > 0; + } + + bool construct() noexcept + { + fd_ = ::socket(SOCK_STREAM, AF_INET, 0); + return fd_ > 0; + } + + bool destroy() noexcept + { + if (!fd_) + return false; + + ::shutdown(fd_, SHUT_RDWR); + + fd_ = 0U; + + return true; + } + }; } // namespace snu::fix #endif // ifndef _SNU_FIX_NETWORK_HPP
\ No newline at end of file diff --git a/dev/lib/fix/parser.hpp b/dev/lib/fix/parser.hpp index e432b6c..e007906 100644 --- a/dev/lib/fix/parser.hpp +++ b/dev/lib/fix/parser.hpp @@ -86,9 +86,9 @@ namespace snu::fix class range_data final { public: - std::basic_string<char_type> msg_magic_; - std::size_t msg_body_len_; - std::vector<std::pair<std::basic_string<char_type>, std::basic_string<char_type>>> msg_body_; + 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>(); @@ -105,19 +105,20 @@ namespace snu::fix return std::basic_string<char_type>{}; } - for (const auto& pair : msg_body_) + for (const auto& pair : this->body_) { if (pair.first == key) { return pair.second; } } + return std::basic_string<char_type>{}; } bool is_valid() { - return msg_magic_.starts_with(range_data<char_type>::begin); + return magic_.starts_with(range_data<char_type>::begin); } operator bool() @@ -170,14 +171,14 @@ namespace snu::fix std::basic_string<char_type> key = in_tmp.substr(0, in_tmp.find(visitor::eq)); std::basic_string<char_type> val = in_tmp.substr(in_tmp.find(visitor::eq) + 1); - if (ret.msg_magic_.empty()) + if (ret.magic_.empty()) { - ret.msg_magic_ = val; + ret.magic_ = val; } else { - ret.msg_body_.emplace_back(std::make_pair(key, val)); - ret.msg_body_len_ += in_tmp.size(); + ret.body_.emplace_back(std::make_pair(key, val)); + ret.body_len_ += in_tmp.size(); } in_tmp.clear(); diff --git a/dev/lib/io/print.hpp b/dev/lib/io/print.hpp index 0a5b5e2..8656214 100644 --- a/dev/lib/io/print.hpp +++ b/dev/lib/io/print.hpp @@ -10,7 +10,7 @@ #include <iostream> -namespace snu +namespace snu::io { template <typename... T> inline void print(T... fmt) @@ -24,6 +24,6 @@ namespace snu std::cout << std::move(fmt...); std::cout << std::endl; } -} // namespace snu +} // namespace snu::io #endif // ifndef _SNU_PRINT_HPP
\ No newline at end of file diff --git a/dev/lib/logic/opt.hpp b/dev/lib/logic/opt.hpp index facbf2c..d4bb73c 100644 --- a/dev/lib/logic/opt.hpp +++ b/dev/lib/logic/opt.hpp @@ -7,10 +7,10 @@ #ifndef _SNU_OPT_HPP #define _SNU_OPT_HPP -#include <lib/detail/error.hpp> +#include <lib/except/error.hpp> #include <utility> -namespace snu::opt +namespace snu { enum class return_type { @@ -116,6 +116,6 @@ namespace snu::opt { return return_type::err; } -} // namespace snu::opt +} // namespace snu #endif /* ifndef _SNU_OPT_HPP */ diff --git a/dev/lib/memory/tracked_ptr.hpp b/dev/lib/memory/tracked_ptr.hpp index b7e55f7..1ff4cca 100644 --- a/dev/lib/memory/tracked_ptr.hpp +++ b/dev/lib/memory/tracked_ptr.hpp @@ -31,6 +31,13 @@ namespace snu::memory std::atomic<size_t> deallocated_count_ = 0; public: + explicit tracked_allocator() = default; + virtual ~tracked_allocator() = default; + + tracked_allocator& operator=(const tracked_allocator&) = default; + tracked_allocator(const tracked_allocator&) = default; + + public: template <typename... U> void retain(T*& ptr, U&&... args) { @@ -68,6 +75,13 @@ namespace snu::memory tracked_allocator<T> allocator_; public: + explicit tracked_mgr() = default; + virtual ~tracked_mgr() = default; + + tracked_mgr& operator=(const tracked_mgr&) = default; + tracked_mgr(const tracked_mgr&) = default; + + public: const tracked_allocator<T>& allocator() noexcept { return allocator_; @@ -105,7 +119,7 @@ namespace snu::memory ptr_ = tracked_ptr::manager().retain(std::forward<U>(args)...); } - ~tracked_ptr() noexcept + virtual ~tracked_ptr() noexcept { this->reset(); } @@ -192,4 +206,11 @@ namespace snu::memory { a.swap(b); } + + /// @brief a Must Pass function is a standard way to verify a container' validity, inspired from NeKernel/VMKernel. + template <typename T> + inline void must_pass(tracked_ptr<T>& ptr) noexcept + { + assert(ptr.manager().allocator().allocated_count_ < ptr.manager().allocator().deallocated_count_); + } } // namespace snu::memory
\ No newline at end of file |
