YPC  0.2.0
bytes_util.h
1 #pragma once
2 #include "ff/net/common/archive.h"
4 #include "ypc/common/byte/fix_bytes.h"
5 
6 namespace ypc {
7 namespace utc {
8 template <typename T> struct is_fix_bytes { const static bool value = false; };
9 template <typename ByteType, size_t N>
10 struct is_fix_bytes<fix_bytes<ByteType, N>> {
11  const static bool value = true;
12 };
13 
14 template <typename T> struct is_bytes { const static bool value = false; };
15 
16 template <typename ByteType, byte_encode Format>
17 struct is_bytes<bytes<ByteType, Format>> {
18  const static bool value = true;
19 };
20 } // namespace utc
21 } // namespace ypc
22 
23 namespace ff {
24 namespace net {
25 template <typename ByteType, ::ypc::utc::byte_encode Format>
26 class archive_helper<::ypc::utc::bytes<ByteType, Format>> {
27 public:
29  static uint32_t serialize(char *buf, const data_t &d, size_t len) {
30  size_t s = d.size();
31  memcpy(buf, (const char *)&s, sizeof(s));
32  memcpy(buf + sizeof(s), (const char *)d.data(), s);
33  return s + sizeof(s);
34  }
35  static uint32_t deserialize(const char *buf, data_t &d, size_t len) {
36  size_t s;
37  memcpy((char *)&s, buf, sizeof(s));
38  d = data_t(s);
39  memcpy((char *)d.data(), buf + sizeof(s), s);
40  return s + sizeof(s);
41  }
42  static uint32_t length(const data_t &d) { return d.size() + sizeof(size_t); }
43 };
44 } // namespace net
45 } // namespace ff
46 
47 namespace std {
48 template <typename ByteType, ::ypc::utc::byte_encode Format>
49 struct hash<::ypc::utc::bytes<ByteType, Format>> {
51  using result_type = std::size_t;
52 
53  result_type operator()(argument_type const &s) const noexcept {
54  return std::hash<std::string>{}(
55  std::string((const char *)s.data(), s.size()));
56  }
57 };
58 } // namespace std
ypc::utc::is_bytes
Definition: bytes_util.h:14
ypc::utc::is_fix_bytes
Definition: bytes_util.h:8
ypc::utc::fix_bytes
Definition: fix_bytes.h:27
bytes.h
process most common data types
ypc::utc::bytes
Definition: bytes.h:143