diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-07-26 01:47:32 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-07-26 01:47:32 +0100 |
| commit | c52dbf5513ae7f106634967162da5cfb01dc5af3 (patch) | |
| tree | b6715d0fdacebd48491b9b05cf85f1d92028f84b /dev | |
| parent | 01565adb9cf5ef991196f56c7f5f7b6161daa005 (diff) | |
feat: SOCL v1.0.2, changelog soon!v1.0.2
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev')
28 files changed, 1356 insertions, 0 deletions
diff --git a/dev/examples/cgi/CMakeLists.txt b/dev/examples/cgi/CMakeLists.txt new file mode 100644 index 0000000..391899f --- /dev/null +++ b/dev/examples/cgi/CMakeLists.txt @@ -0,0 +1,12 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + CGI + VERSION 1.0 + LANGUAGES CXX) + +add_executable(CGI cgi.cc) + +set_property(TARGET CGI PROPERTY CXX_STANDARD 20) +target_include_directories(CGI PUBLIC ../../) diff --git a/dev/examples/cgi/cgi.cc b/dev/examples/cgi/cgi.cc new file mode 100644 index 0000000..b4c0b34 --- /dev/null +++ b/dev/examples/cgi/cgi.cc @@ -0,0 +1,91 @@ +/* + cgi example + written by Amlal El Mahrouss. + licensed under the MIT license + */ + +#include <lib/utility/cgi.hpp> +#include <fstream> +#include <sstream> +#include <string> + +const std::string g_not_found = R"( +<!DOCTYPE html> +<html> +<head> + <meta charset="UTF-8"> + <title>error | snu-lib</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>snu'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) +{ + // ... let's assume we serve data. + + snu::cgi::cgi_writer writer; + std::stringstream ss_file; + + std::ifstream fp("index.html"); + + if (fp.good()) + ss_file << fp.rdbuf(); + else + ss_file << g_not_found; + + fp.close(); + + writer.eval_html(ss_file); + + return 0; +} diff --git a/dev/examples/equiv/CMakeLists.txt b/dev/examples/equiv/CMakeLists.txt new file mode 100644 index 0000000..8b29e23 --- /dev/null +++ b/dev/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 ../../) diff --git a/dev/examples/equiv/equiv.cc b/dev/examples/equiv/equiv.cc new file mode 100644 index 0000000..896637d --- /dev/null +++ b/dev/examples/equiv/equiv.cc @@ -0,0 +1,19 @@ +/* + string checksum example + written by Amlal El Mahrouss. + licensed under the MIT license + */ + +#include <lib/logic/equiv.hpp> +#include <iostream> + +/* finally test it */ +int main(int argc, char** argv) +{ + std::cout << std::boolalpha; + std::cout << snu::equiv::is_same<bool, int>::value << std::endl; + std::cout << snu::equiv::is_same<bool, bool>::value << std::endl; + std::cout << snu::equiv::is_same<int, int>::value << std::endl; + + return 0; +} diff --git a/dev/examples/fix/CMakeLists.txt b/dev/examples/fix/CMakeLists.txt new file mode 100644 index 0000000..db8921b --- /dev/null +++ b/dev/examples/fix/CMakeLists.txt @@ -0,0 +1,12 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + Fix + VERSION 1.0 + LANGUAGES CXX) + +add_executable(Fix fix.cc) + +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 new file mode 100644 index 0000000..f67d8a0 --- /dev/null +++ b/dev/examples/fix/fix.cc @@ -0,0 +1,26 @@ +/* + fix example + written by Amlal El Mahrouss. + licensed under the MIT license + */ + +#include <lib/fix/parser.hpp> +#include <iostream> + +/* finally test it */ +int main(int argc, char** argv) +{ + snu::fix::visitor<char> visitor; + snu::fix::range_data<char> fix = visitor.visit("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|"); + + std::cout << "magic: " << fix.msg_magic_ << std::endl; + std::cout << "is_valid: " << std::boolalpha << fix.is_valid() << std::endl; + + for (auto fields : fix.msg_body_) + { + std::cout << "key: " << fields.first; + std::cout << ", value: " << fields.second << std::endl; + } + + return 0; +} diff --git a/dev/examples/opt/CMakeLists.txt b/dev/examples/opt/CMakeLists.txt new file mode 100644 index 0000000..b63b1de --- /dev/null +++ b/dev/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 ../../) diff --git a/dev/examples/opt/opt.cc b/dev/examples/opt/opt.cc new file mode 100644 index 0000000..a914d8d --- /dev/null +++ b/dev/examples/opt/opt.cc @@ -0,0 +1,44 @@ +/* + string checksum example + written by Amlal El Mahrouss. + licensed under the MIT license + */ + +#include <lib/logic/opt.hpp> +#include <lib/io/print.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 = snu::opt::opt(snu::opt::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... + + snu::println("Testing data..."); + + auto opt = do_some("Ohio", "Ohio"); + opt.expect("Checksum failed, Ohio isn't Ohio!"); + + + return 0; +} diff --git a/dev/examples/tracked_ptr/CMakeLists.txt b/dev/examples/tracked_ptr/CMakeLists.txt new file mode 100644 index 0000000..9396506 --- /dev/null +++ b/dev/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 ../../) diff --git a/dev/examples/tracked_ptr/tracked_ptr.cc b/dev/examples/tracked_ptr/tracked_ptr.cc new file mode 100644 index 0000000..c182494 --- /dev/null +++ b/dev/examples/tracked_ptr/tracked_ptr.cc @@ -0,0 +1,52 @@ +/* + tracked_ptr example + written by Amlal El Mahrouss. + licensed under the MIT license + */ + +#include <lib/memory/tracked_ptr.hpp> +#include <iostream> + +void summon_tracked_ptr() +{ + snu::memory::tracked_ptr<int> ptr = snu::memory::make_tracked(42); + std::cout << ptr.data() << "=" << ptr.manager().allocator().allocated_count_ << std::endl; +} + +void summon_leak_tracked_ptr() +{ + snu::memory::tracked_ptr<int>* ptr = new snu::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(); + + snu::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; + + return 0; +} diff --git a/dev/lib/detail/error.hpp b/dev/lib/detail/error.hpp new file mode 100644 index 0000000..85a5e25 --- /dev/null +++ b/dev/lib/detail/error.hpp @@ -0,0 +1,17 @@ +/* + * File: opt.hpp + * Author: Amlal El Mahrouss, + * Copyright 2023-2025, Amlal El Mahrouss/SNU Systems Corp all rights reserved. + */ + +#ifndef _SNU_ERR_HPP +#define _SNU_ERR_HPP + +#include <stdexcept> + +namespace snu +{ + using runtime_error = std::runtime_error; +} + +#endif // _SNU_ERR_HPP
\ No newline at end of file diff --git a/dev/lib/fix/network.hpp b/dev/lib/fix/network.hpp new file mode 100644 index 0000000..0ca0920 --- /dev/null +++ b/dev/lib/fix/network.hpp @@ -0,0 +1,20 @@ +/* +* 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. + */ + +#ifndef _SNU_FIX_NETWORK_HPP +#define _SNU_FIX_NETWORK_HPP + +#include <arpa/inet.h> +#include <sys/socket.h> + +namespace snu::fix +{ + class network_rx; + class network_tx; +} // 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 new file mode 100644 index 0000000..e432b6c --- /dev/null +++ b/dev/lib/fix/parser.hpp @@ -0,0 +1,198 @@ +/* + * File: fix/parser.hpp + * Purpose: Financial Information Exchange parser in C++ + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp all rights reserved. + */ + +#ifndef _SNU_FIX_PARSER_HPP +#define _SNU_FIX_PARSER_HPP + +#include <cstddef> +#include <cassert> +#include <utility> +#include <string> +#include <vector> + +namespace snu::fix +{ + template <typename char_type> + struct visitor; + + template <typename char_type> + struct range; + + template <typename char_type> + struct range_data; + + /// @brief Buffer+Length structure + template <typename char_type> + using range_ptr_t = range<char_type>; + + namespace detail + { + template <typename char_type> + const char_type* begin_fix(); + + template <> + inline const char* begin_fix<char>() + { + return "FIX."; + } + + template <> + inline const char16_t* begin_fix<char16_t>() + { + return u"FIX."; + } + + template <> + inline const char8_t* begin_fix<char8_t>() + { + return u8"FIX."; + } + } // namespace detail + + template <typename char_type> + struct range final + { + char_type* bytes_; + size_t length_; + + bool is_valid() + { + return bytes_ && length_ > 0; + } + + operator bool() + { + return this->is_valid(); + } + }; + + /// @brief Convert range to usable string. + /// @note This function assumes that the range is valid and contains ASCII bytes. + template <typename char_type> + inline std::basic_string<char_type> to_string(range<char_type>& range) noexcept + { + if (range.length_ < 0) + return std::basic_string<char_type>{}; + + return std::basic_string<char_type>(range.ascii_bytes_, range.length_); + } + + /// @brief a range object containing the FIX packet values. + template <typename char_type> + 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_; + + static inline const char_type* begin = detail::begin_fix<char_type>(); + + explicit range_data() = default; + ~range_data() = default; + + range_data& operator=(const range_data&) = default; + range_data(const range_data&) = default; + + std::basic_string<char_type> operator[](const std::basic_string<char_type>& key) + { + if (key.empty()) + { + return std::basic_string<char_type>{}; + } + + for (const auto& pair : msg_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); + } + + operator bool() + { + return this->is_valid(); + } + }; + + /// @brief visitor object which returns a fix::range_data instance. + template <typename char_type> + class visitor final + { + public: + static constexpr const char_type soh = '|'; + static constexpr const char_type eq = '='; + static constexpr uint32_t base = 10U; + + explicit visitor() = default; + ~visitor() = default; + + visitor& operator=(const visitor&) = default; + visitor(const visitor&) = default; + + range<char_type> operator()(const std::basic_string<char_type>& in) + { + return this->visit(in); + } + + range_data<char_type> visit(const std::basic_string<char_type>& in) + { + thread_local range_data<char_type> ret{}; + + if (in.empty()) + return ret; + + thread_local std::basic_string<char_type> in_tmp{}; + + in_tmp.reserve(in.size()); + + try + { + for (auto& ch : in) + { + if (ch != visitor::soh) + { + in_tmp += ch; + continue; + } + + 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()) + { + ret.msg_magic_ = val; + } + else + { + ret.msg_body_.emplace_back(std::make_pair(key, val)); + ret.msg_body_len_ += in_tmp.size(); + } + + in_tmp.clear(); + } + } + catch (...) + { + in_tmp.clear(); + return ret; + } + + in_tmp.clear(); + return ret; + } + }; +} // namespace snu::fix + +#endif // ifndef _SNU_FIX_PARSER_HPP diff --git a/dev/lib/io/print.hpp b/dev/lib/io/print.hpp new file mode 100644 index 0000000..0a5b5e2 --- /dev/null +++ b/dev/lib/io/print.hpp @@ -0,0 +1,29 @@ +/* + * File: print.hpp + * Purpose: SNU Print library + * Author: Amlal El Mahrouss. (founder@snu.systems) + * Copyright 2025, SNU Systems Corp all rights reserved. + */ + +#ifndef _SNU_PRINT_HPP +#define _SNU_PRINT_HPP + +#include <iostream> + +namespace snu +{ + template <typename... T> + inline void print(T... fmt) + { + std::cout << std::move(fmt...); + } + + template <typename... T> + inline void println(T... fmt) + { + std::cout << std::move(fmt...); + std::cout << std::endl; + } +} // namespace snu + +#endif // ifndef _SNU_PRINT_HPP
\ No newline at end of file diff --git a/dev/lib/logic/equiv.hpp b/dev/lib/logic/equiv.hpp new file mode 100644 index 0000000..4bb5904 --- /dev/null +++ b/dev/lib/logic/equiv.hpp @@ -0,0 +1,102 @@ +/* + * File: equiv.hpp + * Purpose: Equivalence runtime c++ header. + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp all rights reserved. + */ + +#pragma once + +namespace snu::equiv +{ + template <typename T> + struct basic_hash_trait + { + /// @brief hash from T's result. + static typename T::result hash() + { + static T val; + return val.hash(); + } + }; + + template <typename T, typename U> + struct is_same + { + static constexpr bool value = false; + + /// @brief check if hash matches what we expect. + constexpr bool operator()() noexcept + { + return T::hash() == U::hash(); + } + }; + + template <typename T> + struct is_same<T, T> + { + static constexpr bool value = true; + }; + + template <typename T, typename U> + struct is_not_same + { + static constexpr bool value = true; + + constexpr bool operator()() noexcept + { + return T::hash() != U::hash(); + } + }; + + template <typename T> + struct is_not_same<T, T> + { + static constexpr bool value = false; + }; + + template <typename T> + struct equiv_is_int8 + { + private: + T left_ = 255, right_ = 255; + + public: + using result = T; + + constexpr result hash() + { + return (left_ + right_) < 1; + } + }; + + template <typename T> + struct equiv_not_int8 + { + private: + T left_ = 255, right_ = 255; + + public: + using result = T; + + constexpr result hash() + { + return (left_ + right_) > 0; + } + }; + + template <typename T> + struct equiv_is_real + { + private: + T left_ = 5, right_ = 3; + + public: + using result = T; + + constexpr result hash() + { + return left_ / right_; + } + }; +} // namespace snu::equiv
\ No newline at end of file diff --git a/dev/lib/logic/math.hpp b/dev/lib/logic/math.hpp new file mode 100644 index 0000000..2613799 --- /dev/null +++ b/dev/lib/logic/math.hpp @@ -0,0 +1,35 @@ +/* + * File: math.hpp + * Purpose: Mathematics c++ header. + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss all rights reserved. + */ + +#pragma once + +#include <cmath> + +namespace snu::math +{ + template <size_t T> + struct is_non_boolean_integer final + { + static constexpr const bool value = true; + }; + + template <> + struct is_non_boolean_integer<false> final + { + static constexpr const bool value = false; + }; + + template <> + struct is_non_boolean_integer<true> final + { + static constexpr const bool value = false; + }; + + constexpr inline auto not_a_number = NAN; + constexpr inline auto positive_infinity = INFINITY; + constexpr inline auto negative_infinity = -positive_infinity; +} // namespace snu::math
\ No newline at end of file diff --git a/dev/lib/logic/opt.hpp b/dev/lib/logic/opt.hpp new file mode 100644 index 0000000..facbf2c --- /dev/null +++ b/dev/lib/logic/opt.hpp @@ -0,0 +1,121 @@ +/* + * File: opt.hpp + * Author: Amlal El Mahrouss, + * Copyright 2023-2025, Amlal El Mahrouss/SNU Systems Corp all rights reserved. + */ + +#ifndef _SNU_OPT_HPP +#define _SNU_OPT_HPP + +#include <lib/detail/error.hpp> +#include <utility> + +namespace snu::opt +{ + enum class return_type + { + invalid = 0, + okay = 100, + err, + count = err - okay + 1, + }; + + struct opt final + { + explicit opt(const return_type& return_type) + : m_ret(return_type) + { + } + + opt& expect(const char* input) + { + if (m_ret == return_type::err) + { + throw std::runtime_error(input); + } + + return *this; + } + + private: + return_type m_ret; + }; + + template <typename Teller, typename... Lst> + inline return_type eval(Teller tell, Lst&&... arg) + { + return tell(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err; + } + + namespace traits + { + struct int_eq_teller + { + explicit int_eq_teller() + { + } + + bool operator()(int a, int b) + { + return (a == b); + } + }; + + struct int_greater_than_teller + { + explicit int_greater_than_teller() + { + } + + bool operator()(int a, int b) + { + return (a > b); + } + }; + + struct int_less_than_teller + { + explicit int_less_than_teller() + { + } + + bool operator()(int a, int b) + { + return (a < b); + } + }; + } // namespace traits + + template <typename... Lst> + inline return_type eval_less_than(Lst&&... arg) + { + static traits::int_less_than_teller eq; + return eq(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err; + } + + template <typename... Lst> + inline return_type eval_eq(Lst&&... arg) + { + static traits::int_eq_teller less_than; + return less_than(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err; + } + + template <typename... Lst> + inline return_type eval_greater_than(Lst&&... arg) + { + static traits::int_greater_than_teller greater_than; + return greater_than(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err; + } + + inline return_type eval_true() + { + return return_type::okay; + } + + inline return_type eval_false() + { + return return_type::err; + } +} // namespace snu::opt + +#endif /* ifndef _SNU_OPT_HPP */ diff --git a/dev/lib/memory/tracked_ptr.hpp b/dev/lib/memory/tracked_ptr.hpp new file mode 100644 index 0000000..238d521 --- /dev/null +++ b/dev/lib/memory/tracked_ptr.hpp @@ -0,0 +1,196 @@ +/* + * File: memory/tracked_ptr.hpp + * Purpose: Custom smart pointer implementation in C++ + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp all rights reserved. + */ + +#pragma once + +#include <cstddef> +#include <utility> +#include <memory> +#include <atomic> + +namespace snu::memory +{ + template <typename T> + class tracked_allocator; + + template <typename T> + class tracked_mgr; + + template <typename T, typename Mgr> + class tracked_ptr; + + template <typename T> + class tracked_allocator + { + public: + std::atomic<size_t> allocated_count_ = 0; + std::atomic<size_t> deallocated_count_ = 0; + + public: + template <typename... U> + void retain(T*& ptr, U&&... args) + { + ptr = new T(args...); + + if (ptr) + { + ++allocated_count_; + } + else + { + throw std::bad_alloc(); + } + } + + void dispose(T* ptr) + { + if (ptr) + { + if (allocated_count_) + --allocated_count_; + + ++deallocated_count_; + + delete ptr; + ptr = nullptr; + } + } + }; + + template <typename T> + class tracked_mgr + { + private: + tracked_allocator<T> allocator_; + + public: + const tracked_allocator<T>& allocator() + { + return allocator_; + } + + template <typename... U> + T* retain(U&&... args) + { + T* ptr = nullptr; + allocator_.retain(ptr, std::forward<U>(args)...); + return ptr; + } + + void dispose(T* ptr) + { + allocator_.dispose(ptr); + } + }; + + template <typename T, typename Mgr = tracked_mgr<T>> + class tracked_ptr + { + public: + static Mgr& manager() + { + static Mgr mgr; + return mgr; + } + + public: + template <typename... U> + explicit tracked_ptr(U&&... args) + : ptr_(nullptr) + { + ptr_ = tracked_ptr::manager().retain(std::forward<U>(args)...); + } + + ~tracked_ptr() noexcept + { + this->reset(); + } + + tracked_ptr(const tracked_ptr&) = delete; + tracked_ptr& operator=(const tracked_ptr&) = delete; + + public: + void reset() + { + if (ptr_) + { + tracked_ptr::manager().dispose(ptr_); + ptr_ = nullptr; + } + } + + T* get() const + { + return ptr_; + } + + T* data() + { + return ptr_; + } + + T& operator*() const + { + return *ptr_; + } + + T* operator->() const + { + return ptr_; + } + + explicit operator bool() const + { + return ptr_ != nullptr; + } + + void swap(tracked_ptr& other) + { + std::swap(ptr_, other.ptr_); + } + + public: + tracked_ptr(tracked_ptr&& other) noexcept + : ptr_(other.ptr_) + { + other.ptr_ = nullptr; + } + + tracked_ptr& operator=(tracked_ptr&& other) noexcept + { + if (this != &other) + { + this->reset(); + ptr_ = other.ptr_; + other.ptr_ = nullptr; + } + + return *this; + } + + private: + T* ptr_ = nullptr; + }; + + template <typename T> + inline auto make_tracked(T arg) -> tracked_ptr<T> + { + return tracked_ptr<T>(std::forward<T>(arg)); + } + + template <typename U, typename... T> + inline auto make_tracked(T&&... arg) -> tracked_ptr<U> + { + return tracked_ptr<U>(std::forward<T>(arg)...); + } + + template <typename T> + inline void swap(tracked_ptr<T>& a, tracked_ptr<T>& b) + { + a.swap(b); + } +} // namespace snu::memory
\ No newline at end of file diff --git a/dev/lib/utility/cgi.hpp b/dev/lib/utility/cgi.hpp new file mode 100644 index 0000000..068adf1 --- /dev/null +++ b/dev/lib/utility/cgi.hpp @@ -0,0 +1,62 @@ +/* + * File: cgi.hpp + * Author: Amlal El Mahrouss, + * Copyright 2023-2025, Amlal El Mahrouss all rights reserved. + */ + +#ifndef _SNU_CGI_HPP +#define _SNU_CGI_HPP + +#include <cstdio> +#include <string> +#include <sstream> + +namespace snu +{ + namespace cgi + { + /// @brief CGI Writer class, writes to stdout; as CGI expects. + class writer final + { + writer& eval_(const std::string& mime, const std::stringstream& ss) noexcept + { + std::printf("Content-Type: %s\r\n", mime.c_str()); + std::printf("Server: %s\r\n", "snu-cpp-library"); + std::printf("Content-Length: %ld\r\n\r\n", ss.str().size()); + std::printf("%s", ss.str().c_str()); + + return *this; + } + + public: + explicit writer() = default; + ~writer() = default; + + writer& operator=(const writer&) = default; + writer(const writer&) = default; + + public: + writer& eval_html(const std::stringstream& ss_html) + { + return this->eval_("text/html", ss_html); + } + + writer& eval_xml(const std::stringstream& ss_html) + { + return this->eval_("application/xml", ss_html); + } + + writer& eval_json(const std::stringstream& ss_html) + { + return this->eval_("application/json", ss_html); + } + + writer& eval_js(const std::stringstream& ss_html) + { + return this->eval_("text/javascript", ss_html); + } + }; + } // namespace cgi +} // namespace snu + +#endif // ifndef _SNU_CGI_HPP
\ No newline at end of file diff --git a/dev/lib/utility/crc32.hpp b/dev/lib/utility/crc32.hpp new file mode 100644 index 0000000..de78d4e --- /dev/null +++ b/dev/lib/utility/crc32.hpp @@ -0,0 +1,74 @@ +/* + * File: embfs.hpp + * Purpose: Embedded File System. + * Author: Amlal El Mahrouss, + * Copyright 2025, Amlal El Mahrouss all rights reserved. + */ + +#ifndef _SNU_CRC32_HPP +#define _SNU_CRC32_HPP + +#include <cstdint> +#include <cstddef> + +/// @brief Crc32 implementation in C++ +/// @author Amlal EL Mahrouss (founder@snu.systems) + +namespace snu::crc32 +{ + namespace details + { + inline constexpr const std::uint16_t g_crc_sz = 256; + + inline std::uint32_t g_crc_table[g_crc_sz] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, + 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, + 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, + 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, + 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, + 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, + 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, + 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, + 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, + 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, + 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, + 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, + 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, + 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, + 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, + 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, + 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, + 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, + 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, + 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, + 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, + 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; + /// + + inline std::uint32_t + calculate_crc32_(const char* in, size_t len) noexcept + { + if (!in || *in == 0) + return ~0; + + std::uint32_t crc = 0xffffffff; + + while ((len--) > 0) + crc = (crc >> 8) ^ g_crc_table[(crc ^ *(in++)) & 0xFF]; + + return ~crc; + } + } // namespace details +} // namespace snu::crc32 + +#endif // !_SNU_CRC32_HPP
\ No newline at end of file diff --git a/dev/lib/utility/embfs.hpp b/dev/lib/utility/embfs.hpp new file mode 100644 index 0000000..cb08eb9 --- /dev/null +++ b/dev/lib/utility/embfs.hpp @@ -0,0 +1,79 @@ +/* + * File: embfs.hpp + * Purpose: Embedded File System. + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss all rights reserved. + */ + +#ifndef _SNU_EMBFS_HPP +#define _SNU_EMBFS_HPP + +#include <cstdint> +#include <cstddef> + +/// @brief A filesystem designed for tiny storage medias. +/// @author Amlal EL Mahrouss (founder@snu.systems) + +namespace snu::embfs +{ + namespace traits + { + struct embfs_superblock; + struct embfs_inode; + + inline constexpr const size_t _superblock_name_len = 16; + inline constexpr const size_t _superblock_reserve_len = 462; + inline constexpr const size_t _inode_name_len = 128; + inline constexpr const size_t _inode_arr_len = 12; + inline constexpr const size_t _inode_lookup_len = 8; + +#ifdef EMBFS_28BIT_LBA + typedef std::uint32_t lba_t; +#elif defined(EMBFS_48BIT_LBA) + typedef std::uint64_t lba_t; +#else + typedef std::uint32_t lba_t; +#endif + + typedef std::int16_t sword_t; + typedef std::int32_t sdword_t; + + typedef std::uint8_t utf8_char_t; + + /// @brief Superblock data structure + struct embfs_superblock + { + sword_t s_block_mag; + sdword_t s_num_inodes; + sdword_t s_part_size; + sdword_t s_part_used; + sdword_t s_version; + sword_t s_sector_sz; + lba_t s_inode_start; + lba_t s_inode_end; + utf8_char_t s_name[_superblock_name_len]; + utf8_char_t s_reserved[_superblock_reserve_len]; + }; + + /// @brief Indexed node structure. + /// @brief i_name file name + /// @brief i_size_virt, i_size_phys: virtual and physical (sector count) size. + /// @brief i_offset direct block pointer. + /// @brief i_checksum crc32 checksum. + /// @brief i_flags_perms flags and permissions + /// @brief i_acl_* ACL to keep track of inode allocation status. + struct embfs_inode + { + utf8_char_t i_name[_inode_name_len]; + sword_t i_size_virt, i_size_phys; + lba_t i_offset[_inode_lookup_len]; + sword_t i_checksum, i_flags_perms; + lba_t i_acl_creat, i_acl_edit, i_acl_delet; + }; + + /// @brief Indexed node linear array. + typedef embfs_inode embfs_inode_arr_t[_inode_arr_len]; + } // namespace traits +} // namespace snu::embfs + +#endif // ifndef _SNU_EMBFS_HPP
\ No newline at end of file diff --git a/dev/tests/.gitkeep b/dev/tests/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/dev/tests/.gitkeep diff --git a/dev/tests/fix_basic/CMakeLists.txt b/dev/tests/fix_basic/CMakeLists.txt new file mode 100644 index 0000000..3a2542d --- /dev/null +++ b/dev/tests/fix_basic/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.10) +project(TrackedPtrTests) + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip +) + +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +enable_testing() + +add_executable(FIXTestBasic fix_test.cc) +target_link_libraries(FIXTestBasic gtest_main) + +set_property(TARGET FIXTestBasic PROPERTY CXX_STANDARD 20) +target_include_directories(FIXTestBasic PUBLIC ../../) + +include(GoogleTest) +gtest_discover_tests(FIXTestBasic) diff --git a/dev/tests/fix_basic/fix_test.cc b/dev/tests/fix_basic/fix_test.cc new file mode 100644 index 0000000..c9ae0b2 --- /dev/null +++ b/dev/tests/fix_basic/fix_test.cc @@ -0,0 +1,18 @@ +/* + * File: tests/tracked_ptr_test.cc + * Purpose: Custom smart pointer unit tests in C++ + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp all rights reserved. + */ + +#include <lib/fix/parser.hpp> +#include <gtest/gtest.h> + +TEST(FIXTest, BasicFIXUsage) +{ + snu::fix::visitor<char> visitor; + snu::fix::range_data<char> fix = visitor.visit("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|"); + + EXPECT_EQ(fix.msg_magic_, "FIX.4.2"); + EXPECT_TRUE(fix.is_valid()); +}
\ No newline at end of file diff --git a/dev/tests/tracked_ptr_basic/CMakeLists.txt b/dev/tests/tracked_ptr_basic/CMakeLists.txt new file mode 100644 index 0000000..97f7204 --- /dev/null +++ b/dev/tests/tracked_ptr_basic/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.10) +project(TrackedPtrTests) + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip +) + +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +enable_testing() + +add_executable(TrackedPtrTestBasic tracked_ptr_test.cc) +target_link_libraries(TrackedPtrTestBasic gtest_main) + +set_property(TARGET TrackedPtrTestBasic PROPERTY CXX_STANDARD 20) +target_include_directories(TrackedPtrTestBasic PUBLIC ../../) + +include(GoogleTest) +gtest_discover_tests(TrackedPtrTestBasic) diff --git a/dev/tests/tracked_ptr_basic/tracked_ptr_test.cc b/dev/tests/tracked_ptr_basic/tracked_ptr_test.cc new file mode 100644 index 0000000..462ec5f --- /dev/null +++ b/dev/tests/tracked_ptr_basic/tracked_ptr_test.cc @@ -0,0 +1,25 @@ +/* + * File: tests/tracked_ptr_test.cc + * Purpose: Custom smart pointer unit tests in C++ + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp all rights reserved. + */ + +#include <lib/memory/tracked_ptr.hpp> +#include <gtest/gtest.h> + +TEST(TrackedPtrTest, BasicTrackedPtrUsage) +{ + snu::memory::tracked_ptr<int> ptr = snu::memory::make_tracked<int>(42); + + ASSERT_TRUE(ptr); + EXPECT_EQ(*ptr, 42); + + snu::memory::tracked_ptr<int> ptr2; + + snu::memory::swap(ptr, ptr2); + + ptr2.reset(); + + EXPECT_EQ(snu::memory::tracked_ptr<int>::manager().allocator().allocated_count_, 1); +}
\ No newline at end of file diff --git a/dev/tests/tracked_ptr_leak/CMakeLists.txt b/dev/tests/tracked_ptr_leak/CMakeLists.txt new file mode 100644 index 0000000..17df8f1 --- /dev/null +++ b/dev/tests/tracked_ptr_leak/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.10) +project(TrackedPtrTests) + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip +) + +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +enable_testing() + +add_executable(TrackedPtrTestLeak tracked_ptr_test.cc) +target_link_libraries(TrackedPtrTestLeak gtest_main) + +set_property(TARGET TrackedPtrTestLeak PROPERTY CXX_STANDARD 20) +target_include_directories(TrackedPtrTestLeak PUBLIC ../../) + +include(GoogleTest) +gtest_discover_tests(TrackedPtrTestLeak) diff --git a/dev/tests/tracked_ptr_leak/tracked_ptr_test.cc b/dev/tests/tracked_ptr_leak/tracked_ptr_test.cc new file mode 100644 index 0000000..f583938 --- /dev/null +++ b/dev/tests/tracked_ptr_leak/tracked_ptr_test.cc @@ -0,0 +1,19 @@ +/* + * File: tests/tracked_ptr_test.cc + * Purpose: Custom smart pointer unit tests in C++ + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp all rights reserved. + */ + +#include <lib/memory/tracked_ptr.hpp> +#include <gtest/gtest.h> + +TEST(TrackedPtrTest, LeakTrackedPtrUsage) +{ + snu::memory::tracked_ptr<int>* ptr = new snu::memory::tracked_ptr<int>(42); + snu::memory::tracked_ptr<int>* ptr2 = new snu::memory::tracked_ptr<int>(42); + snu::memory::tracked_ptr<int>* ptr3 = new snu::memory::tracked_ptr<int>(42); + + EXPECT_EQ(snu::memory::tracked_ptr<int>::manager().allocator().allocated_count_, 3); + ASSERT_TRUE(snu::memory::tracked_ptr<int>::manager().allocator().deallocated_count_ == 0); +}
\ No newline at end of file |
