]> git.proxmox.com Git - ceph.git/blob - ceph/src/include/byteorder.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / include / byteorder.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2
3 #pragma once
4
5 #include <boost/endian/conversion.hpp>
6
7 #include "int_types.h"
8
9 template<typename T>
10 inline T swab(T val) {
11 return boost::endian::endian_reverse(val);
12 }
13
14 template<typename T>
15 struct ceph_le {
16 private:
17 T v;
18 public:
19 ceph_le() = default;
20 explicit ceph_le(T nv)
21 : v{boost::endian::native_to_little(nv)}
22 {}
23 ceph_le<T>& operator=(T nv) {
24 v = boost::endian::native_to_little(nv);
25 return *this;
26 }
27 operator T() const { return boost::endian::little_to_native(v); }
28 friend inline bool operator==(ceph_le a, ceph_le b) {
29 return a.v == b.v;
30 }
31 } __attribute__ ((packed));
32
33 using ceph_le64 = ceph_le<__u64>;
34 using ceph_le32 = ceph_le<__u32>;
35 using ceph_le16 = ceph_le<__u16>;
36
37 using ceph_les64 = ceph_le<__s64>;
38 using ceph_les32 = ceph_le<__s32>;
39 using ceph_les16 = ceph_le<__s16>;
40
41 inline ceph_les64 init_les64(__s64 x) {
42 ceph_les64 v;
43 v = x;
44 return v;
45 }
46 inline ceph_les32 init_les32(__s32 x) {
47 ceph_les32 v;
48 v = x;
49 return v;
50 }
51 inline ceph_les16 init_les16(__s16 x) {
52 ceph_les16 v;
53 v = x;
54 return v;
55 }