-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSer.cc
53 lines (44 loc) · 1.38 KB
/
Ser.cc
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2014 Adam Crume <[email protected]>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "Ser.hpp"
#include <arpa/inet.h>
#include <cstdlib>
#include <endian.h>
rbd_replay::Ser::Ser(std::ostream &out)
: m_out(out) {
}
void rbd_replay::Ser::write_uint8_t(uint8_t data) {
m_out.write(reinterpret_cast<char*>(&data), sizeof(data));
}
void rbd_replay::Ser::write_uint16_t(uint16_t data) {
data = htons(data);
m_out.write(reinterpret_cast<char*>(&data), sizeof(data));
}
void rbd_replay::Ser::write_uint32_t(uint32_t data) {
data = htonl(data);
m_out.write(reinterpret_cast<char*>(&data), sizeof(data));
}
void rbd_replay::Ser::write_uint64_t(uint64_t data) {
#if __BYTE_ORDER == __LITTLE_ENDIAN
data = (static_cast<uint64_t>(htonl(data)) << 32 | htonl(data >> 32));
#endif
m_out.write(reinterpret_cast<char*>(&data), sizeof(data));
}
void rbd_replay::Ser::write_string(const std::string& data) {
write_uint32_t(data.length());
m_out.write(data.data(), data.length());
}
void rbd_replay::Ser::write_bool(bool data) {
write_uint8_t(data ? 1 : 0);
}