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/optional.hpp"
19 #include "cpp-common/string_view.hpp"
21 #include "borrowed-object.hpp"
22 #include "common-iterator.hpp"
24 #include "internal/utils.hpp"
25 #include "shared-object.hpp"
30 struct ValueRefFuncs final
32 static void get(const bt_value * const libObjPtr) noexcept
34 bt_value_get_ref(libObjPtr);
37 static void put(const bt_value * const libObjPtr) noexcept
39 bt_value_put_ref(libObjPtr);
43 } /* namespace internal */
45 template <typename ObjT, typename LibObjT>
46 using SharedValue = SharedObject<ObjT, LibObjT, internal::ValueRefFuncs>;
48 template <typename LibObjT>
49 class CommonNullValue;
51 template <typename LibObjT>
52 class CommonBoolValue;
54 template <typename LibObjT>
55 class CommonUnsignedIntegerValue;
57 template <typename LibObjT>
58 class CommonSignedIntegerValue;
60 template <typename LibObjT>
61 class CommonRealValue;
63 template <typename LibObjT>
64 class CommonStringValue;
66 template <typename LibObjT>
67 class CommonArrayValue;
69 template <typename LibObjT>
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,
84 template <typename LibObjT>
85 class CommonValue : public BorrowedObject<LibObjT>
88 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
91 using typename BorrowedObject<LibObjT>::_LibObjPtr;
92 using _ThisCommonValue = CommonValue<LibObjT>;
95 using Shared = SharedValue<CommonValue<LibObjT>, LibObjT>;
97 explicit CommonValue(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
101 template <typename OtherLibObjT>
102 CommonValue(const CommonValue<OtherLibObjT> val) noexcept : _ThisBorrowedObject {val}
106 template <typename OtherLibObjT>
107 _ThisCommonValue& operator=(const CommonValue<OtherLibObjT> val) noexcept
109 _ThisBorrowedObject::operator=(val);
113 ValueType type() const noexcept
115 return static_cast<ValueType>(bt_value_get_type(this->libObjPtr()));
118 bool isNull() const noexcept
120 return this->_libTypeIs(BT_VALUE_TYPE_NULL);
123 bool isBool() const noexcept
125 return this->_libTypeIs(BT_VALUE_TYPE_BOOL);
128 bool isInteger() const noexcept
130 return this->_libTypeIs(BT_VALUE_TYPE_INTEGER);
133 bool isUnsignedInteger() const noexcept
135 return this->_libTypeIs(BT_VALUE_TYPE_UNSIGNED_INTEGER);
138 bool isSignedInteger() const noexcept
140 return this->_libTypeIs(BT_VALUE_TYPE_SIGNED_INTEGER);
143 bool isReal() const noexcept
145 return this->_libTypeIs(BT_VALUE_TYPE_REAL);
148 bool isString() const noexcept
150 return this->_libTypeIs(BT_VALUE_TYPE_STRING);
153 bool isArray() const noexcept
155 return this->_libTypeIs(BT_VALUE_TYPE_ARRAY);
158 bool isMap() const noexcept
160 return this->_libTypeIs(BT_VALUE_TYPE_MAP);
163 template <typename OtherLibObjT>
164 bool operator==(const CommonValue<OtherLibObjT> other) const noexcept
166 return static_cast<bool>(bt_value_is_equal(this->libObjPtr(), other.libObjPtr()));
169 template <typename OtherLibObjT>
170 bool operator!=(const CommonValue<OtherLibObjT> other) const noexcept
172 return !(*this == other);
175 Shared shared() const noexcept
177 return Shared::createWithRef(*this);
180 template <typename ValueT>
181 ValueT as() const noexcept
183 return ValueT {this->libObjPtr()};
186 CommonNullValue<LibObjT> asNull() const noexcept;
187 CommonBoolValue<LibObjT> asBool() const noexcept;
188 CommonSignedIntegerValue<LibObjT> asSignedInteger() const noexcept;
189 CommonUnsignedIntegerValue<LibObjT> asUnsignedInteger() const noexcept;
190 CommonRealValue<LibObjT> asReal() const noexcept;
191 CommonStringValue<LibObjT> asString() const noexcept;
192 CommonArrayValue<LibObjT> asArray() const noexcept;
193 CommonMapValue<LibObjT> asMap() const noexcept;
196 bool _libTypeIs(const bt_value_type type) const noexcept
198 return bt_value_type_is(bt_value_get_type(this->libObjPtr()), type);
202 using Value = CommonValue<bt_value>;
203 using ConstValue = CommonValue<const bt_value>;
207 struct ValueTypeDescr
209 using Const = ConstValue;
210 using NonConst = Value;
214 struct TypeDescr<Value> : public ValueTypeDescr
219 struct TypeDescr<ConstValue> : public ValueTypeDescr
223 } /* namespace internal */
225 template <typename LibObjT>
226 class CommonNullValue final : public CommonValue<LibObjT>
229 using typename CommonValue<LibObjT>::_ThisCommonValue;
232 using Shared = SharedValue<CommonNullValue<LibObjT>, LibObjT>;
234 CommonNullValue() noexcept : _ThisCommonValue {bt_value_null}
238 template <typename OtherLibObjT>
239 CommonNullValue(const CommonNullValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
243 template <typename OtherLibObjT>
244 CommonNullValue<LibObjT>& operator=(const CommonNullValue<OtherLibObjT> val) noexcept
246 _ThisCommonValue::operator=(val);
250 Shared shared() const noexcept
252 return Shared::createWithRef(*this);
256 using NullValue = CommonNullValue<bt_value>;
257 using ConstNullValue = CommonNullValue<const bt_value>;
261 struct NullValueTypeDescr
263 using Const = ConstNullValue;
264 using NonConst = NullValue;
268 struct TypeDescr<NullValue> : public NullValueTypeDescr
273 struct TypeDescr<ConstNullValue> : public NullValueTypeDescr
277 } /* namespace internal */
279 template <typename LibObjT>
280 class CommonBoolValue final : public CommonValue<LibObjT>
283 using typename CommonValue<LibObjT>::_LibObjPtr;
284 using typename CommonValue<LibObjT>::_ThisCommonValue;
287 using Shared = SharedValue<CommonBoolValue<LibObjT>, LibObjT>;
290 explicit CommonBoolValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
292 BT_ASSERT_DBG(this->isBool());
295 template <typename OtherLibObjT>
296 CommonBoolValue(const CommonBoolValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
300 static Shared create(const Value rawVal = false)
302 const auto libObjPtr = bt_value_bool_create_init(static_cast<bt_bool>(rawVal));
304 internal::validateCreatedObjPtr(libObjPtr);
305 return CommonBoolValue::Shared::createWithoutRef(libObjPtr);
308 template <typename OtherLibObjT>
309 CommonBoolValue<LibObjT>& operator=(const CommonBoolValue<OtherLibObjT> val) noexcept
311 _ThisCommonValue::operator=(val);
315 CommonBoolValue<LibObjT> operator=(const Value rawVal) const noexcept
317 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
319 bt_value_bool_set(this->libObjPtr(), static_cast<bt_bool>(rawVal));
323 Value value() const noexcept
325 return static_cast<Value>(bt_value_bool_get(this->libObjPtr()));
328 operator Value() const noexcept
330 return this->value();
333 Shared shared() const noexcept
335 return Shared::createWithRef(*this);
339 using BoolValue = CommonBoolValue<bt_value>;
340 using ConstBoolValue = CommonBoolValue<const bt_value>;
344 struct BoolValueTypeDescr
346 using Const = ConstBoolValue;
347 using NonConst = BoolValue;
351 struct TypeDescr<BoolValue> : public BoolValueTypeDescr
356 struct TypeDescr<ConstBoolValue> : public BoolValueTypeDescr
360 } /* namespace internal */
362 template <typename LibObjT>
363 class CommonUnsignedIntegerValue final : public CommonValue<LibObjT>
366 using typename CommonValue<LibObjT>::_LibObjPtr;
367 using typename CommonValue<LibObjT>::_ThisCommonValue;
370 using Shared = SharedValue<CommonUnsignedIntegerValue<LibObjT>, LibObjT>;
371 using Value = std::uint64_t;
373 explicit CommonUnsignedIntegerValue(const _LibObjPtr libObjPtr) noexcept :
374 _ThisCommonValue {libObjPtr}
376 BT_ASSERT_DBG(this->isUnsignedInteger());
379 static Shared create(const Value rawVal = 0)
381 const auto libObjPtr = bt_value_integer_unsigned_create_init(rawVal);
383 internal::validateCreatedObjPtr(libObjPtr);
384 return CommonUnsignedIntegerValue::Shared::createWithoutRef(libObjPtr);
387 template <typename OtherLibObjT>
388 CommonUnsignedIntegerValue(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept :
389 _ThisCommonValue {val}
393 template <typename OtherLibObjT>
394 CommonUnsignedIntegerValue<LibObjT>&
395 operator=(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept
397 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
399 _ThisCommonValue::operator=(val);
403 CommonUnsignedIntegerValue<LibObjT> operator=(const Value rawVal) const noexcept
405 bt_value_integer_unsigned_set(this->libObjPtr(), rawVal);
409 Value value() const noexcept
411 return bt_value_integer_unsigned_get(this->libObjPtr());
414 operator Value() const noexcept
416 return this->value();
419 Shared shared() const noexcept
421 return Shared::createWithRef(*this);
425 using UnsignedIntegerValue = CommonUnsignedIntegerValue<bt_value>;
426 using ConstUnsignedIntegerValue = CommonUnsignedIntegerValue<const bt_value>;
430 struct UnsignedIntegerValueTypeDescr
432 using Const = ConstUnsignedIntegerValue;
433 using NonConst = UnsignedIntegerValue;
437 struct TypeDescr<UnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
442 struct TypeDescr<ConstUnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
446 } /* namespace internal */
448 template <typename LibObjT>
449 class CommonSignedIntegerValue final : public CommonValue<LibObjT>
452 using typename CommonValue<LibObjT>::_LibObjPtr;
453 using typename CommonValue<LibObjT>::_ThisCommonValue;
456 using Shared = SharedValue<CommonSignedIntegerValue<LibObjT>, LibObjT>;
457 using Value = std::int64_t;
459 explicit CommonSignedIntegerValue(const _LibObjPtr libObjPtr) noexcept :
460 _ThisCommonValue {libObjPtr}
462 BT_ASSERT_DBG(this->isSignedInteger());
465 static Shared create(const Value rawVal = 0)
467 const auto libObjPtr = bt_value_integer_signed_create_init(rawVal);
469 internal::validateCreatedObjPtr(libObjPtr);
470 return CommonSignedIntegerValue::Shared::createWithoutRef(libObjPtr);
473 template <typename OtherLibObjT>
474 CommonSignedIntegerValue(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept :
475 _ThisCommonValue {val}
479 template <typename OtherLibObjT>
480 CommonSignedIntegerValue<LibObjT>
481 operator=(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept
483 _ThisCommonValue::operator=(val);
487 CommonSignedIntegerValue<LibObjT> operator=(const Value rawVal) const noexcept
489 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
491 bt_value_integer_signed_set(this->libObjPtr(), rawVal);
495 Value value() const noexcept
497 return bt_value_integer_signed_get(this->libObjPtr());
500 operator Value() const noexcept
502 return this->value();
505 Shared shared() const noexcept
507 return Shared::createWithRef(*this);
511 using SignedIntegerValue = CommonSignedIntegerValue<bt_value>;
512 using ConstSignedIntegerValue = CommonSignedIntegerValue<const bt_value>;
516 struct SignedIntegerValueTypeDescr
518 using Const = ConstSignedIntegerValue;
519 using NonConst = SignedIntegerValue;
523 struct TypeDescr<SignedIntegerValue> : public SignedIntegerValueTypeDescr
528 struct TypeDescr<ConstSignedIntegerValue> : public SignedIntegerValueTypeDescr
532 } /* namespace internal */
534 template <typename LibObjT>
535 class CommonRealValue final : public CommonValue<LibObjT>
538 using typename CommonValue<LibObjT>::_LibObjPtr;
539 using typename CommonValue<LibObjT>::_ThisCommonValue;
542 using Shared = SharedValue<CommonRealValue<LibObjT>, LibObjT>;
543 using Value = double;
545 explicit CommonRealValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
547 BT_ASSERT_DBG(this->isReal());
550 static Shared create(const Value rawVal = 0)
552 const auto libObjPtr = bt_value_real_create_init(rawVal);
554 internal::validateCreatedObjPtr(libObjPtr);
555 return CommonRealValue::Shared::createWithoutRef(libObjPtr);
558 template <typename OtherLibObjT>
559 CommonRealValue(const CommonRealValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
563 template <typename OtherLibObjT>
564 CommonRealValue<LibObjT>& operator=(const CommonRealValue<OtherLibObjT> val) noexcept
566 _ThisCommonValue::operator=(val);
570 CommonRealValue<LibObjT> operator=(const Value rawVal) const noexcept
572 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
574 bt_value_real_set(this->libObjPtr(), rawVal);
578 Value value() const noexcept
580 return bt_value_real_get(this->libObjPtr());
583 operator Value() const noexcept
585 return this->value();
588 Shared shared() const noexcept
590 return Shared::createWithRef(*this);
594 using RealValue = CommonRealValue<bt_value>;
595 using ConstRealValue = CommonRealValue<const bt_value>;
599 struct RealValueTypeDescr
601 using Const = ConstRealValue;
602 using NonConst = RealValue;
606 struct TypeDescr<RealValue> : public RealValueTypeDescr
611 struct TypeDescr<ConstRealValue> : public RealValueTypeDescr
615 } /* namespace internal */
617 template <typename LibObjT>
618 class CommonStringValue final : public CommonValue<LibObjT>
621 using typename CommonValue<LibObjT>::_LibObjPtr;
622 using typename CommonValue<LibObjT>::_ThisCommonValue;
625 using Shared = SharedValue<CommonStringValue<LibObjT>, LibObjT>;
627 explicit CommonStringValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
629 BT_ASSERT_DBG(this->isString());
632 static Shared create(const char * const rawVal = "")
634 const auto libObjPtr = bt_value_string_create_init(rawVal);
636 internal::validateCreatedObjPtr(libObjPtr);
637 return CommonStringValue::Shared::createWithoutRef(libObjPtr);
640 static Shared create(const std::string& rawVal)
642 return CommonStringValue::create(rawVal.data());
645 template <typename OtherLibObjT>
646 CommonStringValue(const CommonStringValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
650 template <typename OtherLibObjT>
651 CommonStringValue<LibObjT>& operator=(const CommonStringValue<OtherLibObjT> val) noexcept
653 _ThisCommonValue::operator=(val);
657 CommonStringValue<LibObjT> operator=(const char * const rawVal) const
659 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
661 const auto status = bt_value_string_set(this->libObjPtr(), rawVal);
663 if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) {
664 throw MemoryError {};
670 CommonStringValue<LibObjT> operator=(const std::string& rawVal) const noexcept
672 return *this = rawVal.data();
675 bpstd::string_view value() const noexcept
677 return bt_value_string_get(this->libObjPtr());
680 Shared shared() const noexcept
682 return Shared::createWithRef(*this);
686 using StringValue = CommonStringValue<bt_value>;
687 using ConstStringValue = CommonStringValue<const bt_value>;
691 struct StringValueTypeDescr
693 using Const = ConstStringValue;
694 using NonConst = StringValue;
698 struct TypeDescr<StringValue> : public StringValueTypeDescr
703 struct TypeDescr<ConstStringValue> : public StringValueTypeDescr
707 template <typename LibObjT>
708 struct CommonArrayValueSpec;
710 /* Functions specific to mutable array values */
712 struct CommonArrayValueSpec<bt_value> final
714 static bt_value *elementByIndex(bt_value * const libValPtr, const std::uint64_t index) noexcept
716 return bt_value_array_borrow_element_by_index(libValPtr, index);
720 /* Functions specific to constant array values */
722 struct CommonArrayValueSpec<const bt_value> final
724 static const bt_value *elementByIndex(const bt_value * const libValPtr,
725 const std::uint64_t index) noexcept
727 return bt_value_array_borrow_element_by_index_const(libValPtr, index);
731 } /* namespace internal */
733 template <typename LibObjT>
734 class CommonArrayValue final : public CommonValue<LibObjT>
737 using typename CommonValue<LibObjT>::_LibObjPtr;
738 using typename CommonValue<LibObjT>::_ThisCommonValue;
741 using Shared = SharedValue<CommonArrayValue<LibObjT>, LibObjT>;
742 using Iterator = CommonIterator<CommonArrayValue<LibObjT>, CommonValue<LibObjT>>;
744 explicit CommonArrayValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
746 BT_ASSERT_DBG(this->isArray());
749 static Shared create()
751 const auto libObjPtr = bt_value_array_create();
753 internal::validateCreatedObjPtr(libObjPtr);
754 return CommonArrayValue::Shared::createWithoutRef(libObjPtr);
757 template <typename OtherLibObjT>
758 CommonArrayValue(const CommonArrayValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
762 template <typename OtherLibObjT>
763 CommonArrayValue<LibObjT>& operator=(const CommonArrayValue<OtherLibObjT> val) noexcept
765 _ThisCommonValue::operator=(val);
769 std::uint64_t length() const noexcept
771 return bt_value_array_get_length(this->libObjPtr());
774 Iterator begin() const noexcept
776 return Iterator {*this, 0};
779 Iterator end() const noexcept
781 return Iterator {*this, this->length()};
784 bool isEmpty() const noexcept
786 return this->length() == 0;
789 CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept
791 return CommonValue<LibObjT> {
792 internal::CommonArrayValueSpec<LibObjT>::elementByIndex(this->libObjPtr(), index)};
795 void append(const Value val) const
797 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
799 const auto status = bt_value_array_append_element(this->libObjPtr(), val.libObjPtr());
801 this->_handleAppendLibStatus(status);
804 void append(const bool rawVal) const
806 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
809 bt_value_array_append_bool_element(this->libObjPtr(), static_cast<bt_bool>(rawVal));
811 this->_handleAppendLibStatus(status);
814 void append(const std::uint64_t rawVal) const
816 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
819 bt_value_array_append_unsigned_integer_element(this->libObjPtr(), rawVal);
821 this->_handleAppendLibStatus(status);
824 void append(const std::int64_t rawVal) const
826 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
828 const auto status = bt_value_array_append_signed_integer_element(this->libObjPtr(), rawVal);
830 this->_handleAppendLibStatus(status);
833 void append(const double rawVal) const
835 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
837 const auto status = bt_value_array_append_real_element(this->libObjPtr(), rawVal);
839 this->_handleAppendLibStatus(status);
842 void append(const char * const rawVal) const
844 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
846 const auto status = bt_value_array_append_string_element(this->libObjPtr(), rawVal);
848 this->_handleAppendLibStatus(status);
851 void append(const std::string& rawVal) const
853 this->append(rawVal.data());
856 CommonArrayValue<bt_value> appendEmptyArray() const;
857 CommonMapValue<bt_value> appendEmptyMap() const;
859 void operator+=(const Value val) const
864 void operator+=(const bool rawVal) const
866 this->append(rawVal);
869 void operator+=(const std::uint64_t rawVal) const
871 this->append(rawVal);
874 void operator+=(const std::int64_t rawVal) const
876 this->append(rawVal);
879 void operator+=(const double rawVal) const
881 this->append(rawVal);
884 void operator+=(const char * const rawVal) const
886 this->append(rawVal);
889 void operator+=(const std::string& rawVal) const
891 this->append(rawVal);
894 Shared shared() const noexcept
896 return Shared::createWithRef(*this);
900 void _handleAppendLibStatus(const bt_value_array_append_element_status status) const
902 if (status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR) {
903 throw MemoryError {};
908 using ArrayValue = CommonArrayValue<bt_value>;
909 using ConstArrayValue = CommonArrayValue<const bt_value>;
913 struct ArrayValueTypeDescr
915 using Const = ConstArrayValue;
916 using NonConst = ArrayValue;
920 struct TypeDescr<ArrayValue> : public ArrayValueTypeDescr
925 struct TypeDescr<ConstArrayValue> : public ArrayValueTypeDescr
930 * Type of a user function passed to `CommonMapValue<ObjT>::forEach()`.
932 * First argument is the entry's key, second is its value.
934 template <typename ObjT>
935 using CommonMapValueForEachUserFunc = std::function<void(const bpstd::string_view&, ObjT)>;
938 * Template of a function to be passed to bt_value_map_foreach_entry()
939 * for bt_value_map_foreach_entry_const() which calls a user function.
941 * `userData` is casted to a `const` pointer to
942 * `CommonMapValueForEachUserFunc<ObjT>` (the user function to call).
944 * This function catches any exception which the user function throws
945 * and returns the `ErrorStatus` value. If there's no execption, this
946 * function returns the `OkStatus` value.
948 template <typename ObjT, typename LibObjT, typename LibStatusT, int OkStatus, int ErrorStatus>
949 LibStatusT mapValueForEachLibFunc(const char * const key, LibObjT * const libObjPtr,
950 void * const userData)
952 const auto& userFunc = *reinterpret_cast<const CommonMapValueForEachUserFunc<ObjT> *>(userData);
955 userFunc(key, ObjT {libObjPtr});
957 return static_cast<LibStatusT>(ErrorStatus);
960 return static_cast<LibStatusT>(OkStatus);
963 template <typename LibObjT>
964 struct CommonMapValueSpec;
966 /* Functions specific to mutable map values */
968 struct CommonMapValueSpec<bt_value> final
970 static bt_value *entryByKey(bt_value * const libValPtr, const char * const key) noexcept
972 return bt_value_map_borrow_entry_value(libValPtr, key);
975 static void forEach(bt_value * const libValPtr,
976 const CommonMapValueForEachUserFunc<Value>& func)
978 const auto status = bt_value_map_foreach_entry(
980 mapValueForEachLibFunc<Value, bt_value, bt_value_map_foreach_entry_func_status,
981 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK,
982 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR>,
983 const_cast<void *>(reinterpret_cast<const void *>(&func)));
986 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_OK:
988 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR:
989 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR:
997 /* Functions specific to constant map values */
999 struct CommonMapValueSpec<const bt_value> final
1001 static const bt_value *entryByKey(const bt_value * const libValPtr,
1002 const char * const key) noexcept
1004 return bt_value_map_borrow_entry_value_const(libValPtr, key);
1007 static void forEach(const bt_value * const libValPtr,
1008 const CommonMapValueForEachUserFunc<ConstValue>& func)
1010 const auto status = bt_value_map_foreach_entry_const(
1012 mapValueForEachLibFunc<ConstValue, const bt_value,
1013 bt_value_map_foreach_entry_const_func_status,
1014 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK,
1015 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_ERROR>,
1016 const_cast<void *>(reinterpret_cast<const void *>(&func)));
1019 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK:
1021 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_USER_ERROR:
1022 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR:
1030 } /* namespace internal */
1032 template <typename LibObjT>
1033 class CommonMapValue final : public CommonValue<LibObjT>
1036 using typename CommonValue<LibObjT>::_LibObjPtr;
1037 using typename CommonValue<LibObjT>::_ThisCommonValue;
1040 using Shared = SharedValue<CommonMapValue<LibObjT>, LibObjT>;
1042 explicit CommonMapValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
1044 BT_ASSERT_DBG(this->isMap());
1047 static Shared create()
1049 const auto libObjPtr = bt_value_map_create();
1051 internal::validateCreatedObjPtr(libObjPtr);
1052 return CommonMapValue::Shared::createWithoutRef(libObjPtr);
1055 template <typename OtherLibObjT>
1056 CommonMapValue(const CommonMapValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
1060 template <typename OtherLibObjT>
1061 CommonMapValue<LibObjT>& operator=(const CommonMapValue<OtherLibObjT> val) noexcept
1063 _ThisCommonValue::operator=(val);
1067 std::uint64_t length() const noexcept
1069 return bt_value_map_get_size(this->libObjPtr());
1072 bool isEmpty() const noexcept
1074 return this->length() == 0;
1077 nonstd::optional<CommonValue<LibObjT>> operator[](const char * const key) const noexcept
1079 const auto libObjPtr =
1080 internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key);
1083 return nonstd::nullopt;
1086 return CommonValue<LibObjT> {libObjPtr};
1089 nonstd::optional<CommonValue<LibObjT>> operator[](const std::string& key) const noexcept
1091 return (*this)[key.data()];
1094 bool hasEntry(const char * const key) const noexcept
1096 return static_cast<bool>(bt_value_map_has_entry(this->libObjPtr(), key));
1099 bool hasEntry(const std::string& key) const noexcept
1101 return this->hasEntry(key.data());
1104 void insert(const char * const key, const Value val) const
1106 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1108 const auto status = bt_value_map_insert_entry(this->libObjPtr(), key, val.libObjPtr());
1110 this->_handleInsertLibStatus(status);
1113 void insert(const std::string& key, const Value val) const
1115 this->insert(key.data(), val);
1118 void insert(const char * const key, const bool rawVal) const
1120 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1123 bt_value_map_insert_bool_entry(this->libObjPtr(), key, static_cast<bt_bool>(rawVal));
1125 this->_handleInsertLibStatus(status);
1128 void insert(const std::string& key, const bool rawVal) const
1130 this->insert(key.data(), rawVal);
1133 void insert(const char * const key, const std::uint64_t rawVal) const
1135 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1138 bt_value_map_insert_unsigned_integer_entry(this->libObjPtr(), key, rawVal);
1140 this->_handleInsertLibStatus(status);
1143 void insert(const std::string& key, const std::uint64_t rawVal) const
1145 this->insert(key.data(), rawVal);
1148 void insert(const char * const key, const std::int64_t rawVal) const
1150 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1153 bt_value_map_insert_signed_integer_entry(this->libObjPtr(), key, rawVal);
1155 this->_handleInsertLibStatus(status);
1158 void insert(const std::string& key, const std::int64_t rawVal) const
1160 this->insert(key.data(), rawVal);
1163 void insert(const char * const key, const double rawVal) const
1165 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1167 const auto status = bt_value_map_insert_real_entry(this->libObjPtr(), key, rawVal);
1169 this->_handleInsertLibStatus(status);
1172 void insert(const std::string& key, const double rawVal) const
1174 this->insert(key.data(), rawVal);
1177 void insert(const char * const key, const char * const rawVal) const
1179 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1181 const auto status = bt_value_map_insert_string_entry(this->libObjPtr(), key, rawVal);
1183 this->_handleInsertLibStatus(status);
1186 void insert(const char * const key, const std::string& rawVal) const
1188 this->insert(key, rawVal.data());
1191 void insert(const std::string& key, const char * const rawVal) const
1193 this->insert(key.data(), rawVal);
1196 void insert(const std::string& key, const std::string& rawVal) const
1198 this->insert(key.data(), rawVal.data());
1201 CommonArrayValue<bt_value> insertEmptyArray(const char *key) const;
1202 CommonArrayValue<bt_value> insertEmptyArray(const std::string& key) const;
1203 CommonMapValue<bt_value> insertEmptyMap(const char *key) const;
1204 CommonMapValue<bt_value> insertEmptyMap(const std::string& key) const;
1206 void forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func) const
1208 internal::CommonMapValueSpec<LibObjT>::forEach(this->libObjPtr(), func);
1211 Shared shared() const noexcept
1213 return Shared::createWithRef(*this);
1217 void _handleInsertLibStatus(const bt_value_map_insert_entry_status status) const
1219 if (status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_MEMORY_ERROR) {
1220 throw MemoryError {};
1225 using MapValue = CommonMapValue<bt_value>;
1226 using ConstMapValue = CommonMapValue<const bt_value>;
1228 namespace internal {
1230 struct MapValueTypeDescr
1232 using Const = ConstMapValue;
1233 using NonConst = MapValue;
1237 struct TypeDescr<MapValue> : public MapValueTypeDescr
1242 struct TypeDescr<ConstMapValue> : public MapValueTypeDescr
1246 } /* namespace internal */
1248 template <typename LibObjT>
1249 CommonNullValue<LibObjT> CommonValue<LibObjT>::asNull() const noexcept
1251 BT_ASSERT_DBG(this->isNull());
1252 return CommonNullValue<LibObjT> {this->libObjPtr()};
1255 template <typename LibObjT>
1256 CommonBoolValue<LibObjT> CommonValue<LibObjT>::asBool() const noexcept
1258 BT_ASSERT_DBG(this->isBool());
1259 return CommonBoolValue<LibObjT> {this->libObjPtr()};
1262 template <typename LibObjT>
1263 CommonSignedIntegerValue<LibObjT> CommonValue<LibObjT>::asSignedInteger() const noexcept
1265 BT_ASSERT_DBG(this->isSignedInteger());
1266 return CommonSignedIntegerValue<LibObjT> {this->libObjPtr()};
1269 template <typename LibObjT>
1270 CommonUnsignedIntegerValue<LibObjT> CommonValue<LibObjT>::asUnsignedInteger() const noexcept
1272 BT_ASSERT_DBG(this->isUnsignedInteger());
1273 return CommonUnsignedIntegerValue<LibObjT> {this->libObjPtr()};
1276 template <typename LibObjT>
1277 CommonRealValue<LibObjT> CommonValue<LibObjT>::asReal() const noexcept
1279 BT_ASSERT_DBG(this->isReal());
1280 return CommonRealValue<LibObjT> {this->libObjPtr()};
1283 template <typename LibObjT>
1284 CommonStringValue<LibObjT> CommonValue<LibObjT>::asString() const noexcept
1286 BT_ASSERT_DBG(this->isString());
1287 return CommonStringValue<LibObjT> {this->libObjPtr()};
1290 template <typename LibObjT>
1291 CommonArrayValue<LibObjT> CommonValue<LibObjT>::asArray() const noexcept
1293 BT_ASSERT_DBG(this->isArray());
1294 return CommonArrayValue<LibObjT> {this->libObjPtr()};
1297 template <typename LibObjT>
1298 CommonMapValue<LibObjT> CommonValue<LibObjT>::asMap() const noexcept
1300 BT_ASSERT_DBG(this->isMap());
1301 return CommonMapValue<LibObjT> {this->libObjPtr()};
1304 template <typename LibObjT>
1305 ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray() const
1307 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1309 bt_value *libElemPtr;
1310 const auto status = bt_value_array_append_empty_array_element(this->libObjPtr(), &libElemPtr);
1312 this->_handleAppendLibStatus(status);
1313 return ArrayValue {libElemPtr};
1316 template <typename LibObjT>
1317 MapValue CommonArrayValue<LibObjT>::appendEmptyMap() const
1319 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1321 bt_value *libElemPtr;
1322 const auto status = bt_value_array_append_empty_map_element(this->libObjPtr(), &libElemPtr);
1324 this->_handleAppendLibStatus(status);
1325 return MapValue {libElemPtr};
1328 template <typename LibObjT>
1329 ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key) const
1331 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1333 bt_value *libEntryPtr;
1334 const auto status = bt_value_map_insert_empty_array_entry(this->libObjPtr(), key, &libEntryPtr);
1336 this->_handleInsertLibStatus(status);
1337 return ArrayValue {libEntryPtr};
1340 template <typename LibObjT>
1341 ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const std::string& key) const
1343 return this->insertEmptyArray(key.data());
1346 template <typename LibObjT>
1347 MapValue CommonMapValue<LibObjT>::insertEmptyMap(const char * const key) const
1349 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1351 bt_value *libEntryPtr;
1352 const auto status = bt_value_map_insert_empty_map_entry(this->libObjPtr(), key, &libEntryPtr);
1354 this->_handleInsertLibStatus(status);
1355 return MapValue {libEntryPtr};
1358 template <typename LibObjT>
1359 MapValue CommonMapValue<LibObjT>::insertEmptyMap(const std::string& key) const
1361 return this->insertEmptyMap(key.data());
1364 inline BoolValue::Shared createValue(const bool rawVal)
1366 return BoolValue::create(rawVal);
1369 inline UnsignedIntegerValue::Shared createValue(const std::uint64_t rawVal)
1371 return UnsignedIntegerValue::create(rawVal);
1374 inline SignedIntegerValue::Shared createValue(const std::int64_t rawVal)
1376 return SignedIntegerValue::create(rawVal);
1379 inline RealValue::Shared createValue(const double rawVal)
1381 return RealValue::create(rawVal);
1384 inline StringValue::Shared createValue(const char * const rawVal)
1386 return StringValue::create(rawVal);
1389 inline StringValue::Shared createValue(const std::string& rawVal)
1391 return StringValue::create(rawVal);
1394 } /* namespace bt2 */
1396 #endif /* BABELTRACE_CPP_COMMON_BT2_VALUE_HPP */