2 * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_HPP
10 #include <type_traits>
12 #include <babeltrace2/babeltrace.h>
14 #include "internal/borrowed-obj.hpp"
20 template <typename ValueT>
21 struct ConstIntegerRangeSpec;
23 /* Functions specific to unsigned integer ranges */
25 struct ConstIntegerRangeSpec<const bt_integer_range_unsigned> final
27 static std::uint64_t lower(const bt_integer_range_unsigned * const libRangePtr) noexcept
29 return bt_integer_range_unsigned_get_lower(libRangePtr);
32 static std::uint64_t upper(const bt_integer_range_unsigned * const libRangePtr) noexcept
34 return bt_integer_range_unsigned_get_upper(libRangePtr);
37 static bool isEqual(const bt_integer_range_unsigned * const libRangePtrA,
38 const bt_integer_range_unsigned * const libRangePtrB) noexcept
40 return static_cast<bool>(bt_integer_range_unsigned_is_equal(libRangePtrA, libRangePtrB));
44 /* Functions specific to signed integer ranges */
46 struct ConstIntegerRangeSpec<const bt_integer_range_signed> final
48 static std::int64_t lower(const bt_integer_range_signed * const libRangePtr) noexcept
50 return bt_integer_range_signed_get_lower(libRangePtr);
53 static std::int64_t upper(const bt_integer_range_signed * const libRangePtr) noexcept
55 return bt_integer_range_signed_get_upper(libRangePtr);
58 static bool isEqual(const bt_integer_range_signed * const libRangePtrA,
59 const bt_integer_range_signed * const libRangePtrB) noexcept
61 return static_cast<bool>(bt_integer_range_signed_is_equal(libRangePtrA, libRangePtrB));
65 } /* namespace internal */
67 template <typename LibObjT>
68 class ConstIntegerRange final : public internal::BorrowedObj<LibObjT>
71 using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
72 using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
73 using _ThisConstIntegerRange = ConstIntegerRange<LibObjT>;
77 typename std::conditional<std::is_same<LibObjT, const bt_integer_range_unsigned>::value,
78 std::uint64_t, std::int64_t>::type;
81 explicit ConstIntegerRange(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
85 ConstIntegerRange(const _ThisConstIntegerRange& range) noexcept : _ThisBorrowedObj {range}
89 _ThisConstIntegerRange& operator=(const _ThisConstIntegerRange& range) noexcept
91 _ThisBorrowedObj::operator=(range);
95 bool operator==(const _ThisConstIntegerRange& other) const noexcept
97 return internal::ConstIntegerRangeSpec<LibObjT>::isEqual(this->libObjPtr(),
101 bool operator!=(const _ThisConstIntegerRange& other) const noexcept
103 return !(*this == other);
106 Value lower() const noexcept
108 return internal::ConstIntegerRangeSpec<LibObjT>::lower(this->libObjPtr());
111 Value upper() const noexcept
113 return internal::ConstIntegerRangeSpec<LibObjT>::upper(this->libObjPtr());
117 using ConstUnsignedIntegerRange = ConstIntegerRange<const bt_integer_range_unsigned>;
118 using ConstSignedIntegerRange = ConstIntegerRange<const bt_integer_range_signed>;
120 } /* namespace bt2 */
122 #endif /* BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_HPP */