-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes.h
More file actions
66 lines (51 loc) · 1.86 KB
/
bytes.h
File metadata and controls
66 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef __BYTES_H__
#define __BYTES_H__
#include <vector>
#include <string>
#include <iostream>
#include <kj/common.h>
#include <kj/string.h>
#include <capnp/serialize.h>
//typedef std::vector<uint8_t> Bytes;
struct Bytes : public std::vector<uint8_t> {
Bytes() {}
Bytes(int n) : std::vector<uint8_t>(n) {}
Bytes(std::string &s) : std::vector<uint8_t>(s.size()) { memcpy(&(*this)[0], &s[0], s.size()); }
Bytes(const char *c) : std::vector<uint8_t>() { std::string s(c); resize(s.size()); memcpy(&(*this)[0], &s[0], s.size()); }
Bytes(capnp::Data::Reader r) : ::Bytes(r.begin(), r.end()) {}
Bytes(capnp::Data::Reader const &r) : ::Bytes(r.begin(), r.end()) {}
Bytes &operator=(capnp::Data::Reader const &r) {*this = Bytes(r.begin(), r.end()); return *this;}
template <typename T>
Bytes(T *c, T *e) : std::vector<uint8_t>(c, e) {}
template <typename T>
Bytes(const T *c, size_t n) : std::vector<uint8_t>(reinterpret_cast<uint8_t const *>(c), reinterpret_cast<uint8_t const *>(c + n)) {};
Bytes(unsigned char *b, unsigned char *e) : std::vector<uint8_t>(b, e) {}
operator std::string() const {
std::string s(size(), 0);
memcpy(&s[0], &(*this)[0], size());
return s;
}
std::string str() const {
std::string s(size(), 0);
memcpy(&s[0], &(*this)[0], size());
return s;
}
kj::ArrayPtr<kj::byte> kjp() {
return kj::ArrayPtr<kj::byte>(&(*this)[0], size());
}
kj::ArrayPtr<::capnp::word const> kjwp() {
return kj::ArrayPtr<::capnp::word const>((::capnp::word const*) &(*this)[0], (size()+1)/2);
}
template <typename T>
T ptr() {
return reinterpret_cast<T>(&(*this)[0]);
}
operator kj::ArrayPtr<kj::byte>() {
//void bla() {
return kj::ArrayPtr<kj::byte>(&(*this)[0], size());
}
};
inline std::ostream &operator<<(std::ostream &out, Bytes const &b) {
return std::cout << b.str();
}
#endif