forked from y-scope/clp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ffi: Extract some serialization methods for use in other files. (y-sc…
- Loading branch information
1 parent
22bcd4b
commit c9c9548
Showing
5 changed files
with
84 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "utils.hpp" | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
#include <json/single_include/nlohmann/json.hpp> | ||
|
||
#include "../../type_utils.hpp" | ||
#include "protocol_constants.hpp" | ||
|
||
namespace clp::ffi::ir_stream { | ||
auto serialize_metadata(nlohmann::json& metadata, std::vector<int8_t>& ir_buf) -> bool { | ||
ir_buf.push_back(cProtocol::Metadata::EncodingJson); | ||
|
||
auto const metadata_serialized | ||
= metadata.dump(-1, ' ', false, nlohmann::json::error_handler_t::ignore); | ||
auto const metadata_serialized_length = metadata_serialized.length(); | ||
if (metadata_serialized_length <= UINT8_MAX) { | ||
ir_buf.push_back(cProtocol::Metadata::LengthUByte); | ||
ir_buf.push_back(bit_cast<int8_t>(static_cast<uint8_t>(metadata_serialized_length))); | ||
} else if (metadata_serialized_length <= UINT16_MAX) { | ||
ir_buf.push_back(cProtocol::Metadata::LengthUShort); | ||
serialize_int(static_cast<uint16_t>(metadata_serialized_length), ir_buf); | ||
} else { | ||
// Can't encode metadata longer than 64 KiB | ||
return false; | ||
} | ||
ir_buf.insert(ir_buf.cend(), metadata_serialized.cbegin(), metadata_serialized.cend()); | ||
|
||
return true; | ||
} | ||
} // namespace clp::ffi::ir_stream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef CLP_FFI_IR_STREAM_UTILS_HPP | ||
#define CLP_FFI_IR_STREAM_UTILS_HPP | ||
|
||
#include <cstdint> | ||
#include <span> | ||
#include <vector> | ||
|
||
#include <json/single_include/nlohmann/json.hpp> | ||
|
||
#include "byteswap.hpp" | ||
|
||
namespace clp::ffi::ir_stream { | ||
/** | ||
* Serializes the given metadata into the IR stream. | ||
* @param metadata | ||
* @param ir_buf | ||
* @return Whether serialization succeeded. | ||
*/ | ||
[[nodiscard]] auto | ||
serialize_metadata(nlohmann::json& metadata, std::vector<int8_t>& ir_buf) -> bool; | ||
|
||
/** | ||
* Serializes the given integer into the IR stream. | ||
* @tparam integer_t | ||
* @param value | ||
* @param ir_buf | ||
*/ | ||
template <typename integer_t> | ||
auto serialize_int(integer_t value, std::vector<int8_t>& ir_buf) -> void; | ||
|
||
template <typename integer_t> | ||
auto serialize_int(integer_t value, std::vector<int8_t>& ir_buf) -> void { | ||
integer_t value_big_endian{}; | ||
static_assert(sizeof(integer_t) == 2 || sizeof(integer_t) == 4 || sizeof(integer_t) == 8); | ||
if constexpr (sizeof(value) == 2) { | ||
value_big_endian = bswap_16(value); | ||
} else if constexpr (sizeof(value) == 4) { | ||
value_big_endian = bswap_32(value); | ||
} else if constexpr (sizeof(value) == 8) { | ||
value_big_endian = bswap_64(value); | ||
} | ||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) | ||
std::span<int8_t> const data_view{reinterpret_cast<int8_t*>(&value_big_endian), sizeof(value)}; | ||
ir_buf.insert(ir_buf.end(), data_view.begin(), data_view.end()); | ||
} | ||
} // namespace clp::ffi::ir_stream | ||
#endif |