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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright (C) 2015 Scylladb, Ltd.
25 #include <boost/endian/conversion.hpp>
26 #include <seastar/core/unaligned.hh>
31 inline T cpu_to_le(T x) noexcept {
32 return boost::endian::native_to_little(x);
35 inline T le_to_cpu(T x) noexcept {
36 return boost::endian::little_to_native(x);
40 inline T cpu_to_be(T x) noexcept {
41 return boost::endian::native_to_big(x);
44 inline T be_to_cpu(T x) noexcept {
45 return boost::endian::big_to_native(x);
49 inline T cpu_to_le(const unaligned<T>& v) noexcept {
50 return cpu_to_le(T(v));
54 inline T le_to_cpu(const unaligned<T>& v) noexcept {
55 return le_to_cpu(T(v));
61 read_le(const char* p) noexcept {
63 std::copy_n(p, sizeof(T), reinterpret_cast<char*>(&datum));
64 return le_to_cpu(datum);
70 write_le(char* p, T datum) noexcept {
71 datum = cpu_to_le(datum);
72 std::copy_n(reinterpret_cast<const char*>(&datum), sizeof(T), p);
78 read_be(const char* p) noexcept {
80 std::copy_n(p, sizeof(T), reinterpret_cast<char*>(&datum));
81 return be_to_cpu(datum);
87 write_be(char* p, T datum) noexcept {
88 datum = cpu_to_be(datum);
89 std::copy_n(reinterpret_cast<const char*>(&datum), sizeof(T), p);
95 consume_be(const char*& p) noexcept {
96 auto ret = read_be<T>(p);
101 template <typename T>
104 produce_be(char*& p, T datum) noexcept {
105 write_be<T>(p, datum);