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 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,
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 void 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));
510 Shared shared() const noexcept
512 return Shared::createWithRef(*this);
516 using BoolValue = CommonBoolValue<bt_value>;
517 using ConstBoolValue = CommonBoolValue<const bt_value>;
521 struct BoolValueTypeDescr
523 using Const = ConstBoolValue;
524 using NonConst = BoolValue;
528 struct TypeDescr<BoolValue> : public BoolValueTypeDescr
533 struct TypeDescr<ConstBoolValue> : public BoolValueTypeDescr
537 } /* namespace internal */
539 template <typename LibObjT>
540 class CommonUnsignedIntegerValue final : public CommonValue<LibObjT>
543 using typename CommonValue<LibObjT>::_ThisCommonValue;
546 using typename CommonValue<LibObjT>::LibObjPtr;
547 using Shared = SharedValue<CommonUnsignedIntegerValue<LibObjT>, LibObjT>;
548 using Value = std::uint64_t;
550 explicit CommonUnsignedIntegerValue(const LibObjPtr libObjPtr) noexcept :
551 _ThisCommonValue {libObjPtr}
553 BT_ASSERT_DBG(this->isUnsignedInteger());
556 static Shared create(const Value rawVal = 0)
558 const auto libObjPtr = bt_value_integer_unsigned_create_init(rawVal);
560 internal::validateCreatedObjPtr(libObjPtr);
561 return CommonUnsignedIntegerValue::Shared::createWithoutRef(libObjPtr);
564 template <typename OtherLibObjT>
565 CommonUnsignedIntegerValue(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept :
566 _ThisCommonValue {val}
570 template <typename OtherLibObjT>
571 CommonUnsignedIntegerValue<LibObjT>
572 operator=(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept
574 _ThisCommonValue::operator=(val);
578 CommonUnsignedIntegerValue<const bt_value> asConst() const noexcept
580 return CommonUnsignedIntegerValue<const bt_value> {*this};
583 RawValueProxy<CommonUnsignedIntegerValue> operator*() const noexcept
585 return RawValueProxy<CommonUnsignedIntegerValue> {*this};
588 void value(const Value val) const noexcept
590 static_assert(!std::is_const<LibObjT>::value,
591 "Not available with `bt2::ConstUnsignedIntegerValue`.");
593 bt_value_integer_unsigned_set(this->libObjPtr(), val);
596 Value value() const noexcept
598 return bt_value_integer_unsigned_get(this->libObjPtr());
601 Shared shared() const noexcept
603 return Shared::createWithRef(*this);
607 using UnsignedIntegerValue = CommonUnsignedIntegerValue<bt_value>;
608 using ConstUnsignedIntegerValue = CommonUnsignedIntegerValue<const bt_value>;
612 struct UnsignedIntegerValueTypeDescr
614 using Const = ConstUnsignedIntegerValue;
615 using NonConst = UnsignedIntegerValue;
619 struct TypeDescr<UnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
624 struct TypeDescr<ConstUnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
628 } /* namespace internal */
630 template <typename LibObjT>
631 class CommonSignedIntegerValue final : public CommonValue<LibObjT>
634 using typename CommonValue<LibObjT>::_ThisCommonValue;
637 using typename CommonValue<LibObjT>::LibObjPtr;
638 using Shared = SharedValue<CommonSignedIntegerValue<LibObjT>, LibObjT>;
639 using Value = std::int64_t;
641 explicit CommonSignedIntegerValue(const LibObjPtr libObjPtr) noexcept :
642 _ThisCommonValue {libObjPtr}
644 BT_ASSERT_DBG(this->isSignedInteger());
647 static Shared create(const Value rawVal = 0)
649 const auto libObjPtr = bt_value_integer_signed_create_init(rawVal);
651 internal::validateCreatedObjPtr(libObjPtr);
652 return CommonSignedIntegerValue::Shared::createWithoutRef(libObjPtr);
655 template <typename OtherLibObjT>
656 CommonSignedIntegerValue(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept :
657 _ThisCommonValue {val}
661 template <typename OtherLibObjT>
662 CommonSignedIntegerValue<LibObjT>
663 operator=(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept
665 _ThisCommonValue::operator=(val);
669 CommonSignedIntegerValue<const bt_value> asConst() const noexcept
671 return CommonSignedIntegerValue<const bt_value> {*this};
674 RawValueProxy<CommonSignedIntegerValue> operator*() const noexcept
676 return RawValueProxy<CommonSignedIntegerValue> {*this};
679 void value(const Value val) const noexcept
681 static_assert(!std::is_const<LibObjT>::value,
682 "Not available with `bt2::ConstSignedIntegerValue`.");
684 bt_value_integer_signed_set(this->libObjPtr(), val);
687 Value value() const noexcept
689 return bt_value_integer_signed_get(this->libObjPtr());
692 Shared shared() const noexcept
694 return Shared::createWithRef(*this);
698 using SignedIntegerValue = CommonSignedIntegerValue<bt_value>;
699 using ConstSignedIntegerValue = CommonSignedIntegerValue<const bt_value>;
703 struct SignedIntegerValueTypeDescr
705 using Const = ConstSignedIntegerValue;
706 using NonConst = SignedIntegerValue;
710 struct TypeDescr<SignedIntegerValue> : public SignedIntegerValueTypeDescr
715 struct TypeDescr<ConstSignedIntegerValue> : public SignedIntegerValueTypeDescr
719 } /* namespace internal */
721 template <typename LibObjT>
722 class CommonRealValue final : public CommonValue<LibObjT>
725 using typename CommonValue<LibObjT>::_ThisCommonValue;
728 using typename CommonValue<LibObjT>::LibObjPtr;
729 using Shared = SharedValue<CommonRealValue<LibObjT>, LibObjT>;
730 using Value = double;
732 explicit CommonRealValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
734 BT_ASSERT_DBG(this->isReal());
737 static Shared create(const Value rawVal = 0)
739 const auto libObjPtr = bt_value_real_create_init(rawVal);
741 internal::validateCreatedObjPtr(libObjPtr);
742 return CommonRealValue::Shared::createWithoutRef(libObjPtr);
745 template <typename OtherLibObjT>
746 CommonRealValue(const CommonRealValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
750 template <typename OtherLibObjT>
751 CommonRealValue<LibObjT> operator=(const CommonRealValue<OtherLibObjT> val) noexcept
753 _ThisCommonValue::operator=(val);
757 CommonRealValue<const bt_value> asConst() const noexcept
759 return CommonRealValue<const bt_value> {*this};
762 RawValueProxy<CommonRealValue> operator*() const noexcept
764 return RawValueProxy<CommonRealValue> {*this};
767 void value(const Value val) const noexcept
769 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstRealValue`.");
771 bt_value_real_set(this->libObjPtr(), val);
774 Value value() const noexcept
776 return bt_value_real_get(this->libObjPtr());
779 Shared shared() const noexcept
781 return Shared::createWithRef(*this);
785 using RealValue = CommonRealValue<bt_value>;
786 using ConstRealValue = CommonRealValue<const bt_value>;
790 struct RealValueTypeDescr
792 using Const = ConstRealValue;
793 using NonConst = RealValue;
797 struct TypeDescr<RealValue> : public RealValueTypeDescr
802 struct TypeDescr<ConstRealValue> : public RealValueTypeDescr
806 } /* namespace internal */
808 template <typename LibObjT>
809 class CommonStringValue final : public CommonValue<LibObjT>
812 using typename CommonValue<LibObjT>::_ThisCommonValue;
815 using typename CommonValue<LibObjT>::LibObjPtr;
816 using Shared = SharedValue<CommonStringValue<LibObjT>, LibObjT>;
817 using Value = bt2c::CStringView;
819 explicit CommonStringValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
821 BT_ASSERT_DBG(this->isString());
824 static Shared create(const bt2c::CStringView rawVal = "")
826 const auto libObjPtr = bt_value_string_create_init(rawVal);
828 internal::validateCreatedObjPtr(libObjPtr);
829 return CommonStringValue::Shared::createWithoutRef(libObjPtr);
832 template <typename OtherLibObjT>
833 CommonStringValue(const CommonStringValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
837 template <typename OtherLibObjT>
838 CommonStringValue<LibObjT> operator=(const CommonStringValue<OtherLibObjT> val) noexcept
840 _ThisCommonValue::operator=(val);
844 CommonStringValue<const bt_value> asConst() const noexcept
846 return CommonStringValue<const bt_value> {*this};
849 RawValueProxy<CommonStringValue> operator*() const noexcept
851 return RawValueProxy<CommonStringValue> {*this};
854 void value(const Value val) const
856 static_assert(!std::is_const<LibObjT>::value,
857 "Not available with `bt2::ConstStringValue`.");
859 const auto status = bt_value_string_set(this->libObjPtr(), *val);
861 if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) {
862 throw MemoryError {};
866 Value value() const noexcept
868 return bt_value_string_get(this->libObjPtr());
871 Shared shared() const noexcept
873 return Shared::createWithRef(*this);
877 using StringValue = CommonStringValue<bt_value>;
878 using ConstStringValue = CommonStringValue<const bt_value>;
882 struct StringValueTypeDescr
884 using Const = ConstStringValue;
885 using NonConst = StringValue;
889 struct TypeDescr<StringValue> : public StringValueTypeDescr
894 struct TypeDescr<ConstStringValue> : public StringValueTypeDescr
898 template <typename LibObjT>
899 struct CommonArrayValueSpec;
901 /* Functions specific to mutable array values */
903 struct CommonArrayValueSpec<bt_value> final
905 static bt_value *elementByIndex(bt_value * const libValPtr, const std::uint64_t index) noexcept
907 return bt_value_array_borrow_element_by_index(libValPtr, index);
911 /* Functions specific to constant array values */
913 struct CommonArrayValueSpec<const bt_value> final
915 static const bt_value *elementByIndex(const bt_value * const libValPtr,
916 const std::uint64_t index) noexcept
918 return bt_value_array_borrow_element_by_index_const(libValPtr, index);
922 } /* namespace internal */
924 template <typename LibObjT>
925 class CommonArrayValue final : public CommonValue<LibObjT>
928 using typename CommonValue<LibObjT>::_ThisCommonValue;
931 using typename CommonValue<LibObjT>::LibObjPtr;
932 using Shared = SharedValue<CommonArrayValue<LibObjT>, LibObjT>;
933 using Iterator = BorrowedObjectIterator<CommonArrayValue<LibObjT>>;
935 explicit CommonArrayValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
937 BT_ASSERT_DBG(this->isArray());
940 static Shared create()
942 const auto libObjPtr = bt_value_array_create();
944 internal::validateCreatedObjPtr(libObjPtr);
945 return CommonArrayValue::Shared::createWithoutRef(libObjPtr);
948 template <typename OtherLibObjT>
949 CommonArrayValue(const CommonArrayValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
953 template <typename OtherLibObjT>
954 CommonArrayValue<LibObjT> operator=(const CommonArrayValue<OtherLibObjT> val) noexcept
956 _ThisCommonValue::operator=(val);
960 CommonArrayValue<const bt_value> asConst() const noexcept
962 return CommonArrayValue<const bt_value> {*this};
965 std::uint64_t length() const noexcept
967 return bt_value_array_get_length(this->libObjPtr());
970 Iterator begin() const noexcept
972 return Iterator {*this, 0};
975 Iterator end() const noexcept
977 return Iterator {*this, this->length()};
980 bool isEmpty() const noexcept
982 return this->length() == 0;
985 CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept
987 return CommonValue<LibObjT> {
988 internal::CommonArrayValueSpec<LibObjT>::elementByIndex(this->libObjPtr(), index)};
991 void append(const Value val) const
993 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
995 const auto status = bt_value_array_append_element(this->libObjPtr(), val.libObjPtr());
997 this->_handleAppendLibStatus(status);
1000 void append(const bool rawVal) const
1002 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1005 bt_value_array_append_bool_element(this->libObjPtr(), static_cast<bt_bool>(rawVal));
1007 this->_handleAppendLibStatus(status);
1010 void append(const std::uint64_t rawVal) const
1012 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1015 bt_value_array_append_unsigned_integer_element(this->libObjPtr(), rawVal);
1017 this->_handleAppendLibStatus(status);
1020 void append(const std::int64_t rawVal) const
1022 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1024 const auto status = bt_value_array_append_signed_integer_element(this->libObjPtr(), rawVal);
1026 this->_handleAppendLibStatus(status);
1029 void append(const double rawVal) const
1031 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1033 const auto status = bt_value_array_append_real_element(this->libObjPtr(), rawVal);
1035 this->_handleAppendLibStatus(status);
1038 void append(const char * const rawVal) const
1040 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1042 const auto status = bt_value_array_append_string_element(this->libObjPtr(), rawVal);
1044 this->_handleAppendLibStatus(status);
1047 void append(const bt2c::CStringView rawVal) const
1049 this->append(rawVal.data());
1052 CommonArrayValue<bt_value> appendEmptyArray() const;
1053 CommonMapValue<bt_value> appendEmptyMap() const;
1055 void operator+=(const Value val) const
1060 void operator+=(const bool rawVal) const
1062 this->append(rawVal);
1065 void operator+=(const std::uint64_t rawVal) const
1067 this->append(rawVal);
1070 void operator+=(const std::int64_t rawVal) const
1072 this->append(rawVal);
1075 void operator+=(const double rawVal) const
1077 this->append(rawVal);
1080 void operator+=(const char * const rawVal) const
1082 this->append(rawVal);
1085 void operator+=(const bt2c::CStringView rawVal) const
1087 this->append(rawVal);
1090 Shared shared() const noexcept
1092 return Shared::createWithRef(*this);
1096 void _handleAppendLibStatus(const bt_value_array_append_element_status status) const
1098 if (status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR) {
1099 throw MemoryError {};
1104 using ArrayValue = CommonArrayValue<bt_value>;
1105 using ConstArrayValue = CommonArrayValue<const bt_value>;
1107 namespace internal {
1109 struct ArrayValueTypeDescr
1111 using Const = ConstArrayValue;
1112 using NonConst = ArrayValue;
1116 struct TypeDescr<ArrayValue> : public ArrayValueTypeDescr
1121 struct TypeDescr<ConstArrayValue> : public ArrayValueTypeDescr
1126 * Type of a user function passed to `CommonMapValue<ObjT>::forEach()`.
1128 * First argument is the entry's key, second is its value.
1130 template <typename ObjT>
1131 using CommonMapValueForEachUserFunc = std::function<void(bt2c::CStringView, ObjT)>;
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.
1137 * `userData` is casted to a `const` pointer to
1138 * `CommonMapValueForEachUserFunc<ObjT>` (the user function to call).
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.
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)
1148 const auto& userFunc = *reinterpret_cast<const CommonMapValueForEachUserFunc<ObjT> *>(userData);
1151 userFunc(key, ObjT {libObjPtr});
1153 return static_cast<LibStatusT>(ErrorStatus);
1156 return static_cast<LibStatusT>(OkStatus);
1159 template <typename LibObjT>
1160 struct CommonMapValueSpec;
1162 /* Functions specific to mutable map values */
1164 struct CommonMapValueSpec<bt_value> final
1166 static bt_value *entryByKey(bt_value * const libValPtr, const char * const key) noexcept
1168 return bt_value_map_borrow_entry_value(libValPtr, key);
1171 static void forEach(bt_value * const libValPtr,
1172 const CommonMapValueForEachUserFunc<Value>& func)
1174 const auto status = bt_value_map_foreach_entry(
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)));
1182 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_OK:
1184 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR:
1185 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR:
1193 /* Functions specific to constant map values */
1195 struct CommonMapValueSpec<const bt_value> final
1197 static const bt_value *entryByKey(const bt_value * const libValPtr,
1198 const char * const key) noexcept
1200 return bt_value_map_borrow_entry_value_const(libValPtr, key);
1203 static void forEach(const bt_value * const libValPtr,
1204 const CommonMapValueForEachUserFunc<ConstValue>& func)
1206 const auto status = bt_value_map_foreach_entry_const(
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)));
1215 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK:
1217 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_USER_ERROR:
1218 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR:
1226 } /* namespace internal */
1228 template <typename LibObjT>
1229 class CommonMapValue final : public CommonValue<LibObjT>
1232 using typename CommonValue<LibObjT>::_ThisCommonValue;
1235 using typename CommonValue<LibObjT>::LibObjPtr;
1236 using Shared = SharedValue<CommonMapValue<LibObjT>, LibObjT>;
1238 explicit CommonMapValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
1240 BT_ASSERT_DBG(this->isMap());
1243 static Shared create()
1245 const auto libObjPtr = bt_value_map_create();
1247 internal::validateCreatedObjPtr(libObjPtr);
1248 return CommonMapValue::Shared::createWithoutRef(libObjPtr);
1251 template <typename OtherLibObjT>
1252 CommonMapValue(const CommonMapValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
1256 template <typename OtherLibObjT>
1257 CommonMapValue<LibObjT> operator=(const CommonMapValue<OtherLibObjT> val) noexcept
1259 _ThisCommonValue::operator=(val);
1263 CommonMapValue<const bt_value> asConst() const noexcept
1265 return CommonMapValue<const bt_value> {*this};
1268 std::uint64_t length() const noexcept
1270 return bt_value_map_get_size(this->libObjPtr());
1273 bool isEmpty() const noexcept
1275 return this->length() == 0;
1278 OptionalBorrowedObject<CommonValue<LibObjT>>
1279 operator[](const bt2c::CStringView key) const noexcept
1281 return internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key);
1284 bool hasEntry(const bt2c::CStringView key) const noexcept
1286 return static_cast<bool>(bt_value_map_has_entry(this->libObjPtr(), key));
1289 void insert(const bt2c::CStringView key, const Value val) const
1291 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1293 const auto status = bt_value_map_insert_entry(this->libObjPtr(), key, val.libObjPtr());
1295 this->_handleInsertLibStatus(status);
1298 void insert(const bt2c::CStringView key, const bool rawVal) const
1300 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1303 bt_value_map_insert_bool_entry(this->libObjPtr(), key, static_cast<bt_bool>(rawVal));
1305 this->_handleInsertLibStatus(status);
1308 void insert(const bt2c::CStringView key, const std::uint64_t rawVal) const
1310 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1313 bt_value_map_insert_unsigned_integer_entry(this->libObjPtr(), key, rawVal);
1315 this->_handleInsertLibStatus(status);
1317 void insert(const bt2c::CStringView key, const std::int64_t rawVal) const
1319 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1322 bt_value_map_insert_signed_integer_entry(this->libObjPtr(), key, rawVal);
1324 this->_handleInsertLibStatus(status);
1327 void insert(const bt2c::CStringView key, const double rawVal) const
1329 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1331 const auto status = bt_value_map_insert_real_entry(this->libObjPtr(), key, rawVal);
1333 this->_handleInsertLibStatus(status);
1336 void insert(const bt2c::CStringView key, const char *rawVal) const
1338 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1340 const auto status = bt_value_map_insert_string_entry(this->libObjPtr(), key, rawVal);
1342 this->_handleInsertLibStatus(status);
1345 void insert(const bt2c::CStringView key, const bt2c::CStringView rawVal) const
1347 return this->insert(key, rawVal.data());
1350 CommonArrayValue<bt_value> insertEmptyArray(bt2c::CStringView key) const;
1351 CommonMapValue<bt_value> insertEmptyMap(bt2c::CStringView key) const;
1353 void forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func) const
1355 internal::CommonMapValueSpec<LibObjT>::forEach(this->libObjPtr(), func);
1358 Shared shared() const noexcept
1360 return Shared::createWithRef(*this);
1364 void _handleInsertLibStatus(const bt_value_map_insert_entry_status status) const
1366 if (status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_MEMORY_ERROR) {
1367 throw MemoryError {};
1372 using MapValue = CommonMapValue<bt_value>;
1373 using ConstMapValue = CommonMapValue<const bt_value>;
1375 namespace internal {
1377 struct MapValueTypeDescr
1379 using Const = ConstMapValue;
1380 using NonConst = MapValue;
1384 struct TypeDescr<MapValue> : public MapValueTypeDescr
1389 struct TypeDescr<ConstMapValue> : public MapValueTypeDescr
1393 } /* namespace internal */
1395 template <typename LibObjT>
1396 ArrayValue CommonValue<LibObjT>::appendEmptyArray() const
1398 return this->asArray().appendEmptyArray();
1401 template <typename LibObjT>
1402 MapValue CommonValue<LibObjT>::appendEmptyMap() const
1404 return this->asArray().appendEmptyMap();
1407 template <typename LibObjT>
1408 ArrayValue CommonValue<LibObjT>::insertEmptyArray(const bt2c::CStringView key) const
1410 return this->asMap().insertEmptyArray(key);
1413 template <typename LibObjT>
1414 MapValue CommonValue<LibObjT>::insertEmptyMap(const bt2c::CStringView key) const
1416 return this->asMap().insertEmptyMap(key);
1419 template <typename LibObjT>
1420 CommonNullValue<LibObjT> CommonValue<LibObjT>::asNull() const noexcept
1422 BT_ASSERT_DBG(this->isNull());
1423 return CommonNullValue<LibObjT> {};
1426 template <typename LibObjT>
1427 CommonBoolValue<LibObjT> CommonValue<LibObjT>::asBool() const noexcept
1429 return CommonBoolValue<LibObjT> {this->libObjPtr()};
1432 template <typename LibObjT>
1433 CommonSignedIntegerValue<LibObjT> CommonValue<LibObjT>::asSignedInteger() const noexcept
1435 return CommonSignedIntegerValue<LibObjT> {this->libObjPtr()};
1438 template <typename LibObjT>
1439 CommonUnsignedIntegerValue<LibObjT> CommonValue<LibObjT>::asUnsignedInteger() const noexcept
1441 return CommonUnsignedIntegerValue<LibObjT> {this->libObjPtr()};
1444 template <typename LibObjT>
1445 CommonRealValue<LibObjT> CommonValue<LibObjT>::asReal() const noexcept
1447 return CommonRealValue<LibObjT> {this->libObjPtr()};
1450 template <typename LibObjT>
1451 CommonStringValue<LibObjT> CommonValue<LibObjT>::asString() const noexcept
1453 return CommonStringValue<LibObjT> {this->libObjPtr()};
1456 template <typename LibObjT>
1457 CommonArrayValue<LibObjT> CommonValue<LibObjT>::asArray() const noexcept
1459 return CommonArrayValue<LibObjT> {this->libObjPtr()};
1462 template <typename LibObjT>
1463 CommonMapValue<LibObjT> CommonValue<LibObjT>::asMap() const noexcept
1465 return CommonMapValue<LibObjT> {this->libObjPtr()};
1468 template <typename LibObjT>
1469 ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray() const
1471 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1473 bt_value *libElemPtr;
1474 const auto status = bt_value_array_append_empty_array_element(this->libObjPtr(), &libElemPtr);
1476 this->_handleAppendLibStatus(status);
1477 return ArrayValue {libElemPtr};
1480 template <typename LibObjT>
1481 MapValue CommonArrayValue<LibObjT>::appendEmptyMap() const
1483 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1485 bt_value *libElemPtr;
1486 const auto status = bt_value_array_append_empty_map_element(this->libObjPtr(), &libElemPtr);
1488 this->_handleAppendLibStatus(status);
1489 return MapValue {libElemPtr};
1492 template <typename LibObjT>
1493 ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const bt2c::CStringView key) const
1495 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1497 bt_value *libEntryPtr;
1498 const auto status = bt_value_map_insert_empty_array_entry(this->libObjPtr(), key, &libEntryPtr);
1500 this->_handleInsertLibStatus(status);
1501 return ArrayValue {libEntryPtr};
1504 template <typename LibObjT>
1505 MapValue CommonMapValue<LibObjT>::insertEmptyMap(const bt2c::CStringView key) const
1507 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1509 bt_value *libEntryPtr;
1510 const auto status = bt_value_map_insert_empty_map_entry(this->libObjPtr(), key, &libEntryPtr);
1512 this->_handleInsertLibStatus(status);
1513 return MapValue {libEntryPtr};
1516 inline BoolValue::Shared createValue(const bool rawVal)
1518 return BoolValue::create(rawVal);
1521 inline UnsignedIntegerValue::Shared createValue(const std::uint64_t rawVal)
1523 return UnsignedIntegerValue::create(rawVal);
1526 inline SignedIntegerValue::Shared createValue(const std::int64_t rawVal)
1528 return SignedIntegerValue::create(rawVal);
1531 inline RealValue::Shared createValue(const double rawVal)
1533 return RealValue::create(rawVal);
1536 inline StringValue::Shared createValue(const char * const rawVal)
1538 return StringValue::create(rawVal);
1541 inline StringValue::Shared createValue(const bt2c::CStringView rawVal)
1543 return StringValue::create(rawVal);
1546 } /* namespace bt2 */
1548 #endif /* BABELTRACE_CPP_COMMON_BT2_VALUE_HPP */