YPC  0.2.0
fix_bytes.h
1 // No use for now, so don't use this for now.
2 #pragma once
3 #include <cstdint>
4 #include <utility>
5 
6 namespace ypc {
7 namespace utc {
8 
9 #if __cplusplus >= 201402L
10 namespace internal {
11 template <typename T, std::size_t N, std::size_t... Ns>
12 std::array<T, N> make_array_impl(std::initializer_list<T> t,
13  std::index_sequence<Ns...>) {
14  return std::array<T, N>{*(t.begin() + Ns)...};
15 }
16 
17 template <typename T, std::size_t N>
18 std::array<T, N> make_array(std::initializer_list<T> t) {
19  if (N < t.size()) {
20  throw std::out_of_range("make_array out of range");
21  }
22  return make_array_impl<T, N>(t, std::make_index_sequence<N>());
23 };
24 } // end namespace internal
25 #endif
26 
27 template <typename ByteType, size_t ByteLength = 32> class fix_bytes {
28 public:
29  using byte_t = ByteType;
30  const static size_t fixed_length;
31 
32  static_assert(sizeof(ByteType) == 1,
33  "cannot use ByteType that with larger size than 1");
34 
35  fix_bytes() : m_value{0} {};
36 #if __cplusplus >= 201402L
37  fix_bytes(std::initializer_list<byte_t> l)
38  : m_value(internal::make_array<byte_t, ByteLength>(l)) {}
39 #endif
40  fix_bytes(const fix_bytes<ByteType, ByteLength> &v) : m_value(v.m_value) {}
41  fix_bytes(fix_bytes &&v) : m_value(std::move(v.m_value)) {}
42 
43  template <typename T> fix_bytes(const T *buf, size_t buf_len) {
44  static_assert(sizeof(T) == sizeof(ByteType), "invalid buf pointer");
45  if (buf_len >= ByteLength) {
46  memcpy(m_value.data(), buf, ByteLength);
47  } else {
48  throw std::runtime_error("unmatched byte length");
49  }
50  }
51  virtual ~fix_bytes() = default;
52 
54  operator=(const fix_bytes<ByteType, ByteLength> &v) {
55  if (&v == this) {
56  return *this;
57  }
58  m_value = v.m_value;
59  return *this;
60  }
61 
63  operator=(fix_bytes<ByteType, ByteLength> &&v) {
64  m_value = std::move(v.m_value);
65  return *this;
66  }
67 
68  bool operator==(const fix_bytes<ByteType, ByteLength> &v) const {
69  return m_value == v.m_value;
70  }
71 
72  bool operator!=(const fix_bytes<ByteType, ByteLength> &v) const {
73  return m_value != v.m_value;
74  }
75  bool operator<(const fix_bytes<ByteType, ByteLength> &v) const {
76  return memcmp(m_value, v.m_value, ByteLength) < 0;
77  }
78  template <size_t BL>
80  operator+(const fix_bytes<ByteType, BL> &v) const {
82  std::copy(ret.m_value.begin(), ret.m_value.begin() + ByteLength,
83  m_value.begin());
84  std::copy(ret.m_value.begin() + ByteLength, ret.m_value.end(),
85  v.m_value.begin());
86  return ret;
87  }
88  const byte_t &operator[](size_t index) const { return m_value[index]; }
89  byte_t &operator[](size_t index) { return m_value[index]; }
90 
91  inline size_t size() const { return ByteLength; }
92 
93  inline const byte_t *value() const { return m_value.data(); }
94 
95  inline byte_t *value() { return m_value.data(); }
96 
97  bool empty() const { return m_value.empty(); }
98 
99 protected:
100  std::array<byte_t, ByteLength> m_value;
101 }; // end class fix_bytes
102 } // namespace utc
103 } // namespace ypc
ypc::utc::fix_bytes
Definition: fix_bytes.h:27