Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle committed Oct 12, 2023
1 parent fb036fc commit 7e69e27
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions include/ylt/struct_pack/endian_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void write_wrapper(writer_t& writer, const char* data) {
auto tmp = _byteswap_ulong(*data);
writer.write(&tmp, block_size);
#elif defined(__clang__) || defined(__GNUC__)
auto tmp = __builtin_bswap16(*data);
auto tmp = __builtin_bswap32(*data);
writer.write(&tmp, block_size);
#else
writer.write(data + 3, 1);
Expand All @@ -54,7 +54,7 @@ void write_wrapper(writer_t& writer, const char* data) {
auto tmp = _byteswap_uint64(*data);
writer.write(&tmp, block_size);
#elif defined(__clang__) || defined(__GNUC__)
auto tmp = __builtin_bswap16(*data);
auto tmp = __builtin_bswap64(*data);
writer.write(&tmp, block_size);
#else
writer.write(data + 7, 1);
Expand Down
7 changes: 5 additions & 2 deletions include/ylt/struct_pack/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@
namespace struct_pack {
namespace detail {

constexpr inline bool is_system_little_endian =
static_cast<const unsigned char &>(1) == 1;
constexpr inline bool is_system_little_endian_impl(int value = 0x01) {
return static_cast<const unsigned char &>(value) == 1;
}

constexpr inline bool is_system_little_endian = is_system_little_endian_impl();

template <typename... Args>
using get_args_type = remove_cvref_t<typename std::conditional<
Expand Down
5 changes: 5 additions & 0 deletions src/struct_pack/tests/test_cross_platform.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <cstdint>
#include <fstream>
#include <iostream>
#include <ylt/struct_pack.hpp>

#include "doctest.h"
#include "test_struct.hpp"
#include "ylt/struct_pack/reflection.hpp"
using namespace struct_pack;
using namespace doctest;

Expand All @@ -21,6 +23,9 @@ void data_gen() {
}

TEST_CASE("testing deserialize other platform data") {
std::cout << "Now endian:"
<< (struct_pack::detail::is_system_little_endian ? "little" : "big")
<< std::endl;
std::ifstream ifs("binary_data/test_cross_platform.dat");
if (!ifs.is_open()) {
ifs.open("src/struct_pack/tests/binary_data/test_cross_platform.dat");
Expand Down

0 comments on commit 7e69e27

Please sign in to comment.