Add C++ interface for the libbabeltrace2 `bt_integer_range_*` API
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 16 Dec 2020 21:16:55 +0000 (16:16 -0500)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 28 Jan 2022 16:22:26 +0000 (11:22 -0500)
This patch adds C++ wrappers for Babeltrace 2 integer range objects.

The two new available types are `bt2::ConstUnsignedIntegerRange` and
`bt2::ConstSignedIntegerRange`.

Get the lower and upper bounds of an integer range object with its
lower() and upper() methods.

Compare compatible integer ranges with with operator==() and
operator!=().

You can't create such objects: they are what integer range set methods
(not part of `src/cpp-common/bt2` yet) return.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I712a34756aa170b862b358e3553814ffa59547e8
Reviewed-on: https://review.lttng.org/c/babeltrace/+/4597

src/cpp-common/bt2/integer-range.hpp [new file with mode: 0644]

diff --git a/src/cpp-common/bt2/integer-range.hpp b/src/cpp-common/bt2/integer-range.hpp
new file mode 100644 (file)
index 0000000..7e94e9e
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_HPP
+#define BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_HPP
+
+#include <type_traits>
+#include <cstdint>
+#include <babeltrace2/babeltrace.h>
+
+#include "internal/borrowed-obj.hpp"
+
+namespace bt2 {
+
+namespace internal {
+
+template <typename ValueT>
+struct ConstIntegerRangeSpec;
+
+// Functions specific to unsigned integer ranges
+template <>
+struct ConstIntegerRangeSpec<const bt_integer_range_unsigned> final
+{
+    static std::uint64_t lower(const bt_integer_range_unsigned * const libRangePtr) noexcept
+    {
+        return bt_integer_range_unsigned_get_lower(libRangePtr);
+    }
+
+    static std::uint64_t upper(const bt_integer_range_unsigned * const libRangePtr) noexcept
+    {
+        return bt_integer_range_unsigned_get_upper(libRangePtr);
+    }
+
+    static bool isEqual(const bt_integer_range_unsigned * const libRangePtrA,
+                        const bt_integer_range_unsigned * const libRangePtrB) noexcept
+    {
+        return static_cast<bool>(bt_integer_range_unsigned_is_equal(libRangePtrA, libRangePtrB));
+    }
+};
+
+// Functions specific to signed integer ranges
+template <>
+struct ConstIntegerRangeSpec<const bt_integer_range_signed> final
+{
+    static std::int64_t lower(const bt_integer_range_signed * const libRangePtr) noexcept
+    {
+        return bt_integer_range_signed_get_lower(libRangePtr);
+    }
+
+    static std::int64_t upper(const bt_integer_range_signed * const libRangePtr) noexcept
+    {
+        return bt_integer_range_signed_get_upper(libRangePtr);
+    }
+
+    static bool isEqual(const bt_integer_range_signed * const libRangePtrA,
+                        const bt_integer_range_signed * const libRangePtrB) noexcept
+    {
+        return static_cast<bool>(bt_integer_range_signed_is_equal(libRangePtrA, libRangePtrB));
+    }
+};
+
+} // namespace internal
+
+template <typename LibObjT>
+class ConstIntegerRange final : public internal::BorrowedObj<LibObjT>
+{
+private:
+    using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
+    using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
+    using _ThisConstIntegerRange = ConstIntegerRange<LibObjT>;
+
+public:
+    using Value =
+        typename std::conditional<std::is_same<LibObjT, const bt_integer_range_unsigned>::value,
+                                  std::uint64_t, std::int64_t>::type;
+
+public:
+    explicit ConstIntegerRange(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
+    {
+    }
+
+    ConstIntegerRange(const _ThisConstIntegerRange& range) noexcept : _ThisBorrowedObj {range}
+    {
+    }
+
+    _ThisConstIntegerRange& operator=(const _ThisConstIntegerRange& range) noexcept
+    {
+        _ThisBorrowedObj::operator=(range);
+        return *this;
+    }
+
+    bool operator==(const _ThisConstIntegerRange& other) const noexcept
+    {
+        return internal::ConstIntegerRangeSpec<LibObjT>::isEqual(this->_libObjPtr(),
+                                                                 other._libObjPtr());
+    }
+
+    bool operator!=(const _ThisConstIntegerRange& other) const noexcept
+    {
+        return !(*this == other);
+    }
+
+    Value lower() const noexcept
+    {
+        return internal::ConstIntegerRangeSpec<LibObjT>::lower(this->_libObjPtr());
+    }
+
+    Value upper() const noexcept
+    {
+        return internal::ConstIntegerRangeSpec<LibObjT>::upper(this->_libObjPtr());
+    }
+};
+
+using ConstUnsignedIntegerRange = ConstIntegerRange<const bt_integer_range_unsigned>;
+using ConstSignedIntegerRange = ConstIntegerRange<const bt_integer_range_signed>;
+
+} // namespace bt2
+
+#endif // BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_HPP
This page took 0.02539 seconds and 4 git commands to generate.