Skip to content

Commit

Permalink
Refactor c++ and add missing null checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlion committed Apr 25, 2024
1 parent 2d570a8 commit edeab0d
Show file tree
Hide file tree
Showing 17 changed files with 602 additions and 531 deletions.
75 changes: 39 additions & 36 deletions cpp/src/ffi_go/ir/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,47 @@ namespace ffi_go::ir {
using namespace ffi::ir_stream;

namespace {
/**
* Generic helper for ir_decoder_decode_*_log_message
*/
template <class encoded_var_view_t>
[[nodiscard]] auto decode_log_message(
StringView logtype,
encoded_var_view_t vars,
StringView dict_vars,
Int32tSpan dict_var_end_offsets,
void* ir_decoder,
StringView* log_msg_view
) -> int {
using encoded_var_t = typename std::conditional<
std::is_same_v<Int64tSpan, encoded_var_view_t>,
ffi::eight_byte_encoded_variable_t,
ffi::four_byte_encoded_variable_t>::type;
Decoder* decoder{static_cast<Decoder*>(ir_decoder)};
auto& log_msg{decoder->m_log_message};
log_msg.reserve(logtype.m_size + dict_vars.m_size);

IRErrorCode err{IRErrorCode_Success};
try {
log_msg = ffi::decode_message<encoded_var_t>(
std::string_view(logtype.m_data, logtype.m_size),
vars.m_data,
vars.m_size,
std::string_view(dict_vars.m_data, dict_vars.m_size),
dict_var_end_offsets.m_data,
dict_var_end_offsets.m_size
);
} catch (ffi::EncodingException const& e) {
err = IRErrorCode_Decode_Error;
}
/**
* Generic helper for ir_decoder_decode_*_log_message
*/
template <class encoded_var_view_t>
[[nodiscard]] auto decode_log_message(
StringView logtype,
encoded_var_view_t vars,
StringView dict_vars,
Int32tSpan dict_var_end_offsets,
void* ir_decoder,
StringView* log_msg_view
) -> int {
using encoded_var_t = typename std::conditional<
std::is_same_v<Int64tSpan, encoded_var_view_t>,
ffi::eight_byte_encoded_variable_t,
ffi::four_byte_encoded_variable_t>::type;
if (nullptr == ir_decoder || nullptr == log_msg_view) {
return static_cast<int>(IRErrorCode_Corrupted_IR);
}
Decoder* decoder{static_cast<Decoder*>(ir_decoder)};
auto& log_msg{decoder->m_log_message};
log_msg.reserve(logtype.m_size + dict_vars.m_size);

log_msg_view->m_data = log_msg.data();
log_msg_view->m_size = log_msg.size();
return static_cast<int>(err);
IRErrorCode err{IRErrorCode_Success};
try {
log_msg = ffi::decode_message<encoded_var_t>(
std::string_view(logtype.m_data, logtype.m_size),
vars.m_data,
vars.m_size,
std::string_view(dict_vars.m_data, dict_vars.m_size),
dict_var_end_offsets.m_data,
dict_var_end_offsets.m_size
);
} catch (ffi::EncodingException const& e) {
err = IRErrorCode_Decode_Error;
}

log_msg_view->m_data = log_msg.data();
log_msg_view->m_size = log_msg.size();
return static_cast<int>(err);
}
} // namespace

extern "C" auto ir_decoder_new() -> void* {
Expand Down
Loading

0 comments on commit edeab0d

Please sign in to comment.