Skip to content

Commit

Permalink
updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Macheta committed Nov 30, 2023
1 parent 91d8397 commit fb98636
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ The easiest way is to use built-in CMake FetchContent:

```cmake
include(FetchContent)
FetchContent_Declare(
ecpp_endian
GIT_REPOSITORY https://github.com/jmacheta/endian.git
GIT_TAG main
)
FetchContent_Declare(ecpp_endian URL https://github.com/jmacheta/endian/tarball/latest)
FetchContent_MakeAvailable(ecpp_endian)
```
Expand All @@ -26,5 +22,26 @@ target_link_libraries(my_target PUBLIC ecpp::endian)
```

## Usage
### byteswap - unconditionally reserse byte order in integral types:
```cpp
#include <ecpp/endian.hpp>

todo
uint64_t a = 0x1122'3344'5566'7788U;
static_assert(0x8877'6655'4433'2211U == ecpp::byteswap(a));

```
### hton / ntoh - convert endiannes from/to network endianness (big-endiann)
```cpp
// On little endian systems:
uint64_t a = 0x1122'3344'5566'7788U;
static_assert(0x8877'6655'4433'2211U == ecpp::hton(a));
static_assert(a == ecpp::ntoh(ecpp::hton(a)));
// On big endian systems:
uint64_t a = 0x1122'3344'5566'7788U;
static_assert(a == ecpp::hton(a));
static_assert(a == ecpp::ntoh(ecpp::hton(a)));
```
## Limitations
While every function is marked as `constexpr`, in C++ standards prior to C++20, this might be untrue due to the non-constexpr `std::memcpy`. I have checked many clang/gcc versions, and this does not seem to be an issue, but it's worth noting.
2 changes: 1 addition & 1 deletion include/ecpp/endian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ template <std::integral T> [[nodiscard]] constexpr T byteswap(T value) noexcept
return std::bit_cast<T>(value_representation);
}
#else
template <std::integral T> [[nodiscard]] inline T byteswap(T value) noexcept {
template <std::integral T> [[nodiscard]] constexpr T byteswap(T value) noexcept {
alignas(T) std::array<std::byte, sizeof(T)> value_representation;
std::memcpy(value_representation.data(), &value, sizeof(value));
std::ranges::reverse_copy(value_representation, reinterpret_cast<std::byte *>(&value));
Expand Down

0 comments on commit fb98636

Please sign in to comment.