Add `src/cpp-common/std-int.hpp`
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 19 May 2022 12:38:05 +0000 (08:38 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 11 Sep 2023 15:24:02 +0000 (11:24 -0400)
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 <eeppeliteloop@gmail.com>
Change-Id: I69fc9ac7e01c2a82e4ad13afa553ef8e8720bd57
Reviewed-on: https://review.lttng.org/c/babeltrace/+/8086
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10839
Tested-by: jenkins <jenkins@lttng.org>
src/cpp-common/Makefile.am
src/cpp-common/std-int.hpp [new file with mode: 0644]

index b222ec70730d3c631f0717d4c08b5000eea76987..c9c07af4649baad35f8a731bdf38f4ca0b656c22 100644 (file)
@@ -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 (file)
index 0000000..2bdb4ec
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_STD_INT_HPP
+#define BABELTRACE_CPP_COMMON_STD_INT_HPP
+
+#include <cstdint>
+
+namespace bt2_common {
+namespace internal {
+
+template <std::size_t LenBitsV, bool IsSignedV>
+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 <std::size_t LenBitsV, bool IsSignedV>
+using StdIntT = typename internal::StdIntTBase<LenBitsV, IsSignedV>::Type;
+
+} /* namespace bt2_common */
+
+#endif /* BABELTRACE_CPP_COMMON_STD_INT_HPP */
This page took 0.025815 seconds and 4 git commands to generate.