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