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
11 #include <type_traits>
13 #include <babeltrace2/babeltrace.h>
15 #include "borrowed-object.hpp"
21 template <typename ValueT>
22 struct ConstIntegerRangeSpec;
24 /* Functions specific to unsigned integer ranges */
26 struct ConstIntegerRangeSpec<const bt_integer_range_unsigned> final
28 static std::uint64_t lower(const bt_integer_range_unsigned * const libRangePtr) noexcept
30 return bt_integer_range_unsigned_get_lower(libRangePtr);
33 static std::uint64_t upper(const bt_integer_range_unsigned * const libRangePtr) noexcept
35 return bt_integer_range_unsigned_get_upper(libRangePtr);
38 static bool isEqual(const bt_integer_range_unsigned * const libRangePtrA,
39 const bt_integer_range_unsigned * const libRangePtrB) noexcept
41 return static_cast<bool>(bt_integer_range_unsigned_is_equal(libRangePtrA, libRangePtrB));
45 /* Functions specific to signed integer ranges */
47 struct ConstIntegerRangeSpec<const bt_integer_range_signed> final
49 static std::int64_t lower(const bt_integer_range_signed * const libRangePtr) noexcept
51 return bt_integer_range_signed_get_lower(libRangePtr);
54 static std::int64_t upper(const bt_integer_range_signed * const libRangePtr) noexcept
56 return bt_integer_range_signed_get_upper(libRangePtr);
59 static bool isEqual(const bt_integer_range_signed * const libRangePtrA,
60 const bt_integer_range_signed * const libRangePtrB) noexcept
62 return static_cast<bool>(bt_integer_range_signed_is_equal(libRangePtrA, libRangePtrB));
66 } /* namespace internal */
68 template <typename LibObjT>
69 class ConstIntegerRange final : public BorrowedObject<LibObjT>
72 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
73 using typename BorrowedObject<LibObjT>::_LibObjPtr;
74 using _ThisConstIntegerRange = ConstIntegerRange<LibObjT>;
78 typename std::conditional<std::is_same<LibObjT, const bt_integer_range_unsigned>::value,
79 std::uint64_t, std::int64_t>::type;
82 explicit ConstIntegerRange(const _LibObjPtr libObjPtr) noexcept :
83 _ThisBorrowedObject {libObjPtr}
87 ConstIntegerRange(const _ThisConstIntegerRange& range) noexcept : _ThisBorrowedObject {range}
91 _ThisConstIntegerRange& operator=(const _ThisConstIntegerRange& range) noexcept
93 _ThisBorrowedObject::operator=(range);
97 bool operator==(const _ThisConstIntegerRange& other) const noexcept
99 return internal::ConstIntegerRangeSpec<LibObjT>::isEqual(this->libObjPtr(),
103 bool operator!=(const _ThisConstIntegerRange& other) const noexcept
105 return !(*this == other);
108 Value lower() const noexcept
110 return internal::ConstIntegerRangeSpec<LibObjT>::lower(this->libObjPtr());
113 Value upper() const noexcept
115 return internal::ConstIntegerRangeSpec<LibObjT>::upper(this->libObjPtr());
119 using ConstUnsignedIntegerRange = ConstIntegerRange<const bt_integer_range_unsigned>;
120 using ConstSignedIntegerRange = ConstIntegerRange<const bt_integer_range_signed>;
122 } /* namespace bt2 */
124 #endif /* BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_HPP */