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