From b0d0e894a4394eed25dbb5cead12edd08a510a92 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 24 Aug 2025 14:13:34 +0200 Subject: feat! use `chunk_string` in the CGI module. Add additional constructors for `chunk_string` Signed-off-by: Amlal El Mahrouss --- dev/examples/cgi/CMakeLists.txt | 4 ++++ dev/examples/cgi/cgi.cc | 11 ++--------- dev/examples/fix/fix.cc | 2 +- dev/lib/core/includes.hpp | 4 ---- dev/lib/except/error.hpp | 4 ++-- dev/lib/fix/parser.hpp | 2 ++ dev/lib/io/print.hpp | 1 - dev/lib/logic/math.hpp | 4 ++-- dev/lib/memory/tracked_ptr.hpp | 30 ++++++++++++++++++++--------- dev/lib/utility/cgi_writer.hpp | 36 +++++++++++++++++++---------------- dev/lib/utility/chunk_string.hpp | 17 ++++++++++++++--- dev/tests/chunk_string/CMakeLists.txt | 3 --- 12 files changed, 68 insertions(+), 50 deletions(-) (limited to 'dev') diff --git a/dev/examples/cgi/CMakeLists.txt b/dev/examples/cgi/CMakeLists.txt index 391899f..9c3cbf6 100644 --- a/dev/examples/cgi/CMakeLists.txt +++ b/dev/examples/cgi/CMakeLists.txt @@ -6,7 +6,11 @@ project( VERSION 1.0 LANGUAGES CXX) +find_package(Boost REQUIRED COMPONENTS container) + add_executable(CGI cgi.cc) +target_link_libraries(CGI PRIVATE Boost::container) + 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 index 4872cb5..7d8353a 100644 --- a/dev/examples/cgi/cgi.cc +++ b/dev/examples/cgi/cgi.cc @@ -5,11 +5,8 @@ */ #include -#include -#include -#include -const std::string error_html = R"( +static snu::basic_chunk_string text_sample = R"( @@ -72,11 +69,7 @@ const std::string error_html = R"( int main(int argc, char** argv) { snu::cgi::basic_writer<> writer; - - std::stringstream ss_file; - ss_file << error_html; - - writer.html(ss_file); + writer << text_sample; return 0; } diff --git a/dev/examples/fix/fix.cc b/dev/examples/fix/fix.cc index ce48186..8730cad 100644 --- a/dev/examples/fix/fix.cc +++ b/dev/examples/fix/fix.cc @@ -4,7 +4,7 @@ licensed under the MIT license */ -#include +#include #include #include #include diff --git a/dev/lib/core/includes.hpp b/dev/lib/core/includes.hpp index 4beec1c..3238342 100644 --- a/dev/lib/core/includes.hpp +++ b/dev/lib/core/includes.hpp @@ -12,7 +12,3 @@ #include #include #include - -#include -#include -#include diff --git a/dev/lib/except/error.hpp b/dev/lib/except/error.hpp index 486d2e6..862dc3e 100644 --- a/dev/lib/except/error.hpp +++ b/dev/lib/except/error.hpp @@ -9,12 +9,12 @@ #include -namespace snu::except +namespace snu { using runtime_error = std::runtime_error; using fix_error = runtime_error; using math_error = runtime_error; using cgi_error = runtime_error; -} // namespace snu::except +} // namespace snu #endif // _SNU_ERR_HPP \ No newline at end of file diff --git a/dev/lib/fix/parser.hpp b/dev/lib/fix/parser.hpp index 8181359..1ace160 100644 --- a/dev/lib/fix/parser.hpp +++ b/dev/lib/fix/parser.hpp @@ -209,6 +209,8 @@ namespace snu::fix ::kill(::getpid(), SIGTRAP); } } + + using fix_tag_type = std::uint32_t; } // namespace snu::fix #endif // ifndef _SNU_FIX_PARSER_HPP diff --git a/dev/lib/io/print.hpp b/dev/lib/io/print.hpp index eb425c1..840b373 100644 --- a/dev/lib/io/print.hpp +++ b/dev/lib/io/print.hpp @@ -8,7 +8,6 @@ #ifndef _SNU_PRINT_HPP #define _SNU_PRINT_HPP -#include #include #include diff --git a/dev/lib/logic/math.hpp b/dev/lib/logic/math.hpp index cab995a..155251b 100644 --- a/dev/lib/logic/math.hpp +++ b/dev/lib/logic/math.hpp @@ -9,7 +9,7 @@ #include -namespace snu::math +namespace snu { template struct is_non_boolean_integer final @@ -32,4 +32,4 @@ namespace snu::math 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 +} // namespace snu \ No newline at end of file diff --git a/dev/lib/memory/tracked_ptr.hpp b/dev/lib/memory/tracked_ptr.hpp index da7e46c..7030f40 100644 --- a/dev/lib/memory/tracked_ptr.hpp +++ b/dev/lib/memory/tracked_ptr.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include @@ -45,19 +45,24 @@ namespace snu::memory template void retain(T*& ptr, U&&... args) { - ptr = new T(args...); + ptr = new T(std::forward(args)...); if (ptr) { ++allocated_count_; + return; } - else - { - throw std::bad_alloc(); - } + + throw std::bad_alloc(); + } + + template + void must_retain(T*& ptr, U&&... args) noexcept + { + this->retain(ptr, args...); } - void dispose(T*& ptr) + void dispose(T*& ptr) noexcept { if (ptr) { @@ -96,10 +101,17 @@ namespace snu::memory { T* ptr = nullptr; allocator_.retain(ptr, std::forward(args)...); + return ptr; } - void dispose(T*& ptr) + template + T* must_retain(U&&... args) noexcept + { + return this->retain(std::forward(args)...); + } + + void dispose(T*& ptr) noexcept { allocator_.dispose(ptr); } @@ -190,7 +202,7 @@ namespace snu::memory } private: - T* ptr_ = nullptr; + T* ptr_{nullptr}; }; template diff --git a/dev/lib/utility/cgi_writer.hpp b/dev/lib/utility/cgi_writer.hpp index 69d0170..70b32cf 100644 --- a/dev/lib/utility/cgi_writer.hpp +++ b/dev/lib/utility/cgi_writer.hpp @@ -7,9 +7,10 @@ #ifndef _SNU_CGI_WRITER_HPP #define _SNU_CGI_WRITER_HPP -#include -#include +#include +#include #include +#include namespace snu { @@ -20,18 +21,16 @@ namespace snu class basic_writer { private: - basic_writer& eval_(const std::basic_string& mime, const std::basic_stringstream& ss) noexcept + basic_writer& eval_(const snu::basic_chunk_string& mime, const snu::basic_chunk_string& ss) noexcept { - std::printf("Content-Type: %s\r\n", mime.c_str()); - std::printf("Server: %s\r\n", "socl-cgi-system"); - std::printf("Content-Length: %ld\r\n\r\n", ss.str().size()); + std::basic_stringstream ss_out; - auto ss_cc = ss.str(); + ss_out << std::format("Content-Type: {}\r\n", mime.str()); + ss_out << std::format("Server: {}\r\n", "SOCL-CGI/1.0"); + ss_out << std::format("Content-Length: {}\r\n\r\n", ss.str().size()); + ss_out << ss.str(); - for (auto& ch : ss_cc) - { - std::printf("%c", ch); - } + snu::io::print(ss_out.str()); return *this; } @@ -44,27 +43,32 @@ namespace snu basic_writer(const basic_writer&) = default; public: - basic_writer& binary(const std::basic_stringstream& ss_in) + friend void operator<<(basic_writer& self, const snu::basic_chunk_string& ss_in) + { + self = self.eval_("text/plain", ss_in); + } + + basic_writer& binary(const snu::basic_chunk_string& ss_in) { return this->eval_("application/octet-stream", ss_in); } - basic_writer& html(const std::basic_stringstream& ss_in) + basic_writer& html(const snu::basic_chunk_string& ss_in) { return this->eval_("text/html", ss_in); } - basic_writer& xml(const std::basic_stringstream& ss_in) + basic_writer& xml(const snu::basic_chunk_string& ss_in) { return this->eval_("application/xml", ss_in); } - basic_writer& json(const std::basic_stringstream& ss_in) + basic_writer& json(const snu::basic_chunk_string& ss_in) { return this->eval_("application/json", ss_in); } - basic_writer& js(const std::basic_stringstream& ss_in) + basic_writer& js(const snu::basic_chunk_string& ss_in) { return this->eval_("text/javascript", ss_in); } diff --git a/dev/lib/utility/chunk_string.hpp b/dev/lib/utility/chunk_string.hpp index 6b060ae..79e0dd6 100644 --- a/dev/lib/utility/chunk_string.hpp +++ b/dev/lib/utility/chunk_string.hpp @@ -29,8 +29,19 @@ namespace snu constexpr const static auto max_chunk_size = 4096; public: - explicit basic_chunk_string() = default; - virtual ~basic_chunk_string() = default; + 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&) = default; basic_chunk_string(const basic_chunk_string&) = default; @@ -56,7 +67,7 @@ namespace snu return *this; } - const std::basic_string& str() noexcept + const std::basic_string& str() const noexcept { return packed_chunks_; } diff --git a/dev/tests/chunk_string/CMakeLists.txt b/dev/tests/chunk_string/CMakeLists.txt index ae0e1e7..5a4a27c 100644 --- a/dev/tests/chunk_string/CMakeLists.txt +++ b/dev/tests/chunk_string/CMakeLists.txt @@ -21,8 +21,5 @@ target_link_libraries(BasicChunkUsage PRIVATE gtest_main Boost::container) set_property(TARGET BasicChunkUsage PROPERTY CXX_STANDARD 20) target_include_directories(BasicChunkUsage PUBLIC ../../) -# FIXME: Make a pkg-config for the boost library here! -target_include_directories(BasicChunkUsage PUBLIC /opt/homebrew/Cellar/boost/1.87.0/include) - include(GoogleTest) gtest_discover_tests(BasicChunkUsage) -- cgit v1.2.3