Skip to content

Commit

Permalink
aslp-cpp: also return encoding name
Browse files Browse the repository at this point in the history
  • Loading branch information
katrinafyi committed Jul 2, 2024
1 parent 8ce869f commit 2a4bb83
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 5 additions & 2 deletions aslp-cpp/include/aslp-cpp/aslp-cpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ namespace httplib {
class Client;
} // namespace httplib;

// tuple of encoding and semantics
using aslp_opcode_result_t = std::tuple<std::string, std::string>;

class aslp_connection
{
std::unique_ptr<httplib::Client> client {nullptr};

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();
};
Expand Down Expand Up @@ -54,7 +57,7 @@ class aslp_client {
auto static start(const std::string& addr, int server_port) -> std::unique_ptr<aslp_client>;

/** 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() {
Expand Down
9 changes: 6 additions & 3 deletions aslp-cpp/source/aslp-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion aslp-cpp/test/source/aslp-cpp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 2a4bb83

Please sign in to comment.