cpp-common/bt2: remove useless copy operations
[babeltrace.git] / src / cpp-common / bt2 / integer-range-set.hpp
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>
12
13 #include <babeltrace2/babeltrace.h>
14
15 #include "borrowed-object-iterator.hpp"
16 #include "borrowed-object.hpp"
17 #include "exc.hpp"
18 #include "integer-range.hpp"
19 #include "internal/utils.hpp"
20 #include "shared-object.hpp"
21
22 namespace bt2 {
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) noexcept
32 {
33 bt_integer_range_set_unsigned_get_ref(libObjPtr);
34 }
35
36 static void put(const bt_integer_range_set_unsigned * const libObjPtr) noexcept
37 {
38 bt_integer_range_set_unsigned_put_ref(libObjPtr);
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) noexcept
46 {
47 bt_integer_range_set_signed_get_ref(libObjPtr);
48 }
49
50 static void put(const bt_integer_range_set_signed * const libObjPtr) noexcept
51 {
52 bt_integer_range_set_signed_put_ref(libObjPtr);
53 }
54 };
55
56 template <typename LibObjT>
57 struct CommonIntegerRangeSetSpec;
58
59 /* Functions specific to unsigned integer range sets */
60 template <>
61 struct CommonIntegerRangeSetSpec<const bt_integer_range_set_unsigned> final
62 {
63 static std::uint64_t length(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
96 /* Functions specific to signed integer range sets */
97 template <>
98 struct CommonIntegerRangeSetSpec<const bt_integer_range_set_signed> final
99 {
100 static std::uint64_t length(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
132 } /* namespace internal */
133
134 template <typename LibObjT>
135 class CommonIntegerRangeSet final : public BorrowedObject<LibObjT>
136 {
137 private:
138 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
139 using typename BorrowedObject<LibObjT>::_LibObjPtr;
140 using _ConstLibObjT = typename std::add_const<LibObjT>::type;
141 using _Spec = internal::CommonIntegerRangeSetSpec<_ConstLibObjT>;
142
143 public:
144 using Shared = SharedObject<CommonIntegerRangeSet, LibObjT,
145 internal::IntegerRangeSetRefFuncs<_ConstLibObjT>>;
146
147 using Range = typename std::conditional<
148 std::is_same<_ConstLibObjT, const bt_integer_range_set_unsigned>::value,
149 ConstUnsignedIntegerRange, ConstSignedIntegerRange>::type;
150
151 using Value = typename Range::Value;
152 using Iterator = BorrowedObjectIterator<CommonIntegerRangeSet>;
153
154 explicit CommonIntegerRangeSet(const _LibObjPtr libObjPtr) noexcept :
155 _ThisBorrowedObject {libObjPtr}
156 {
157 }
158
159 static Shared create()
160 {
161 const auto libObjPtr = _Spec::create();
162
163 internal::validateCreatedObjPtr(libObjPtr);
164 return CommonIntegerRangeSet::Shared::createWithoutRef(libObjPtr);
165 }
166
167 template <typename OtherLibObjT>
168 CommonIntegerRangeSet(const CommonIntegerRangeSet<OtherLibObjT> rangeSet) noexcept :
169 _ThisBorrowedObject {rangeSet}
170 {
171 }
172
173 template <typename OtherLibObjT>
174 CommonIntegerRangeSet operator=(const CommonIntegerRangeSet<OtherLibObjT> rangeSet) noexcept
175 {
176 _ThisBorrowedObject::operator=(rangeSet);
177 return *this;
178 }
179
180 _ConstLibObjT asConst() const noexcept
181 {
182 return _ConstLibObjT {*this};
183 }
184
185 template <typename OtherLibObjT>
186 bool operator==(const CommonIntegerRangeSet<OtherLibObjT> other) const noexcept
187 {
188 return _Spec::isEqual(this->libObjPtr(), other.libObjPtr());
189 }
190
191 template <typename OtherLibObjT>
192 bool operator!=(const CommonIntegerRangeSet<OtherLibObjT> other) const noexcept
193 {
194 return !(*this == other);
195 }
196
197 void addRange(const Value lower, const Value upper) const
198 {
199 static_assert(
200 !std::is_const<LibObjT>::value,
201 "Not available with `bt2::ConstUnsignedIntegerRangeSet` or `bt2::ConstSignedIntegerRangeSet`.");
202
203 const auto status = _Spec::addRange(this->libObjPtr(), lower, upper);
204
205 if (status == BT_INTEGER_RANGE_SET_ADD_RANGE_STATUS_MEMORY_ERROR) {
206 throw MemoryError {};
207 }
208 }
209
210 std::uint64_t length() const noexcept
211 {
212 return _Spec::length(this->libObjPtr());
213 }
214
215 Range operator[](const std::uint64_t index) const noexcept
216 {
217 return Range {_Spec::rangeByIndex(this->libObjPtr(), index)};
218 }
219
220 Iterator begin() const noexcept
221 {
222 return Iterator {*this, 0};
223 }
224
225 Iterator end() const noexcept
226 {
227 return Iterator {*this, this->length()};
228 }
229
230 Shared shared() const noexcept
231 {
232 return Shared::createWithRef(*this);
233 }
234 };
235
236 using UnsignedIntegerRangeSet = CommonIntegerRangeSet<bt_integer_range_set_unsigned>;
237 using ConstUnsignedIntegerRangeSet = CommonIntegerRangeSet<const bt_integer_range_set_unsigned>;
238 using SignedIntegerRangeSet = CommonIntegerRangeSet<bt_integer_range_set_signed>;
239 using ConstSignedIntegerRangeSet = CommonIntegerRangeSet<const bt_integer_range_set_signed>;
240
241 namespace internal {
242
243 struct UnsignedIntegerRangeSetTypeDescr
244 {
245 using Const = ConstUnsignedIntegerRangeSet;
246 using NonConst = UnsignedIntegerRangeSet;
247 };
248
249 template <>
250 struct TypeDescr<UnsignedIntegerRangeSet> : public UnsignedIntegerRangeSetTypeDescr
251 {
252 };
253
254 template <>
255 struct TypeDescr<ConstUnsignedIntegerRangeSet> : public UnsignedIntegerRangeSetTypeDescr
256 {
257 };
258
259 struct SignedIntegerRangeSetTypeDescr
260 {
261 using Const = ConstSignedIntegerRangeSet;
262 using NonConst = SignedIntegerRangeSet;
263 };
264
265 template <>
266 struct TypeDescr<SignedIntegerRangeSet> : public SignedIntegerRangeSetTypeDescr
267 {
268 };
269
270 template <>
271 struct TypeDescr<ConstSignedIntegerRangeSet> : public SignedIntegerRangeSetTypeDescr
272 {
273 };
274
275 } /* namespace internal */
276 } /* namespace bt2 */
277
278 #endif /* BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_SET_HPP */
This page took 0.036431 seconds and 4 git commands to generate.