Commit | Line | Data |
---|---|---|
a1e31878 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_FIELD_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_FIELD_HPP | |
9 | ||
10 | #include <type_traits> | |
11 | #include <cstdint> | |
12 | #include <babeltrace2/babeltrace.h> | |
13 | ||
14 | #include "common/assert.h" | |
15 | #include "internal/borrowed-obj.hpp" | |
4927bae7 | 16 | #include "internal/utils.hpp" |
a1e31878 PP |
17 | #include "cpp-common/optional.hpp" |
18 | #include "cpp-common/string_view.hpp" | |
19 | #include "field-class.hpp" | |
20 | ||
21 | namespace bt2 { | |
22 | ||
23 | template <typename LibObjT> | |
24 | class CommonBoolField; | |
25 | ||
26 | template <typename LibObjT> | |
27 | class CommonBitArrayField; | |
28 | ||
29 | template <typename LibObjT> | |
30 | class CommonUnsignedIntegerField; | |
31 | ||
32 | template <typename LibObjT> | |
33 | class CommonSignedIntegerField; | |
34 | ||
35 | template <typename LibObjT> | |
36 | class CommonUnsignedEnumerationField; | |
37 | ||
38 | template <typename LibObjT> | |
39 | class CommonSignedEnumerationField; | |
40 | ||
41 | template <typename LibObjT> | |
42 | class CommonSinglePrecisionRealField; | |
43 | ||
44 | template <typename LibObjT> | |
45 | class CommonDoublePrecisionRealField; | |
46 | ||
47 | template <typename LibObjT> | |
48 | class CommonStringField; | |
49 | ||
50 | template <typename LibObjT> | |
51 | class CommonStructureField; | |
52 | ||
53 | template <typename LibObjT> | |
54 | class CommonArrayField; | |
55 | ||
56 | template <typename LibObjT> | |
57 | class CommonDynamicArrayField; | |
58 | ||
59 | template <typename LibObjT> | |
60 | class CommonOptionField; | |
61 | ||
62 | template <typename LibObjT> | |
63 | class CommonVariantField; | |
64 | ||
65 | namespace internal { | |
66 | ||
67 | template <typename LibObjT> | |
68 | struct CommonFieldSpec; | |
69 | ||
b5f55e9f | 70 | /* Functions specific to mutable fields */ |
a1e31878 PP |
71 | template <> |
72 | struct CommonFieldSpec<bt_field> final | |
73 | { | |
74 | static bt_field_class *cls(bt_field * const libObjPtr) noexcept | |
75 | { | |
76 | return bt_field_borrow_class(libObjPtr); | |
77 | } | |
78 | }; | |
79 | ||
b5f55e9f | 80 | /* Functions specific to constant fields */ |
a1e31878 PP |
81 | template <> |
82 | struct CommonFieldSpec<const bt_field> final | |
83 | { | |
84 | static const bt_field_class *cls(const bt_field * const libObjPtr) noexcept | |
85 | { | |
86 | return bt_field_borrow_class_const(libObjPtr); | |
87 | } | |
88 | }; | |
89 | ||
b5f55e9f | 90 | } /* namespace internal */ |
a1e31878 PP |
91 | |
92 | template <typename LibObjT> | |
93 | class CommonField : public internal::BorrowedObj<LibObjT> | |
94 | { | |
95 | private: | |
96 | using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj; | |
97 | ||
98 | protected: | |
99 | using typename internal::BorrowedObj<LibObjT>::_LibObjPtr; | |
100 | using _ThisCommonField = CommonField<LibObjT>; | |
101 | ||
102 | public: | |
103 | using Class = | |
104 | typename std::conditional<std::is_const<LibObjT>::value, ConstFieldClass, FieldClass>::type; | |
105 | ||
106 | explicit CommonField(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr} | |
107 | { | |
108 | } | |
109 | ||
110 | template <typename OtherLibObjT> | |
111 | CommonField(const CommonField<OtherLibObjT>& val) noexcept : _ThisBorrowedObj {val} | |
112 | { | |
113 | } | |
114 | ||
115 | template <typename OtherLibObjT> | |
116 | _ThisCommonField& operator=(const CommonField<OtherLibObjT>& val) noexcept | |
117 | { | |
118 | _ThisBorrowedObj::operator=(val); | |
119 | return *this; | |
120 | } | |
121 | ||
122 | FieldClassType classType() const noexcept | |
123 | { | |
341a67c4 | 124 | return static_cast<FieldClassType>(bt_field_get_class_type(this->libObjPtr())); |
a1e31878 PP |
125 | } |
126 | ||
127 | ConstFieldClass cls() const noexcept | |
128 | { | |
341a67c4 | 129 | return ConstFieldClass {internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())}; |
a1e31878 PP |
130 | } |
131 | ||
132 | Class cls() noexcept | |
133 | { | |
341a67c4 | 134 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
135 | } |
136 | ||
137 | bool isBool() const noexcept | |
138 | { | |
139 | return this->cls().isBool(); | |
140 | } | |
141 | ||
142 | bool isBitArray() const noexcept | |
143 | { | |
144 | return this->cls().isBitArray(); | |
145 | } | |
146 | ||
147 | bool isUnsignedInteger() const noexcept | |
148 | { | |
149 | return this->cls().isUnsignedInteger(); | |
150 | } | |
151 | ||
152 | bool isSignedInteger() const noexcept | |
153 | { | |
154 | return this->cls().isSignedInteger(); | |
155 | } | |
156 | ||
157 | bool isUnsignedEnumeration() const noexcept | |
158 | { | |
159 | return this->cls().isUnsignedEnumeration(); | |
160 | } | |
161 | ||
162 | bool isSignedEnumeration() const noexcept | |
163 | { | |
164 | return this->cls().isSignedEnumeration(); | |
165 | } | |
166 | ||
167 | bool isSinglePrecisionReal() const noexcept | |
168 | { | |
169 | return this->cls().isSinglePrecisionReal(); | |
170 | } | |
171 | ||
172 | bool isDoublePrecisionReal() const noexcept | |
173 | { | |
174 | return this->cls().isDoublePrecisionReal(); | |
175 | } | |
176 | ||
177 | bool isString() const noexcept | |
178 | { | |
179 | return this->cls().isString(); | |
180 | } | |
181 | ||
182 | bool isStructure() const noexcept | |
183 | { | |
184 | return this->cls().isStructure(); | |
185 | } | |
186 | ||
187 | bool isArray() const noexcept | |
188 | { | |
189 | return this->cls().isArray(); | |
190 | } | |
191 | ||
192 | bool isDynamicArray() const noexcept | |
193 | { | |
194 | return this->cls().isDynamicArray(); | |
195 | } | |
196 | ||
197 | bool isOption() const noexcept | |
198 | { | |
199 | return this->cls().isOption(); | |
200 | } | |
201 | ||
202 | bool isVariant() const noexcept | |
203 | { | |
204 | return this->cls().isVariant(); | |
205 | } | |
206 | ||
45e0ded5 PP |
207 | template <typename FieldT> |
208 | FieldT as() const noexcept | |
209 | { | |
210 | return FieldT {this->libObjPtr()}; | |
211 | } | |
212 | ||
a1e31878 PP |
213 | CommonBoolField<LibObjT> asBool() const noexcept; |
214 | CommonBitArrayField<LibObjT> asBitArray() const noexcept; | |
215 | CommonUnsignedIntegerField<LibObjT> asUnsignedInteger() const noexcept; | |
216 | CommonSignedIntegerField<LibObjT> asSignedInteger() const noexcept; | |
217 | CommonUnsignedEnumerationField<LibObjT> asUnsignedEnumeration() const noexcept; | |
218 | CommonSignedEnumerationField<LibObjT> asSignedEnumeration() const noexcept; | |
219 | CommonSinglePrecisionRealField<LibObjT> asSinglePrecisionReal() const noexcept; | |
220 | CommonDoublePrecisionRealField<LibObjT> asDoublePrecisionReal() const noexcept; | |
221 | CommonStringField<LibObjT> asString() const noexcept; | |
222 | CommonStructureField<LibObjT> asStructure() const noexcept; | |
223 | CommonArrayField<LibObjT> asArray() const noexcept; | |
224 | CommonDynamicArrayField<LibObjT> asDynamicArray() const noexcept; | |
225 | CommonOptionField<LibObjT> asOption() const noexcept; | |
226 | CommonVariantField<LibObjT> asVariant() const noexcept; | |
227 | }; | |
228 | ||
229 | using Field = CommonField<bt_field>; | |
230 | using ConstField = CommonField<const bt_field>; | |
231 | ||
4927bae7 PP |
232 | namespace internal { |
233 | ||
234 | struct FieldTypeDescr | |
235 | { | |
236 | using Const = ConstField; | |
237 | using NonConst = Field; | |
238 | }; | |
239 | ||
240 | template <> | |
241 | struct TypeDescr<Field> : public FieldTypeDescr | |
242 | { | |
243 | }; | |
244 | ||
245 | template <> | |
246 | struct TypeDescr<ConstField> : public FieldTypeDescr | |
247 | { | |
248 | }; | |
249 | ||
250 | } /* namespace internal */ | |
251 | ||
a1e31878 PP |
252 | template <typename LibObjT> |
253 | class CommonBoolField final : public CommonField<LibObjT> | |
254 | { | |
255 | private: | |
256 | using typename CommonField<LibObjT>::_LibObjPtr; | |
257 | using typename CommonField<LibObjT>::_ThisCommonField; | |
258 | ||
259 | public: | |
260 | using Value = bool; | |
261 | ||
262 | explicit CommonBoolField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} | |
263 | { | |
264 | BT_ASSERT_DBG(this->isBool()); | |
265 | } | |
266 | ||
267 | template <typename OtherLibObjT> | |
268 | CommonBoolField(const CommonBoolField<OtherLibObjT>& val) noexcept : _ThisCommonField {val} | |
269 | { | |
270 | } | |
271 | ||
272 | template <typename OtherLibObjT> | |
273 | CommonBoolField<LibObjT>& operator=(const CommonBoolField<OtherLibObjT>& val) noexcept | |
274 | { | |
275 | _ThisCommonField::operator=(val); | |
276 | return *this; | |
277 | } | |
278 | ||
279 | CommonBoolField<LibObjT>& operator=(const Value val) noexcept | |
280 | { | |
281 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
282 | ||
341a67c4 | 283 | bt_field_bool_set_value(this->libObjPtr(), static_cast<bt_bool>(val)); |
a1e31878 PP |
284 | return *this; |
285 | } | |
286 | ||
287 | Value value() const noexcept | |
288 | { | |
341a67c4 | 289 | return static_cast<Value>(bt_field_bool_get_value(this->libObjPtr())); |
a1e31878 PP |
290 | } |
291 | ||
292 | operator Value() const noexcept | |
293 | { | |
294 | return this->value(); | |
295 | } | |
296 | }; | |
297 | ||
298 | using BoolField = CommonBoolField<bt_field>; | |
299 | using ConstBoolField = CommonBoolField<const bt_field>; | |
300 | ||
4927bae7 PP |
301 | namespace internal { |
302 | ||
303 | struct BoolFieldTypeDescr | |
304 | { | |
305 | using Const = ConstBoolField; | |
306 | using NonConst = BoolField; | |
307 | }; | |
308 | ||
309 | template <> | |
310 | struct TypeDescr<BoolField> : public BoolFieldTypeDescr | |
311 | { | |
312 | }; | |
313 | ||
314 | template <> | |
315 | struct TypeDescr<ConstBoolField> : public BoolFieldTypeDescr | |
316 | { | |
317 | }; | |
318 | ||
319 | } /* namespace internal */ | |
320 | ||
a1e31878 PP |
321 | template <typename LibObjT> |
322 | class CommonBitArrayField final : public CommonField<LibObjT> | |
323 | { | |
324 | private: | |
325 | using typename CommonField<LibObjT>::_LibObjPtr; | |
326 | using typename CommonField<LibObjT>::_ThisCommonField; | |
327 | ||
328 | public: | |
329 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstBitArrayFieldClass, | |
330 | BitArrayFieldClass>::type; | |
331 | ||
332 | explicit CommonBitArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} | |
333 | { | |
334 | BT_ASSERT_DBG(this->isBitArray()); | |
335 | } | |
336 | ||
337 | template <typename OtherLibObjT> | |
338 | CommonBitArrayField(const CommonBitArrayField<OtherLibObjT>& val) noexcept : | |
339 | _ThisCommonField {val} | |
340 | { | |
341 | } | |
342 | ||
343 | template <typename OtherLibObjT> | |
344 | CommonBitArrayField<LibObjT>& operator=(const CommonBitArrayField<OtherLibObjT>& val) noexcept | |
345 | { | |
346 | _ThisCommonField::operator=(val); | |
347 | return *this; | |
348 | } | |
349 | ||
350 | ConstBitArrayFieldClass cls() const noexcept | |
351 | { | |
352 | return ConstBitArrayFieldClass { | |
341a67c4 | 353 | internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())}; |
a1e31878 PP |
354 | } |
355 | ||
356 | Class cls() noexcept | |
357 | { | |
341a67c4 | 358 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
359 | } |
360 | ||
361 | CommonBitArrayField<LibObjT>& operator=(const std::uint64_t bits) noexcept | |
362 | { | |
363 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
364 | ||
341a67c4 | 365 | bt_field_bit_array_set_value_as_integer(this->libObjPtr(), bits); |
a1e31878 PP |
366 | return *this; |
367 | } | |
368 | ||
369 | std::uint64_t valueAsInteger() const noexcept | |
370 | { | |
341a67c4 | 371 | return bt_field_bit_array_get_value_as_integer(this->libObjPtr()); |
a1e31878 PP |
372 | } |
373 | ||
374 | bool bitValue(const std::uint64_t index) const noexcept | |
375 | { | |
376 | BT_ASSERT_DBG(index < this->cls().length()); | |
377 | return static_cast<bool>(this->valueAsInteger() & (1ULL << index)); | |
378 | } | |
379 | }; | |
380 | ||
381 | using BitArrayField = CommonBitArrayField<bt_field>; | |
382 | using ConstBitArrayField = CommonBitArrayField<const bt_field>; | |
383 | ||
4927bae7 PP |
384 | namespace internal { |
385 | ||
386 | struct BitArrayFieldTypeDescr | |
387 | { | |
388 | using Const = ConstBitArrayField; | |
389 | using NonConst = BitArrayField; | |
390 | }; | |
391 | ||
392 | template <> | |
393 | struct TypeDescr<BitArrayField> : public BitArrayFieldTypeDescr | |
394 | { | |
395 | }; | |
396 | ||
397 | template <> | |
398 | struct TypeDescr<ConstBitArrayField> : public BitArrayFieldTypeDescr | |
399 | { | |
400 | }; | |
401 | ||
402 | } /* namespace internal */ | |
403 | ||
a1e31878 PP |
404 | template <typename LibObjT> |
405 | class CommonUnsignedIntegerField final : public CommonField<LibObjT> | |
406 | { | |
407 | private: | |
408 | using typename CommonField<LibObjT>::_ThisCommonField; | |
409 | ||
410 | protected: | |
411 | using typename CommonField<LibObjT>::_LibObjPtr; | |
412 | using _ThisCommonUnsignedIntegerField = CommonUnsignedIntegerField<LibObjT>; | |
413 | ||
414 | public: | |
415 | using Value = std::uint64_t; | |
416 | ||
417 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass, | |
418 | IntegerFieldClass>::type; | |
419 | ||
420 | explicit CommonUnsignedIntegerField(const _LibObjPtr libObjPtr) noexcept : | |
421 | _ThisCommonField {libObjPtr} | |
422 | { | |
423 | BT_ASSERT_DBG(this->isUnsignedInteger()); | |
424 | } | |
425 | ||
426 | template <typename OtherLibObjT> | |
427 | CommonUnsignedIntegerField(const CommonUnsignedIntegerField<OtherLibObjT>& val) noexcept : | |
428 | _ThisCommonField {val} | |
429 | { | |
430 | } | |
431 | ||
432 | template <typename OtherLibObjT> | |
433 | _ThisCommonUnsignedIntegerField& | |
434 | operator=(const CommonUnsignedIntegerField<OtherLibObjT>& val) noexcept | |
435 | { | |
436 | _ThisCommonField::operator=(val); | |
437 | return *this; | |
438 | } | |
439 | ||
440 | ConstIntegerFieldClass cls() const noexcept | |
441 | { | |
442 | return ConstIntegerFieldClass { | |
341a67c4 | 443 | internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())}; |
a1e31878 PP |
444 | } |
445 | ||
446 | Class cls() noexcept | |
447 | { | |
341a67c4 | 448 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
449 | } |
450 | ||
451 | CommonUnsignedIntegerField<LibObjT>& operator=(const Value val) noexcept | |
452 | { | |
453 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
454 | ||
341a67c4 | 455 | bt_field_integer_unsigned_set_value(this->libObjPtr(), val); |
a1e31878 PP |
456 | return *this; |
457 | } | |
458 | ||
459 | Value value() const noexcept | |
460 | { | |
341a67c4 | 461 | return bt_field_integer_unsigned_get_value(this->libObjPtr()); |
a1e31878 PP |
462 | } |
463 | ||
464 | operator Value() const noexcept | |
465 | { | |
466 | return this->value(); | |
467 | } | |
468 | }; | |
469 | ||
470 | using UnsignedIntegerField = CommonUnsignedIntegerField<bt_field>; | |
471 | using ConstUnsignedIntegerField = CommonUnsignedIntegerField<const bt_field>; | |
472 | ||
4927bae7 PP |
473 | namespace internal { |
474 | ||
475 | struct UnsignedIntegerFieldTypeDescr | |
476 | { | |
477 | using Const = ConstUnsignedIntegerField; | |
478 | using NonConst = UnsignedIntegerField; | |
479 | }; | |
480 | ||
481 | template <> | |
482 | struct TypeDescr<UnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr | |
483 | { | |
484 | }; | |
485 | ||
486 | template <> | |
487 | struct TypeDescr<ConstUnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr | |
488 | { | |
489 | }; | |
490 | ||
491 | } /* namespace internal */ | |
492 | ||
a1e31878 PP |
493 | template <typename LibObjT> |
494 | class CommonSignedIntegerField final : public CommonField<LibObjT> | |
495 | { | |
496 | private: | |
497 | using typename CommonField<LibObjT>::_ThisCommonField; | |
498 | ||
499 | protected: | |
500 | using typename CommonField<LibObjT>::_LibObjPtr; | |
501 | using _ThisCommonSignedIntegerField = CommonSignedIntegerField<LibObjT>; | |
502 | ||
503 | public: | |
504 | using Value = std::uint64_t; | |
505 | ||
506 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass, | |
507 | IntegerFieldClass>::type; | |
508 | ||
509 | explicit CommonSignedIntegerField(const _LibObjPtr libObjPtr) noexcept : | |
510 | _ThisCommonField {libObjPtr} | |
511 | { | |
512 | BT_ASSERT_DBG(this->isSignedInteger()); | |
513 | } | |
514 | ||
515 | template <typename OtherLibObjT> | |
516 | CommonSignedIntegerField(const CommonSignedIntegerField<OtherLibObjT>& val) noexcept : | |
517 | _ThisCommonField {val} | |
518 | { | |
519 | } | |
520 | ||
521 | template <typename OtherLibObjT> | |
522 | _ThisCommonSignedIntegerField& | |
523 | operator=(const CommonSignedIntegerField<OtherLibObjT>& val) noexcept | |
524 | { | |
525 | _ThisCommonField::operator=(val); | |
526 | return *this; | |
527 | } | |
528 | ||
529 | ConstIntegerFieldClass cls() const noexcept | |
530 | { | |
531 | return ConstIntegerFieldClass { | |
341a67c4 | 532 | internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())}; |
a1e31878 PP |
533 | } |
534 | ||
535 | Class cls() noexcept | |
536 | { | |
341a67c4 | 537 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
538 | } |
539 | ||
540 | CommonSignedIntegerField<LibObjT>& operator=(const Value val) noexcept | |
541 | { | |
542 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
543 | ||
341a67c4 | 544 | bt_field_integer_signed_set_value(this->libObjPtr(), val); |
a1e31878 PP |
545 | return *this; |
546 | } | |
547 | ||
548 | Value value() const noexcept | |
549 | { | |
341a67c4 | 550 | return bt_field_integer_signed_get_value(this->libObjPtr()); |
a1e31878 PP |
551 | } |
552 | ||
553 | operator Value() const noexcept | |
554 | { | |
555 | return this->value(); | |
556 | } | |
557 | }; | |
558 | ||
559 | using SignedIntegerField = CommonSignedIntegerField<bt_field>; | |
560 | using ConstSignedIntegerField = CommonSignedIntegerField<const bt_field>; | |
561 | ||
4927bae7 PP |
562 | namespace internal { |
563 | ||
564 | struct SignedIntegerFieldTypeDescr | |
565 | { | |
566 | using Const = ConstSignedIntegerField; | |
567 | using NonConst = SignedIntegerField; | |
568 | }; | |
569 | ||
570 | template <> | |
571 | struct TypeDescr<SignedIntegerField> : public SignedIntegerFieldTypeDescr | |
572 | { | |
573 | }; | |
574 | ||
575 | template <> | |
576 | struct TypeDescr<ConstSignedIntegerField> : public SignedIntegerFieldTypeDescr | |
577 | { | |
578 | }; | |
579 | ||
580 | } /* namespace internal */ | |
581 | ||
a1e31878 PP |
582 | class EnumerationFieldClassMappingLabels |
583 | { | |
584 | public: | |
585 | explicit EnumerationFieldClassMappingLabels( | |
586 | const bt_field_class_enumeration_mapping_label_array labels, const std::uint64_t size) : | |
587 | _mLabels {labels}, | |
588 | _mSize {size} | |
589 | { | |
590 | } | |
591 | ||
592 | EnumerationFieldClassMappingLabels(const EnumerationFieldClassMappingLabels&) noexcept = | |
593 | default; | |
594 | ||
595 | EnumerationFieldClassMappingLabels& | |
596 | operator=(const EnumerationFieldClassMappingLabels&) noexcept = default; | |
597 | ||
598 | std::uint64_t size() const noexcept | |
599 | { | |
600 | return _mSize; | |
601 | } | |
602 | ||
603 | bpstd::string_view operator[](const std::uint64_t index) const noexcept | |
604 | { | |
605 | return _mLabels[index]; | |
606 | } | |
607 | ||
608 | private: | |
609 | bt_field_class_enumeration_mapping_label_array _mLabels; | |
610 | std::uint64_t _mSize; | |
611 | }; | |
612 | ||
613 | template <typename LibObjT> | |
614 | class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField<LibObjT> | |
615 | { | |
616 | private: | |
617 | using typename CommonUnsignedIntegerField<LibObjT>::_ThisCommonUnsignedIntegerField; | |
618 | using typename CommonField<LibObjT>::_LibObjPtr; | |
619 | ||
620 | public: | |
621 | using Class = | |
622 | typename std::conditional<std::is_const<LibObjT>::value, ConstUnsignedEnumerationFieldClass, | |
623 | UnsignedEnumerationFieldClass>::type; | |
624 | ||
625 | explicit CommonUnsignedEnumerationField(const _LibObjPtr libObjPtr) noexcept : | |
626 | _ThisCommonUnsignedIntegerField {libObjPtr} | |
627 | { | |
628 | BT_ASSERT_DBG(this->isUnsignedEnumeration()); | |
629 | } | |
630 | ||
631 | template <typename OtherLibObjT> | |
632 | CommonUnsignedEnumerationField(const CommonUnsignedEnumerationField<OtherLibObjT>& val) noexcept | |
633 | : | |
634 | _ThisCommonUnsignedIntegerField {val} | |
635 | { | |
636 | } | |
637 | ||
638 | template <typename OtherLibObjT> | |
639 | CommonUnsignedEnumerationField<LibObjT>& | |
640 | operator=(const CommonUnsignedEnumerationField<OtherLibObjT>& val) noexcept | |
641 | { | |
642 | _ThisCommonUnsignedIntegerField::operator=(val); | |
643 | return *this; | |
644 | } | |
645 | ||
646 | ConstUnsignedEnumerationFieldClass cls() const noexcept | |
647 | { | |
648 | return ConstUnsignedEnumerationFieldClass { | |
341a67c4 | 649 | internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())}; |
a1e31878 PP |
650 | } |
651 | ||
652 | Class cls() noexcept | |
653 | { | |
341a67c4 | 654 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
655 | } |
656 | ||
657 | EnumerationFieldClassMappingLabels labels() const | |
658 | { | |
659 | bt_field_class_enumeration_mapping_label_array labelArray; | |
660 | std::uint64_t count; | |
341a67c4 | 661 | const auto status = bt_field_enumeration_unsigned_get_mapping_labels(this->libObjPtr(), |
a1e31878 PP |
662 | &labelArray, &count); |
663 | ||
664 | if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) { | |
39278ebc | 665 | throw MemoryError {}; |
a1e31878 PP |
666 | } |
667 | ||
668 | return EnumerationFieldClassMappingLabels {labelArray, count}; | |
669 | } | |
670 | }; | |
671 | ||
672 | using UnsignedEnumerationField = CommonUnsignedEnumerationField<bt_field>; | |
673 | using ConstUnsignedEnumerationField = CommonUnsignedEnumerationField<const bt_field>; | |
674 | ||
4927bae7 PP |
675 | namespace internal { |
676 | ||
677 | struct UnsignedEnumerationFieldTypeDescr | |
678 | { | |
679 | using Const = ConstUnsignedEnumerationField; | |
680 | using NonConst = UnsignedEnumerationField; | |
681 | }; | |
682 | ||
683 | template <> | |
684 | struct TypeDescr<UnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr | |
685 | { | |
686 | }; | |
687 | ||
688 | template <> | |
689 | struct TypeDescr<ConstUnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr | |
690 | { | |
691 | }; | |
692 | ||
693 | } /* namespace internal */ | |
694 | ||
a1e31878 PP |
695 | template <typename LibObjT> |
696 | class CommonSignedEnumerationField final : public CommonSignedIntegerField<LibObjT> | |
697 | { | |
698 | private: | |
699 | using typename CommonSignedIntegerField<LibObjT>::_ThisCommonSignedIntegerField; | |
700 | using typename CommonField<LibObjT>::_LibObjPtr; | |
701 | ||
702 | public: | |
703 | using Class = | |
704 | typename std::conditional<std::is_const<LibObjT>::value, ConstSignedEnumerationFieldClass, | |
705 | SignedEnumerationFieldClass>::type; | |
706 | ||
707 | explicit CommonSignedEnumerationField(const _LibObjPtr libObjPtr) noexcept : | |
708 | _ThisCommonSignedIntegerField {libObjPtr} | |
709 | { | |
710 | BT_ASSERT_DBG(this->isSignedEnumeration()); | |
711 | } | |
712 | ||
713 | template <typename OtherLibObjT> | |
714 | CommonSignedEnumerationField(const CommonSignedEnumerationField<OtherLibObjT>& val) noexcept : | |
715 | _ThisCommonSignedIntegerField {val} | |
716 | { | |
717 | } | |
718 | ||
719 | template <typename OtherLibObjT> | |
720 | CommonSignedEnumerationField<LibObjT>& | |
721 | operator=(const CommonSignedEnumerationField<OtherLibObjT>& val) noexcept | |
722 | { | |
723 | _ThisCommonSignedIntegerField::operator=(val); | |
724 | return *this; | |
725 | } | |
726 | ||
727 | ConstSignedEnumerationFieldClass cls() const noexcept | |
728 | { | |
729 | return ConstSignedEnumerationFieldClass { | |
341a67c4 | 730 | internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())}; |
a1e31878 PP |
731 | } |
732 | ||
733 | Class cls() noexcept | |
734 | { | |
341a67c4 | 735 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
736 | } |
737 | ||
738 | EnumerationFieldClassMappingLabels labels() const | |
739 | { | |
740 | bt_field_class_enumeration_mapping_label_array labelArray; | |
741 | std::uint64_t count; | |
742 | const auto status = | |
341a67c4 | 743 | bt_field_enumeration_signed_get_mapping_labels(this->libObjPtr(), &labelArray, &count); |
a1e31878 PP |
744 | |
745 | if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) { | |
39278ebc | 746 | throw MemoryError {}; |
a1e31878 PP |
747 | } |
748 | ||
749 | return EnumerationFieldClassMappingLabels {labelArray, count}; | |
750 | } | |
751 | }; | |
752 | ||
753 | using SignedEnumerationField = CommonSignedEnumerationField<bt_field>; | |
754 | using ConstSignedEnumerationField = CommonSignedEnumerationField<const bt_field>; | |
755 | ||
4927bae7 PP |
756 | namespace internal { |
757 | ||
758 | struct SignedEnumerationFieldTypeDescr | |
759 | { | |
760 | using Const = ConstSignedEnumerationField; | |
761 | using NonConst = SignedEnumerationField; | |
762 | }; | |
763 | ||
764 | template <> | |
765 | struct TypeDescr<SignedEnumerationField> : public SignedEnumerationFieldTypeDescr | |
766 | { | |
767 | }; | |
768 | ||
769 | template <> | |
770 | struct TypeDescr<ConstSignedEnumerationField> : public SignedEnumerationFieldTypeDescr | |
771 | { | |
772 | }; | |
773 | ||
774 | } /* namespace internal */ | |
775 | ||
a1e31878 PP |
776 | template <typename LibObjT> |
777 | class CommonSinglePrecisionRealField final : public CommonField<LibObjT> | |
778 | { | |
779 | private: | |
780 | using typename CommonField<LibObjT>::_LibObjPtr; | |
781 | using typename CommonField<LibObjT>::_ThisCommonField; | |
782 | ||
783 | public: | |
784 | using Value = float; | |
785 | ||
786 | explicit CommonSinglePrecisionRealField(const _LibObjPtr libObjPtr) noexcept : | |
787 | _ThisCommonField {libObjPtr} | |
788 | { | |
789 | BT_ASSERT_DBG(this->isSinglePrecisionReal()); | |
790 | } | |
791 | ||
792 | template <typename OtherLibObjT> | |
793 | CommonSinglePrecisionRealField(const CommonSinglePrecisionRealField<OtherLibObjT>& val) noexcept | |
794 | : | |
795 | _ThisCommonField {val} | |
796 | { | |
797 | } | |
798 | ||
799 | template <typename OtherLibObjT> | |
800 | CommonSinglePrecisionRealField<LibObjT>& | |
801 | operator=(const CommonSinglePrecisionRealField<OtherLibObjT>& val) noexcept | |
802 | { | |
803 | _ThisCommonField::operator=(val); | |
804 | return *this; | |
805 | } | |
806 | ||
807 | CommonSinglePrecisionRealField<LibObjT>& operator=(const Value val) noexcept | |
808 | { | |
809 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
810 | ||
341a67c4 | 811 | bt_field_real_single_precision_set_value(this->libObjPtr(), val); |
a1e31878 PP |
812 | return *this; |
813 | } | |
814 | ||
815 | Value value() const noexcept | |
816 | { | |
341a67c4 | 817 | return bt_field_real_single_precision_get_value(this->libObjPtr()); |
a1e31878 PP |
818 | } |
819 | ||
820 | operator Value() const noexcept | |
821 | { | |
822 | return this->value(); | |
823 | } | |
824 | }; | |
825 | ||
826 | using SinglePrecisionRealField = CommonSinglePrecisionRealField<bt_field>; | |
827 | using ConstSinglePrecisionRealField = CommonSinglePrecisionRealField<const bt_field>; | |
828 | ||
4927bae7 PP |
829 | namespace internal { |
830 | ||
831 | struct SinglePrecisionRealFieldTypeDescr | |
832 | { | |
833 | using Const = ConstSinglePrecisionRealField; | |
834 | using NonConst = SinglePrecisionRealField; | |
835 | }; | |
836 | ||
837 | template <> | |
838 | struct TypeDescr<SinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr | |
839 | { | |
840 | }; | |
841 | ||
842 | template <> | |
843 | struct TypeDescr<ConstSinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr | |
844 | { | |
845 | }; | |
846 | ||
847 | } /* namespace internal */ | |
848 | ||
a1e31878 PP |
849 | template <typename LibObjT> |
850 | class CommonDoublePrecisionRealField final : public CommonField<LibObjT> | |
851 | { | |
852 | private: | |
853 | using typename CommonField<LibObjT>::_LibObjPtr; | |
854 | using typename CommonField<LibObjT>::_ThisCommonField; | |
855 | ||
856 | public: | |
857 | using Value = double; | |
858 | ||
859 | explicit CommonDoublePrecisionRealField(const _LibObjPtr libObjPtr) noexcept : | |
860 | _ThisCommonField {libObjPtr} | |
861 | { | |
862 | BT_ASSERT_DBG(this->isDoublePrecisionReal()); | |
863 | } | |
864 | ||
865 | template <typename OtherLibObjT> | |
866 | CommonDoublePrecisionRealField(const CommonDoublePrecisionRealField<OtherLibObjT>& val) noexcept | |
867 | : | |
868 | _ThisCommonField {val} | |
869 | { | |
870 | } | |
871 | ||
872 | template <typename OtherLibObjT> | |
873 | CommonDoublePrecisionRealField<LibObjT>& | |
874 | operator=(const CommonDoublePrecisionRealField<OtherLibObjT>& val) noexcept | |
875 | { | |
876 | _ThisCommonField::operator=(val); | |
877 | return *this; | |
878 | } | |
879 | ||
880 | CommonDoublePrecisionRealField<LibObjT>& operator=(const Value val) noexcept | |
881 | { | |
882 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
883 | ||
341a67c4 | 884 | bt_field_real_single_precision_set_value(this->libObjPtr(), val); |
a1e31878 PP |
885 | return *this; |
886 | } | |
887 | ||
888 | Value value() const noexcept | |
889 | { | |
341a67c4 | 890 | return bt_field_real_single_precision_get_value(this->libObjPtr()); |
a1e31878 PP |
891 | } |
892 | ||
893 | operator Value() const noexcept | |
894 | { | |
895 | return this->value(); | |
896 | } | |
897 | }; | |
898 | ||
899 | using DoublePrecisionRealField = CommonDoublePrecisionRealField<bt_field>; | |
900 | using ConstDoublePrecisionRealField = CommonDoublePrecisionRealField<const bt_field>; | |
901 | ||
4927bae7 PP |
902 | namespace internal { |
903 | ||
904 | struct DoublePrecisionRealFieldTypeDescr | |
905 | { | |
906 | using Const = ConstDoublePrecisionRealField; | |
907 | using NonConst = DoublePrecisionRealField; | |
908 | }; | |
909 | ||
910 | template <> | |
911 | struct TypeDescr<DoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr | |
912 | { | |
913 | }; | |
914 | ||
915 | template <> | |
916 | struct TypeDescr<ConstDoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr | |
917 | { | |
918 | }; | |
919 | ||
920 | } /* namespace internal */ | |
921 | ||
a1e31878 PP |
922 | template <typename LibObjT> |
923 | class CommonStringField final : public CommonField<LibObjT> | |
924 | { | |
925 | private: | |
926 | using typename CommonField<LibObjT>::_LibObjPtr; | |
927 | using typename CommonField<LibObjT>::_ThisCommonField; | |
928 | ||
929 | public: | |
930 | explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} | |
931 | { | |
932 | BT_ASSERT_DBG(this->isString()); | |
933 | } | |
934 | ||
935 | template <typename OtherLibObjT> | |
936 | CommonStringField(const CommonStringField<OtherLibObjT>& val) noexcept : _ThisCommonField {val} | |
937 | { | |
938 | } | |
939 | ||
940 | template <typename OtherLibObjT> | |
941 | CommonStringField<LibObjT>& operator=(const CommonStringField<OtherLibObjT>& val) noexcept | |
942 | { | |
943 | _ThisCommonField::operator=(val); | |
944 | return *this; | |
945 | } | |
946 | ||
947 | CommonStringField<LibObjT>& operator=(const char * const val) noexcept | |
948 | { | |
949 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
950 | ||
341a67c4 | 951 | const auto status = bt_field_string_set_value(this->libObjPtr(), val); |
a1e31878 PP |
952 | |
953 | if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) { | |
39278ebc | 954 | throw MemoryError {}; |
a1e31878 PP |
955 | } |
956 | ||
957 | return *this; | |
958 | } | |
959 | ||
960 | CommonStringField<LibObjT>& operator=(const std::string& val) noexcept | |
961 | { | |
962 | return *this = val.data(); | |
963 | } | |
964 | ||
965 | void clear() noexcept | |
966 | { | |
967 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
968 | ||
341a67c4 | 969 | bt_field_string_clear(this->libObjPtr()); |
a1e31878 PP |
970 | } |
971 | ||
972 | bpstd::string_view value() const noexcept | |
973 | { | |
341a67c4 | 974 | return bt_field_string_get_value(this->libObjPtr()); |
a1e31878 PP |
975 | } |
976 | }; | |
977 | ||
978 | using StringField = CommonStringField<bt_field>; | |
979 | using ConstStringField = CommonStringField<const bt_field>; | |
980 | ||
981 | namespace internal { | |
982 | ||
4927bae7 PP |
983 | struct StringFieldTypeDescr |
984 | { | |
985 | using Const = ConstStringField; | |
986 | using NonConst = StringField; | |
987 | }; | |
988 | ||
989 | template <> | |
990 | struct TypeDescr<StringField> : public StringFieldTypeDescr | |
991 | { | |
992 | }; | |
993 | ||
994 | template <> | |
995 | struct TypeDescr<ConstStringField> : public StringFieldTypeDescr | |
996 | { | |
997 | }; | |
998 | ||
a1e31878 PP |
999 | template <typename LibObjT> |
1000 | struct CommonStructureFieldSpec; | |
1001 | ||
b5f55e9f | 1002 | /* Functions specific to mutable structure fields */ |
a1e31878 PP |
1003 | template <> |
1004 | struct CommonStructureFieldSpec<bt_field> final | |
1005 | { | |
1006 | static bt_field *memberFieldByIndex(bt_field * const libObjPtr, | |
1007 | const std::uint64_t index) noexcept | |
1008 | { | |
1009 | return bt_field_structure_borrow_member_field_by_index(libObjPtr, index); | |
1010 | } | |
1011 | ||
1012 | static bt_field *memberFieldByName(bt_field * const libObjPtr, const char * const name) noexcept | |
1013 | { | |
1014 | return bt_field_structure_borrow_member_field_by_name(libObjPtr, name); | |
1015 | } | |
1016 | }; | |
1017 | ||
b5f55e9f | 1018 | /* Functions specific to constant structure fields */ |
a1e31878 PP |
1019 | template <> |
1020 | struct CommonStructureFieldSpec<const bt_field> final | |
1021 | { | |
1022 | static const bt_field *memberFieldByIndex(const bt_field * const libObjPtr, | |
1023 | const std::uint64_t index) noexcept | |
1024 | { | |
1025 | return bt_field_structure_borrow_member_field_by_index_const(libObjPtr, index); | |
1026 | } | |
1027 | ||
1028 | static const bt_field *memberFieldByName(const bt_field * const libObjPtr, | |
1029 | const char * const name) noexcept | |
1030 | { | |
1031 | return bt_field_structure_borrow_member_field_by_name_const(libObjPtr, name); | |
1032 | } | |
1033 | }; | |
1034 | ||
b5f55e9f | 1035 | } /* namespace internal */ |
a1e31878 PP |
1036 | |
1037 | template <typename LibObjT> | |
1038 | class CommonStructureField final : public CommonField<LibObjT> | |
1039 | { | |
1040 | private: | |
1041 | using typename CommonField<LibObjT>::_LibObjPtr; | |
1042 | using typename CommonField<LibObjT>::_ThisCommonField; | |
1043 | using _Spec = internal::CommonStructureFieldSpec<LibObjT>; | |
1044 | ||
1045 | public: | |
1046 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstStructureFieldClass, | |
1047 | StructureFieldClass>::type; | |
1048 | ||
1049 | explicit CommonStructureField(const _LibObjPtr libObjPtr) noexcept : | |
1050 | _ThisCommonField {libObjPtr} | |
1051 | { | |
1052 | BT_ASSERT_DBG(this->isStructure()); | |
1053 | } | |
1054 | ||
1055 | template <typename OtherLibObjT> | |
1056 | CommonStructureField(const CommonStructureField<OtherLibObjT>& val) noexcept : | |
1057 | _ThisCommonField {val} | |
1058 | { | |
1059 | } | |
1060 | ||
1061 | template <typename OtherLibObjT> | |
1062 | CommonStructureField<LibObjT>& operator=(const CommonStructureField<OtherLibObjT>& val) noexcept | |
1063 | { | |
1064 | _ThisCommonField::operator=(val); | |
1065 | return *this; | |
1066 | } | |
1067 | ||
1068 | ConstStructureFieldClass cls() const noexcept | |
1069 | { | |
1070 | return ConstStructureFieldClass { | |
341a67c4 | 1071 | internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())}; |
a1e31878 PP |
1072 | } |
1073 | ||
1074 | Class cls() noexcept | |
1075 | { | |
341a67c4 | 1076 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
1077 | } |
1078 | ||
1079 | std::uint64_t size() const noexcept | |
1080 | { | |
1081 | return this->cls().size(); | |
1082 | } | |
1083 | ||
1084 | ConstField operator[](const std::uint64_t index) const noexcept | |
1085 | { | |
1086 | return ConstField {internal::CommonStructureFieldSpec<const bt_field>::memberFieldByIndex( | |
341a67c4 | 1087 | this->libObjPtr(), index)}; |
a1e31878 PP |
1088 | } |
1089 | ||
1090 | CommonField<LibObjT> operator[](const std::uint64_t index) noexcept | |
1091 | { | |
341a67c4 | 1092 | return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)}; |
a1e31878 PP |
1093 | } |
1094 | ||
1095 | nonstd::optional<ConstField> operator[](const char * const name) const noexcept | |
1096 | { | |
1097 | const auto libObjPtr = | |
341a67c4 FD |
1098 | internal::CommonStructureFieldSpec<const bt_field>::memberFieldByName(this->libObjPtr(), |
1099 | name); | |
a1e31878 PP |
1100 | |
1101 | if (libObjPtr) { | |
1102 | return ConstField {libObjPtr}; | |
1103 | } | |
1104 | ||
1105 | return nonstd::nullopt; | |
1106 | } | |
1107 | ||
1108 | nonstd::optional<ConstField> operator[](const std::string& name) const noexcept | |
1109 | { | |
1110 | return (*this)[name.data()]; | |
1111 | } | |
1112 | ||
1113 | nonstd::optional<CommonField<LibObjT>> operator[](const char * const name) noexcept | |
1114 | { | |
341a67c4 | 1115 | const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name); |
a1e31878 PP |
1116 | |
1117 | if (libObjPtr) { | |
1118 | return CommonField<LibObjT> {libObjPtr}; | |
1119 | } | |
1120 | ||
1121 | return nonstd::nullopt; | |
1122 | } | |
1123 | ||
1124 | nonstd::optional<CommonField<LibObjT>> operator[](const std::string& name) noexcept | |
1125 | { | |
1126 | return (*this)[name.data()]; | |
1127 | } | |
1128 | }; | |
1129 | ||
1130 | using StructureField = CommonStructureField<bt_field>; | |
1131 | using ConstStructureField = CommonStructureField<const bt_field>; | |
1132 | ||
1133 | namespace internal { | |
1134 | ||
4927bae7 PP |
1135 | struct StructureFieldTypeDescr |
1136 | { | |
1137 | using Const = ConstStructureField; | |
1138 | using NonConst = StructureField; | |
1139 | }; | |
1140 | ||
1141 | template <> | |
1142 | struct TypeDescr<StructureField> : public StructureFieldTypeDescr | |
1143 | { | |
1144 | }; | |
1145 | ||
1146 | template <> | |
1147 | struct TypeDescr<ConstStructureField> : public StructureFieldTypeDescr | |
1148 | { | |
1149 | }; | |
1150 | ||
a1e31878 PP |
1151 | template <typename LibObjT> |
1152 | struct CommonArrayFieldSpec; | |
1153 | ||
b5f55e9f | 1154 | /* Functions specific to mutable array fields */ |
a1e31878 PP |
1155 | template <> |
1156 | struct CommonArrayFieldSpec<bt_field> final | |
1157 | { | |
1158 | static bt_field *elementFieldByIndex(bt_field * const libObjPtr, | |
1159 | const std::uint64_t index) noexcept | |
1160 | { | |
1161 | return bt_field_array_borrow_element_field_by_index(libObjPtr, index); | |
1162 | } | |
1163 | }; | |
1164 | ||
b5f55e9f | 1165 | /* Functions specific to constant array fields */ |
a1e31878 PP |
1166 | template <> |
1167 | struct CommonArrayFieldSpec<const bt_field> final | |
1168 | { | |
1169 | static const bt_field *elementFieldByIndex(const bt_field * const libObjPtr, | |
1170 | const std::uint64_t index) noexcept | |
1171 | { | |
1172 | return bt_field_array_borrow_element_field_by_index_const(libObjPtr, index); | |
1173 | } | |
1174 | }; | |
1175 | ||
b5f55e9f | 1176 | } /* namespace internal */ |
a1e31878 PP |
1177 | |
1178 | template <typename LibObjT> | |
1179 | class CommonArrayField : public CommonField<LibObjT> | |
1180 | { | |
1181 | private: | |
1182 | using typename CommonField<LibObjT>::_ThisCommonField; | |
1183 | using _Spec = internal::CommonArrayFieldSpec<LibObjT>; | |
1184 | ||
1185 | protected: | |
1186 | using typename CommonField<LibObjT>::_LibObjPtr; | |
1187 | using _ThisCommonArrayField = CommonArrayField<LibObjT>; | |
1188 | ||
1189 | public: | |
1190 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstArrayFieldClass, | |
1191 | ArrayFieldClass>::type; | |
1192 | ||
1193 | explicit CommonArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} | |
1194 | { | |
1195 | BT_ASSERT_DBG(this->isArray()); | |
1196 | } | |
1197 | ||
1198 | template <typename OtherLibObjT> | |
1199 | CommonArrayField(const CommonArrayField<OtherLibObjT>& val) noexcept : _ThisCommonField {val} | |
1200 | { | |
1201 | } | |
1202 | ||
1203 | template <typename OtherLibObjT> | |
1204 | _ThisCommonArrayField& operator=(const CommonArrayField<OtherLibObjT>& val) noexcept | |
1205 | { | |
1206 | _ThisCommonField::operator=(val); | |
1207 | return *this; | |
1208 | } | |
1209 | ||
1210 | ConstArrayFieldClass cls() const noexcept | |
1211 | { | |
1212 | return ConstArrayFieldClass { | |
341a67c4 | 1213 | internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())}; |
a1e31878 PP |
1214 | } |
1215 | ||
1216 | Class cls() noexcept | |
1217 | { | |
341a67c4 | 1218 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
1219 | } |
1220 | ||
1221 | std::uint64_t length() const noexcept | |
1222 | { | |
341a67c4 | 1223 | return bt_field_array_get_length(this->libObjPtr()); |
a1e31878 PP |
1224 | } |
1225 | ||
1226 | ConstField operator[](const std::uint64_t index) const noexcept | |
1227 | { | |
1228 | return ConstField {internal::CommonArrayFieldSpec<const bt_field>::elementFieldByIndex( | |
341a67c4 | 1229 | this->libObjPtr(), index)}; |
a1e31878 PP |
1230 | } |
1231 | ||
1232 | CommonField<LibObjT> operator[](const std::uint64_t index) noexcept | |
1233 | { | |
341a67c4 | 1234 | return CommonField<LibObjT> {_Spec::elementFieldByIndex(this->libObjPtr(), index)}; |
a1e31878 PP |
1235 | } |
1236 | }; | |
1237 | ||
1238 | using ArrayField = CommonArrayField<bt_field>; | |
1239 | using ConstArrayField = CommonArrayField<const bt_field>; | |
1240 | ||
4927bae7 PP |
1241 | namespace internal { |
1242 | ||
1243 | struct ArrayFieldTypeDescr | |
1244 | { | |
1245 | using Const = ConstArrayField; | |
1246 | using NonConst = ArrayField; | |
1247 | }; | |
1248 | ||
1249 | template <> | |
1250 | struct TypeDescr<ArrayField> : public ArrayFieldTypeDescr | |
1251 | { | |
1252 | }; | |
1253 | ||
1254 | template <> | |
1255 | struct TypeDescr<ConstArrayField> : public ArrayFieldTypeDescr | |
1256 | { | |
1257 | }; | |
1258 | ||
1259 | } /* namespace internal */ | |
1260 | ||
a1e31878 PP |
1261 | template <typename LibObjT> |
1262 | class CommonDynamicArrayField : public CommonArrayField<LibObjT> | |
1263 | { | |
1264 | private: | |
1265 | using typename CommonField<LibObjT>::_LibObjPtr; | |
1266 | using typename CommonArrayField<LibObjT>::_ThisCommonArrayField; | |
1267 | ||
1268 | public: | |
1269 | explicit CommonDynamicArrayField(const _LibObjPtr libObjPtr) noexcept : | |
1270 | _ThisCommonArrayField {libObjPtr} | |
1271 | { | |
1272 | BT_ASSERT_DBG(this->isDynamicArray()); | |
1273 | } | |
1274 | ||
1275 | template <typename OtherLibObjT> | |
1276 | CommonDynamicArrayField(const CommonDynamicArrayField<OtherLibObjT>& val) noexcept : | |
1277 | _ThisCommonArrayField {val} | |
1278 | { | |
1279 | } | |
1280 | ||
1281 | template <typename OtherLibObjT> | |
1282 | CommonDynamicArrayField<LibObjT>& | |
1283 | operator=(const CommonDynamicArrayField<OtherLibObjT>& val) noexcept | |
1284 | { | |
1285 | _ThisCommonArrayField::operator=(val); | |
1286 | return *this; | |
1287 | } | |
1288 | ||
1289 | std::uint64_t length() const noexcept | |
1290 | { | |
1291 | return _ThisCommonArrayField::length(); | |
1292 | } | |
1293 | ||
1294 | void length(const std::uint64_t length) | |
1295 | { | |
1296 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1297 | ||
341a67c4 | 1298 | const auto status = bt_field_array_dynamic_set_length(this->libObjPtr(), length); |
a1e31878 PP |
1299 | |
1300 | if (status == BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_MEMORY_ERROR) { | |
39278ebc | 1301 | throw MemoryError {}; |
a1e31878 PP |
1302 | } |
1303 | } | |
1304 | }; | |
1305 | ||
1306 | using DynamicArrayField = CommonDynamicArrayField<bt_field>; | |
1307 | using ConstDynamicArrayField = CommonDynamicArrayField<const bt_field>; | |
1308 | ||
1309 | namespace internal { | |
1310 | ||
4927bae7 PP |
1311 | struct DynamicArrayFieldTypeDescr |
1312 | { | |
1313 | using Const = ConstDynamicArrayField; | |
1314 | using NonConst = DynamicArrayField; | |
1315 | }; | |
1316 | ||
1317 | template <> | |
1318 | struct TypeDescr<DynamicArrayField> : public DynamicArrayFieldTypeDescr | |
1319 | { | |
1320 | }; | |
1321 | ||
1322 | template <> | |
1323 | struct TypeDescr<ConstDynamicArrayField> : public DynamicArrayFieldTypeDescr | |
1324 | { | |
1325 | }; | |
1326 | ||
a1e31878 PP |
1327 | template <typename LibObjT> |
1328 | struct CommonOptionFieldSpec; | |
1329 | ||
b5f55e9f | 1330 | /* Functions specific to mutable option fields */ |
a1e31878 PP |
1331 | template <> |
1332 | struct CommonOptionFieldSpec<bt_field> final | |
1333 | { | |
1334 | static bt_field *field(bt_field * const libObjPtr) noexcept | |
1335 | { | |
1336 | return bt_field_option_borrow_field(libObjPtr); | |
1337 | } | |
1338 | }; | |
1339 | ||
b5f55e9f | 1340 | /* Functions specific to constant option fields */ |
a1e31878 PP |
1341 | template <> |
1342 | struct CommonOptionFieldSpec<const bt_field> final | |
1343 | { | |
1344 | static const bt_field *field(const bt_field * const libObjPtr) noexcept | |
1345 | { | |
1346 | return bt_field_option_borrow_field_const(libObjPtr); | |
1347 | } | |
1348 | }; | |
1349 | ||
b5f55e9f | 1350 | } /* namespace internal */ |
a1e31878 PP |
1351 | |
1352 | template <typename LibObjT> | |
1353 | class CommonOptionField : public CommonField<LibObjT> | |
1354 | { | |
1355 | private: | |
1356 | using typename CommonField<LibObjT>::_LibObjPtr; | |
1357 | using typename CommonField<LibObjT>::_ThisCommonField; | |
1358 | using _Spec = internal::CommonOptionFieldSpec<LibObjT>; | |
1359 | ||
1360 | public: | |
1361 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstOptionFieldClass, | |
1362 | OptionFieldClass>::type; | |
1363 | ||
1364 | explicit CommonOptionField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} | |
1365 | { | |
1366 | BT_ASSERT_DBG(this->isOption()); | |
1367 | } | |
1368 | ||
1369 | template <typename OtherLibObjT> | |
1370 | CommonOptionField(const CommonOptionField<OtherLibObjT>& val) noexcept : _ThisCommonField {val} | |
1371 | { | |
1372 | } | |
1373 | ||
1374 | template <typename OtherLibObjT> | |
1375 | CommonOptionField<LibObjT>& operator=(const CommonOptionField<OtherLibObjT>& val) noexcept | |
1376 | { | |
1377 | _ThisCommonField::operator=(val); | |
1378 | return *this; | |
1379 | } | |
1380 | ||
1381 | ConstOptionFieldClass cls() const noexcept | |
1382 | { | |
1383 | return ConstOptionFieldClass { | |
341a67c4 | 1384 | internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())}; |
a1e31878 PP |
1385 | } |
1386 | ||
1387 | Class cls() noexcept | |
1388 | { | |
341a67c4 | 1389 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
1390 | } |
1391 | ||
1392 | void hasField(const bool hasField) noexcept | |
1393 | { | |
1394 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1395 | ||
341a67c4 | 1396 | bt_field_option_set_has_field(this->libObjPtr(), static_cast<bt_bool>(hasField)); |
a1e31878 PP |
1397 | } |
1398 | ||
1399 | bool hasField() const noexcept | |
1400 | { | |
1401 | return this->field(); | |
1402 | } | |
1403 | ||
1404 | nonstd::optional<ConstField> field() const noexcept | |
1405 | { | |
1406 | const auto libObjPtr = | |
341a67c4 | 1407 | internal::CommonOptionFieldSpec<const bt_field>::field(this->libObjPtr()); |
a1e31878 PP |
1408 | |
1409 | if (libObjPtr) { | |
1410 | return ConstField {libObjPtr}; | |
1411 | } | |
1412 | ||
1413 | return nonstd::nullopt; | |
1414 | } | |
1415 | ||
1416 | nonstd::optional<CommonField<LibObjT>> field() noexcept | |
1417 | { | |
341a67c4 | 1418 | const auto libObjPtr = _Spec::field(this->libObjPtr()); |
a1e31878 PP |
1419 | |
1420 | if (libObjPtr) { | |
1421 | return CommonField<LibObjT> {libObjPtr}; | |
1422 | } | |
1423 | ||
1424 | return nonstd::nullopt; | |
1425 | } | |
1426 | }; | |
1427 | ||
1428 | using OptionField = CommonOptionField<bt_field>; | |
1429 | using ConstOptionField = CommonOptionField<const bt_field>; | |
1430 | ||
1431 | namespace internal { | |
1432 | ||
4927bae7 PP |
1433 | struct OptionFieldTypeDescr |
1434 | { | |
1435 | using Const = ConstOptionField; | |
1436 | using NonConst = OptionField; | |
1437 | }; | |
1438 | ||
1439 | template <> | |
1440 | struct TypeDescr<OptionField> : public OptionFieldTypeDescr | |
1441 | { | |
1442 | }; | |
1443 | ||
1444 | template <> | |
1445 | struct TypeDescr<ConstOptionField> : public OptionFieldTypeDescr | |
1446 | { | |
1447 | }; | |
1448 | ||
a1e31878 PP |
1449 | template <typename LibObjT> |
1450 | struct CommonVariantFieldSpec; | |
1451 | ||
b5f55e9f | 1452 | /* Functions specific to mutable variant fields */ |
a1e31878 PP |
1453 | template <> |
1454 | struct CommonVariantFieldSpec<bt_field> final | |
1455 | { | |
1456 | static bt_field *selectedOptionField(bt_field * const libObjPtr) noexcept | |
1457 | { | |
1458 | return bt_field_variant_borrow_selected_option_field(libObjPtr); | |
1459 | } | |
1460 | }; | |
1461 | ||
b5f55e9f | 1462 | /* Functions specific to constant variant fields */ |
a1e31878 PP |
1463 | template <> |
1464 | struct CommonVariantFieldSpec<const bt_field> final | |
1465 | { | |
1466 | static const bt_field *selectedOptionField(const bt_field * const libObjPtr) noexcept | |
1467 | { | |
1468 | return bt_field_variant_borrow_selected_option_field_const(libObjPtr); | |
1469 | } | |
1470 | }; | |
1471 | ||
b5f55e9f | 1472 | } /* namespace internal */ |
a1e31878 PP |
1473 | |
1474 | template <typename LibObjT> | |
1475 | class CommonVariantField : public CommonField<LibObjT> | |
1476 | { | |
1477 | private: | |
1478 | using typename CommonField<LibObjT>::_LibObjPtr; | |
1479 | using typename CommonField<LibObjT>::_ThisCommonField; | |
1480 | using _Spec = internal::CommonVariantFieldSpec<LibObjT>; | |
1481 | ||
1482 | public: | |
1483 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstVariantFieldClass, | |
1484 | VariantFieldClass>::type; | |
1485 | ||
1486 | explicit CommonVariantField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} | |
1487 | { | |
1488 | BT_ASSERT_DBG(this->isVariant()); | |
1489 | } | |
1490 | ||
1491 | template <typename OtherLibObjT> | |
1492 | CommonVariantField(const CommonVariantField<OtherLibObjT>& val) noexcept : | |
1493 | _ThisCommonField {val} | |
1494 | { | |
1495 | } | |
1496 | ||
1497 | template <typename OtherLibObjT> | |
1498 | CommonVariantField<LibObjT>& operator=(const CommonVariantField<OtherLibObjT>& val) noexcept | |
1499 | { | |
1500 | _ThisCommonField::operator=(val); | |
1501 | return *this; | |
1502 | } | |
1503 | ||
1504 | ConstVariantFieldClass cls() const noexcept | |
1505 | { | |
1506 | return ConstVariantFieldClass { | |
341a67c4 | 1507 | internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())}; |
a1e31878 PP |
1508 | } |
1509 | ||
1510 | Class cls() noexcept | |
1511 | { | |
341a67c4 | 1512 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
1513 | } |
1514 | ||
1515 | void selectOption(const std::uint64_t index) noexcept | |
1516 | { | |
1517 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1518 | ||
341a67c4 | 1519 | static_cast<void>(bt_field_variant_select_option_by_index(this->libObjPtr(), index)); |
a1e31878 PP |
1520 | } |
1521 | ||
1522 | ConstField selectedOptionField() const noexcept | |
1523 | { | |
1524 | return ConstField {internal::CommonVariantFieldSpec<const bt_field>::selectedOptionField( | |
341a67c4 | 1525 | this->libObjPtr())}; |
a1e31878 PP |
1526 | } |
1527 | ||
1528 | CommonField<LibObjT> selectedOptionField() noexcept | |
1529 | { | |
341a67c4 | 1530 | return CommonField<LibObjT> {_Spec::selectedOptionField(this->libObjPtr())}; |
a1e31878 PP |
1531 | } |
1532 | ||
1533 | std::uint64_t selectedOptionIndex() const noexcept | |
1534 | { | |
341a67c4 | 1535 | return bt_field_variant_get_selected_option_index(this->libObjPtr()); |
a1e31878 PP |
1536 | } |
1537 | }; | |
1538 | ||
1539 | using VariantField = CommonVariantField<bt_field>; | |
1540 | using ConstVariantField = CommonVariantField<const bt_field>; | |
1541 | ||
4927bae7 PP |
1542 | namespace internal { |
1543 | ||
1544 | struct VariantFieldTypeDescr | |
1545 | { | |
1546 | using Const = ConstVariantField; | |
1547 | using NonConst = VariantField; | |
1548 | }; | |
1549 | ||
1550 | template <> | |
1551 | struct TypeDescr<VariantField> : public VariantFieldTypeDescr | |
1552 | { | |
1553 | }; | |
1554 | ||
1555 | template <> | |
1556 | struct TypeDescr<ConstVariantField> : public VariantFieldTypeDescr | |
1557 | { | |
1558 | }; | |
1559 | ||
1560 | } /* namespace internal */ | |
1561 | ||
a1e31878 PP |
1562 | template <typename LibObjT> |
1563 | CommonBoolField<LibObjT> CommonField<LibObjT>::asBool() const noexcept | |
1564 | { | |
1565 | BT_ASSERT_DBG(this->isBool()); | |
341a67c4 | 1566 | return CommonBoolField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1567 | } |
1568 | ||
1569 | template <typename LibObjT> | |
1570 | CommonBitArrayField<LibObjT> CommonField<LibObjT>::asBitArray() const noexcept | |
1571 | { | |
1572 | BT_ASSERT_DBG(this->isBitArray()); | |
341a67c4 | 1573 | return CommonBitArrayField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1574 | } |
1575 | ||
1576 | template <typename LibObjT> | |
1577 | CommonUnsignedIntegerField<LibObjT> CommonField<LibObjT>::asUnsignedInteger() const noexcept | |
1578 | { | |
1579 | BT_ASSERT_DBG(this->isUnsignedInteger()); | |
341a67c4 | 1580 | return CommonUnsignedIntegerField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1581 | } |
1582 | ||
1583 | template <typename LibObjT> | |
1584 | CommonSignedIntegerField<LibObjT> CommonField<LibObjT>::asSignedInteger() const noexcept | |
1585 | { | |
1586 | BT_ASSERT_DBG(this->isSignedInteger()); | |
341a67c4 | 1587 | return CommonSignedIntegerField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1588 | } |
1589 | ||
1590 | template <typename LibObjT> | |
1591 | CommonUnsignedEnumerationField<LibObjT> CommonField<LibObjT>::asUnsignedEnumeration() const noexcept | |
1592 | { | |
1593 | BT_ASSERT_DBG(this->isUnsignedEnumeration()); | |
341a67c4 | 1594 | return CommonUnsignedEnumerationField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1595 | } |
1596 | ||
1597 | template <typename LibObjT> | |
1598 | CommonSignedEnumerationField<LibObjT> CommonField<LibObjT>::asSignedEnumeration() const noexcept | |
1599 | { | |
1600 | BT_ASSERT_DBG(this->isSignedEnumeration()); | |
341a67c4 | 1601 | return CommonSignedEnumerationField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1602 | } |
1603 | ||
1604 | template <typename LibObjT> | |
1605 | CommonSinglePrecisionRealField<LibObjT> CommonField<LibObjT>::asSinglePrecisionReal() const noexcept | |
1606 | { | |
1607 | BT_ASSERT_DBG(this->isSinglePrecisionReal()); | |
341a67c4 | 1608 | return CommonSinglePrecisionRealField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1609 | } |
1610 | ||
1611 | template <typename LibObjT> | |
1612 | CommonDoublePrecisionRealField<LibObjT> CommonField<LibObjT>::asDoublePrecisionReal() const noexcept | |
1613 | { | |
1614 | BT_ASSERT_DBG(this->isDoublePrecisionReal()); | |
341a67c4 | 1615 | return CommonDoublePrecisionRealField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1616 | } |
1617 | ||
1618 | template <typename LibObjT> | |
1619 | CommonStringField<LibObjT> CommonField<LibObjT>::asString() const noexcept | |
1620 | { | |
1621 | BT_ASSERT_DBG(this->isString()); | |
341a67c4 | 1622 | return CommonStringField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1623 | } |
1624 | ||
1625 | template <typename LibObjT> | |
1626 | CommonStructureField<LibObjT> CommonField<LibObjT>::asStructure() const noexcept | |
1627 | { | |
1628 | BT_ASSERT_DBG(this->isStructure()); | |
341a67c4 | 1629 | return CommonStructureField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1630 | } |
1631 | ||
1632 | template <typename LibObjT> | |
1633 | CommonArrayField<LibObjT> CommonField<LibObjT>::asArray() const noexcept | |
1634 | { | |
1635 | BT_ASSERT_DBG(this->isArray()); | |
341a67c4 | 1636 | return CommonArrayField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1637 | } |
1638 | ||
1639 | template <typename LibObjT> | |
1640 | CommonDynamicArrayField<LibObjT> CommonField<LibObjT>::asDynamicArray() const noexcept | |
1641 | { | |
1642 | BT_ASSERT_DBG(this->isDynamicArray()); | |
341a67c4 | 1643 | return CommonDynamicArrayField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1644 | } |
1645 | ||
1646 | template <typename LibObjT> | |
1647 | CommonOptionField<LibObjT> CommonField<LibObjT>::asOption() const noexcept | |
1648 | { | |
1649 | BT_ASSERT_DBG(this->isOption()); | |
341a67c4 | 1650 | return CommonOptionField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1651 | } |
1652 | ||
1653 | template <typename LibObjT> | |
1654 | CommonVariantField<LibObjT> CommonField<LibObjT>::asVariant() const noexcept | |
1655 | { | |
1656 | BT_ASSERT_DBG(this->isVariant()); | |
341a67c4 | 1657 | return CommonVariantField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1658 | } |
1659 | ||
b5f55e9f | 1660 | } /* namespace bt2 */ |
a1e31878 | 1661 | |
b5f55e9f | 1662 | #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_HPP */ |