]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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 | ||
f51cf556 | 24 | #ifndef SEASTAR_MODULE |
11fdf7f2 | 25 | #include <algorithm> |
1e59de90 | 26 | #include <boost/endian/conversion.hpp> |
11fdf7f2 | 27 | #include <seastar/core/unaligned.hh> |
f51cf556 TL |
28 | #include <seastar/util/modules.hh> |
29 | #endif | |
11fdf7f2 TL |
30 | |
31 | namespace seastar { | |
32 | ||
f51cf556 TL |
33 | SEASTAR_MODULE_EXPORT_BEGIN |
34 | ||
1e59de90 TL |
35 | template <typename T> |
36 | inline T cpu_to_le(T x) noexcept { | |
37 | return boost::endian::native_to_little(x); | |
38 | } | |
39 | template <typename T> | |
40 | inline T le_to_cpu(T x) noexcept { | |
41 | return boost::endian::little_to_native(x); | |
42 | } | |
f67539c2 | 43 | |
1e59de90 TL |
44 | template <typename T> |
45 | inline T cpu_to_be(T x) noexcept { | |
46 | return boost::endian::native_to_big(x); | |
47 | } | |
48 | template <typename T> | |
49 | inline T be_to_cpu(T x) noexcept { | |
50 | return boost::endian::big_to_native(x); | |
51 | } | |
11fdf7f2 TL |
52 | |
53 | template <typename T> | |
f67539c2 | 54 | inline T cpu_to_le(const unaligned<T>& v) noexcept { |
11fdf7f2 TL |
55 | return cpu_to_le(T(v)); |
56 | } | |
57 | ||
58 | template <typename T> | |
f67539c2 | 59 | inline T le_to_cpu(const unaligned<T>& v) noexcept { |
11fdf7f2 TL |
60 | return le_to_cpu(T(v)); |
61 | } | |
62 | ||
63 | template <typename T> | |
64 | inline | |
65 | T | |
f67539c2 | 66 | read_le(const char* p) noexcept { |
11fdf7f2 TL |
67 | T datum; |
68 | std::copy_n(p, sizeof(T), reinterpret_cast<char*>(&datum)); | |
69 | return le_to_cpu(datum); | |
70 | } | |
71 | ||
72 | template <typename T> | |
73 | inline | |
74 | void | |
f67539c2 | 75 | write_le(char* p, T datum) noexcept { |
11fdf7f2 TL |
76 | datum = cpu_to_le(datum); |
77 | std::copy_n(reinterpret_cast<const char*>(&datum), sizeof(T), p); | |
78 | } | |
79 | ||
80 | template <typename T> | |
81 | inline | |
82 | T | |
f67539c2 | 83 | read_be(const char* p) noexcept { |
11fdf7f2 TL |
84 | T datum; |
85 | std::copy_n(p, sizeof(T), reinterpret_cast<char*>(&datum)); | |
86 | return be_to_cpu(datum); | |
87 | } | |
88 | ||
89 | template <typename T> | |
90 | inline | |
91 | void | |
f67539c2 | 92 | write_be(char* p, T datum) noexcept { |
11fdf7f2 TL |
93 | datum = cpu_to_be(datum); |
94 | std::copy_n(reinterpret_cast<const char*>(&datum), sizeof(T), p); | |
95 | } | |
96 | ||
97 | template <typename T> | |
98 | inline | |
99 | T | |
f67539c2 | 100 | consume_be(const char*& p) noexcept { |
11fdf7f2 TL |
101 | auto ret = read_be<T>(p); |
102 | p += sizeof(T); | |
103 | return ret; | |
104 | } | |
105 | ||
106 | template <typename T> | |
107 | inline | |
108 | void | |
f67539c2 | 109 | produce_be(char*& p, T datum) noexcept { |
11fdf7f2 TL |
110 | write_be<T>(p, datum); |
111 | p += sizeof(T); | |
112 | } | |
113 | ||
f51cf556 TL |
114 | SEASTAR_MODULE_EXPORT_END |
115 | ||
11fdf7f2 | 116 | } |