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