From 2a4bb833b022a85db81f04423f4a744667b90fad Mon Sep 17 00:00:00 2001 From: rina Date: Tue, 2 Jul 2024 10:43:03 +1000 Subject: [PATCH] aslp-cpp: also return encoding name --- aslp-cpp/include/aslp-cpp/aslp-cpp.hpp | 7 +++++-- aslp-cpp/source/aslp-cpp.cpp | 9 ++++++--- aslp-cpp/test/source/aslp-cpp_test.cpp | 3 ++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/aslp-cpp/include/aslp-cpp/aslp-cpp.hpp b/aslp-cpp/include/aslp-cpp/aslp-cpp.hpp index 08feb239..485e334a 100644 --- a/aslp-cpp/include/aslp-cpp/aslp-cpp.hpp +++ b/aslp-cpp/include/aslp-cpp/aslp-cpp.hpp @@ -7,6 +7,9 @@ namespace httplib { class Client; } // namespace httplib; +// tuple of encoding and semantics +using aslp_opcode_result_t = std::tuple; + class aslp_connection { std::unique_ptr client {nullptr}; @@ -14,7 +17,7 @@ class aslp_connection public: aslp_connection(const std::string& server_addr, int server_port); aslp_connection(aslp_connection&&) noexcept; - auto get_opcode(uint32_t opcode) -> std::string; + auto get_opcode(uint32_t opcode) -> aslp_opcode_result_t; void wait_active(); ~aslp_connection(); }; @@ -54,7 +57,7 @@ class aslp_client { auto static start(const std::string& addr, int server_port) -> std::unique_ptr; /** Returns the semantics for the given opcode, as a newline-separated string. */ - auto get_opcode(uint32_t opcode) -> std::string; + auto get_opcode(uint32_t opcode) -> aslp_opcode_result_t; /** Destroys the aslp_client and terminates the managed server as well. */ virtual ~aslp_client() { diff --git a/aslp-cpp/source/aslp-cpp.cpp b/aslp-cpp/source/aslp-cpp.cpp index 57cf5e75..598d504f 100644 --- a/aslp-cpp/source/aslp-cpp.cpp +++ b/aslp-cpp/source/aslp-cpp.cpp @@ -147,7 +147,7 @@ void aslp_connection::wait_active() std::cout << "\n"; } -std::string aslp_connection::get_opcode(uint32_t opcode) +aslp_opcode_result_t aslp_connection::get_opcode(uint32_t opcode) { auto codestr = std::format("{:#x}", opcode); std::cout << codestr << "\n"; @@ -167,7 +167,10 @@ std::string aslp_connection::get_opcode(uint32_t opcode) if (!result.contains("semantics")) { throw std::runtime_error("semantics missing"); } - return result["semantics"]; + if (!result.contains("encoding")) { + throw std::runtime_error("encoding name missing"); + } + return {result["encoding"], result["semantics"]}; } aslp_connection::aslp_connection(const std::string& server_addr, @@ -179,7 +182,7 @@ aslp_connection::aslp_connection(const std::string& server_addr, aslp_connection::aslp_connection(aslp_connection&&) noexcept = default; aslp_connection::~aslp_connection() = default; -std::string aslp_client::get_opcode(uint32_t opcode) +aslp_opcode_result_t aslp_client::get_opcode(uint32_t opcode) { aslp_connection conn {server_addr, server_port}; conn.wait_active(); diff --git a/aslp-cpp/test/source/aslp-cpp_test.cpp b/aslp-cpp/test/source/aslp-cpp_test.cpp index ed1e825f..2bf68775 100644 --- a/aslp-cpp/test/source/aslp-cpp_test.cpp +++ b/aslp-cpp/test/source/aslp-cpp_test.cpp @@ -8,7 +8,8 @@ auto main() -> int auto s = aslp_client::start(); try { - auto c = s->get_opcode(0xFD430091); + std::string c; + std::tie(std::ignore, c) = s->get_opcode(0xFD430091); std::cout << c << "\n"; } catch (std::runtime_error &e) { std::cout << " error " << e.what() << "\n";