2 * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_SET_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_SET_HPP
11 #include <type_traits>
13 #include <babeltrace2/babeltrace.h>
15 #include "borrowed-object-iterator.hpp"
16 #include "borrowed-object.hpp"
18 #include "integer-range.hpp"
19 #include "internal/utils.hpp"
20 #include "shared-object.hpp"
25 template <typename LibObjT>
26 struct IntegerRangeSetRefFuncs;
29 struct IntegerRangeSetRefFuncs<const bt_integer_range_set_unsigned> final
31 static void get(const bt_integer_range_set_unsigned * const libObjPtr) noexcept
33 bt_integer_range_set_unsigned_get_ref(libObjPtr);
36 static void put(const bt_integer_range_set_unsigned * const libObjPtr) noexcept
38 bt_integer_range_set_unsigned_put_ref(libObjPtr);
43 struct IntegerRangeSetRefFuncs<const bt_integer_range_set_signed> final
45 static void get(const bt_integer_range_set_signed * const libObjPtr) noexcept
47 bt_integer_range_set_signed_get_ref(libObjPtr);
50 static void put(const bt_integer_range_set_signed * const libObjPtr) noexcept
52 bt_integer_range_set_signed_put_ref(libObjPtr);
56 template <typename LibObjT>
57 struct CommonIntegerRangeSetSpec;
59 /* Functions specific to unsigned integer range sets */
61 struct CommonIntegerRangeSetSpec<const bt_integer_range_set_unsigned> final
63 static std::uint64_t length(const bt_integer_range_set_unsigned * const libRangePtr) noexcept
65 return bt_integer_range_set_get_range_count(
66 bt_integer_range_set_unsigned_as_range_set_const(libRangePtr));
69 static const bt_integer_range_unsigned *
70 rangeByIndex(const bt_integer_range_set_unsigned * const libRangePtr,
71 const std::uint64_t index) noexcept
73 return bt_integer_range_set_unsigned_borrow_range_by_index_const(libRangePtr, index);
76 static bool isEqual(const bt_integer_range_set_unsigned * const libRangePtrA,
77 const bt_integer_range_set_unsigned * const libRangePtrB) noexcept
79 return static_cast<bool>(
80 bt_integer_range_set_unsigned_is_equal(libRangePtrA, libRangePtrB));
83 static bt_integer_range_set_add_range_status
84 addRange(bt_integer_range_set_unsigned * const libRangePtr, const std::uint64_t lower,
85 const std::uint64_t upper) noexcept
87 return bt_integer_range_set_unsigned_add_range(libRangePtr, lower, upper);
90 static bt_integer_range_set_unsigned *create() noexcept
92 return bt_integer_range_set_unsigned_create();
96 /* Functions specific to signed integer range sets */
98 struct CommonIntegerRangeSetSpec<const bt_integer_range_set_signed> final
100 static std::uint64_t length(const bt_integer_range_set_signed * const libRangePtr) noexcept
102 return bt_integer_range_set_get_range_count(
103 bt_integer_range_set_signed_as_range_set_const(libRangePtr));
106 static const bt_integer_range_signed *
107 rangeByIndex(const bt_integer_range_set_signed * const libRangePtr,
108 const std::uint64_t index) noexcept
110 return bt_integer_range_set_signed_borrow_range_by_index_const(libRangePtr, index);
113 static bool isEqual(const bt_integer_range_set_signed * const libRangePtrA,
114 const bt_integer_range_set_signed * const libRangePtrB) noexcept
116 return static_cast<bool>(bt_integer_range_set_signed_is_equal(libRangePtrA, libRangePtrB));
119 static bt_integer_range_set_add_range_status
120 addRange(bt_integer_range_set_signed * const libRangePtr, const std::int64_t lower,
121 const std::int64_t upper) noexcept
123 return bt_integer_range_set_signed_add_range(libRangePtr, lower, upper);
126 static bt_integer_range_set_signed *create() noexcept
128 return bt_integer_range_set_signed_create();
132 } /* namespace internal */
134 template <typename LibObjT>
135 class CommonIntegerRangeSet final : public BorrowedObject<LibObjT>
138 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
139 using typename BorrowedObject<LibObjT>::_LibObjPtr;
140 using _ConstLibObjT = typename std::add_const<LibObjT>::type;
141 using _RefFuncs = internal::IntegerRangeSetRefFuncs<_ConstLibObjT>;
142 using _Spec = internal::CommonIntegerRangeSetSpec<_ConstLibObjT>;
143 using _ThisCommonIntegerRangeSet = CommonIntegerRangeSet<LibObjT>;
146 using Shared = SharedObject<_ThisCommonIntegerRangeSet, LibObjT, _RefFuncs>;
148 using Range = typename std::conditional<
149 std::is_same<_ConstLibObjT, const bt_integer_range_set_unsigned>::value,
150 ConstUnsignedIntegerRange, ConstSignedIntegerRange>::type;
152 using Value = typename Range::Value;
153 using Iterator = BorrowedObjectIterator<CommonIntegerRangeSet>;
155 explicit CommonIntegerRangeSet(const _LibObjPtr libObjPtr) noexcept :
156 _ThisBorrowedObject {libObjPtr}
160 static Shared create()
162 const auto libObjPtr = _Spec::create();
164 internal::validateCreatedObjPtr(libObjPtr);
165 return CommonIntegerRangeSet::Shared::createWithoutRef(libObjPtr);
168 template <typename OtherLibObjT>
169 CommonIntegerRangeSet(const CommonIntegerRangeSet<OtherLibObjT> rangeSet) noexcept :
170 _ThisBorrowedObject {rangeSet}
174 template <typename OtherLibObjT>
175 _ThisCommonIntegerRangeSet&
176 operator=(const CommonIntegerRangeSet<OtherLibObjT> rangeSet) noexcept
178 _ThisBorrowedObject::operator=(rangeSet);
182 _ConstLibObjT asConst() const noexcept
184 return _ConstLibObjT {*this};
187 template <typename OtherLibObjT>
188 bool operator==(const CommonIntegerRangeSet<OtherLibObjT> other) const noexcept
190 return _Spec::isEqual(this->libObjPtr(), other.libObjPtr());
193 template <typename OtherLibObjT>
194 bool operator!=(const CommonIntegerRangeSet<OtherLibObjT> other) const noexcept
196 return !(*this == other);
199 void addRange(const Value lower, const Value upper) const
202 !std::is_const<LibObjT>::value,
203 "Not available with `bt2::ConstUnsignedIntegerRangeSet` or `bt2::ConstSignedIntegerRangeSet`.");
205 const auto status = _Spec::addRange(this->libObjPtr(), lower, upper);
207 if (status == BT_INTEGER_RANGE_SET_ADD_RANGE_STATUS_MEMORY_ERROR) {
208 throw MemoryError {};
212 std::uint64_t length() const noexcept
214 return _Spec::length(this->libObjPtr());
217 Range operator[](const std::uint64_t index) const noexcept
219 return Range {_Spec::rangeByIndex(this->libObjPtr(), index)};
222 Iterator begin() const noexcept
224 return Iterator {*this, 0};
227 Iterator end() const noexcept
229 return Iterator {*this, this->length()};
232 Shared shared() const noexcept
234 return Shared::createWithRef(*this);
238 using UnsignedIntegerRangeSet = CommonIntegerRangeSet<bt_integer_range_set_unsigned>;
239 using ConstUnsignedIntegerRangeSet = CommonIntegerRangeSet<const bt_integer_range_set_unsigned>;
240 using SignedIntegerRangeSet = CommonIntegerRangeSet<bt_integer_range_set_signed>;
241 using ConstSignedIntegerRangeSet = CommonIntegerRangeSet<const bt_integer_range_set_signed>;
245 struct UnsignedIntegerRangeSetTypeDescr
247 using Const = ConstUnsignedIntegerRangeSet;
248 using NonConst = UnsignedIntegerRangeSet;
252 struct TypeDescr<UnsignedIntegerRangeSet> : public UnsignedIntegerRangeSetTypeDescr
257 struct TypeDescr<ConstUnsignedIntegerRangeSet> : public UnsignedIntegerRangeSetTypeDescr
261 struct SignedIntegerRangeSetTypeDescr
263 using Const = ConstSignedIntegerRangeSet;
264 using NonConst = SignedIntegerRangeSet;
268 struct TypeDescr<SignedIntegerRangeSet> : public SignedIntegerRangeSetTypeDescr
273 struct TypeDescr<ConstSignedIntegerRangeSet> : public SignedIntegerRangeSetTypeDescr
277 } /* namespace internal */
278 } /* namespace bt2 */
280 #endif /* BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_SET_HPP */