Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
byteorder.hh
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright (C) 2015 Scylladb, Ltd.
20 */
21
22#pragma once
23
24#ifndef SEASTAR_MODULE
25#include <algorithm>
26#include <boost/endian/conversion.hpp>
27#include <seastar/core/unaligned.hh>
28#include <seastar/util/modules.hh>
29#endif
30
31namespace seastar {
32
33SEASTAR_MODULE_EXPORT_BEGIN
34
35template <typename T>
36inline T cpu_to_le(T x) noexcept {
37 return boost::endian::native_to_little(x);
38}
39template <typename T>
40inline T le_to_cpu(T x) noexcept {
41 return boost::endian::little_to_native(x);
42}
43
44template <typename T>
45inline T cpu_to_be(T x) noexcept {
46 return boost::endian::native_to_big(x);
47}
48template <typename T>
49inline T be_to_cpu(T x) noexcept {
50 return boost::endian::big_to_native(x);
51}
52
53template <typename T>
54inline T cpu_to_le(const unaligned<T>& v) noexcept {
55 return cpu_to_le(T(v));
56}
57
58template <typename T>
59inline T le_to_cpu(const unaligned<T>& v) noexcept {
60 return le_to_cpu(T(v));
61}
62
63template <typename T>
64inline
65T
66read_le(const char* p) noexcept {
67 T datum;
68 std::copy_n(p, sizeof(T), reinterpret_cast<char*>(&datum));
69 return le_to_cpu(datum);
70}
71
72template <typename T>
73inline
74void
75write_le(char* p, T datum) noexcept {
76 datum = cpu_to_le(datum);
77 std::copy_n(reinterpret_cast<const char*>(&datum), sizeof(T), p);
78}
79
80template <typename T>
81inline
82T
83read_be(const char* p) noexcept {
84 T datum;
85 std::copy_n(p, sizeof(T), reinterpret_cast<char*>(&datum));
86 return be_to_cpu(datum);
87}
88
89template <typename T>
90inline
91void
92write_be(char* p, T datum) noexcept {
93 datum = cpu_to_be(datum);
94 std::copy_n(reinterpret_cast<const char*>(&datum), sizeof(T), p);
95}
96
97template <typename T>
98inline
99T
100consume_be(const char*& p) noexcept {
101 auto ret = read_be<T>(p);
102 p += sizeof(T);
103 return ret;
104}
105
106template <typename T>
107inline
108void
109produce_be(char*& p, T datum) noexcept {
110 write_be<T>(p, datum);
111 p += sizeof(T);
112}
113
114SEASTAR_MODULE_EXPORT_END
115
116}
Seastar API namespace.
Definition: abort_on_ebadf.hh:26