Skip to content

Commit

Permalink
Move byteswap_inplace functions from detail into protozero namespace.
Browse files Browse the repository at this point in the history
They can be useful outside protozero.
  • Loading branch information
joto committed Jul 11, 2018
1 parent a44efc3 commit da5bfc0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
22 changes: 16 additions & 6 deletions include/protozero/byteswap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,45 @@ inline uint64_t byteswap_impl(uint64_t value) noexcept {
#endif
}

} // end namespace detail

inline void byteswap_inplace(uint32_t* ptr) noexcept {
*ptr = byteswap_impl(*ptr);
*ptr = detail::byteswap_impl(*ptr);
}

inline void byteswap_inplace(uint64_t* ptr) noexcept {
*ptr = byteswap_impl(*ptr);
*ptr = detail::byteswap_impl(*ptr);
}

inline void byteswap_inplace(int32_t* ptr) noexcept {
auto bptr = reinterpret_cast<uint32_t*>(ptr);
*bptr = byteswap_impl(*bptr);
*bptr = detail::byteswap_impl(*bptr);
}

inline void byteswap_inplace(int64_t* ptr) noexcept {
auto bptr = reinterpret_cast<uint64_t*>(ptr);
*bptr = byteswap_impl(*bptr);
*bptr = detail::byteswap_impl(*bptr);
}

inline void byteswap_inplace(float* ptr) noexcept {
auto bptr = reinterpret_cast<uint32_t*>(ptr);
*bptr = byteswap_impl(*bptr);
*bptr = detail::byteswap_impl(*bptr);
}

inline void byteswap_inplace(double* ptr) noexcept {
auto bptr = reinterpret_cast<uint64_t*>(ptr);
*bptr = byteswap_impl(*bptr);
*bptr = detail::byteswap_impl(*bptr);
}

namespace detail {

// Added for backwards compatibility with any code that might use this
// function (even if it shouldn't have). Will be removed in a later
// version of protozero.
using ::protozero::byteswap_inplace;

} // end namespace detail

} // end namespace protozero

#endif // PROTOZERO_BYTESWAP_HPP
2 changes: 1 addition & 1 deletion include/protozero/iterators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class const_fixed_iterator {
value_type result;
std::memcpy(&result, m_data, sizeof(value_type));
#if PROTOZERO_BYTE_ORDER != PROTOZERO_LITTLE_ENDIAN
detail::byteswap_inplace(&result);
byteswap_inplace(&result);
#endif
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion include/protozero/pbf_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class pbf_reader {
skip_bytes(sizeof(T));
std::memcpy(&result, data, sizeof(T));
#if PROTOZERO_BYTE_ORDER != PROTOZERO_LITTLE_ENDIAN
detail::byteswap_inplace(&result);
byteswap_inplace(&result);
#endif
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion include/protozero/pbf_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class pbf_writer {
protozero_assert(m_pos == 0 && "you can't add fields to a parent pbf_writer if there is an existing pbf_writer for a submessage");
protozero_assert(m_data);
#if PROTOZERO_BYTE_ORDER != PROTOZERO_LITTLE_ENDIAN
detail::byteswap_inplace(&value);
byteswap_inplace(&value);
#endif
m_data->append(reinterpret_cast<const char*>(&value), sizeof(T));
}
Expand Down
24 changes: 12 additions & 12 deletions test/unit/test_endian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
#include <protozero/byteswap.hpp>

static int32_t check_swap_4(int32_t data) noexcept {
protozero::detail::byteswap_inplace(&data);
protozero::detail::byteswap_inplace(&data);
protozero::byteswap_inplace(&data);
protozero::byteswap_inplace(&data);
return data;
}

static int64_t check_swap_8(int64_t data) noexcept {
protozero::detail::byteswap_inplace(&data);
protozero::detail::byteswap_inplace(&data);
protozero::byteswap_inplace(&data);
protozero::byteswap_inplace(&data);
return data;
}

Expand All @@ -40,32 +40,32 @@ TEST_CASE("byte swapping") {

TEST_CASE("byte swap uint32_t") {
uint32_t a = 17;
protozero::detail::byteswap_inplace(&a);
protozero::detail::byteswap_inplace(&a);
protozero::byteswap_inplace(&a);
protozero::byteswap_inplace(&a);

REQUIRE(17 == a);
}

TEST_CASE("byte swap uint64_t") {
uint64_t a = 347529808;
protozero::detail::byteswap_inplace(&a);
protozero::detail::byteswap_inplace(&a);
protozero::byteswap_inplace(&a);
protozero::byteswap_inplace(&a);

REQUIRE(347529808 == a);
}

TEST_CASE("byte swap double") {
double a = 1.1;
protozero::detail::byteswap_inplace(&a);
protozero::detail::byteswap_inplace(&a);
protozero::byteswap_inplace(&a);
protozero::byteswap_inplace(&a);

REQUIRE(a == Approx(1.1));
}

TEST_CASE("byte swap float") {
float a = 1.1f;
protozero::detail::byteswap_inplace(&a);
protozero::detail::byteswap_inplace(&a);
protozero::byteswap_inplace(&a);
protozero::byteswap_inplace(&a);

REQUIRE(a == Approx(1.1f));
}
Expand Down

0 comments on commit da5bfc0

Please sign in to comment.