Honeycomb  0.1
Component-Model Framework
Bytes.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/Misc/BitOp.h"
5 #include "Honey/Misc/Range.h"
6 #include "Honey/Memory/Buffer.h"
7 
8 namespace honey
9 {
10 
12 typedef uint8 byte;
13 
15 constexpr byte operator"" _b(unsigned long long int i) { return static_cast<byte>(i); }
17 constexpr byte operator"" _b(char c) { return static_cast<byte>(c); }
18 
22 
23 template<szt N> struct ByteArray;
24 
26 class Bytes : public vector<byte>
27 {
28 public:
29  using vector::vector;
30 
31  Bytes() = default;
32  Bytes(ByteBufConst bs) : vector(bs.begin(), bs.end()) {}
33  template<szt N>
34  Bytes(const ByteArray<N>& bs) : vector(bs.begin(), bs.end()) {}
35 
37  friend ostream& operator<<(ostream& os, const Bytes& val);
39  friend istream& operator>>(istream& is, Bytes& val);
40 };
41 
43 inline Bytes operator"" _b(const char* str, szt len) { return Bytes(str, str+len); }
44 
46 inline ostream& operator<<(ostream& os, ByteBufConst val) { os << Bytes(val); return os; }
47 
49 template<class Int>
50 typename std::enable_if<std::is_integral<Int>::value, Bytes>::type
51  toBytes(Int val, Endian order = Endian::big)
52 {
53  typedef typename std::make_unsigned<Int>::type Unsigned;
54  Bytes bs(sizeof(Int));
55  switch (order)
56  {
57  case Endian::little:
58  BitOp::toPartsLittle(static_cast<Unsigned>(val), bs.data());
59  break;
60  case Endian::big:
61  BitOp::toPartsBig(static_cast<Unsigned>(val), bs.data());
62  break;
63  }
64  return bs;
65 }
66 
68 template<class Int>
69 typename std::enable_if<std::is_integral<Int>::value, Int>::type
70  fromBytes(const Bytes& bs, Endian order = Endian::big)
71 {
72  typedef typename std::make_unsigned<Int>::type Unsigned;
73  switch (order)
74  {
75  case Endian::little:
76  return static_cast<Int>(BitOp::fromPartsLittle<Unsigned>(bs.data()));
77  case Endian::big:
78  return static_cast<Int>(BitOp::fromPartsBig<Unsigned>(bs.data()));
79  }
80 }
81 
83 template<szt N>
84 struct ByteArray : array<byte, N>
85 {
86  typedef array<byte, N> Super;
87 
88  ByteArray() = default;
90  template<class... Bytes>
91  ByteArray(byte b, Bytes&&... bs) : Super{b, forward<Bytes>(bs)...} {}
92  ByteArray(ByteBufConst bs) { assert(bs.size() == this->size()); std::copy(bs.begin(), bs.end(), this->begin()); }
93  ByteArray(const Bytes& bs) { assert(bs.size() == this->size()); std::copy(bs.begin(), bs.end(), this->begin()); }
94 
96  friend ostream& operator<<(ostream& os, const ByteArray& val) { os << Bytes(val); return os; }
98  friend istream& operator>>(istream& is, ByteArray& val) { Bytes bs; is >> bs; val = bs; return is; }
99 };
100 
101 }
A contiguous region of referenced (not owned by object) memory.
Definition: Buffer.h:17
low byte first
friend istream & operator>>(istream &is, Bytes &val)
Read bytes from string stream using current decoding.
Definition: Bytes.cpp:16
ByteArray()=default
uint8 byte
An unsigned 8-bit integer.
Definition: Bytes.h:12
friend istream & operator>>(istream &is, ByteArray &val)
Read byte array from string stream using current decoding.
Definition: Bytes.h:98
std::enable_if< std::is_integral< Int >::value, Int >::type fromBytes(const Bytes &bs, Endian order=Endian::big)
Convert bytes to integral value.
Definition: Bytes.h:70
friend ostream & operator<<(ostream &os, const ByteArray &val)
Write byte array to string stream using current encoding.
Definition: Bytes.h:96
Buffer< const byte > ByteBufConst
Definition: Bytes.h:21
Bytes(const ByteArray< N > &bs)
Definition: Bytes.h:34
Bytes()=default
high byte first
ByteArray(ByteBufConst bs)
Definition: Bytes.h:92
ByteArray(const Bytes &bs)
Definition: Bytes.h:93
unsigned char uint8
Definition: Core.h:12
T * end() const
Returns an iterator to the end.
Definition: Buffer.h:67
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
szt size() const
Returns the number of elements.
Definition: Buffer.h:78
friend ostream & operator<<(ostream &os, const Bytes &val)
Write bytes to string stream using current encoding.
Definition: Bytes.cpp:9
String of bytes.
Definition: Bytes.h:26
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
ByteArray(byte b, Bytes &&...bs)
Construct from list of byte values.
Definition: Bytes.h:91
std::enable_if< std::is_integral< Int >::value, Bytes >::type toBytes(Int val, Endian order=Endian::big)
Convert integral value to bytes.
Definition: Bytes.h:51
Buffer< byte > ByteBuf
A buffer of bytes.
Definition: Bytes.h:20
int size(const StdContainer &cont)
Safely get the size of a std container as a signed integer.
Definition: StdUtil.h:19
T * begin() const
Returns an iterator to the beginning.
Definition: Buffer.h:65
Bytes(ByteBufConst bs)
Definition: Bytes.h:32
Fixed array of N bytes.
Definition: Bytes.h:23
ostream & operator<<(ostream &os, const Bytes &val)
Definition: Bytes.cpp:9
array< byte, N > Super
Definition: Bytes.h:86
Endian
Endian (byte order) types.
Definition: BitOp.h:11
Global Honeycomb namespace.