Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
feat: support uint8_t for data_output and data_input (#792)
Browse files Browse the repository at this point in the history
  • Loading branch information
levy5307 authored Mar 16, 2021
1 parent 2582a5c commit 4a9a50c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/dsn/utility/endians.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ namespace dsn {

namespace endian {

inline uint8_t hton(uint8_t v) { return v; }

inline uint16_t hton(uint16_t v) { return htobe16(v); }

inline uint32_t hton(uint32_t v) { return htobe32(v); }

inline uint64_t hton(uint64_t v) { return htobe64(v); }

inline uint8_t ntoh(uint8_t v) { return v; }

inline uint16_t ntoh(uint16_t v) { return be16toh(v); }

inline uint32_t ntoh(uint32_t v) { return be32toh(v); }
Expand All @@ -35,6 +39,8 @@ class data_output

explicit data_output(std::string &s) : data_output(&s[0], s.length()) {}

data_output &write_u8(uint8_t val) { return write_unsigned(val); }

data_output &write_u16(uint16_t val) { return write_unsigned(val); }

data_output &write_u32(uint32_t val) { return write_unsigned(val); }
Expand Down Expand Up @@ -71,6 +77,8 @@ class data_input
public:
explicit data_input(string_view s) : _p(s.data()), _size(s.size()) {}

uint8_t read_u8() { return read_unsigned<uint8_t>(); }

uint16_t read_u16() { return read_unsigned<uint16_t>(); }

uint32_t read_u32() { return read_unsigned<uint32_t>(); }
Expand Down
7 changes: 7 additions & 0 deletions src/utils/test/endian_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ TEST(endian, write_and_read)
ASSERT_EQ(100, data_input(data).read_u32());
}

{
std::string data;
data.resize(1);
data_output(data).write_u8(100);
ASSERT_EQ(100, data_input(data).read_u8());
}

{
std::string data;
data.resize(1000 * 8);
Expand Down

0 comments on commit 4a9a50c

Please sign in to comment.