Add {h,n}to{h,n}{s,l} to Bytes

This commit is contained in:
Théophile Bastian 2016-11-20 00:14:40 +01:00
parent bd5a1666a5
commit c53125384d
2 changed files with 15 additions and 2 deletions

View file

@ -12,6 +12,7 @@
#include "Bytes.h"
#include <cstring>
#include <algorithm>
#include <arpa/inet.h>
using namespace std;
Bytes::Bytes() : data(), firstIndex(0) {}
@ -52,11 +53,15 @@ Bytes& Bytes::operator<<(u8 v) {
return *this;
}
Bytes& Bytes::operator<<(u16 v) {
insertData<u16>(v);
insertData<u16>(htons(v));
return *this;
}
Bytes& Bytes::operator<<(u32 v) {
insertData<u32>(v);
insertData<u32>(htonl(v));
return *this;
}
Bytes& Bytes::operator<<(u64 v) {
insertData<u64>(v);
return *this;
}
Bytes& Bytes::operator<<(const Bytes& v) {
@ -79,10 +84,16 @@ Bytes& Bytes::operator>>(u8& v) {
}
Bytes& Bytes::operator>>(u16& v) {
extractData<u16>(v);
htons(v);
return *this;
}
Bytes& Bytes::operator>>(u32& v) {
extractData<u32>(v);
htonl(v);
return *this;
}
Bytes& Bytes::operator>>(u64& v) {
extractData<u64>(v);
return *this;
}
Bytes& Bytes::operator>>(char& v) {

View file

@ -41,6 +41,7 @@ class Bytes {
Bytes& operator<<(u8 v);
Bytes& operator<<(u16 v);
Bytes& operator<<(u32 v);
Bytes& operator<<(u64 v);
Bytes& operator<<(const Bytes& v);
Bytes& operator<<(const char* str);
/// Appends the given data to the vector. Returns *this to allow
@ -49,6 +50,7 @@ class Bytes {
Bytes& operator>>(u8& v);
Bytes& operator>>(u16& v);
Bytes& operator>>(u32& v);
Bytes& operator>>(u64& v);
Bytes& operator>>(char& c);
/// Extracts the given data type from the vector. Returns *this to
/// allow chaining.