2 * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_VALUE_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_VALUE_HPP
12 #include <type_traits>
14 #include <babeltrace2/babeltrace.h>
16 #include "common/assert.h"
17 #include "common/common.h"
18 #include "cpp-common/bt2c/c-string-view.hpp"
20 #include "borrowed-object-iterator.hpp"
21 #include "borrowed-object.hpp"
23 #include "internal/utils.hpp"
24 #include "optional-borrowed-object.hpp"
25 #include "raw-value-proxy.hpp"
26 #include "shared-object.hpp"
31 struct ValueRefFuncs final
33 static void get(const bt_value * const libObjPtr) noexcept
35 bt_value_get_ref(libObjPtr);
38 static void put(const bt_value * const libObjPtr) noexcept
40 bt_value_put_ref(libObjPtr);
44 } /* namespace internal */
46 template <typename ObjT, typename LibObjT>
47 using SharedValue = SharedObject<ObjT, LibObjT, internal::ValueRefFuncs>;
49 template <typename LibObjT>
50 class CommonNullValue;
52 template <typename LibObjT>
53 class CommonBoolValue;
55 template <typename LibObjT>
56 class CommonUnsignedIntegerValue;
58 template <typename LibObjT>
59 class CommonSignedIntegerValue;
61 template <typename LibObjT>
62 class CommonRealValue;
64 template <typename LibObjT>
65 class CommonStringValue;
67 template <typename LibObjT>
68 class CommonArrayValue;
70 template <typename LibObjT>
75 Null = BT_VALUE_TYPE_NULL,
76 Bool = BT_VALUE_TYPE_BOOL,
77 UnsignedInteger = BT_VALUE_TYPE_UNSIGNED_INTEGER,
78 SignedInteger = 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,
85 template <typename ValueObjT>
86 class CommonValueRawValueProxy final
89 explicit CommonValueRawValueProxy(const ValueObjT obj) : _mObj {obj}
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;
109 template <typename LibObjT>
110 class CommonValue : public BorrowedObject<LibObjT>
113 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
116 using _ThisCommonValue = CommonValue<LibObjT>;
119 using typename BorrowedObject<LibObjT>::LibObjPtr;
120 using Shared = SharedValue<CommonValue<LibObjT>, LibObjT>;
122 explicit CommonValue(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
126 template <typename OtherLibObjT>
127 CommonValue(const CommonValue<OtherLibObjT> val) noexcept : _ThisBorrowedObject {val}
131 template <typename OtherLibObjT>
132 _ThisCommonValue operator=(const CommonValue<OtherLibObjT> val) noexcept
134 _ThisBorrowedObject::operator=(val);
138 CommonValue<const bt_value> asConst() const noexcept
140 return CommonValue<const bt_value> {*this};
143 ValueType type() const noexcept
145 return static_cast<ValueType>(bt_value_get_type(this->libObjPtr()));
148 bool isNull() const noexcept
150 return this->_libTypeIs(BT_VALUE_TYPE_NULL);
153 bool isBool() const noexcept
155 return this->_libTypeIs(BT_VALUE_TYPE_BOOL);
158 bool isInteger() const noexcept
160 return this->_libTypeIs(BT_VALUE_TYPE_INTEGER);
163 bool isUnsignedInteger() const noexcept
165 return this->_libTypeIs(BT_VALUE_TYPE_UNSIGNED_INTEGER);
168 bool isSignedInteger() const noexcept
170 return this->_libTypeIs(BT_VALUE_TYPE_SIGNED_INTEGER);
173 bool isReal() const noexcept
175 return this->_libTypeIs(BT_VALUE_TYPE_REAL);
178 bool isString() const noexcept
180 return this->_libTypeIs(BT_VALUE_TYPE_STRING);
183 bool isArray() const noexcept
185 return this->_libTypeIs(BT_VALUE_TYPE_ARRAY);
188 bool isMap() const noexcept
190 return this->_libTypeIs(BT_VALUE_TYPE_MAP);
193 template <typename OtherLibObjT>
194 bool operator==(const CommonValue<OtherLibObjT> other) const noexcept
196 return static_cast<bool>(bt_value_is_equal(this->libObjPtr(), other.libObjPtr()));
199 template <typename OtherLibObjT>
200 bool operator!=(const CommonValue<OtherLibObjT> other) const noexcept
202 return !(*this == other);
205 CommonValueRawValueProxy<CommonValue> operator*() const noexcept
207 return CommonValueRawValueProxy<CommonValue> {*this};
210 std::uint64_t arrayLength() const noexcept
212 return this->asArray().length();
215 bool arrayIsEmpty() const noexcept
217 return this->asArray().isEmpty();
220 CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept
222 return this->asArray()[index];
225 template <typename T>
226 void append(T&& elem) const
228 this->asArray().append(std::forward<T>(elem));
231 CommonArrayValue<bt_value> appendEmptyArray() const;
232 CommonMapValue<bt_value> appendEmptyMap() const;
234 std::uint64_t mapLength() const noexcept
236 return this->asMap().length();
239 bool mapIsEmpty() const noexcept
241 return this->asMap().isEmpty();
244 template <typename KeyT>
245 OptionalBorrowedObject<CommonValue<LibObjT>> operator[](KeyT&& key) const noexcept
247 return this->asMap()[std::forward<KeyT>(key)];
250 template <typename KeyT>
251 bool hasEntry(KeyT&& key) const noexcept
253 return this->asMap().hasEntry(std::forward<KeyT>(key));
256 template <typename KeyT, typename ValT>
257 void insert(KeyT&& key, ValT&& val) const
259 this->asMap().insert(std::forward<KeyT>(key), std::forward<ValT>(val));
262 CommonArrayValue<bt_value> insertEmptyArray(bt2c::CStringView key) const;
263 CommonMapValue<bt_value> insertEmptyMap(bt2c::CStringView key) const;
265 Shared shared() const noexcept
267 return Shared::createWithRef(*this);
270 template <typename ValueT>
271 ValueT as() const noexcept
273 return ValueT {this->libObjPtr()};
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;
286 bool _libTypeIs(const bt_value_type type) const noexcept
288 return bt_value_type_is(bt_value_get_type(this->libObjPtr()), type);
292 using Value = CommonValue<bt_value>;
293 using ConstValue = CommonValue<const bt_value>;
295 template <typename ValueObjT>
296 CommonValueRawValueProxy<ValueObjT>&
297 CommonValueRawValueProxy<ValueObjT>::operator=(const bool rawVal) noexcept
299 _mObj.asBool().value(rawVal);
303 template <typename ValueObjT>
304 CommonValueRawValueProxy<ValueObjT>&
305 CommonValueRawValueProxy<ValueObjT>::operator=(const std::int64_t rawVal) noexcept
307 _mObj.asSignedInteger().value(rawVal);
311 template <typename ValueObjT>
312 CommonValueRawValueProxy<ValueObjT>&
313 CommonValueRawValueProxy<ValueObjT>::operator=(const std::uint64_t rawVal) noexcept
315 _mObj.asUnsignedInteger().value(rawVal);
319 template <typename ValueObjT>
320 CommonValueRawValueProxy<ValueObjT>&
321 CommonValueRawValueProxy<ValueObjT>::operator=(const double rawVal) noexcept
323 _mObj.asReal().value(rawVal);
327 template <typename ValueObjT>
328 CommonValueRawValueProxy<ValueObjT>&
329 CommonValueRawValueProxy<ValueObjT>::operator=(const char * const rawVal)
331 _mObj.asString().value(rawVal);
335 template <typename ValueObjT>
336 CommonValueRawValueProxy<ValueObjT>&
337 CommonValueRawValueProxy<ValueObjT>::operator=(const bt2c::CStringView rawVal)
339 _mObj.asString().value(rawVal);
343 template <typename ValueObjT>
344 CommonValueRawValueProxy<ValueObjT>::operator bool() const noexcept
346 return _mObj.asBool().value();
349 template <typename ValueObjT>
350 CommonValueRawValueProxy<ValueObjT>::operator std::int64_t() const noexcept
352 return _mObj.asSignedInteger().value();
355 template <typename ValueObjT>
356 CommonValueRawValueProxy<ValueObjT>::operator std::uint64_t() const noexcept
358 return _mObj.asUnsignedInteger().value();
361 template <typename ValueObjT>
362 CommonValueRawValueProxy<ValueObjT>::operator double() const noexcept
364 return _mObj.asReal().value();
367 template <typename ValueObjT>
368 CommonValueRawValueProxy<ValueObjT>::operator bt2c::CStringView() const noexcept
370 return _mObj.asString().value();
375 struct ValueTypeDescr
377 using Const = ConstValue;
378 using NonConst = Value;
382 struct TypeDescr<Value> : public ValueTypeDescr
387 struct TypeDescr<ConstValue> : public ValueTypeDescr
391 } /* namespace internal */
393 template <typename LibObjT>
394 class CommonNullValue final : public CommonValue<LibObjT>
397 using typename CommonValue<LibObjT>::_ThisCommonValue;
400 using Shared = SharedValue<CommonNullValue<LibObjT>, LibObjT>;
402 CommonNullValue() noexcept : _ThisCommonValue {bt_value_null}
406 template <typename OtherLibObjT>
407 CommonNullValue(const CommonNullValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
411 template <typename OtherLibObjT>
412 CommonNullValue<LibObjT> operator=(const CommonNullValue<OtherLibObjT> val) noexcept
414 _ThisCommonValue::operator=(val);
418 CommonNullValue<const bt_value> asConst() const noexcept
420 return CommonNullValue<const bt_value> {*this};
423 Shared shared() const noexcept
425 return Shared::createWithRef(*this);
429 using NullValue = CommonNullValue<bt_value>;
430 using ConstNullValue = CommonNullValue<const bt_value>;
434 struct NullValueTypeDescr
436 using Const = ConstNullValue;
437 using NonConst = NullValue;
441 struct TypeDescr<NullValue> : public NullValueTypeDescr
446 struct TypeDescr<ConstNullValue> : public NullValueTypeDescr
450 } /* namespace internal */
452 template <typename LibObjT>
453 class CommonBoolValue final : public CommonValue<LibObjT>
456 using typename CommonValue<LibObjT>::_ThisCommonValue;
459 using typename CommonValue<LibObjT>::LibObjPtr;
460 using Shared = SharedValue<CommonBoolValue<LibObjT>, LibObjT>;
463 explicit CommonBoolValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
465 BT_ASSERT_DBG(this->isBool());
468 template <typename OtherLibObjT>
469 CommonBoolValue(const CommonBoolValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
473 static Shared create(const Value rawVal = false)
475 const auto libObjPtr = bt_value_bool_create_init(static_cast<bt_bool>(rawVal));
477 internal::validateCreatedObjPtr(libObjPtr);
478 return CommonBoolValue::Shared::createWithoutRef(libObjPtr);
481 template <typename OtherLibObjT>
482 CommonBoolValue<LibObjT> operator=(const CommonBoolValue<OtherLibObjT> val) noexcept
484 _ThisCommonValue::operator=(val);
488 CommonBoolValue<const bt_value> asConst() const noexcept
490 return CommonBoolValue<const bt_value> {*this};
493 RawValueProxy<CommonBoolValue> operator*() const noexcept
495 return RawValueProxy<CommonBoolValue> {*this};
498 Value value() const noexcept
500 return static_cast<Value>(bt_value_bool_get(this->libObjPtr()));
503 CommonBoolValue value(const Value val) const noexcept
505 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstBoolValue`.");
507 bt_value_bool_set(this->libObjPtr(), static_cast<bt_bool>(val));
511 Shared shared() const noexcept
513 return Shared::createWithRef(*this);
517 using BoolValue = CommonBoolValue<bt_value>;
518 using ConstBoolValue = CommonBoolValue<const bt_value>;
522 struct BoolValueTypeDescr
524 using Const = ConstBoolValue;
525 using NonConst = BoolValue;
529 struct TypeDescr<BoolValue> : public BoolValueTypeDescr
534 struct TypeDescr<ConstBoolValue> : public BoolValueTypeDescr
538 } /* namespace internal */
540 template <typename LibObjT>
541 class CommonUnsignedIntegerValue final : public CommonValue<LibObjT>
544 using typename CommonValue<LibObjT>::_ThisCommonValue;
547 using typename CommonValue<LibObjT>::LibObjPtr;
548 using Shared = SharedValue<CommonUnsignedIntegerValue<LibObjT>, LibObjT>;
549 using Value = std::uint64_t;
551 explicit CommonUnsignedIntegerValue(const LibObjPtr libObjPtr) noexcept :
552 _ThisCommonValue {libObjPtr}
554 BT_ASSERT_DBG(this->isUnsignedInteger());
557 static Shared create(const Value rawVal = 0)
559 const auto libObjPtr = bt_value_integer_unsigned_create_init(rawVal);
561 internal::validateCreatedObjPtr(libObjPtr);
562 return CommonUnsignedIntegerValue::Shared::createWithoutRef(libObjPtr);
565 template <typename OtherLibObjT>
566 CommonUnsignedIntegerValue(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept :
567 _ThisCommonValue {val}
571 template <typename OtherLibObjT>
572 CommonUnsignedIntegerValue<LibObjT>
573 operator=(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept
575 _ThisCommonValue::operator=(val);
579 CommonUnsignedIntegerValue<const bt_value> asConst() const noexcept
581 return CommonUnsignedIntegerValue<const bt_value> {*this};
584 RawValueProxy<CommonUnsignedIntegerValue> operator*() const noexcept
586 return RawValueProxy<CommonUnsignedIntegerValue> {*this};
589 CommonUnsignedIntegerValue value(const Value val) const noexcept
591 static_assert(!std::is_const<LibObjT>::value,
592 "Not available with `bt2::ConstUnsignedIntegerValue`.");
594 bt_value_integer_unsigned_set(this->libObjPtr(), val);
598 Value value() const noexcept
600 return bt_value_integer_unsigned_get(this->libObjPtr());
603 Shared shared() const noexcept
605 return Shared::createWithRef(*this);
609 using UnsignedIntegerValue = CommonUnsignedIntegerValue<bt_value>;
610 using ConstUnsignedIntegerValue = CommonUnsignedIntegerValue<const bt_value>;
614 struct UnsignedIntegerValueTypeDescr
616 using Const = ConstUnsignedIntegerValue;
617 using NonConst = UnsignedIntegerValue;
621 struct TypeDescr<UnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
626 struct TypeDescr<ConstUnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
630 } /* namespace internal */
632 template <typename LibObjT>
633 class CommonSignedIntegerValue final : public CommonValue<LibObjT>
636 using typename CommonValue<LibObjT>::_ThisCommonValue;
639 using typename CommonValue<LibObjT>::LibObjPtr;
640 using Shared = SharedValue<CommonSignedIntegerValue<LibObjT>, LibObjT>;
641 using Value = std::int64_t;
643 explicit CommonSignedIntegerValue(const LibObjPtr libObjPtr) noexcept :
644 _ThisCommonValue {libObjPtr}
646 BT_ASSERT_DBG(this->isSignedInteger());
649 static Shared create(const Value rawVal = 0)
651 const auto libObjPtr = bt_value_integer_signed_create_init(rawVal);
653 internal::validateCreatedObjPtr(libObjPtr);
654 return CommonSignedIntegerValue::Shared::createWithoutRef(libObjPtr);
657 template <typename OtherLibObjT>
658 CommonSignedIntegerValue(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept :
659 _ThisCommonValue {val}
663 template <typename OtherLibObjT>
664 CommonSignedIntegerValue<LibObjT>
665 operator=(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept
667 _ThisCommonValue::operator=(val);
671 CommonSignedIntegerValue<const bt_value> asConst() const noexcept
673 return CommonSignedIntegerValue<const bt_value> {*this};
676 RawValueProxy<CommonSignedIntegerValue> operator*() const noexcept
678 return RawValueProxy<CommonSignedIntegerValue> {*this};
681 CommonSignedIntegerValue value(const Value val) const noexcept
683 static_assert(!std::is_const<LibObjT>::value,
684 "Not available with `bt2::ConstSignedIntegerValue`.");
686 bt_value_integer_signed_set(this->libObjPtr(), val);
690 Value value() const noexcept
692 return bt_value_integer_signed_get(this->libObjPtr());
695 Shared shared() const noexcept
697 return Shared::createWithRef(*this);
701 using SignedIntegerValue = CommonSignedIntegerValue<bt_value>;
702 using ConstSignedIntegerValue = CommonSignedIntegerValue<const bt_value>;
706 struct SignedIntegerValueTypeDescr
708 using Const = ConstSignedIntegerValue;
709 using NonConst = SignedIntegerValue;
713 struct TypeDescr<SignedIntegerValue> : public SignedIntegerValueTypeDescr
718 struct TypeDescr<ConstSignedIntegerValue> : public SignedIntegerValueTypeDescr
722 } /* namespace internal */
724 template <typename LibObjT>
725 class CommonRealValue final : public CommonValue<LibObjT>
728 using typename CommonValue<LibObjT>::_ThisCommonValue;
731 using typename CommonValue<LibObjT>::LibObjPtr;
732 using Shared = SharedValue<CommonRealValue<LibObjT>, LibObjT>;
733 using Value = double;
735 explicit CommonRealValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
737 BT_ASSERT_DBG(this->isReal());
740 static Shared create(const Value rawVal = 0)
742 const auto libObjPtr = bt_value_real_create_init(rawVal);
744 internal::validateCreatedObjPtr(libObjPtr);
745 return CommonRealValue::Shared::createWithoutRef(libObjPtr);
748 template <typename OtherLibObjT>
749 CommonRealValue(const CommonRealValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
753 template <typename OtherLibObjT>
754 CommonRealValue<LibObjT> operator=(const CommonRealValue<OtherLibObjT> val) noexcept
756 _ThisCommonValue::operator=(val);
760 CommonRealValue<const bt_value> asConst() const noexcept
762 return CommonRealValue<const bt_value> {*this};
765 RawValueProxy<CommonRealValue> operator*() const noexcept
767 return RawValueProxy<CommonRealValue> {*this};
770 CommonRealValue value(const Value val) const noexcept
772 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstRealValue`.");
774 bt_value_real_set(this->libObjPtr(), val);
778 Value value() const noexcept
780 return bt_value_real_get(this->libObjPtr());
783 Shared shared() const noexcept
785 return Shared::createWithRef(*this);
789 using RealValue = CommonRealValue<bt_value>;
790 using ConstRealValue = CommonRealValue<const bt_value>;
794 struct RealValueTypeDescr
796 using Const = ConstRealValue;
797 using NonConst = RealValue;
801 struct TypeDescr<RealValue> : public RealValueTypeDescr
806 struct TypeDescr<ConstRealValue> : public RealValueTypeDescr
810 } /* namespace internal */
812 template <typename LibObjT>
813 class CommonStringValue final : public CommonValue<LibObjT>
816 using typename CommonValue<LibObjT>::_ThisCommonValue;
819 using typename CommonValue<LibObjT>::LibObjPtr;
820 using Shared = SharedValue<CommonStringValue<LibObjT>, LibObjT>;
821 using Value = bt2c::CStringView;
823 explicit CommonStringValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
825 BT_ASSERT_DBG(this->isString());
828 static Shared create(const bt2c::CStringView rawVal = "")
830 const auto libObjPtr = bt_value_string_create_init(rawVal);
832 internal::validateCreatedObjPtr(libObjPtr);
833 return CommonStringValue::Shared::createWithoutRef(libObjPtr);
836 template <typename OtherLibObjT>
837 CommonStringValue(const CommonStringValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
841 template <typename OtherLibObjT>
842 CommonStringValue<LibObjT> operator=(const CommonStringValue<OtherLibObjT> val) noexcept
844 _ThisCommonValue::operator=(val);
848 CommonStringValue<const bt_value> asConst() const noexcept
850 return CommonStringValue<const bt_value> {*this};
853 RawValueProxy<CommonStringValue> operator*() const noexcept
855 return RawValueProxy<CommonStringValue> {*this};
858 CommonStringValue value(const Value val) const
860 static_assert(!std::is_const<LibObjT>::value,
861 "Not available with `bt2::ConstStringValue`.");
863 const auto status = bt_value_string_set(this->libObjPtr(), *val);
865 if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) {
866 throw MemoryError {};
872 Value value() const noexcept
874 return bt_value_string_get(this->libObjPtr());
877 Shared shared() const noexcept
879 return Shared::createWithRef(*this);
883 using StringValue = CommonStringValue<bt_value>;
884 using ConstStringValue = CommonStringValue<const bt_value>;
888 struct StringValueTypeDescr
890 using Const = ConstStringValue;
891 using NonConst = StringValue;
895 struct TypeDescr<StringValue> : public StringValueTypeDescr
900 struct TypeDescr<ConstStringValue> : public StringValueTypeDescr
904 template <typename LibObjT>
905 struct CommonArrayValueSpec;
907 /* Functions specific to mutable array values */
909 struct CommonArrayValueSpec<bt_value> final
911 static bt_value *elementByIndex(bt_value * const libValPtr, const std::uint64_t index) noexcept
913 return bt_value_array_borrow_element_by_index(libValPtr, index);
917 /* Functions specific to constant array values */
919 struct CommonArrayValueSpec<const bt_value> final
921 static const bt_value *elementByIndex(const bt_value * const libValPtr,
922 const std::uint64_t index) noexcept
924 return bt_value_array_borrow_element_by_index_const(libValPtr, index);
928 } /* namespace internal */
930 template <typename LibObjT>
931 class CommonArrayValue final : public CommonValue<LibObjT>
934 using typename CommonValue<LibObjT>::_ThisCommonValue;
937 using typename CommonValue<LibObjT>::LibObjPtr;
938 using Shared = SharedValue<CommonArrayValue<LibObjT>, LibObjT>;
939 using Iterator = BorrowedObjectIterator<CommonArrayValue<LibObjT>>;
941 explicit CommonArrayValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
943 BT_ASSERT_DBG(this->isArray());
946 static Shared create()
948 const auto libObjPtr = bt_value_array_create();
950 internal::validateCreatedObjPtr(libObjPtr);
951 return CommonArrayValue::Shared::createWithoutRef(libObjPtr);
954 template <typename OtherLibObjT>
955 CommonArrayValue(const CommonArrayValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
959 template <typename OtherLibObjT>
960 CommonArrayValue<LibObjT> operator=(const CommonArrayValue<OtherLibObjT> val) noexcept
962 _ThisCommonValue::operator=(val);
966 CommonArrayValue<const bt_value> asConst() const noexcept
968 return CommonArrayValue<const bt_value> {*this};
971 std::uint64_t length() const noexcept
973 return bt_value_array_get_length(this->libObjPtr());
976 Iterator begin() const noexcept
978 return Iterator {*this, 0};
981 Iterator end() const noexcept
983 return Iterator {*this, this->length()};
986 bool isEmpty() const noexcept
988 return this->length() == 0;
991 CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept
993 return CommonValue<LibObjT> {
994 internal::CommonArrayValueSpec<LibObjT>::elementByIndex(this->libObjPtr(), index)};
997 CommonArrayValue append(const Value val) const
999 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1001 const auto status = bt_value_array_append_element(this->libObjPtr(), val.libObjPtr());
1003 this->_handleAppendLibStatus(status);
1007 CommonArrayValue append(const bool rawVal) const
1009 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1012 bt_value_array_append_bool_element(this->libObjPtr(), static_cast<bt_bool>(rawVal));
1014 this->_handleAppendLibStatus(status);
1018 CommonArrayValue append(const std::uint64_t rawVal) const
1020 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1023 bt_value_array_append_unsigned_integer_element(this->libObjPtr(), rawVal);
1025 this->_handleAppendLibStatus(status);
1029 CommonArrayValue append(const std::int64_t rawVal) const
1031 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1033 const auto status = bt_value_array_append_signed_integer_element(this->libObjPtr(), rawVal);
1035 this->_handleAppendLibStatus(status);
1039 CommonArrayValue append(const double rawVal) const
1041 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1043 const auto status = bt_value_array_append_real_element(this->libObjPtr(), rawVal);
1045 this->_handleAppendLibStatus(status);
1049 CommonArrayValue append(const char * const rawVal) const
1051 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1053 const auto status = bt_value_array_append_string_element(this->libObjPtr(), rawVal);
1055 this->_handleAppendLibStatus(status);
1059 CommonArrayValue append(const bt2c::CStringView rawVal) const
1061 return this->append(rawVal.data());
1064 CommonArrayValue<bt_value> appendEmptyArray() const;
1065 CommonMapValue<bt_value> appendEmptyMap() const;
1067 void operator+=(const Value val) const
1072 void operator+=(const bool rawVal) const
1074 this->append(rawVal);
1077 void operator+=(const std::uint64_t rawVal) const
1079 this->append(rawVal);
1082 void operator+=(const std::int64_t rawVal) const
1084 this->append(rawVal);
1087 void operator+=(const double rawVal) const
1089 this->append(rawVal);
1092 void operator+=(const char * const rawVal) const
1094 this->append(rawVal);
1097 void operator+=(const bt2c::CStringView rawVal) const
1099 this->append(rawVal);
1102 Shared shared() const noexcept
1104 return Shared::createWithRef(*this);
1108 void _handleAppendLibStatus(const bt_value_array_append_element_status status) const
1110 if (status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR) {
1111 throw MemoryError {};
1116 using ArrayValue = CommonArrayValue<bt_value>;
1117 using ConstArrayValue = CommonArrayValue<const bt_value>;
1119 namespace internal {
1121 struct ArrayValueTypeDescr
1123 using Const = ConstArrayValue;
1124 using NonConst = ArrayValue;
1128 struct TypeDescr<ArrayValue> : public ArrayValueTypeDescr
1133 struct TypeDescr<ConstArrayValue> : public ArrayValueTypeDescr
1138 * Type of a user function passed to `CommonMapValue<ObjT>::forEach()`.
1140 * First argument is the entry's key, second is its value.
1142 template <typename ObjT>
1143 using CommonMapValueForEachUserFunc = std::function<void(bt2c::CStringView, ObjT)>;
1146 * Template of a function to be passed to bt_value_map_foreach_entry()
1147 * for bt_value_map_foreach_entry_const() which calls a user function.
1149 * `userData` is casted to a `const` pointer to
1150 * `CommonMapValueForEachUserFunc<ObjT>` (the user function to call).
1152 * This function catches any exception which the user function throws
1153 * and returns the `ErrorStatus` value. If there's no exception, this
1154 * function returns the `OkStatus` value.
1156 template <typename ObjT, typename LibObjT, typename LibStatusT, int OkStatus, int ErrorStatus>
1157 LibStatusT mapValueForEachLibFunc(const char * const key, LibObjT * const libObjPtr,
1158 void * const userData)
1160 const auto& userFunc = *reinterpret_cast<const CommonMapValueForEachUserFunc<ObjT> *>(userData);
1163 userFunc(key, ObjT {libObjPtr});
1165 return static_cast<LibStatusT>(ErrorStatus);
1168 return static_cast<LibStatusT>(OkStatus);
1171 template <typename LibObjT>
1172 struct CommonMapValueSpec;
1174 /* Functions specific to mutable map values */
1176 struct CommonMapValueSpec<bt_value> final
1178 static bt_value *entryByKey(bt_value * const libValPtr, const char * const key) noexcept
1180 return bt_value_map_borrow_entry_value(libValPtr, key);
1183 static void forEach(bt_value * const libValPtr,
1184 const CommonMapValueForEachUserFunc<Value>& func)
1186 const auto status = bt_value_map_foreach_entry(
1188 mapValueForEachLibFunc<Value, bt_value, bt_value_map_foreach_entry_func_status,
1189 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK,
1190 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR>,
1191 const_cast<void *>(reinterpret_cast<const void *>(&func)));
1194 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_OK:
1196 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR:
1197 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR:
1205 /* Functions specific to constant map values */
1207 struct CommonMapValueSpec<const bt_value> final
1209 static const bt_value *entryByKey(const bt_value * const libValPtr,
1210 const char * const key) noexcept
1212 return bt_value_map_borrow_entry_value_const(libValPtr, key);
1215 static void forEach(const bt_value * const libValPtr,
1216 const CommonMapValueForEachUserFunc<ConstValue>& func)
1218 const auto status = bt_value_map_foreach_entry_const(
1220 mapValueForEachLibFunc<ConstValue, const bt_value,
1221 bt_value_map_foreach_entry_const_func_status,
1222 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK,
1223 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_ERROR>,
1224 const_cast<void *>(reinterpret_cast<const void *>(&func)));
1227 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK:
1229 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_USER_ERROR:
1230 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR:
1238 } /* namespace internal */
1240 template <typename LibObjT>
1241 class CommonMapValue final : public CommonValue<LibObjT>
1244 using typename CommonValue<LibObjT>::_ThisCommonValue;
1247 using typename CommonValue<LibObjT>::LibObjPtr;
1248 using Shared = SharedValue<CommonMapValue<LibObjT>, LibObjT>;
1250 explicit CommonMapValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
1252 BT_ASSERT_DBG(this->isMap());
1255 static Shared create()
1257 const auto libObjPtr = bt_value_map_create();
1259 internal::validateCreatedObjPtr(libObjPtr);
1260 return CommonMapValue::Shared::createWithoutRef(libObjPtr);
1263 template <typename OtherLibObjT>
1264 CommonMapValue(const CommonMapValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
1268 template <typename OtherLibObjT>
1269 CommonMapValue<LibObjT> operator=(const CommonMapValue<OtherLibObjT> val) noexcept
1271 _ThisCommonValue::operator=(val);
1275 CommonMapValue<const bt_value> asConst() const noexcept
1277 return CommonMapValue<const bt_value> {*this};
1280 std::uint64_t length() const noexcept
1282 return bt_value_map_get_size(this->libObjPtr());
1285 bool isEmpty() const noexcept
1287 return this->length() == 0;
1290 OptionalBorrowedObject<CommonValue<LibObjT>>
1291 operator[](const bt2c::CStringView key) const noexcept
1293 return internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key);
1296 bool hasEntry(const bt2c::CStringView key) const noexcept
1298 return static_cast<bool>(bt_value_map_has_entry(this->libObjPtr(), key));
1301 CommonMapValue insert(const bt2c::CStringView key, const Value val) const
1303 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1305 const auto status = bt_value_map_insert_entry(this->libObjPtr(), key, val.libObjPtr());
1307 this->_handleInsertLibStatus(status);
1311 CommonMapValue insert(const bt2c::CStringView key, const bool rawVal) const
1313 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1316 bt_value_map_insert_bool_entry(this->libObjPtr(), key, static_cast<bt_bool>(rawVal));
1318 this->_handleInsertLibStatus(status);
1322 CommonMapValue insert(const bt2c::CStringView key, const std::uint64_t rawVal) const
1324 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1327 bt_value_map_insert_unsigned_integer_entry(this->libObjPtr(), key, rawVal);
1329 this->_handleInsertLibStatus(status);
1333 CommonMapValue insert(const bt2c::CStringView key, const std::int64_t rawVal) const
1335 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1338 bt_value_map_insert_signed_integer_entry(this->libObjPtr(), key, rawVal);
1340 this->_handleInsertLibStatus(status);
1344 CommonMapValue insert(const bt2c::CStringView key, const double rawVal) const
1346 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1348 const auto status = bt_value_map_insert_real_entry(this->libObjPtr(), key, rawVal);
1350 this->_handleInsertLibStatus(status);
1354 CommonMapValue insert(const bt2c::CStringView key, const char *rawVal) const
1356 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1358 const auto status = bt_value_map_insert_string_entry(this->libObjPtr(), key, rawVal);
1360 this->_handleInsertLibStatus(status);
1364 CommonMapValue insert(const bt2c::CStringView key, const bt2c::CStringView rawVal) const
1366 return this->insert(key, rawVal.data());
1369 CommonArrayValue<bt_value> insertEmptyArray(bt2c::CStringView key) const;
1370 CommonMapValue<bt_value> insertEmptyMap(bt2c::CStringView key) const;
1373 forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func) const
1375 internal::CommonMapValueSpec<LibObjT>::forEach(this->libObjPtr(), func);
1379 Shared shared() const noexcept
1381 return Shared::createWithRef(*this);
1385 void _handleInsertLibStatus(const bt_value_map_insert_entry_status status) const
1387 if (status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_MEMORY_ERROR) {
1388 throw MemoryError {};
1393 using MapValue = CommonMapValue<bt_value>;
1394 using ConstMapValue = CommonMapValue<const bt_value>;
1396 namespace internal {
1398 struct MapValueTypeDescr
1400 using Const = ConstMapValue;
1401 using NonConst = MapValue;
1405 struct TypeDescr<MapValue> : public MapValueTypeDescr
1410 struct TypeDescr<ConstMapValue> : public MapValueTypeDescr
1414 } /* namespace internal */
1416 template <typename LibObjT>
1417 ArrayValue CommonValue<LibObjT>::appendEmptyArray() const
1419 return this->asArray().appendEmptyArray();
1422 template <typename LibObjT>
1423 MapValue CommonValue<LibObjT>::appendEmptyMap() const
1425 return this->asArray().appendEmptyMap();
1428 template <typename LibObjT>
1429 ArrayValue CommonValue<LibObjT>::insertEmptyArray(const bt2c::CStringView key) const
1431 return this->asMap().insertEmptyArray(key);
1434 template <typename LibObjT>
1435 MapValue CommonValue<LibObjT>::insertEmptyMap(const bt2c::CStringView key) const
1437 return this->asMap().insertEmptyMap(key);
1440 template <typename LibObjT>
1441 CommonNullValue<LibObjT> CommonValue<LibObjT>::asNull() const noexcept
1443 BT_ASSERT_DBG(this->isNull());
1444 return CommonNullValue<LibObjT> {};
1447 template <typename LibObjT>
1448 CommonBoolValue<LibObjT> CommonValue<LibObjT>::asBool() const noexcept
1450 return CommonBoolValue<LibObjT> {this->libObjPtr()};
1453 template <typename LibObjT>
1454 CommonSignedIntegerValue<LibObjT> CommonValue<LibObjT>::asSignedInteger() const noexcept
1456 return CommonSignedIntegerValue<LibObjT> {this->libObjPtr()};
1459 template <typename LibObjT>
1460 CommonUnsignedIntegerValue<LibObjT> CommonValue<LibObjT>::asUnsignedInteger() const noexcept
1462 return CommonUnsignedIntegerValue<LibObjT> {this->libObjPtr()};
1465 template <typename LibObjT>
1466 CommonRealValue<LibObjT> CommonValue<LibObjT>::asReal() const noexcept
1468 return CommonRealValue<LibObjT> {this->libObjPtr()};
1471 template <typename LibObjT>
1472 CommonStringValue<LibObjT> CommonValue<LibObjT>::asString() const noexcept
1474 return CommonStringValue<LibObjT> {this->libObjPtr()};
1477 template <typename LibObjT>
1478 CommonArrayValue<LibObjT> CommonValue<LibObjT>::asArray() const noexcept
1480 return CommonArrayValue<LibObjT> {this->libObjPtr()};
1483 template <typename LibObjT>
1484 CommonMapValue<LibObjT> CommonValue<LibObjT>::asMap() const noexcept
1486 return CommonMapValue<LibObjT> {this->libObjPtr()};
1489 template <typename LibObjT>
1490 ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray() const
1492 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1494 bt_value *libElemPtr;
1495 const auto status = bt_value_array_append_empty_array_element(this->libObjPtr(), &libElemPtr);
1497 this->_handleAppendLibStatus(status);
1498 return ArrayValue {libElemPtr};
1501 template <typename LibObjT>
1502 MapValue CommonArrayValue<LibObjT>::appendEmptyMap() const
1504 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1506 bt_value *libElemPtr;
1507 const auto status = bt_value_array_append_empty_map_element(this->libObjPtr(), &libElemPtr);
1509 this->_handleAppendLibStatus(status);
1510 return MapValue {libElemPtr};
1513 template <typename LibObjT>
1514 ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const bt2c::CStringView key) const
1516 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1518 bt_value *libEntryPtr;
1519 const auto status = bt_value_map_insert_empty_array_entry(this->libObjPtr(), key, &libEntryPtr);
1521 this->_handleInsertLibStatus(status);
1522 return ArrayValue {libEntryPtr};
1525 template <typename LibObjT>
1526 MapValue CommonMapValue<LibObjT>::insertEmptyMap(const bt2c::CStringView key) const
1528 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1530 bt_value *libEntryPtr;
1531 const auto status = bt_value_map_insert_empty_map_entry(this->libObjPtr(), key, &libEntryPtr);
1533 this->_handleInsertLibStatus(status);
1534 return MapValue {libEntryPtr};
1537 inline BoolValue::Shared createValue(const bool rawVal)
1539 return BoolValue::create(rawVal);
1542 inline UnsignedIntegerValue::Shared createValue(const std::uint64_t rawVal)
1544 return UnsignedIntegerValue::create(rawVal);
1547 inline SignedIntegerValue::Shared createValue(const std::int64_t rawVal)
1549 return SignedIntegerValue::create(rawVal);
1552 inline RealValue::Shared createValue(const double rawVal)
1554 return RealValue::create(rawVal);
1557 inline StringValue::Shared createValue(const char * const rawVal)
1559 return StringValue::create(rawVal);
1562 inline StringValue::Shared createValue(const bt2c::CStringView rawVal)
1564 return StringValue::create(rawVal);
1567 } /* namespace bt2 */
1569 #endif /* BABELTRACE_CPP_COMMON_BT2_VALUE_HPP */