| 1 | /* |
| 2 | * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | */ |
| 6 | |
| 7 | #ifndef BABELTRACE_CPP_COMMON_BT2_VALUE_HPP |
| 8 | #define BABELTRACE_CPP_COMMON_BT2_VALUE_HPP |
| 9 | |
| 10 | #include <cstdint> |
| 11 | #include <functional> |
| 12 | #include <type_traits> |
| 13 | |
| 14 | #include <babeltrace2/babeltrace.h> |
| 15 | |
| 16 | #include "common/assert.h" |
| 17 | #include "common/common.h" |
| 18 | #include "cpp-common/bt2c/c-string-view.hpp" |
| 19 | #include "cpp-common/vendor/wise-enum/wise_enum.h" |
| 20 | |
| 21 | #include "borrowed-object-iterator.hpp" |
| 22 | #include "borrowed-object.hpp" |
| 23 | #include "exc.hpp" |
| 24 | #include "internal/utils.hpp" |
| 25 | #include "optional-borrowed-object.hpp" |
| 26 | #include "raw-value-proxy.hpp" |
| 27 | #include "shared-object.hpp" |
| 28 | |
| 29 | namespace bt2 { |
| 30 | namespace internal { |
| 31 | |
| 32 | struct ValueRefFuncs final |
| 33 | { |
| 34 | static void get(const bt_value * const libObjPtr) noexcept |
| 35 | { |
| 36 | bt_value_get_ref(libObjPtr); |
| 37 | } |
| 38 | |
| 39 | static void put(const bt_value * const libObjPtr) noexcept |
| 40 | { |
| 41 | bt_value_put_ref(libObjPtr); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | } /* namespace internal */ |
| 46 | |
| 47 | template <typename ObjT, typename LibObjT> |
| 48 | using SharedValue = SharedObject<ObjT, LibObjT, internal::ValueRefFuncs>; |
| 49 | |
| 50 | template <typename LibObjT> |
| 51 | class CommonNullValue; |
| 52 | |
| 53 | template <typename LibObjT> |
| 54 | class CommonBoolValue; |
| 55 | |
| 56 | template <typename LibObjT> |
| 57 | class CommonUnsignedIntegerValue; |
| 58 | |
| 59 | template <typename LibObjT> |
| 60 | class CommonSignedIntegerValue; |
| 61 | |
| 62 | template <typename LibObjT> |
| 63 | class CommonRealValue; |
| 64 | |
| 65 | template <typename LibObjT> |
| 66 | class CommonStringValue; |
| 67 | |
| 68 | template <typename LibObjT> |
| 69 | class CommonArrayValue; |
| 70 | |
| 71 | template <typename LibObjT> |
| 72 | class CommonMapValue; |
| 73 | |
| 74 | /* clang-format off */ |
| 75 | |
| 76 | WISE_ENUM_CLASS(ValueType, |
| 77 | (Null, BT_VALUE_TYPE_NULL), |
| 78 | (Bool, BT_VALUE_TYPE_BOOL), |
| 79 | (UnsignedInteger, BT_VALUE_TYPE_UNSIGNED_INTEGER), |
| 80 | (SignedInteger, BT_VALUE_TYPE_SIGNED_INTEGER), |
| 81 | (Real, BT_VALUE_TYPE_REAL), |
| 82 | (String, BT_VALUE_TYPE_STRING), |
| 83 | (Array, BT_VALUE_TYPE_ARRAY), |
| 84 | (Map, BT_VALUE_TYPE_MAP)); |
| 85 | |
| 86 | /* clang-format on */ |
| 87 | |
| 88 | template <typename ValueObjT> |
| 89 | class CommonValueRawValueProxy final |
| 90 | { |
| 91 | public: |
| 92 | explicit CommonValueRawValueProxy(const ValueObjT obj) : _mObj {obj} |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | CommonValueRawValueProxy& operator=(bool rawVal) noexcept; |
| 97 | CommonValueRawValueProxy& operator=(std::int64_t rawVal) noexcept; |
| 98 | CommonValueRawValueProxy& operator=(std::uint64_t rawVal) noexcept; |
| 99 | CommonValueRawValueProxy& operator=(double rawVal) noexcept; |
| 100 | CommonValueRawValueProxy& operator=(const char *rawVal); |
| 101 | CommonValueRawValueProxy& operator=(bt2c::CStringView rawVal); |
| 102 | operator bool() const noexcept; |
| 103 | operator std::int64_t() const noexcept; |
| 104 | operator std::uint64_t() const noexcept; |
| 105 | operator double() const noexcept; |
| 106 | operator bt2c::CStringView() const noexcept; |
| 107 | |
| 108 | private: |
| 109 | ValueObjT _mObj; |
| 110 | }; |
| 111 | |
| 112 | template <typename LibObjT> |
| 113 | class CommonValue : public BorrowedObject<LibObjT> |
| 114 | { |
| 115 | private: |
| 116 | using typename BorrowedObject<LibObjT>::_ThisBorrowedObject; |
| 117 | |
| 118 | protected: |
| 119 | using _ThisCommonValue = CommonValue<LibObjT>; |
| 120 | |
| 121 | public: |
| 122 | using typename BorrowedObject<LibObjT>::LibObjPtr; |
| 123 | using Shared = SharedValue<CommonValue<LibObjT>, LibObjT>; |
| 124 | |
| 125 | explicit CommonValue(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | template <typename OtherLibObjT> |
| 130 | CommonValue(const CommonValue<OtherLibObjT> val) noexcept : _ThisBorrowedObject {val} |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | template <typename OtherLibObjT> |
| 135 | _ThisCommonValue operator=(const CommonValue<OtherLibObjT> val) noexcept |
| 136 | { |
| 137 | _ThisBorrowedObject::operator=(val); |
| 138 | return *this; |
| 139 | } |
| 140 | |
| 141 | CommonValue<const bt_value> asConst() const noexcept |
| 142 | { |
| 143 | return CommonValue<const bt_value> {*this}; |
| 144 | } |
| 145 | |
| 146 | ValueType type() const noexcept |
| 147 | { |
| 148 | return static_cast<ValueType>(bt_value_get_type(this->libObjPtr())); |
| 149 | } |
| 150 | |
| 151 | bool isNull() const noexcept |
| 152 | { |
| 153 | return this->_libTypeIs(BT_VALUE_TYPE_NULL); |
| 154 | } |
| 155 | |
| 156 | bool isBool() const noexcept |
| 157 | { |
| 158 | return this->_libTypeIs(BT_VALUE_TYPE_BOOL); |
| 159 | } |
| 160 | |
| 161 | bool isInteger() const noexcept |
| 162 | { |
| 163 | return this->_libTypeIs(BT_VALUE_TYPE_INTEGER); |
| 164 | } |
| 165 | |
| 166 | bool isUnsignedInteger() const noexcept |
| 167 | { |
| 168 | return this->_libTypeIs(BT_VALUE_TYPE_UNSIGNED_INTEGER); |
| 169 | } |
| 170 | |
| 171 | bool isSignedInteger() const noexcept |
| 172 | { |
| 173 | return this->_libTypeIs(BT_VALUE_TYPE_SIGNED_INTEGER); |
| 174 | } |
| 175 | |
| 176 | bool isReal() const noexcept |
| 177 | { |
| 178 | return this->_libTypeIs(BT_VALUE_TYPE_REAL); |
| 179 | } |
| 180 | |
| 181 | bool isString() const noexcept |
| 182 | { |
| 183 | return this->_libTypeIs(BT_VALUE_TYPE_STRING); |
| 184 | } |
| 185 | |
| 186 | bool isArray() const noexcept |
| 187 | { |
| 188 | return this->_libTypeIs(BT_VALUE_TYPE_ARRAY); |
| 189 | } |
| 190 | |
| 191 | bool isMap() const noexcept |
| 192 | { |
| 193 | return this->_libTypeIs(BT_VALUE_TYPE_MAP); |
| 194 | } |
| 195 | |
| 196 | template <typename OtherLibObjT> |
| 197 | bool operator==(const CommonValue<OtherLibObjT> other) const noexcept |
| 198 | { |
| 199 | return static_cast<bool>(bt_value_is_equal(this->libObjPtr(), other.libObjPtr())); |
| 200 | } |
| 201 | |
| 202 | template <typename OtherLibObjT> |
| 203 | bool operator!=(const CommonValue<OtherLibObjT> other) const noexcept |
| 204 | { |
| 205 | return !(*this == other); |
| 206 | } |
| 207 | |
| 208 | CommonValueRawValueProxy<CommonValue> operator*() const noexcept |
| 209 | { |
| 210 | return CommonValueRawValueProxy<CommonValue> {*this}; |
| 211 | } |
| 212 | |
| 213 | std::uint64_t arrayLength() const noexcept |
| 214 | { |
| 215 | return this->asArray().length(); |
| 216 | } |
| 217 | |
| 218 | bool arrayIsEmpty() const noexcept |
| 219 | { |
| 220 | return this->asArray().isEmpty(); |
| 221 | } |
| 222 | |
| 223 | CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept |
| 224 | { |
| 225 | return this->asArray()[index]; |
| 226 | } |
| 227 | |
| 228 | template <typename T> |
| 229 | void append(T&& elem) const |
| 230 | { |
| 231 | this->asArray().append(std::forward<T>(elem)); |
| 232 | } |
| 233 | |
| 234 | CommonArrayValue<bt_value> appendEmptyArray() const; |
| 235 | CommonMapValue<bt_value> appendEmptyMap() const; |
| 236 | |
| 237 | std::uint64_t mapLength() const noexcept |
| 238 | { |
| 239 | return this->asMap().length(); |
| 240 | } |
| 241 | |
| 242 | bool mapIsEmpty() const noexcept |
| 243 | { |
| 244 | return this->asMap().isEmpty(); |
| 245 | } |
| 246 | |
| 247 | template <typename KeyT> |
| 248 | OptionalBorrowedObject<CommonValue<LibObjT>> operator[](KeyT&& key) const noexcept |
| 249 | { |
| 250 | return this->asMap()[std::forward<KeyT>(key)]; |
| 251 | } |
| 252 | |
| 253 | template <typename KeyT> |
| 254 | bool hasEntry(KeyT&& key) const noexcept |
| 255 | { |
| 256 | return this->asMap().hasEntry(std::forward<KeyT>(key)); |
| 257 | } |
| 258 | |
| 259 | template <typename KeyT, typename ValT> |
| 260 | void insert(KeyT&& key, ValT&& val) const |
| 261 | { |
| 262 | this->asMap().insert(std::forward<KeyT>(key), std::forward<ValT>(val)); |
| 263 | } |
| 264 | |
| 265 | CommonArrayValue<bt_value> insertEmptyArray(bt2c::CStringView key) const; |
| 266 | CommonMapValue<bt_value> insertEmptyMap(bt2c::CStringView key) const; |
| 267 | |
| 268 | Shared shared() const noexcept |
| 269 | { |
| 270 | return Shared::createWithRef(*this); |
| 271 | } |
| 272 | |
| 273 | template <typename ValueT> |
| 274 | ValueT as() const noexcept |
| 275 | { |
| 276 | return ValueT {this->libObjPtr()}; |
| 277 | } |
| 278 | |
| 279 | CommonNullValue<LibObjT> asNull() const noexcept; |
| 280 | CommonBoolValue<LibObjT> asBool() const noexcept; |
| 281 | CommonSignedIntegerValue<LibObjT> asSignedInteger() const noexcept; |
| 282 | CommonUnsignedIntegerValue<LibObjT> asUnsignedInteger() const noexcept; |
| 283 | CommonRealValue<LibObjT> asReal() const noexcept; |
| 284 | CommonStringValue<LibObjT> asString() const noexcept; |
| 285 | CommonArrayValue<LibObjT> asArray() const noexcept; |
| 286 | CommonMapValue<LibObjT> asMap() const noexcept; |
| 287 | |
| 288 | protected: |
| 289 | bool _libTypeIs(const bt_value_type type) const noexcept |
| 290 | { |
| 291 | return bt_value_type_is(bt_value_get_type(this->libObjPtr()), type); |
| 292 | } |
| 293 | }; |
| 294 | |
| 295 | using Value = CommonValue<bt_value>; |
| 296 | using ConstValue = CommonValue<const bt_value>; |
| 297 | |
| 298 | template <typename ValueObjT> |
| 299 | CommonValueRawValueProxy<ValueObjT>& |
| 300 | CommonValueRawValueProxy<ValueObjT>::operator=(const bool rawVal) noexcept |
| 301 | { |
| 302 | _mObj.asBool().value(rawVal); |
| 303 | return *this; |
| 304 | } |
| 305 | |
| 306 | template <typename ValueObjT> |
| 307 | CommonValueRawValueProxy<ValueObjT>& |
| 308 | CommonValueRawValueProxy<ValueObjT>::operator=(const std::int64_t rawVal) noexcept |
| 309 | { |
| 310 | _mObj.asSignedInteger().value(rawVal); |
| 311 | return *this; |
| 312 | } |
| 313 | |
| 314 | template <typename ValueObjT> |
| 315 | CommonValueRawValueProxy<ValueObjT>& |
| 316 | CommonValueRawValueProxy<ValueObjT>::operator=(const std::uint64_t rawVal) noexcept |
| 317 | { |
| 318 | _mObj.asUnsignedInteger().value(rawVal); |
| 319 | return *this; |
| 320 | } |
| 321 | |
| 322 | template <typename ValueObjT> |
| 323 | CommonValueRawValueProxy<ValueObjT>& |
| 324 | CommonValueRawValueProxy<ValueObjT>::operator=(const double rawVal) noexcept |
| 325 | { |
| 326 | _mObj.asReal().value(rawVal); |
| 327 | return *this; |
| 328 | } |
| 329 | |
| 330 | template <typename ValueObjT> |
| 331 | CommonValueRawValueProxy<ValueObjT>& |
| 332 | CommonValueRawValueProxy<ValueObjT>::operator=(const char * const rawVal) |
| 333 | { |
| 334 | _mObj.asString().value(rawVal); |
| 335 | return *this; |
| 336 | } |
| 337 | |
| 338 | template <typename ValueObjT> |
| 339 | CommonValueRawValueProxy<ValueObjT>& |
| 340 | CommonValueRawValueProxy<ValueObjT>::operator=(const bt2c::CStringView rawVal) |
| 341 | { |
| 342 | _mObj.asString().value(rawVal); |
| 343 | return *this; |
| 344 | } |
| 345 | |
| 346 | template <typename ValueObjT> |
| 347 | CommonValueRawValueProxy<ValueObjT>::operator bool() const noexcept |
| 348 | { |
| 349 | return _mObj.asBool().value(); |
| 350 | } |
| 351 | |
| 352 | template <typename ValueObjT> |
| 353 | CommonValueRawValueProxy<ValueObjT>::operator std::int64_t() const noexcept |
| 354 | { |
| 355 | return _mObj.asSignedInteger().value(); |
| 356 | } |
| 357 | |
| 358 | template <typename ValueObjT> |
| 359 | CommonValueRawValueProxy<ValueObjT>::operator std::uint64_t() const noexcept |
| 360 | { |
| 361 | return _mObj.asUnsignedInteger().value(); |
| 362 | } |
| 363 | |
| 364 | template <typename ValueObjT> |
| 365 | CommonValueRawValueProxy<ValueObjT>::operator double() const noexcept |
| 366 | { |
| 367 | return _mObj.asReal().value(); |
| 368 | } |
| 369 | |
| 370 | template <typename ValueObjT> |
| 371 | CommonValueRawValueProxy<ValueObjT>::operator bt2c::CStringView() const noexcept |
| 372 | { |
| 373 | return _mObj.asString().value(); |
| 374 | } |
| 375 | |
| 376 | namespace internal { |
| 377 | |
| 378 | struct ValueTypeDescr |
| 379 | { |
| 380 | using Const = ConstValue; |
| 381 | using NonConst = Value; |
| 382 | }; |
| 383 | |
| 384 | template <> |
| 385 | struct TypeDescr<Value> : public ValueTypeDescr |
| 386 | { |
| 387 | }; |
| 388 | |
| 389 | template <> |
| 390 | struct TypeDescr<ConstValue> : public ValueTypeDescr |
| 391 | { |
| 392 | }; |
| 393 | |
| 394 | } /* namespace internal */ |
| 395 | |
| 396 | template <typename LibObjT> |
| 397 | class CommonNullValue final : public CommonValue<LibObjT> |
| 398 | { |
| 399 | private: |
| 400 | using typename CommonValue<LibObjT>::_ThisCommonValue; |
| 401 | |
| 402 | public: |
| 403 | using Shared = SharedValue<CommonNullValue<LibObjT>, LibObjT>; |
| 404 | |
| 405 | CommonNullValue() noexcept : _ThisCommonValue {bt_value_null} |
| 406 | { |
| 407 | } |
| 408 | |
| 409 | template <typename OtherLibObjT> |
| 410 | CommonNullValue(const CommonNullValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val} |
| 411 | { |
| 412 | } |
| 413 | |
| 414 | template <typename OtherLibObjT> |
| 415 | CommonNullValue<LibObjT> operator=(const CommonNullValue<OtherLibObjT> val) noexcept |
| 416 | { |
| 417 | _ThisCommonValue::operator=(val); |
| 418 | return *this; |
| 419 | } |
| 420 | |
| 421 | CommonNullValue<const bt_value> asConst() const noexcept |
| 422 | { |
| 423 | return CommonNullValue<const bt_value> {*this}; |
| 424 | } |
| 425 | |
| 426 | Shared shared() const noexcept |
| 427 | { |
| 428 | return Shared::createWithRef(*this); |
| 429 | } |
| 430 | }; |
| 431 | |
| 432 | using NullValue = CommonNullValue<bt_value>; |
| 433 | using ConstNullValue = CommonNullValue<const bt_value>; |
| 434 | |
| 435 | namespace internal { |
| 436 | |
| 437 | struct NullValueTypeDescr |
| 438 | { |
| 439 | using Const = ConstNullValue; |
| 440 | using NonConst = NullValue; |
| 441 | }; |
| 442 | |
| 443 | template <> |
| 444 | struct TypeDescr<NullValue> : public NullValueTypeDescr |
| 445 | { |
| 446 | }; |
| 447 | |
| 448 | template <> |
| 449 | struct TypeDescr<ConstNullValue> : public NullValueTypeDescr |
| 450 | { |
| 451 | }; |
| 452 | |
| 453 | } /* namespace internal */ |
| 454 | |
| 455 | template <typename LibObjT> |
| 456 | class CommonBoolValue final : public CommonValue<LibObjT> |
| 457 | { |
| 458 | private: |
| 459 | using typename CommonValue<LibObjT>::_ThisCommonValue; |
| 460 | |
| 461 | public: |
| 462 | using typename CommonValue<LibObjT>::LibObjPtr; |
| 463 | using Shared = SharedValue<CommonBoolValue<LibObjT>, LibObjT>; |
| 464 | using Value = bool; |
| 465 | |
| 466 | explicit CommonBoolValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} |
| 467 | { |
| 468 | BT_ASSERT_DBG(this->isBool()); |
| 469 | } |
| 470 | |
| 471 | template <typename OtherLibObjT> |
| 472 | CommonBoolValue(const CommonBoolValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val} |
| 473 | { |
| 474 | } |
| 475 | |
| 476 | static Shared create(const Value rawVal = false) |
| 477 | { |
| 478 | const auto libObjPtr = bt_value_bool_create_init(static_cast<bt_bool>(rawVal)); |
| 479 | |
| 480 | internal::validateCreatedObjPtr(libObjPtr); |
| 481 | return CommonBoolValue::Shared::createWithoutRef(libObjPtr); |
| 482 | } |
| 483 | |
| 484 | template <typename OtherLibObjT> |
| 485 | CommonBoolValue<LibObjT> operator=(const CommonBoolValue<OtherLibObjT> val) noexcept |
| 486 | { |
| 487 | _ThisCommonValue::operator=(val); |
| 488 | return *this; |
| 489 | } |
| 490 | |
| 491 | CommonBoolValue<const bt_value> asConst() const noexcept |
| 492 | { |
| 493 | return CommonBoolValue<const bt_value> {*this}; |
| 494 | } |
| 495 | |
| 496 | RawValueProxy<CommonBoolValue> operator*() const noexcept |
| 497 | { |
| 498 | return RawValueProxy<CommonBoolValue> {*this}; |
| 499 | } |
| 500 | |
| 501 | Value value() const noexcept |
| 502 | { |
| 503 | return static_cast<Value>(bt_value_bool_get(this->libObjPtr())); |
| 504 | } |
| 505 | |
| 506 | CommonBoolValue value(const Value val) const noexcept |
| 507 | { |
| 508 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstBoolValue`."); |
| 509 | |
| 510 | bt_value_bool_set(this->libObjPtr(), static_cast<bt_bool>(val)); |
| 511 | return *this; |
| 512 | } |
| 513 | |
| 514 | Shared shared() const noexcept |
| 515 | { |
| 516 | return Shared::createWithRef(*this); |
| 517 | } |
| 518 | }; |
| 519 | |
| 520 | using BoolValue = CommonBoolValue<bt_value>; |
| 521 | using ConstBoolValue = CommonBoolValue<const bt_value>; |
| 522 | |
| 523 | namespace internal { |
| 524 | |
| 525 | struct BoolValueTypeDescr |
| 526 | { |
| 527 | using Const = ConstBoolValue; |
| 528 | using NonConst = BoolValue; |
| 529 | }; |
| 530 | |
| 531 | template <> |
| 532 | struct TypeDescr<BoolValue> : public BoolValueTypeDescr |
| 533 | { |
| 534 | }; |
| 535 | |
| 536 | template <> |
| 537 | struct TypeDescr<ConstBoolValue> : public BoolValueTypeDescr |
| 538 | { |
| 539 | }; |
| 540 | |
| 541 | } /* namespace internal */ |
| 542 | |
| 543 | template <typename LibObjT> |
| 544 | class CommonUnsignedIntegerValue final : public CommonValue<LibObjT> |
| 545 | { |
| 546 | private: |
| 547 | using typename CommonValue<LibObjT>::_ThisCommonValue; |
| 548 | |
| 549 | public: |
| 550 | using typename CommonValue<LibObjT>::LibObjPtr; |
| 551 | using Shared = SharedValue<CommonUnsignedIntegerValue<LibObjT>, LibObjT>; |
| 552 | using Value = std::uint64_t; |
| 553 | |
| 554 | explicit CommonUnsignedIntegerValue(const LibObjPtr libObjPtr) noexcept : |
| 555 | _ThisCommonValue {libObjPtr} |
| 556 | { |
| 557 | BT_ASSERT_DBG(this->isUnsignedInteger()); |
| 558 | } |
| 559 | |
| 560 | static Shared create(const Value rawVal = 0) |
| 561 | { |
| 562 | const auto libObjPtr = bt_value_integer_unsigned_create_init(rawVal); |
| 563 | |
| 564 | internal::validateCreatedObjPtr(libObjPtr); |
| 565 | return CommonUnsignedIntegerValue::Shared::createWithoutRef(libObjPtr); |
| 566 | } |
| 567 | |
| 568 | template <typename OtherLibObjT> |
| 569 | CommonUnsignedIntegerValue(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept : |
| 570 | _ThisCommonValue {val} |
| 571 | { |
| 572 | } |
| 573 | |
| 574 | template <typename OtherLibObjT> |
| 575 | CommonUnsignedIntegerValue<LibObjT> |
| 576 | operator=(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept |
| 577 | { |
| 578 | _ThisCommonValue::operator=(val); |
| 579 | return *this; |
| 580 | } |
| 581 | |
| 582 | CommonUnsignedIntegerValue<const bt_value> asConst() const noexcept |
| 583 | { |
| 584 | return CommonUnsignedIntegerValue<const bt_value> {*this}; |
| 585 | } |
| 586 | |
| 587 | RawValueProxy<CommonUnsignedIntegerValue> operator*() const noexcept |
| 588 | { |
| 589 | return RawValueProxy<CommonUnsignedIntegerValue> {*this}; |
| 590 | } |
| 591 | |
| 592 | CommonUnsignedIntegerValue value(const Value val) const noexcept |
| 593 | { |
| 594 | static_assert(!std::is_const<LibObjT>::value, |
| 595 | "Not available with `bt2::ConstUnsignedIntegerValue`."); |
| 596 | |
| 597 | bt_value_integer_unsigned_set(this->libObjPtr(), val); |
| 598 | return *this; |
| 599 | } |
| 600 | |
| 601 | Value value() const noexcept |
| 602 | { |
| 603 | return bt_value_integer_unsigned_get(this->libObjPtr()); |
| 604 | } |
| 605 | |
| 606 | Shared shared() const noexcept |
| 607 | { |
| 608 | return Shared::createWithRef(*this); |
| 609 | } |
| 610 | }; |
| 611 | |
| 612 | using UnsignedIntegerValue = CommonUnsignedIntegerValue<bt_value>; |
| 613 | using ConstUnsignedIntegerValue = CommonUnsignedIntegerValue<const bt_value>; |
| 614 | |
| 615 | namespace internal { |
| 616 | |
| 617 | struct UnsignedIntegerValueTypeDescr |
| 618 | { |
| 619 | using Const = ConstUnsignedIntegerValue; |
| 620 | using NonConst = UnsignedIntegerValue; |
| 621 | }; |
| 622 | |
| 623 | template <> |
| 624 | struct TypeDescr<UnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr |
| 625 | { |
| 626 | }; |
| 627 | |
| 628 | template <> |
| 629 | struct TypeDescr<ConstUnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr |
| 630 | { |
| 631 | }; |
| 632 | |
| 633 | } /* namespace internal */ |
| 634 | |
| 635 | template <typename LibObjT> |
| 636 | class CommonSignedIntegerValue final : public CommonValue<LibObjT> |
| 637 | { |
| 638 | private: |
| 639 | using typename CommonValue<LibObjT>::_ThisCommonValue; |
| 640 | |
| 641 | public: |
| 642 | using typename CommonValue<LibObjT>::LibObjPtr; |
| 643 | using Shared = SharedValue<CommonSignedIntegerValue<LibObjT>, LibObjT>; |
| 644 | using Value = std::int64_t; |
| 645 | |
| 646 | explicit CommonSignedIntegerValue(const LibObjPtr libObjPtr) noexcept : |
| 647 | _ThisCommonValue {libObjPtr} |
| 648 | { |
| 649 | BT_ASSERT_DBG(this->isSignedInteger()); |
| 650 | } |
| 651 | |
| 652 | static Shared create(const Value rawVal = 0) |
| 653 | { |
| 654 | const auto libObjPtr = bt_value_integer_signed_create_init(rawVal); |
| 655 | |
| 656 | internal::validateCreatedObjPtr(libObjPtr); |
| 657 | return CommonSignedIntegerValue::Shared::createWithoutRef(libObjPtr); |
| 658 | } |
| 659 | |
| 660 | template <typename OtherLibObjT> |
| 661 | CommonSignedIntegerValue(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept : |
| 662 | _ThisCommonValue {val} |
| 663 | { |
| 664 | } |
| 665 | |
| 666 | template <typename OtherLibObjT> |
| 667 | CommonSignedIntegerValue<LibObjT> |
| 668 | operator=(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept |
| 669 | { |
| 670 | _ThisCommonValue::operator=(val); |
| 671 | return *this; |
| 672 | } |
| 673 | |
| 674 | CommonSignedIntegerValue<const bt_value> asConst() const noexcept |
| 675 | { |
| 676 | return CommonSignedIntegerValue<const bt_value> {*this}; |
| 677 | } |
| 678 | |
| 679 | RawValueProxy<CommonSignedIntegerValue> operator*() const noexcept |
| 680 | { |
| 681 | return RawValueProxy<CommonSignedIntegerValue> {*this}; |
| 682 | } |
| 683 | |
| 684 | CommonSignedIntegerValue value(const Value val) const noexcept |
| 685 | { |
| 686 | static_assert(!std::is_const<LibObjT>::value, |
| 687 | "Not available with `bt2::ConstSignedIntegerValue`."); |
| 688 | |
| 689 | bt_value_integer_signed_set(this->libObjPtr(), val); |
| 690 | return *this; |
| 691 | } |
| 692 | |
| 693 | Value value() const noexcept |
| 694 | { |
| 695 | return bt_value_integer_signed_get(this->libObjPtr()); |
| 696 | } |
| 697 | |
| 698 | Shared shared() const noexcept |
| 699 | { |
| 700 | return Shared::createWithRef(*this); |
| 701 | } |
| 702 | }; |
| 703 | |
| 704 | using SignedIntegerValue = CommonSignedIntegerValue<bt_value>; |
| 705 | using ConstSignedIntegerValue = CommonSignedIntegerValue<const bt_value>; |
| 706 | |
| 707 | namespace internal { |
| 708 | |
| 709 | struct SignedIntegerValueTypeDescr |
| 710 | { |
| 711 | using Const = ConstSignedIntegerValue; |
| 712 | using NonConst = SignedIntegerValue; |
| 713 | }; |
| 714 | |
| 715 | template <> |
| 716 | struct TypeDescr<SignedIntegerValue> : public SignedIntegerValueTypeDescr |
| 717 | { |
| 718 | }; |
| 719 | |
| 720 | template <> |
| 721 | struct TypeDescr<ConstSignedIntegerValue> : public SignedIntegerValueTypeDescr |
| 722 | { |
| 723 | }; |
| 724 | |
| 725 | } /* namespace internal */ |
| 726 | |
| 727 | template <typename LibObjT> |
| 728 | class CommonRealValue final : public CommonValue<LibObjT> |
| 729 | { |
| 730 | private: |
| 731 | using typename CommonValue<LibObjT>::_ThisCommonValue; |
| 732 | |
| 733 | public: |
| 734 | using typename CommonValue<LibObjT>::LibObjPtr; |
| 735 | using Shared = SharedValue<CommonRealValue<LibObjT>, LibObjT>; |
| 736 | using Value = double; |
| 737 | |
| 738 | explicit CommonRealValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} |
| 739 | { |
| 740 | BT_ASSERT_DBG(this->isReal()); |
| 741 | } |
| 742 | |
| 743 | static Shared create(const Value rawVal = 0) |
| 744 | { |
| 745 | const auto libObjPtr = bt_value_real_create_init(rawVal); |
| 746 | |
| 747 | internal::validateCreatedObjPtr(libObjPtr); |
| 748 | return CommonRealValue::Shared::createWithoutRef(libObjPtr); |
| 749 | } |
| 750 | |
| 751 | template <typename OtherLibObjT> |
| 752 | CommonRealValue(const CommonRealValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val} |
| 753 | { |
| 754 | } |
| 755 | |
| 756 | template <typename OtherLibObjT> |
| 757 | CommonRealValue<LibObjT> operator=(const CommonRealValue<OtherLibObjT> val) noexcept |
| 758 | { |
| 759 | _ThisCommonValue::operator=(val); |
| 760 | return *this; |
| 761 | } |
| 762 | |
| 763 | CommonRealValue<const bt_value> asConst() const noexcept |
| 764 | { |
| 765 | return CommonRealValue<const bt_value> {*this}; |
| 766 | } |
| 767 | |
| 768 | RawValueProxy<CommonRealValue> operator*() const noexcept |
| 769 | { |
| 770 | return RawValueProxy<CommonRealValue> {*this}; |
| 771 | } |
| 772 | |
| 773 | CommonRealValue value(const Value val) const noexcept |
| 774 | { |
| 775 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstRealValue`."); |
| 776 | |
| 777 | bt_value_real_set(this->libObjPtr(), val); |
| 778 | return *this; |
| 779 | } |
| 780 | |
| 781 | Value value() const noexcept |
| 782 | { |
| 783 | return bt_value_real_get(this->libObjPtr()); |
| 784 | } |
| 785 | |
| 786 | Shared shared() const noexcept |
| 787 | { |
| 788 | return Shared::createWithRef(*this); |
| 789 | } |
| 790 | }; |
| 791 | |
| 792 | using RealValue = CommonRealValue<bt_value>; |
| 793 | using ConstRealValue = CommonRealValue<const bt_value>; |
| 794 | |
| 795 | namespace internal { |
| 796 | |
| 797 | struct RealValueTypeDescr |
| 798 | { |
| 799 | using Const = ConstRealValue; |
| 800 | using NonConst = RealValue; |
| 801 | }; |
| 802 | |
| 803 | template <> |
| 804 | struct TypeDescr<RealValue> : public RealValueTypeDescr |
| 805 | { |
| 806 | }; |
| 807 | |
| 808 | template <> |
| 809 | struct TypeDescr<ConstRealValue> : public RealValueTypeDescr |
| 810 | { |
| 811 | }; |
| 812 | |
| 813 | } /* namespace internal */ |
| 814 | |
| 815 | template <typename LibObjT> |
| 816 | class CommonStringValue final : public CommonValue<LibObjT> |
| 817 | { |
| 818 | private: |
| 819 | using typename CommonValue<LibObjT>::_ThisCommonValue; |
| 820 | |
| 821 | public: |
| 822 | using typename CommonValue<LibObjT>::LibObjPtr; |
| 823 | using Shared = SharedValue<CommonStringValue<LibObjT>, LibObjT>; |
| 824 | using Value = bt2c::CStringView; |
| 825 | |
| 826 | explicit CommonStringValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} |
| 827 | { |
| 828 | BT_ASSERT_DBG(this->isString()); |
| 829 | } |
| 830 | |
| 831 | static Shared create(const bt2c::CStringView rawVal = "") |
| 832 | { |
| 833 | const auto libObjPtr = bt_value_string_create_init(rawVal); |
| 834 | |
| 835 | internal::validateCreatedObjPtr(libObjPtr); |
| 836 | return CommonStringValue::Shared::createWithoutRef(libObjPtr); |
| 837 | } |
| 838 | |
| 839 | template <typename OtherLibObjT> |
| 840 | CommonStringValue(const CommonStringValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val} |
| 841 | { |
| 842 | } |
| 843 | |
| 844 | template <typename OtherLibObjT> |
| 845 | CommonStringValue<LibObjT> operator=(const CommonStringValue<OtherLibObjT> val) noexcept |
| 846 | { |
| 847 | _ThisCommonValue::operator=(val); |
| 848 | return *this; |
| 849 | } |
| 850 | |
| 851 | CommonStringValue<const bt_value> asConst() const noexcept |
| 852 | { |
| 853 | return CommonStringValue<const bt_value> {*this}; |
| 854 | } |
| 855 | |
| 856 | RawValueProxy<CommonStringValue> operator*() const noexcept |
| 857 | { |
| 858 | return RawValueProxy<CommonStringValue> {*this}; |
| 859 | } |
| 860 | |
| 861 | CommonStringValue value(const Value val) const |
| 862 | { |
| 863 | static_assert(!std::is_const<LibObjT>::value, |
| 864 | "Not available with `bt2::ConstStringValue`."); |
| 865 | |
| 866 | const auto status = bt_value_string_set(this->libObjPtr(), *val); |
| 867 | |
| 868 | if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) { |
| 869 | throw MemoryError {}; |
| 870 | } |
| 871 | |
| 872 | return *this; |
| 873 | } |
| 874 | |
| 875 | Value value() const noexcept |
| 876 | { |
| 877 | return bt_value_string_get(this->libObjPtr()); |
| 878 | } |
| 879 | |
| 880 | Shared shared() const noexcept |
| 881 | { |
| 882 | return Shared::createWithRef(*this); |
| 883 | } |
| 884 | }; |
| 885 | |
| 886 | using StringValue = CommonStringValue<bt_value>; |
| 887 | using ConstStringValue = CommonStringValue<const bt_value>; |
| 888 | |
| 889 | namespace internal { |
| 890 | |
| 891 | struct StringValueTypeDescr |
| 892 | { |
| 893 | using Const = ConstStringValue; |
| 894 | using NonConst = StringValue; |
| 895 | }; |
| 896 | |
| 897 | template <> |
| 898 | struct TypeDescr<StringValue> : public StringValueTypeDescr |
| 899 | { |
| 900 | }; |
| 901 | |
| 902 | template <> |
| 903 | struct TypeDescr<ConstStringValue> : public StringValueTypeDescr |
| 904 | { |
| 905 | }; |
| 906 | |
| 907 | template <typename LibObjT> |
| 908 | struct CommonArrayValueSpec; |
| 909 | |
| 910 | /* Functions specific to mutable array values */ |
| 911 | template <> |
| 912 | struct CommonArrayValueSpec<bt_value> final |
| 913 | { |
| 914 | static bt_value *elementByIndex(bt_value * const libValPtr, const std::uint64_t index) noexcept |
| 915 | { |
| 916 | return bt_value_array_borrow_element_by_index(libValPtr, index); |
| 917 | } |
| 918 | }; |
| 919 | |
| 920 | /* Functions specific to constant array values */ |
| 921 | template <> |
| 922 | struct CommonArrayValueSpec<const bt_value> final |
| 923 | { |
| 924 | static const bt_value *elementByIndex(const bt_value * const libValPtr, |
| 925 | const std::uint64_t index) noexcept |
| 926 | { |
| 927 | return bt_value_array_borrow_element_by_index_const(libValPtr, index); |
| 928 | } |
| 929 | }; |
| 930 | |
| 931 | } /* namespace internal */ |
| 932 | |
| 933 | template <typename LibObjT> |
| 934 | class CommonArrayValue final : public CommonValue<LibObjT> |
| 935 | { |
| 936 | private: |
| 937 | using typename CommonValue<LibObjT>::_ThisCommonValue; |
| 938 | |
| 939 | public: |
| 940 | using typename CommonValue<LibObjT>::LibObjPtr; |
| 941 | using Shared = SharedValue<CommonArrayValue<LibObjT>, LibObjT>; |
| 942 | using Iterator = BorrowedObjectIterator<CommonArrayValue<LibObjT>>; |
| 943 | |
| 944 | explicit CommonArrayValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} |
| 945 | { |
| 946 | BT_ASSERT_DBG(this->isArray()); |
| 947 | } |
| 948 | |
| 949 | static Shared create() |
| 950 | { |
| 951 | const auto libObjPtr = bt_value_array_create(); |
| 952 | |
| 953 | internal::validateCreatedObjPtr(libObjPtr); |
| 954 | return CommonArrayValue::Shared::createWithoutRef(libObjPtr); |
| 955 | } |
| 956 | |
| 957 | template <typename OtherLibObjT> |
| 958 | CommonArrayValue(const CommonArrayValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val} |
| 959 | { |
| 960 | } |
| 961 | |
| 962 | template <typename OtherLibObjT> |
| 963 | CommonArrayValue<LibObjT> operator=(const CommonArrayValue<OtherLibObjT> val) noexcept |
| 964 | { |
| 965 | _ThisCommonValue::operator=(val); |
| 966 | return *this; |
| 967 | } |
| 968 | |
| 969 | CommonArrayValue<const bt_value> asConst() const noexcept |
| 970 | { |
| 971 | return CommonArrayValue<const bt_value> {*this}; |
| 972 | } |
| 973 | |
| 974 | std::uint64_t length() const noexcept |
| 975 | { |
| 976 | return bt_value_array_get_length(this->libObjPtr()); |
| 977 | } |
| 978 | |
| 979 | Iterator begin() const noexcept |
| 980 | { |
| 981 | return Iterator {*this, 0}; |
| 982 | } |
| 983 | |
| 984 | Iterator end() const noexcept |
| 985 | { |
| 986 | return Iterator {*this, this->length()}; |
| 987 | } |
| 988 | |
| 989 | bool isEmpty() const noexcept |
| 990 | { |
| 991 | return this->length() == 0; |
| 992 | } |
| 993 | |
| 994 | CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept |
| 995 | { |
| 996 | return CommonValue<LibObjT> { |
| 997 | internal::CommonArrayValueSpec<LibObjT>::elementByIndex(this->libObjPtr(), index)}; |
| 998 | } |
| 999 | |
| 1000 | CommonArrayValue append(const Value val) const |
| 1001 | { |
| 1002 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`."); |
| 1003 | |
| 1004 | const auto status = bt_value_array_append_element(this->libObjPtr(), val.libObjPtr()); |
| 1005 | |
| 1006 | this->_handleAppendLibStatus(status); |
| 1007 | return *this; |
| 1008 | } |
| 1009 | |
| 1010 | CommonArrayValue append(const bool rawVal) const |
| 1011 | { |
| 1012 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`."); |
| 1013 | |
| 1014 | const auto status = |
| 1015 | bt_value_array_append_bool_element(this->libObjPtr(), static_cast<bt_bool>(rawVal)); |
| 1016 | |
| 1017 | this->_handleAppendLibStatus(status); |
| 1018 | return *this; |
| 1019 | } |
| 1020 | |
| 1021 | CommonArrayValue append(const std::uint64_t rawVal) const |
| 1022 | { |
| 1023 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`."); |
| 1024 | |
| 1025 | const auto status = |
| 1026 | bt_value_array_append_unsigned_integer_element(this->libObjPtr(), rawVal); |
| 1027 | |
| 1028 | this->_handleAppendLibStatus(status); |
| 1029 | return *this; |
| 1030 | } |
| 1031 | |
| 1032 | CommonArrayValue append(const std::int64_t rawVal) const |
| 1033 | { |
| 1034 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`."); |
| 1035 | |
| 1036 | const auto status = bt_value_array_append_signed_integer_element(this->libObjPtr(), rawVal); |
| 1037 | |
| 1038 | this->_handleAppendLibStatus(status); |
| 1039 | return *this; |
| 1040 | } |
| 1041 | |
| 1042 | CommonArrayValue append(const double rawVal) const |
| 1043 | { |
| 1044 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`."); |
| 1045 | |
| 1046 | const auto status = bt_value_array_append_real_element(this->libObjPtr(), rawVal); |
| 1047 | |
| 1048 | this->_handleAppendLibStatus(status); |
| 1049 | return *this; |
| 1050 | } |
| 1051 | |
| 1052 | CommonArrayValue append(const char * const rawVal) const |
| 1053 | { |
| 1054 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`."); |
| 1055 | |
| 1056 | const auto status = bt_value_array_append_string_element(this->libObjPtr(), rawVal); |
| 1057 | |
| 1058 | this->_handleAppendLibStatus(status); |
| 1059 | return *this; |
| 1060 | } |
| 1061 | |
| 1062 | CommonArrayValue append(const bt2c::CStringView rawVal) const |
| 1063 | { |
| 1064 | return this->append(rawVal.data()); |
| 1065 | } |
| 1066 | |
| 1067 | CommonArrayValue<bt_value> appendEmptyArray() const; |
| 1068 | CommonMapValue<bt_value> appendEmptyMap() const; |
| 1069 | |
| 1070 | void operator+=(const Value val) const |
| 1071 | { |
| 1072 | this->append(val); |
| 1073 | } |
| 1074 | |
| 1075 | void operator+=(const bool rawVal) const |
| 1076 | { |
| 1077 | this->append(rawVal); |
| 1078 | } |
| 1079 | |
| 1080 | void operator+=(const std::uint64_t rawVal) const |
| 1081 | { |
| 1082 | this->append(rawVal); |
| 1083 | } |
| 1084 | |
| 1085 | void operator+=(const std::int64_t rawVal) const |
| 1086 | { |
| 1087 | this->append(rawVal); |
| 1088 | } |
| 1089 | |
| 1090 | void operator+=(const double rawVal) const |
| 1091 | { |
| 1092 | this->append(rawVal); |
| 1093 | } |
| 1094 | |
| 1095 | void operator+=(const char * const rawVal) const |
| 1096 | { |
| 1097 | this->append(rawVal); |
| 1098 | } |
| 1099 | |
| 1100 | void operator+=(const bt2c::CStringView rawVal) const |
| 1101 | { |
| 1102 | this->append(rawVal); |
| 1103 | } |
| 1104 | |
| 1105 | Shared shared() const noexcept |
| 1106 | { |
| 1107 | return Shared::createWithRef(*this); |
| 1108 | } |
| 1109 | |
| 1110 | private: |
| 1111 | void _handleAppendLibStatus(const bt_value_array_append_element_status status) const |
| 1112 | { |
| 1113 | if (status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR) { |
| 1114 | throw MemoryError {}; |
| 1115 | } |
| 1116 | } |
| 1117 | }; |
| 1118 | |
| 1119 | using ArrayValue = CommonArrayValue<bt_value>; |
| 1120 | using ConstArrayValue = CommonArrayValue<const bt_value>; |
| 1121 | |
| 1122 | namespace internal { |
| 1123 | |
| 1124 | struct ArrayValueTypeDescr |
| 1125 | { |
| 1126 | using Const = ConstArrayValue; |
| 1127 | using NonConst = ArrayValue; |
| 1128 | }; |
| 1129 | |
| 1130 | template <> |
| 1131 | struct TypeDescr<ArrayValue> : public ArrayValueTypeDescr |
| 1132 | { |
| 1133 | }; |
| 1134 | |
| 1135 | template <> |
| 1136 | struct TypeDescr<ConstArrayValue> : public ArrayValueTypeDescr |
| 1137 | { |
| 1138 | }; |
| 1139 | |
| 1140 | /* |
| 1141 | * Type of a user function passed to `CommonMapValue<ObjT>::forEach()`. |
| 1142 | * |
| 1143 | * First argument is the entry's key, second is its value. |
| 1144 | */ |
| 1145 | template <typename ObjT> |
| 1146 | using CommonMapValueForEachUserFunc = std::function<void(bt2c::CStringView, ObjT)>; |
| 1147 | |
| 1148 | /* |
| 1149 | * Template of a function to be passed to bt_value_map_foreach_entry() |
| 1150 | * for bt_value_map_foreach_entry_const() which calls a user function. |
| 1151 | * |
| 1152 | * `userData` is casted to a `const` pointer to |
| 1153 | * `CommonMapValueForEachUserFunc<ObjT>` (the user function to call). |
| 1154 | * |
| 1155 | * This function catches any exception which the user function throws |
| 1156 | * and returns the `ErrorStatus` value. If there's no exception, this |
| 1157 | * function returns the `OkStatus` value. |
| 1158 | */ |
| 1159 | template <typename ObjT, typename LibObjT, typename LibStatusT, int OkStatus, int ErrorStatus> |
| 1160 | LibStatusT mapValueForEachLibFunc(const char * const key, LibObjT * const libObjPtr, |
| 1161 | void * const userData) |
| 1162 | { |
| 1163 | const auto& userFunc = *reinterpret_cast<const CommonMapValueForEachUserFunc<ObjT> *>(userData); |
| 1164 | |
| 1165 | try { |
| 1166 | userFunc(key, ObjT {libObjPtr}); |
| 1167 | } catch (...) { |
| 1168 | return static_cast<LibStatusT>(ErrorStatus); |
| 1169 | } |
| 1170 | |
| 1171 | return static_cast<LibStatusT>(OkStatus); |
| 1172 | } |
| 1173 | |
| 1174 | template <typename LibObjT> |
| 1175 | struct CommonMapValueSpec; |
| 1176 | |
| 1177 | /* Functions specific to mutable map values */ |
| 1178 | template <> |
| 1179 | struct CommonMapValueSpec<bt_value> final |
| 1180 | { |
| 1181 | static bt_value *entryByKey(bt_value * const libValPtr, const char * const key) noexcept |
| 1182 | { |
| 1183 | return bt_value_map_borrow_entry_value(libValPtr, key); |
| 1184 | } |
| 1185 | |
| 1186 | static void forEach(bt_value * const libValPtr, |
| 1187 | const CommonMapValueForEachUserFunc<Value>& func) |
| 1188 | { |
| 1189 | const auto status = bt_value_map_foreach_entry( |
| 1190 | libValPtr, |
| 1191 | mapValueForEachLibFunc<Value, bt_value, bt_value_map_foreach_entry_func_status, |
| 1192 | BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK, |
| 1193 | BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR>, |
| 1194 | const_cast<void *>(reinterpret_cast<const void *>(&func))); |
| 1195 | |
| 1196 | switch (status) { |
| 1197 | case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_OK: |
| 1198 | return; |
| 1199 | case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR: |
| 1200 | case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR: |
| 1201 | throw Error {}; |
| 1202 | default: |
| 1203 | bt_common_abort(); |
| 1204 | } |
| 1205 | } |
| 1206 | }; |
| 1207 | |
| 1208 | /* Functions specific to constant map values */ |
| 1209 | template <> |
| 1210 | struct CommonMapValueSpec<const bt_value> final |
| 1211 | { |
| 1212 | static const bt_value *entryByKey(const bt_value * const libValPtr, |
| 1213 | const char * const key) noexcept |
| 1214 | { |
| 1215 | return bt_value_map_borrow_entry_value_const(libValPtr, key); |
| 1216 | } |
| 1217 | |
| 1218 | static void forEach(const bt_value * const libValPtr, |
| 1219 | const CommonMapValueForEachUserFunc<ConstValue>& func) |
| 1220 | { |
| 1221 | const auto status = bt_value_map_foreach_entry_const( |
| 1222 | libValPtr, |
| 1223 | mapValueForEachLibFunc<ConstValue, const bt_value, |
| 1224 | bt_value_map_foreach_entry_const_func_status, |
| 1225 | BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK, |
| 1226 | BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_ERROR>, |
| 1227 | const_cast<void *>(reinterpret_cast<const void *>(&func))); |
| 1228 | |
| 1229 | switch (status) { |
| 1230 | case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK: |
| 1231 | return; |
| 1232 | case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_USER_ERROR: |
| 1233 | case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR: |
| 1234 | throw Error {}; |
| 1235 | default: |
| 1236 | bt_common_abort(); |
| 1237 | } |
| 1238 | } |
| 1239 | }; |
| 1240 | |
| 1241 | } /* namespace internal */ |
| 1242 | |
| 1243 | template <typename LibObjT> |
| 1244 | class CommonMapValue final : public CommonValue<LibObjT> |
| 1245 | { |
| 1246 | private: |
| 1247 | using typename CommonValue<LibObjT>::_ThisCommonValue; |
| 1248 | |
| 1249 | public: |
| 1250 | using typename CommonValue<LibObjT>::LibObjPtr; |
| 1251 | using Shared = SharedValue<CommonMapValue<LibObjT>, LibObjT>; |
| 1252 | |
| 1253 | explicit CommonMapValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} |
| 1254 | { |
| 1255 | BT_ASSERT_DBG(this->isMap()); |
| 1256 | } |
| 1257 | |
| 1258 | static Shared create() |
| 1259 | { |
| 1260 | const auto libObjPtr = bt_value_map_create(); |
| 1261 | |
| 1262 | internal::validateCreatedObjPtr(libObjPtr); |
| 1263 | return CommonMapValue::Shared::createWithoutRef(libObjPtr); |
| 1264 | } |
| 1265 | |
| 1266 | template <typename OtherLibObjT> |
| 1267 | CommonMapValue(const CommonMapValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val} |
| 1268 | { |
| 1269 | } |
| 1270 | |
| 1271 | template <typename OtherLibObjT> |
| 1272 | CommonMapValue<LibObjT> operator=(const CommonMapValue<OtherLibObjT> val) noexcept |
| 1273 | { |
| 1274 | _ThisCommonValue::operator=(val); |
| 1275 | return *this; |
| 1276 | } |
| 1277 | |
| 1278 | CommonMapValue<const bt_value> asConst() const noexcept |
| 1279 | { |
| 1280 | return CommonMapValue<const bt_value> {*this}; |
| 1281 | } |
| 1282 | |
| 1283 | std::uint64_t length() const noexcept |
| 1284 | { |
| 1285 | return bt_value_map_get_size(this->libObjPtr()); |
| 1286 | } |
| 1287 | |
| 1288 | bool isEmpty() const noexcept |
| 1289 | { |
| 1290 | return this->length() == 0; |
| 1291 | } |
| 1292 | |
| 1293 | OptionalBorrowedObject<CommonValue<LibObjT>> |
| 1294 | operator[](const bt2c::CStringView key) const noexcept |
| 1295 | { |
| 1296 | return internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key); |
| 1297 | } |
| 1298 | |
| 1299 | bool hasEntry(const bt2c::CStringView key) const noexcept |
| 1300 | { |
| 1301 | return static_cast<bool>(bt_value_map_has_entry(this->libObjPtr(), key)); |
| 1302 | } |
| 1303 | |
| 1304 | CommonMapValue insert(const bt2c::CStringView key, const Value val) const |
| 1305 | { |
| 1306 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`."); |
| 1307 | |
| 1308 | const auto status = bt_value_map_insert_entry(this->libObjPtr(), key, val.libObjPtr()); |
| 1309 | |
| 1310 | this->_handleInsertLibStatus(status); |
| 1311 | return *this; |
| 1312 | } |
| 1313 | |
| 1314 | CommonMapValue insert(const bt2c::CStringView key, const bool rawVal) const |
| 1315 | { |
| 1316 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`."); |
| 1317 | |
| 1318 | const auto status = |
| 1319 | bt_value_map_insert_bool_entry(this->libObjPtr(), key, static_cast<bt_bool>(rawVal)); |
| 1320 | |
| 1321 | this->_handleInsertLibStatus(status); |
| 1322 | return *this; |
| 1323 | } |
| 1324 | |
| 1325 | CommonMapValue insert(const bt2c::CStringView key, const std::uint64_t rawVal) const |
| 1326 | { |
| 1327 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`."); |
| 1328 | |
| 1329 | const auto status = |
| 1330 | bt_value_map_insert_unsigned_integer_entry(this->libObjPtr(), key, rawVal); |
| 1331 | |
| 1332 | this->_handleInsertLibStatus(status); |
| 1333 | return *this; |
| 1334 | } |
| 1335 | |
| 1336 | CommonMapValue insert(const bt2c::CStringView key, const std::int64_t rawVal) const |
| 1337 | { |
| 1338 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`."); |
| 1339 | |
| 1340 | const auto status = |
| 1341 | bt_value_map_insert_signed_integer_entry(this->libObjPtr(), key, rawVal); |
| 1342 | |
| 1343 | this->_handleInsertLibStatus(status); |
| 1344 | return *this; |
| 1345 | } |
| 1346 | |
| 1347 | CommonMapValue insert(const bt2c::CStringView key, const double rawVal) const |
| 1348 | { |
| 1349 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`."); |
| 1350 | |
| 1351 | const auto status = bt_value_map_insert_real_entry(this->libObjPtr(), key, rawVal); |
| 1352 | |
| 1353 | this->_handleInsertLibStatus(status); |
| 1354 | return *this; |
| 1355 | } |
| 1356 | |
| 1357 | CommonMapValue insert(const bt2c::CStringView key, const char *rawVal) const |
| 1358 | { |
| 1359 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`."); |
| 1360 | |
| 1361 | const auto status = bt_value_map_insert_string_entry(this->libObjPtr(), key, rawVal); |
| 1362 | |
| 1363 | this->_handleInsertLibStatus(status); |
| 1364 | return *this; |
| 1365 | } |
| 1366 | |
| 1367 | CommonMapValue insert(const bt2c::CStringView key, const bt2c::CStringView rawVal) const |
| 1368 | { |
| 1369 | return this->insert(key, rawVal.data()); |
| 1370 | } |
| 1371 | |
| 1372 | CommonArrayValue<bt_value> insertEmptyArray(bt2c::CStringView key) const; |
| 1373 | CommonMapValue<bt_value> insertEmptyMap(bt2c::CStringView key) const; |
| 1374 | |
| 1375 | CommonMapValue |
| 1376 | forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func) const |
| 1377 | { |
| 1378 | internal::CommonMapValueSpec<LibObjT>::forEach(this->libObjPtr(), func); |
| 1379 | return *this; |
| 1380 | } |
| 1381 | |
| 1382 | Shared shared() const noexcept |
| 1383 | { |
| 1384 | return Shared::createWithRef(*this); |
| 1385 | } |
| 1386 | |
| 1387 | private: |
| 1388 | void _handleInsertLibStatus(const bt_value_map_insert_entry_status status) const |
| 1389 | { |
| 1390 | if (status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_MEMORY_ERROR) { |
| 1391 | throw MemoryError {}; |
| 1392 | } |
| 1393 | } |
| 1394 | }; |
| 1395 | |
| 1396 | using MapValue = CommonMapValue<bt_value>; |
| 1397 | using ConstMapValue = CommonMapValue<const bt_value>; |
| 1398 | |
| 1399 | namespace internal { |
| 1400 | |
| 1401 | struct MapValueTypeDescr |
| 1402 | { |
| 1403 | using Const = ConstMapValue; |
| 1404 | using NonConst = MapValue; |
| 1405 | }; |
| 1406 | |
| 1407 | template <> |
| 1408 | struct TypeDescr<MapValue> : public MapValueTypeDescr |
| 1409 | { |
| 1410 | }; |
| 1411 | |
| 1412 | template <> |
| 1413 | struct TypeDescr<ConstMapValue> : public MapValueTypeDescr |
| 1414 | { |
| 1415 | }; |
| 1416 | |
| 1417 | } /* namespace internal */ |
| 1418 | |
| 1419 | template <typename LibObjT> |
| 1420 | ArrayValue CommonValue<LibObjT>::appendEmptyArray() const |
| 1421 | { |
| 1422 | return this->asArray().appendEmptyArray(); |
| 1423 | } |
| 1424 | |
| 1425 | template <typename LibObjT> |
| 1426 | MapValue CommonValue<LibObjT>::appendEmptyMap() const |
| 1427 | { |
| 1428 | return this->asArray().appendEmptyMap(); |
| 1429 | } |
| 1430 | |
| 1431 | template <typename LibObjT> |
| 1432 | ArrayValue CommonValue<LibObjT>::insertEmptyArray(const bt2c::CStringView key) const |
| 1433 | { |
| 1434 | return this->asMap().insertEmptyArray(key); |
| 1435 | } |
| 1436 | |
| 1437 | template <typename LibObjT> |
| 1438 | MapValue CommonValue<LibObjT>::insertEmptyMap(const bt2c::CStringView key) const |
| 1439 | { |
| 1440 | return this->asMap().insertEmptyMap(key); |
| 1441 | } |
| 1442 | |
| 1443 | template <typename LibObjT> |
| 1444 | CommonNullValue<LibObjT> CommonValue<LibObjT>::asNull() const noexcept |
| 1445 | { |
| 1446 | BT_ASSERT_DBG(this->isNull()); |
| 1447 | return CommonNullValue<LibObjT> {}; |
| 1448 | } |
| 1449 | |
| 1450 | template <typename LibObjT> |
| 1451 | CommonBoolValue<LibObjT> CommonValue<LibObjT>::asBool() const noexcept |
| 1452 | { |
| 1453 | return CommonBoolValue<LibObjT> {this->libObjPtr()}; |
| 1454 | } |
| 1455 | |
| 1456 | template <typename LibObjT> |
| 1457 | CommonSignedIntegerValue<LibObjT> CommonValue<LibObjT>::asSignedInteger() const noexcept |
| 1458 | { |
| 1459 | return CommonSignedIntegerValue<LibObjT> {this->libObjPtr()}; |
| 1460 | } |
| 1461 | |
| 1462 | template <typename LibObjT> |
| 1463 | CommonUnsignedIntegerValue<LibObjT> CommonValue<LibObjT>::asUnsignedInteger() const noexcept |
| 1464 | { |
| 1465 | return CommonUnsignedIntegerValue<LibObjT> {this->libObjPtr()}; |
| 1466 | } |
| 1467 | |
| 1468 | template <typename LibObjT> |
| 1469 | CommonRealValue<LibObjT> CommonValue<LibObjT>::asReal() const noexcept |
| 1470 | { |
| 1471 | return CommonRealValue<LibObjT> {this->libObjPtr()}; |
| 1472 | } |
| 1473 | |
| 1474 | template <typename LibObjT> |
| 1475 | CommonStringValue<LibObjT> CommonValue<LibObjT>::asString() const noexcept |
| 1476 | { |
| 1477 | return CommonStringValue<LibObjT> {this->libObjPtr()}; |
| 1478 | } |
| 1479 | |
| 1480 | template <typename LibObjT> |
| 1481 | CommonArrayValue<LibObjT> CommonValue<LibObjT>::asArray() const noexcept |
| 1482 | { |
| 1483 | return CommonArrayValue<LibObjT> {this->libObjPtr()}; |
| 1484 | } |
| 1485 | |
| 1486 | template <typename LibObjT> |
| 1487 | CommonMapValue<LibObjT> CommonValue<LibObjT>::asMap() const noexcept |
| 1488 | { |
| 1489 | return CommonMapValue<LibObjT> {this->libObjPtr()}; |
| 1490 | } |
| 1491 | |
| 1492 | template <typename LibObjT> |
| 1493 | ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray() const |
| 1494 | { |
| 1495 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`."); |
| 1496 | |
| 1497 | bt_value *libElemPtr; |
| 1498 | const auto status = bt_value_array_append_empty_array_element(this->libObjPtr(), &libElemPtr); |
| 1499 | |
| 1500 | this->_handleAppendLibStatus(status); |
| 1501 | return ArrayValue {libElemPtr}; |
| 1502 | } |
| 1503 | |
| 1504 | template <typename LibObjT> |
| 1505 | MapValue CommonArrayValue<LibObjT>::appendEmptyMap() const |
| 1506 | { |
| 1507 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`."); |
| 1508 | |
| 1509 | bt_value *libElemPtr; |
| 1510 | const auto status = bt_value_array_append_empty_map_element(this->libObjPtr(), &libElemPtr); |
| 1511 | |
| 1512 | this->_handleAppendLibStatus(status); |
| 1513 | return MapValue {libElemPtr}; |
| 1514 | } |
| 1515 | |
| 1516 | template <typename LibObjT> |
| 1517 | ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const bt2c::CStringView key) const |
| 1518 | { |
| 1519 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`."); |
| 1520 | |
| 1521 | bt_value *libEntryPtr; |
| 1522 | const auto status = bt_value_map_insert_empty_array_entry(this->libObjPtr(), key, &libEntryPtr); |
| 1523 | |
| 1524 | this->_handleInsertLibStatus(status); |
| 1525 | return ArrayValue {libEntryPtr}; |
| 1526 | } |
| 1527 | |
| 1528 | template <typename LibObjT> |
| 1529 | MapValue CommonMapValue<LibObjT>::insertEmptyMap(const bt2c::CStringView key) const |
| 1530 | { |
| 1531 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`."); |
| 1532 | |
| 1533 | bt_value *libEntryPtr; |
| 1534 | const auto status = bt_value_map_insert_empty_map_entry(this->libObjPtr(), key, &libEntryPtr); |
| 1535 | |
| 1536 | this->_handleInsertLibStatus(status); |
| 1537 | return MapValue {libEntryPtr}; |
| 1538 | } |
| 1539 | |
| 1540 | inline BoolValue::Shared createValue(const bool rawVal) |
| 1541 | { |
| 1542 | return BoolValue::create(rawVal); |
| 1543 | } |
| 1544 | |
| 1545 | inline UnsignedIntegerValue::Shared createValue(const std::uint64_t rawVal) |
| 1546 | { |
| 1547 | return UnsignedIntegerValue::create(rawVal); |
| 1548 | } |
| 1549 | |
| 1550 | inline SignedIntegerValue::Shared createValue(const std::int64_t rawVal) |
| 1551 | { |
| 1552 | return SignedIntegerValue::create(rawVal); |
| 1553 | } |
| 1554 | |
| 1555 | inline RealValue::Shared createValue(const double rawVal) |
| 1556 | { |
| 1557 | return RealValue::create(rawVal); |
| 1558 | } |
| 1559 | |
| 1560 | inline StringValue::Shared createValue(const char * const rawVal) |
| 1561 | { |
| 1562 | return StringValue::create(rawVal); |
| 1563 | } |
| 1564 | |
| 1565 | inline StringValue::Shared createValue(const bt2c::CStringView rawVal) |
| 1566 | { |
| 1567 | return StringValue::create(rawVal); |
| 1568 | } |
| 1569 | |
| 1570 | } /* namespace bt2 */ |
| 1571 | |
| 1572 | #endif /* BABELTRACE_CPP_COMMON_BT2_VALUE_HPP */ |