From 2f1bfb2a34b73d37444f0707292b4427d6db72c9 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Thu, 19 May 2022 08:38:05 -0400 Subject: [PATCH] Add `src/cpp-common/std-int.hpp` This new header contains the `StdIntT` trait structure to get the standard integer type from a length (bits) and a signedness. For example, `bt2_common::StdIntT<32, true>::Type` is `std::int32_t`. Signed-off-by: Philippe Proulx Change-Id: I69fc9ac7e01c2a82e4ad13afa553ef8e8720bd57 Reviewed-on: https://review.lttng.org/c/babeltrace/+/8086 Reviewed-on: https://review.lttng.org/c/babeltrace/+/10839 Tested-by: jenkins --- src/cpp-common/Makefile.am | 3 +- src/cpp-common/std-int.hpp | 81 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/cpp-common/std-int.hpp diff --git a/src/cpp-common/Makefile.am b/src/cpp-common/Makefile.am index b222ec70..c9c07af4 100644 --- a/src/cpp-common/Makefile.am +++ b/src/cpp-common/Makefile.am @@ -18,4 +18,5 @@ EXTRA_DIST = bt2 \ safe-ops.hpp \ align.hpp \ uuid.hpp \ - vector.hpp + vector.hpp \ + std-int.hpp diff --git a/src/cpp-common/std-int.hpp b/src/cpp-common/std-int.hpp new file mode 100644 index 00000000..2bdb4ec7 --- /dev/null +++ b/src/cpp-common/std-int.hpp @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2022 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_STD_INT_HPP +#define BABELTRACE_CPP_COMMON_STD_INT_HPP + +#include + +namespace bt2_common { +namespace internal { + +template +struct StdIntTBase; + +template <> +struct StdIntTBase<8, true> +{ + using Type = std::int8_t; +}; + +template <> +struct StdIntTBase<8, false> +{ + using Type = std::uint8_t; +}; + +template <> +struct StdIntTBase<16, true> +{ + using Type = std::int16_t; +}; + +template <> +struct StdIntTBase<16, false> +{ + using Type = std::uint16_t; +}; + +template <> +struct StdIntTBase<32, true> +{ + using Type = std::int32_t; +}; + +template <> +struct StdIntTBase<32, false> +{ + using Type = std::uint32_t; +}; + +template <> +struct StdIntTBase<64, true> +{ + using Type = std::int64_t; +}; + +template <> +struct StdIntTBase<64, false> +{ + using Type = std::uint64_t; +}; + +} /* namespace internal */ + +/* + * Standard fixed-length integer type `Type` of length `LenBitsV` bits + * and signedness `IsSignedV`. + * + * `LenBitsV` must be one of 8, 16, 32, or 64. + * + * For example, `StdIntT<32, true>` is `std::int32_t`. + */ +template +using StdIntT = typename internal::StdIntTBase::Type; + +} /* namespace bt2_common */ + +#endif /* BABELTRACE_CPP_COMMON_STD_INT_HPP */ -- 2.34.1