cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / cpp-common / bt2 / integer-range-set.hpp
CommitLineData
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
56862ee2 15#include "borrowed-object-iterator.hpp"
0d218157 16#include "borrowed-object.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
22namespace bt2 {
64ded3fb
PP
23namespace internal {
24
25template <typename LibObjT>
26struct IntegerRangeSetRefFuncs;
27
28template <>
29struct IntegerRangeSetRefFuncs<const bt_integer_range_set_unsigned> final
30{
c677c492 31 static void get(const bt_integer_range_set_unsigned * const libObjPtr) noexcept
64ded3fb
PP
32 {
33 bt_integer_range_set_unsigned_get_ref(libObjPtr);
34 }
35
c677c492 36 static void put(const bt_integer_range_set_unsigned * const libObjPtr) noexcept
64ded3fb 37 {
1796d135 38 bt_integer_range_set_unsigned_put_ref(libObjPtr);
64ded3fb
PP
39 }
40};
41
42template <>
43struct IntegerRangeSetRefFuncs<const bt_integer_range_set_signed> final
44{
c677c492 45 static void get(const bt_integer_range_set_signed * const libObjPtr) noexcept
64ded3fb
PP
46 {
47 bt_integer_range_set_signed_get_ref(libObjPtr);
48 }
49
c677c492 50 static void put(const bt_integer_range_set_signed * const libObjPtr) noexcept
64ded3fb 51 {
1796d135 52 bt_integer_range_set_signed_put_ref(libObjPtr);
64ded3fb
PP
53 }
54};
55
56template <typename LibObjT>
57struct CommonIntegerRangeSetSpec;
58
b5f55e9f 59/* Functions specific to unsigned integer range sets */
64ded3fb
PP
60template <>
61struct CommonIntegerRangeSetSpec<const bt_integer_range_set_unsigned> final
62{
c0b73c63 63 static std::uint64_t length(const bt_integer_range_set_unsigned * const libRangePtr) noexcept
64ded3fb
PP
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
97template <>
98struct CommonIntegerRangeSetSpec<const bt_integer_range_set_signed> final
99{
c0b73c63 100 static std::uint64_t length(const bt_integer_range_set_signed * const libRangePtr) noexcept
64ded3fb
PP
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
PP
133
134template <typename LibObjT>
0d218157 135class CommonIntegerRangeSet final : public BorrowedObject<LibObjT>
64ded3fb 136{
64ded3fb 137private:
0d218157 138 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
64ded3fb 139 using _ConstLibObjT = typename std::add_const<LibObjT>::type;
64ded3fb 140 using _Spec = internal::CommonIntegerRangeSetSpec<_ConstLibObjT>;
64ded3fb
PP
141
142public:
d246c457
PP
143 using typename BorrowedObject<LibObjT>::LibObjPtr;
144
ac19444e
PP
145 using Shared = SharedObject<CommonIntegerRangeSet, LibObjT,
146 internal::IntegerRangeSetRefFuncs<_ConstLibObjT>>;
64ded3fb
PP
147
148 using Range = typename std::conditional<
149 std::is_same<_ConstLibObjT, const bt_integer_range_set_unsigned>::value,
150 ConstUnsignedIntegerRange, ConstSignedIntegerRange>::type;
151
152 using Value = typename Range::Value;
56862ee2 153 using Iterator = BorrowedObjectIterator<CommonIntegerRangeSet>;
64ded3fb 154
d246c457 155 explicit CommonIntegerRangeSet(const LibObjPtr libObjPtr) noexcept :
0d218157 156 _ThisBorrowedObject {libObjPtr}
64ded3fb
PP
157 {
158 }
159
160 static Shared create()
161 {
162 const auto libObjPtr = _Spec::create();
163
164 internal::validateCreatedObjPtr(libObjPtr);
c9c0b6e2 165 return CommonIntegerRangeSet::Shared::createWithoutRef(libObjPtr);
64ded3fb
PP
166 }
167
168 template <typename OtherLibObjT>
100fa861 169 CommonIntegerRangeSet(const CommonIntegerRangeSet<OtherLibObjT> rangeSet) noexcept :
0d218157 170 _ThisBorrowedObject {rangeSet}
64ded3fb
PP
171 {
172 }
173
174 template <typename OtherLibObjT>
ac30a470 175 CommonIntegerRangeSet operator=(const CommonIntegerRangeSet<OtherLibObjT> rangeSet) noexcept
64ded3fb 176 {
0d218157 177 _ThisBorrowedObject::operator=(rangeSet);
64ded3fb
PP
178 return *this;
179 }
180
328a274a
PP
181 _ConstLibObjT asConst() const noexcept
182 {
183 return _ConstLibObjT {*this};
184 }
185
64ded3fb 186 template <typename OtherLibObjT>
100fa861 187 bool operator==(const CommonIntegerRangeSet<OtherLibObjT> other) const noexcept
64ded3fb 188 {
341a67c4 189 return _Spec::isEqual(this->libObjPtr(), other.libObjPtr());
64ded3fb
PP
190 }
191
192 template <typename OtherLibObjT>
100fa861 193 bool operator!=(const CommonIntegerRangeSet<OtherLibObjT> other) const noexcept
64ded3fb
PP
194 {
195 return !(*this == other);
196 }
197
2a24eba8 198 CommonIntegerRangeSet addRange(const Value lower, const Value upper) const
64ded3fb 199 {
5c895f64
PP
200 static_assert(
201 !std::is_const<LibObjT>::value,
202 "Not available with `bt2::ConstUnsignedIntegerRangeSet` or `bt2::ConstSignedIntegerRangeSet`.");
64ded3fb 203
341a67c4 204 const auto status = _Spec::addRange(this->libObjPtr(), lower, upper);
64ded3fb
PP
205
206 if (status == BT_INTEGER_RANGE_SET_ADD_RANGE_STATUS_MEMORY_ERROR) {
39278ebc 207 throw MemoryError {};
64ded3fb 208 }
2a24eba8
SM
209
210 return *this;
64ded3fb
PP
211 }
212
c0b73c63 213 std::uint64_t length() const noexcept
64ded3fb 214 {
c0b73c63 215 return _Spec::length(this->libObjPtr());
64ded3fb
PP
216 }
217
218 Range operator[](const std::uint64_t index) const noexcept
219 {
341a67c4 220 return Range {_Spec::rangeByIndex(this->libObjPtr(), index)};
64ded3fb
PP
221 }
222
70cfba94
FD
223 Iterator begin() const noexcept
224 {
225 return Iterator {*this, 0};
226 }
227
228 Iterator end() const noexcept
229 {
c0b73c63 230 return Iterator {*this, this->length()};
70cfba94
FD
231 }
232
64ded3fb
PP
233 Shared shared() const noexcept
234 {
c9c0b6e2 235 return Shared::createWithRef(*this);
64ded3fb
PP
236 }
237};
238
239using UnsignedIntegerRangeSet = CommonIntegerRangeSet<bt_integer_range_set_unsigned>;
240using ConstUnsignedIntegerRangeSet = CommonIntegerRangeSet<const bt_integer_range_set_unsigned>;
241using SignedIntegerRangeSet = CommonIntegerRangeSet<bt_integer_range_set_signed>;
242using ConstSignedIntegerRangeSet = CommonIntegerRangeSet<const bt_integer_range_set_signed>;
243
4927bae7
PP
244namespace internal {
245
246struct UnsignedIntegerRangeSetTypeDescr
247{
248 using Const = ConstUnsignedIntegerRangeSet;
249 using NonConst = UnsignedIntegerRangeSet;
250};
251
252template <>
253struct TypeDescr<UnsignedIntegerRangeSet> : public UnsignedIntegerRangeSetTypeDescr
254{
255};
256
257template <>
258struct TypeDescr<ConstUnsignedIntegerRangeSet> : public UnsignedIntegerRangeSetTypeDescr
259{
260};
261
262struct SignedIntegerRangeSetTypeDescr
263{
264 using Const = ConstSignedIntegerRangeSet;
265 using NonConst = SignedIntegerRangeSet;
266};
267
268template <>
269struct TypeDescr<SignedIntegerRangeSet> : public SignedIntegerRangeSetTypeDescr
270{
271};
272
273template <>
274struct TypeDescr<ConstSignedIntegerRangeSet> : public SignedIntegerRangeSetTypeDescr
275{
276};
277
278} /* namespace internal */
b5f55e9f 279} /* namespace bt2 */
64ded3fb 280
b5f55e9f 281#endif /* BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_SET_HPP */
This page took 0.05313 seconds and 4 git commands to generate.