Commit | Line | Data |
---|---|---|
64ded3fb PP |
1 | /* |
2 | * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
7 | #ifndef BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_SET_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_SET_HPP | |
9 | ||
10 | #include <cstdint> | |
11 | #include <type_traits> | |
c802cacb | 12 | |
64ded3fb PP |
13 | #include <babeltrace2/babeltrace.h> |
14 | ||
0d218157 | 15 | #include "borrowed-object.hpp" |
70cfba94 | 16 | #include "common-iter.hpp" |
c802cacb SM |
17 | #include "exc.hpp" |
18 | #include "integer-range.hpp" | |
64ded3fb | 19 | #include "internal/utils.hpp" |
7f5cdaf0 | 20 | #include "shared-object.hpp" |
64ded3fb PP |
21 | |
22 | namespace bt2 { | |
64ded3fb PP |
23 | namespace internal { |
24 | ||
25 | template <typename LibObjT> | |
26 | struct IntegerRangeSetRefFuncs; | |
27 | ||
28 | template <> | |
29 | struct IntegerRangeSetRefFuncs<const bt_integer_range_set_unsigned> final | |
30 | { | |
31 | static void get(const bt_integer_range_set_unsigned * const libObjPtr) | |
32 | { | |
33 | bt_integer_range_set_unsigned_get_ref(libObjPtr); | |
34 | } | |
35 | ||
36 | static void put(const bt_integer_range_set_unsigned * const libObjPtr) | |
37 | { | |
1796d135 | 38 | bt_integer_range_set_unsigned_put_ref(libObjPtr); |
64ded3fb PP |
39 | } |
40 | }; | |
41 | ||
42 | template <> | |
43 | struct IntegerRangeSetRefFuncs<const bt_integer_range_set_signed> final | |
44 | { | |
45 | static void get(const bt_integer_range_set_signed * const libObjPtr) | |
46 | { | |
47 | bt_integer_range_set_signed_get_ref(libObjPtr); | |
48 | } | |
49 | ||
50 | static void put(const bt_integer_range_set_signed * const libObjPtr) | |
51 | { | |
1796d135 | 52 | bt_integer_range_set_signed_put_ref(libObjPtr); |
64ded3fb PP |
53 | } |
54 | }; | |
55 | ||
56 | template <typename LibObjT> | |
57 | struct CommonIntegerRangeSetSpec; | |
58 | ||
b5f55e9f | 59 | /* Functions specific to unsigned integer range sets */ |
64ded3fb PP |
60 | template <> |
61 | struct CommonIntegerRangeSetSpec<const bt_integer_range_set_unsigned> final | |
62 | { | |
63 | static std::uint64_t size(const bt_integer_range_set_unsigned * const libRangePtr) noexcept | |
64 | { | |
65 | return bt_integer_range_set_get_range_count( | |
66 | bt_integer_range_set_unsigned_as_range_set_const(libRangePtr)); | |
67 | } | |
68 | ||
69 | static const bt_integer_range_unsigned * | |
70 | rangeByIndex(const bt_integer_range_set_unsigned * const libRangePtr, | |
71 | const std::uint64_t index) noexcept | |
72 | { | |
73 | return bt_integer_range_set_unsigned_borrow_range_by_index_const(libRangePtr, index); | |
74 | } | |
75 | ||
76 | static bool isEqual(const bt_integer_range_set_unsigned * const libRangePtrA, | |
77 | const bt_integer_range_set_unsigned * const libRangePtrB) noexcept | |
78 | { | |
79 | return static_cast<bool>( | |
80 | bt_integer_range_set_unsigned_is_equal(libRangePtrA, libRangePtrB)); | |
81 | } | |
82 | ||
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 | |
86 | { | |
87 | return bt_integer_range_set_unsigned_add_range(libRangePtr, lower, upper); | |
88 | } | |
89 | ||
90 | static bt_integer_range_set_unsigned *create() noexcept | |
91 | { | |
92 | return bt_integer_range_set_unsigned_create(); | |
93 | } | |
94 | }; | |
95 | ||
b5f55e9f | 96 | /* Functions specific to signed integer range sets */ |
64ded3fb PP |
97 | template <> |
98 | struct CommonIntegerRangeSetSpec<const bt_integer_range_set_signed> final | |
99 | { | |
100 | static std::uint64_t size(const bt_integer_range_set_signed * const libRangePtr) noexcept | |
101 | { | |
102 | return bt_integer_range_set_get_range_count( | |
103 | bt_integer_range_set_signed_as_range_set_const(libRangePtr)); | |
104 | } | |
105 | ||
106 | static const bt_integer_range_signed * | |
107 | rangeByIndex(const bt_integer_range_set_signed * const libRangePtr, | |
108 | const std::uint64_t index) noexcept | |
109 | { | |
110 | return bt_integer_range_set_signed_borrow_range_by_index_const(libRangePtr, index); | |
111 | } | |
112 | ||
113 | static bool isEqual(const bt_integer_range_set_signed * const libRangePtrA, | |
114 | const bt_integer_range_set_signed * const libRangePtrB) noexcept | |
115 | { | |
116 | return static_cast<bool>(bt_integer_range_set_signed_is_equal(libRangePtrA, libRangePtrB)); | |
117 | } | |
118 | ||
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 | |
122 | { | |
123 | return bt_integer_range_set_signed_add_range(libRangePtr, lower, upper); | |
124 | } | |
125 | ||
126 | static bt_integer_range_set_signed *create() noexcept | |
127 | { | |
128 | return bt_integer_range_set_signed_create(); | |
129 | } | |
130 | }; | |
131 | ||
b5f55e9f | 132 | } /* namespace internal */ |
64ded3fb | 133 | |
12435a68 PP |
134 | template <typename LibObjT> |
135 | class ConstVariantWithIntegerSelectorFieldClassOption; | |
136 | ||
137 | template <typename LibObjT, typename RangeSetT> | |
138 | class CommonVariantWithIntegerSelectorFieldClass; | |
139 | ||
74fc764d PP |
140 | template <typename LibObjT> |
141 | class CommonTraceClass; | |
142 | ||
64ded3fb | 143 | template <typename LibObjT> |
0d218157 | 144 | class CommonIntegerRangeSet final : public BorrowedObject<LibObjT> |
64ded3fb | 145 | { |
341a67c4 | 146 | /* Allow operator==() to call `other.libObjPtr()` */ |
64ded3fb PP |
147 | friend class CommonIntegerRangeSet<bt_integer_range_set_unsigned>; |
148 | friend class CommonIntegerRangeSet<const bt_integer_range_set_unsigned>; | |
149 | friend class CommonIntegerRangeSet<bt_integer_range_set_signed>; | |
150 | friend class CommonIntegerRangeSet<const bt_integer_range_set_signed>; | |
151 | ||
341a67c4 | 152 | /* Allow appendOption() to call `ranges.libObjPtr()` */ |
12435a68 PP |
153 | friend class CommonVariantWithIntegerSelectorFieldClass< |
154 | bt_field_class, | |
155 | ConstVariantWithIntegerSelectorFieldClassOption< | |
156 | const bt_field_class_variant_with_selector_field_integer_unsigned_option>>; | |
157 | ||
158 | friend class CommonVariantWithIntegerSelectorFieldClass< | |
159 | bt_field_class, | |
160 | ConstVariantWithIntegerSelectorFieldClassOption< | |
161 | const bt_field_class_variant_with_selector_field_integer_signed_option>>; | |
162 | ||
341a67c4 | 163 | /* Allow create*FieldClass() to call `ranges.libObjPtr()` */ |
74fc764d PP |
164 | friend class CommonTraceClass<bt_trace_class>; |
165 | ||
64ded3fb | 166 | private: |
0d218157 PP |
167 | using typename BorrowedObject<LibObjT>::_ThisBorrowedObject; |
168 | using typename BorrowedObject<LibObjT>::_LibObjPtr; | |
64ded3fb PP |
169 | using _ConstLibObjT = typename std::add_const<LibObjT>::type; |
170 | using _RefFuncs = internal::IntegerRangeSetRefFuncs<_ConstLibObjT>; | |
171 | using _Spec = internal::CommonIntegerRangeSetSpec<_ConstLibObjT>; | |
172 | using _ThisCommonIntegerRangeSet = CommonIntegerRangeSet<LibObjT>; | |
173 | ||
174 | public: | |
7f5cdaf0 | 175 | using Shared = SharedObject<_ThisCommonIntegerRangeSet, LibObjT, _RefFuncs>; |
64ded3fb PP |
176 | |
177 | using Range = typename std::conditional< | |
178 | std::is_same<_ConstLibObjT, const bt_integer_range_set_unsigned>::value, | |
179 | ConstUnsignedIntegerRange, ConstSignedIntegerRange>::type; | |
180 | ||
181 | using Value = typename Range::Value; | |
70cfba94 | 182 | using Iterator = CommonIterator<CommonIntegerRangeSet, Range>; |
64ded3fb PP |
183 | |
184 | explicit CommonIntegerRangeSet(const _LibObjPtr libObjPtr) noexcept : | |
0d218157 | 185 | _ThisBorrowedObject {libObjPtr} |
64ded3fb PP |
186 | { |
187 | } | |
188 | ||
189 | static Shared create() | |
190 | { | |
191 | const auto libObjPtr = _Spec::create(); | |
192 | ||
193 | internal::validateCreatedObjPtr(libObjPtr); | |
c9c0b6e2 | 194 | return CommonIntegerRangeSet::Shared::createWithoutRef(libObjPtr); |
64ded3fb PP |
195 | } |
196 | ||
197 | template <typename OtherLibObjT> | |
100fa861 | 198 | CommonIntegerRangeSet(const CommonIntegerRangeSet<OtherLibObjT> rangeSet) noexcept : |
0d218157 | 199 | _ThisBorrowedObject {rangeSet} |
64ded3fb PP |
200 | { |
201 | } | |
202 | ||
203 | template <typename OtherLibObjT> | |
204 | _ThisCommonIntegerRangeSet& | |
100fa861 | 205 | operator=(const CommonIntegerRangeSet<OtherLibObjT> rangeSet) noexcept |
64ded3fb | 206 | { |
0d218157 | 207 | _ThisBorrowedObject::operator=(rangeSet); |
64ded3fb PP |
208 | return *this; |
209 | } | |
210 | ||
211 | template <typename OtherLibObjT> | |
100fa861 | 212 | bool operator==(const CommonIntegerRangeSet<OtherLibObjT> other) const noexcept |
64ded3fb | 213 | { |
341a67c4 | 214 | return _Spec::isEqual(this->libObjPtr(), other.libObjPtr()); |
64ded3fb PP |
215 | } |
216 | ||
217 | template <typename OtherLibObjT> | |
100fa861 | 218 | bool operator!=(const CommonIntegerRangeSet<OtherLibObjT> other) const noexcept |
64ded3fb PP |
219 | { |
220 | return !(*this == other); | |
221 | } | |
222 | ||
223 | void addRange(const Value lower, const Value upper) | |
224 | { | |
225 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
226 | ||
341a67c4 | 227 | const auto status = _Spec::addRange(this->libObjPtr(), lower, upper); |
64ded3fb PP |
228 | |
229 | if (status == BT_INTEGER_RANGE_SET_ADD_RANGE_STATUS_MEMORY_ERROR) { | |
39278ebc | 230 | throw MemoryError {}; |
64ded3fb PP |
231 | } |
232 | } | |
233 | ||
234 | std::uint64_t size() const noexcept | |
235 | { | |
341a67c4 | 236 | return _Spec::size(this->libObjPtr()); |
64ded3fb PP |
237 | } |
238 | ||
239 | Range operator[](const std::uint64_t index) const noexcept | |
240 | { | |
341a67c4 | 241 | return Range {_Spec::rangeByIndex(this->libObjPtr(), index)}; |
64ded3fb PP |
242 | } |
243 | ||
70cfba94 FD |
244 | Iterator begin() const noexcept |
245 | { | |
246 | return Iterator {*this, 0}; | |
247 | } | |
248 | ||
249 | Iterator end() const noexcept | |
250 | { | |
251 | return Iterator {*this, this->size()}; | |
252 | } | |
253 | ||
64ded3fb PP |
254 | Shared shared() const noexcept |
255 | { | |
c9c0b6e2 | 256 | return Shared::createWithRef(*this); |
64ded3fb PP |
257 | } |
258 | }; | |
259 | ||
260 | using UnsignedIntegerRangeSet = CommonIntegerRangeSet<bt_integer_range_set_unsigned>; | |
261 | using ConstUnsignedIntegerRangeSet = CommonIntegerRangeSet<const bt_integer_range_set_unsigned>; | |
262 | using SignedIntegerRangeSet = CommonIntegerRangeSet<bt_integer_range_set_signed>; | |
263 | using ConstSignedIntegerRangeSet = CommonIntegerRangeSet<const bt_integer_range_set_signed>; | |
264 | ||
4927bae7 PP |
265 | namespace internal { |
266 | ||
267 | struct UnsignedIntegerRangeSetTypeDescr | |
268 | { | |
269 | using Const = ConstUnsignedIntegerRangeSet; | |
270 | using NonConst = UnsignedIntegerRangeSet; | |
271 | }; | |
272 | ||
273 | template <> | |
274 | struct TypeDescr<UnsignedIntegerRangeSet> : public UnsignedIntegerRangeSetTypeDescr | |
275 | { | |
276 | }; | |
277 | ||
278 | template <> | |
279 | struct TypeDescr<ConstUnsignedIntegerRangeSet> : public UnsignedIntegerRangeSetTypeDescr | |
280 | { | |
281 | }; | |
282 | ||
283 | struct SignedIntegerRangeSetTypeDescr | |
284 | { | |
285 | using Const = ConstSignedIntegerRangeSet; | |
286 | using NonConst = SignedIntegerRangeSet; | |
287 | }; | |
288 | ||
289 | template <> | |
290 | struct TypeDescr<SignedIntegerRangeSet> : public SignedIntegerRangeSetTypeDescr | |
291 | { | |
292 | }; | |
293 | ||
294 | template <> | |
295 | struct TypeDescr<ConstSignedIntegerRangeSet> : public SignedIntegerRangeSetTypeDescr | |
296 | { | |
297 | }; | |
298 | ||
299 | } /* namespace internal */ | |
b5f55e9f | 300 | } /* namespace bt2 */ |
64ded3fb | 301 | |
b5f55e9f | 302 | #endif /* BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_SET_HPP */ |