|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include <array> |
| 23 | +#include <bit> |
| 24 | +#include <concepts> |
| 25 | + |
| 26 | +/// \file iceberg/util/endian.h |
| 27 | +/// \brief Endianness conversion utilities |
| 28 | + |
| 29 | +namespace iceberg { |
| 30 | + |
| 31 | +/// \brief Concept for values that can be written in little-endian format. |
| 32 | +template <typename T> |
| 33 | +concept EndianConvertible = std::is_arithmetic_v<T>; |
| 34 | + |
| 35 | +/// \brief Concept for values that can be written in big-endian format, |
| 36 | +template <typename T> |
| 37 | +concept BigEndianWritable = std::same_as<T, std::array<uint8_t, 16>>; |
| 38 | + |
| 39 | +/// \brief Convert a value to little-endian format. |
| 40 | +template <EndianConvertible T> |
| 41 | +T ToLittleEndian(T value) { |
| 42 | + if constexpr (std::endian::native != std::endian::little && sizeof(T) > 1) { |
| 43 | + return std::byteswap(value); |
| 44 | + } |
| 45 | + return value; |
| 46 | +} |
| 47 | + |
| 48 | +/// \brief Convert a value from little-endian format. |
| 49 | +template <EndianConvertible T> |
| 50 | +T FromLittleEndian(T value) { |
| 51 | + if constexpr (std::endian::native != std::endian::little && sizeof(T) > 1) { |
| 52 | + return std::byteswap(value); |
| 53 | + } |
| 54 | + return value; |
| 55 | +} |
| 56 | + |
| 57 | +template <EndianConvertible T> |
| 58 | +constexpr T ToBigEndian(T value) { |
| 59 | + if constexpr (std::endian::native == std::endian::big || sizeof(T) <= 1) { |
| 60 | + return value; |
| 61 | + } else { |
| 62 | + return std::byteswap(value); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// \brief Convert a value from big-endian format to native. |
| 67 | +template <EndianConvertible T> |
| 68 | +constexpr T FromBigEndian(T value) { |
| 69 | + if constexpr (std::endian::native == std::endian::big || sizeof(T) <= 1) { |
| 70 | + return value; |
| 71 | + } else { |
| 72 | + return std::byteswap(value); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +} // namespace iceberg |
0 commit comments