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 CommonClockClass;
87 template <typename LibObjT>
88 class CommonFieldClass;
90 template <typename LibObjT>
91 class CommonTraceClass;
93 template <typename LibObjT>
94 class CommonStreamClass;
96 template <typename LibObjT>
97 class CommonEventClass;
99 template <typename LibObjT>
102 template <typename LibObjT>
103 class CommonValue : public BorrowedObject<LibObjT>
105 /* Allow append() to call `val.libObjPtr()` */
106 friend class CommonArrayValue<bt_value>;
108 /* Allow insert() to call `val.libObjPtr()` */
109 friend class CommonMapValue<bt_value>;
111 /* Allow userAttributes() to call `val.libObjPtr()` */
112 friend class CommonClockClass<bt_clock_class>;
113 friend class CommonFieldClass<bt_field_class>;
114 friend class CommonTraceClass<bt_trace_class>;
115 friend class CommonStreamClass<bt_stream_class>;
116 friend class CommonEventClass<bt_event_class>;
117 friend class CommonStream<bt_stream>;
119 /* Allow operator==() to call `other.libObjPtr()` */
120 friend class CommonValue<bt_value>;
121 friend class CommonValue<const bt_value>;
124 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
127 using typename BorrowedObject<LibObjT>::_LibObjPtr;
128 using _ThisCommonValue = CommonValue<LibObjT>;
131 using Shared = SharedValue<CommonValue<LibObjT>, LibObjT>;
133 explicit CommonValue(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
137 template <typename OtherLibObjT>
138 CommonValue(const CommonValue<OtherLibObjT> val) noexcept : _ThisBorrowedObject {val}
142 template <typename OtherLibObjT>
143 _ThisCommonValue& operator=(const CommonValue<OtherLibObjT> val) noexcept
145 _ThisBorrowedObject::operator=(val);
149 ValueType type() const noexcept
151 return static_cast<ValueType>(bt_value_get_type(this->libObjPtr()));
154 bool isNull() const noexcept
156 return this->_libTypeIs(BT_VALUE_TYPE_NULL);
159 bool isBool() const noexcept
161 return this->_libTypeIs(BT_VALUE_TYPE_BOOL);
164 bool isInteger() const noexcept
166 return this->_libTypeIs(BT_VALUE_TYPE_INTEGER);
169 bool isUnsignedInteger() const noexcept
171 return this->_libTypeIs(BT_VALUE_TYPE_UNSIGNED_INTEGER);
174 bool isSignedInteger() const noexcept
176 return this->_libTypeIs(BT_VALUE_TYPE_SIGNED_INTEGER);
179 bool isReal() const noexcept
181 return this->_libTypeIs(BT_VALUE_TYPE_REAL);
184 bool isString() const noexcept
186 return this->_libTypeIs(BT_VALUE_TYPE_STRING);
189 bool isArray() const noexcept
191 return this->_libTypeIs(BT_VALUE_TYPE_ARRAY);
194 bool isMap() const noexcept
196 return this->_libTypeIs(BT_VALUE_TYPE_MAP);
199 template <typename OtherLibObjT>
200 bool operator==(const CommonValue<OtherLibObjT> other) const noexcept
202 return static_cast<bool>(bt_value_is_equal(this->libObjPtr(), other.libObjPtr()));
205 template <typename OtherLibObjT>
206 bool operator!=(const CommonValue<OtherLibObjT> other) const noexcept
208 return !(*this == other);
211 Shared shared() const noexcept
213 return Shared::createWithRef(*this);
216 template <typename ValueT>
217 ValueT as() const noexcept
219 return ValueT {this->libObjPtr()};
222 CommonNullValue<LibObjT> asNull() const noexcept;
223 CommonBoolValue<LibObjT> asBool() const noexcept;
224 CommonSignedIntegerValue<LibObjT> asSignedInteger() const noexcept;
225 CommonUnsignedIntegerValue<LibObjT> asUnsignedInteger() const noexcept;
226 CommonRealValue<LibObjT> asReal() const noexcept;
227 CommonStringValue<LibObjT> asString() const noexcept;
228 CommonArrayValue<LibObjT> asArray() const noexcept;
229 CommonMapValue<LibObjT> asMap() const noexcept;
232 bool _libTypeIs(const bt_value_type type) const noexcept
234 return bt_value_type_is(bt_value_get_type(this->libObjPtr()), type);
238 using Value = CommonValue<bt_value>;
239 using ConstValue = CommonValue<const bt_value>;
243 struct ValueTypeDescr
245 using Const = ConstValue;
246 using NonConst = Value;
250 struct TypeDescr<Value> : public ValueTypeDescr
255 struct TypeDescr<ConstValue> : public ValueTypeDescr
259 } /* namespace internal */
261 template <typename LibObjT>
262 class CommonNullValue final : public CommonValue<LibObjT>
265 using typename CommonValue<LibObjT>::_ThisCommonValue;
268 using Shared = SharedValue<CommonNullValue<LibObjT>, LibObjT>;
270 CommonNullValue() noexcept : _ThisCommonValue {bt_value_null}
274 template <typename OtherLibObjT>
275 CommonNullValue(const CommonNullValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
279 template <typename OtherLibObjT>
280 CommonNullValue<LibObjT>& operator=(const CommonNullValue<OtherLibObjT> val) noexcept
282 _ThisCommonValue::operator=(val);
286 Shared shared() const noexcept
288 return Shared::createWithRef(*this);
292 using NullValue = CommonNullValue<bt_value>;
293 using ConstNullValue = CommonNullValue<const bt_value>;
297 struct NullValueTypeDescr
299 using Const = ConstNullValue;
300 using NonConst = NullValue;
304 struct TypeDescr<NullValue> : public NullValueTypeDescr
309 struct TypeDescr<ConstNullValue> : public NullValueTypeDescr
313 } /* namespace internal */
315 template <typename LibObjT>
316 class CommonBoolValue final : public CommonValue<LibObjT>
319 using typename CommonValue<LibObjT>::_LibObjPtr;
320 using typename CommonValue<LibObjT>::_ThisCommonValue;
323 using Shared = SharedValue<CommonBoolValue<LibObjT>, LibObjT>;
326 explicit CommonBoolValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
328 BT_ASSERT_DBG(this->isBool());
331 template <typename OtherLibObjT>
332 CommonBoolValue(const CommonBoolValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
336 static Shared create(const Value rawVal = false)
338 const auto libObjPtr = bt_value_bool_create_init(static_cast<bt_bool>(rawVal));
340 internal::validateCreatedObjPtr(libObjPtr);
341 return CommonBoolValue::Shared::createWithoutRef(libObjPtr);
344 template <typename OtherLibObjT>
345 CommonBoolValue<LibObjT>& operator=(const CommonBoolValue<OtherLibObjT> val) noexcept
347 _ThisCommonValue::operator=(val);
351 CommonBoolValue<LibObjT> operator=(const Value rawVal) const noexcept
353 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
355 bt_value_bool_set(this->libObjPtr(), static_cast<bt_bool>(rawVal));
359 Value value() const noexcept
361 return static_cast<Value>(bt_value_bool_get(this->libObjPtr()));
364 operator Value() const noexcept
366 return this->value();
369 Shared shared() const noexcept
371 return Shared::createWithRef(*this);
375 using BoolValue = CommonBoolValue<bt_value>;
376 using ConstBoolValue = CommonBoolValue<const bt_value>;
380 struct BoolValueTypeDescr
382 using Const = ConstBoolValue;
383 using NonConst = BoolValue;
387 struct TypeDescr<BoolValue> : public BoolValueTypeDescr
392 struct TypeDescr<ConstBoolValue> : public BoolValueTypeDescr
396 } /* namespace internal */
398 template <typename LibObjT>
399 class CommonUnsignedIntegerValue final : public CommonValue<LibObjT>
402 using typename CommonValue<LibObjT>::_LibObjPtr;
403 using typename CommonValue<LibObjT>::_ThisCommonValue;
406 using Shared = SharedValue<CommonUnsignedIntegerValue<LibObjT>, LibObjT>;
407 using Value = std::uint64_t;
409 explicit CommonUnsignedIntegerValue(const _LibObjPtr libObjPtr) noexcept :
410 _ThisCommonValue {libObjPtr}
412 BT_ASSERT_DBG(this->isUnsignedInteger());
415 static Shared create(const Value rawVal = 0)
417 const auto libObjPtr = bt_value_integer_unsigned_create_init(rawVal);
419 internal::validateCreatedObjPtr(libObjPtr);
420 return CommonUnsignedIntegerValue::Shared::createWithoutRef(libObjPtr);
423 template <typename OtherLibObjT>
424 CommonUnsignedIntegerValue(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept :
425 _ThisCommonValue {val}
429 template <typename OtherLibObjT>
430 CommonUnsignedIntegerValue<LibObjT>&
431 operator=(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept
433 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
435 _ThisCommonValue::operator=(val);
439 CommonUnsignedIntegerValue<LibObjT> operator=(const Value rawVal) const noexcept
441 bt_value_integer_unsigned_set(this->libObjPtr(), rawVal);
445 Value value() const noexcept
447 return bt_value_integer_unsigned_get(this->libObjPtr());
450 operator Value() const noexcept
452 return this->value();
455 Shared shared() const noexcept
457 return Shared::createWithRef(*this);
461 using UnsignedIntegerValue = CommonUnsignedIntegerValue<bt_value>;
462 using ConstUnsignedIntegerValue = CommonUnsignedIntegerValue<const bt_value>;
466 struct UnsignedIntegerValueTypeDescr
468 using Const = ConstUnsignedIntegerValue;
469 using NonConst = UnsignedIntegerValue;
473 struct TypeDescr<UnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
478 struct TypeDescr<ConstUnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
482 } /* namespace internal */
484 template <typename LibObjT>
485 class CommonSignedIntegerValue final : public CommonValue<LibObjT>
488 using typename CommonValue<LibObjT>::_LibObjPtr;
489 using typename CommonValue<LibObjT>::_ThisCommonValue;
492 using Shared = SharedValue<CommonSignedIntegerValue<LibObjT>, LibObjT>;
493 using Value = std::int64_t;
495 explicit CommonSignedIntegerValue(const _LibObjPtr libObjPtr) noexcept :
496 _ThisCommonValue {libObjPtr}
498 BT_ASSERT_DBG(this->isSignedInteger());
501 static Shared create(const Value rawVal = 0)
503 const auto libObjPtr = bt_value_integer_signed_create_init(rawVal);
505 internal::validateCreatedObjPtr(libObjPtr);
506 return CommonSignedIntegerValue::Shared::createWithoutRef(libObjPtr);
509 template <typename OtherLibObjT>
510 CommonSignedIntegerValue(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept :
511 _ThisCommonValue {val}
515 template <typename OtherLibObjT>
516 CommonSignedIntegerValue<LibObjT>
517 operator=(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept
519 _ThisCommonValue::operator=(val);
523 CommonSignedIntegerValue<LibObjT> operator=(const Value rawVal) const noexcept
525 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
527 bt_value_integer_signed_set(this->libObjPtr(), rawVal);
531 Value value() const noexcept
533 return bt_value_integer_signed_get(this->libObjPtr());
536 operator Value() const noexcept
538 return this->value();
541 Shared shared() const noexcept
543 return Shared::createWithRef(*this);
547 using SignedIntegerValue = CommonSignedIntegerValue<bt_value>;
548 using ConstSignedIntegerValue = CommonSignedIntegerValue<const bt_value>;
552 struct SignedIntegerValueTypeDescr
554 using Const = ConstSignedIntegerValue;
555 using NonConst = SignedIntegerValue;
559 struct TypeDescr<SignedIntegerValue> : public SignedIntegerValueTypeDescr
564 struct TypeDescr<ConstSignedIntegerValue> : public SignedIntegerValueTypeDescr
568 } /* namespace internal */
570 template <typename LibObjT>
571 class CommonRealValue final : public CommonValue<LibObjT>
574 using typename CommonValue<LibObjT>::_LibObjPtr;
575 using typename CommonValue<LibObjT>::_ThisCommonValue;
578 using Shared = SharedValue<CommonRealValue<LibObjT>, LibObjT>;
579 using Value = double;
581 explicit CommonRealValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
583 BT_ASSERT_DBG(this->isReal());
586 static Shared create(const Value rawVal = 0)
588 const auto libObjPtr = bt_value_real_create_init(rawVal);
590 internal::validateCreatedObjPtr(libObjPtr);
591 return CommonRealValue::Shared::createWithoutRef(libObjPtr);
594 template <typename OtherLibObjT>
595 CommonRealValue(const CommonRealValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
599 template <typename OtherLibObjT>
600 CommonRealValue<LibObjT>& operator=(const CommonRealValue<OtherLibObjT> val) noexcept
602 _ThisCommonValue::operator=(val);
606 CommonRealValue<LibObjT> operator=(const Value rawVal) const noexcept
608 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
610 bt_value_real_set(this->libObjPtr(), rawVal);
614 Value value() const noexcept
616 return bt_value_real_get(this->libObjPtr());
619 operator Value() const noexcept
621 return this->value();
624 Shared shared() const noexcept
626 return Shared::createWithRef(*this);
630 using RealValue = CommonRealValue<bt_value>;
631 using ConstRealValue = CommonRealValue<const bt_value>;
635 struct RealValueTypeDescr
637 using Const = ConstRealValue;
638 using NonConst = RealValue;
642 struct TypeDescr<RealValue> : public RealValueTypeDescr
647 struct TypeDescr<ConstRealValue> : public RealValueTypeDescr
651 } /* namespace internal */
653 template <typename LibObjT>
654 class CommonStringValue final : public CommonValue<LibObjT>
657 using typename CommonValue<LibObjT>::_LibObjPtr;
658 using typename CommonValue<LibObjT>::_ThisCommonValue;
661 using Shared = SharedValue<CommonStringValue<LibObjT>, LibObjT>;
663 explicit CommonStringValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
665 BT_ASSERT_DBG(this->isString());
668 static Shared create(const char * const rawVal = "")
670 const auto libObjPtr = bt_value_string_create_init(rawVal);
672 internal::validateCreatedObjPtr(libObjPtr);
673 return CommonStringValue::Shared::createWithoutRef(libObjPtr);
676 static Shared create(const std::string& rawVal)
678 return CommonStringValue::create(rawVal.data());
681 template <typename OtherLibObjT>
682 CommonStringValue(const CommonStringValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
686 template <typename OtherLibObjT>
687 CommonStringValue<LibObjT>& operator=(const CommonStringValue<OtherLibObjT> val) noexcept
689 _ThisCommonValue::operator=(val);
693 CommonStringValue<LibObjT> operator=(const char * const rawVal) const
695 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
697 const auto status = bt_value_string_set(this->libObjPtr(), rawVal);
699 if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) {
700 throw MemoryError {};
706 CommonStringValue<LibObjT> operator=(const std::string& rawVal) const noexcept
708 return *this = rawVal.data();
711 bpstd::string_view value() const noexcept
713 return bt_value_string_get(this->libObjPtr());
716 Shared shared() const noexcept
718 return Shared::createWithRef(*this);
722 using StringValue = CommonStringValue<bt_value>;
723 using ConstStringValue = CommonStringValue<const bt_value>;
727 struct StringValueTypeDescr
729 using Const = ConstStringValue;
730 using NonConst = StringValue;
734 struct TypeDescr<StringValue> : public StringValueTypeDescr
739 struct TypeDescr<ConstStringValue> : public StringValueTypeDescr
743 template <typename LibObjT>
744 struct CommonArrayValueSpec;
746 /* Functions specific to mutable array values */
748 struct CommonArrayValueSpec<bt_value> final
750 static bt_value *elementByIndex(bt_value * const libValPtr, const std::uint64_t index) noexcept
752 return bt_value_array_borrow_element_by_index(libValPtr, index);
756 /* Functions specific to constant array values */
758 struct CommonArrayValueSpec<const bt_value> final
760 static const bt_value *elementByIndex(const bt_value * const libValPtr,
761 const std::uint64_t index) noexcept
763 return bt_value_array_borrow_element_by_index_const(libValPtr, index);
767 } /* namespace internal */
769 template <typename LibObjT>
770 class CommonArrayValue final : public CommonValue<LibObjT>
773 using typename CommonValue<LibObjT>::_LibObjPtr;
774 using typename CommonValue<LibObjT>::_ThisCommonValue;
777 using Shared = SharedValue<CommonArrayValue<LibObjT>, LibObjT>;
778 using Iterator = CommonIterator<CommonArrayValue<LibObjT>, CommonValue<LibObjT>>;
780 explicit CommonArrayValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
782 BT_ASSERT_DBG(this->isArray());
785 static Shared create()
787 const auto libObjPtr = bt_value_array_create();
789 internal::validateCreatedObjPtr(libObjPtr);
790 return CommonArrayValue::Shared::createWithoutRef(libObjPtr);
793 template <typename OtherLibObjT>
794 CommonArrayValue(const CommonArrayValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
798 template <typename OtherLibObjT>
799 CommonArrayValue<LibObjT>& operator=(const CommonArrayValue<OtherLibObjT> val) noexcept
801 _ThisCommonValue::operator=(val);
805 std::uint64_t length() const noexcept
807 return bt_value_array_get_length(this->libObjPtr());
810 /* Required by the `CommonIterator` template class */
811 std::uint64_t size() const noexcept
813 return this->length();
816 Iterator begin() const noexcept
818 return Iterator {*this, 0};
821 Iterator end() const noexcept
823 return Iterator {*this, this->length()};
826 bool isEmpty() const noexcept
828 return this->length() == 0;
831 CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept
833 return CommonValue<LibObjT> {
834 internal::CommonArrayValueSpec<LibObjT>::elementByIndex(this->libObjPtr(), index)};
837 void append(const Value val) const
839 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
841 const auto status = bt_value_array_append_element(this->libObjPtr(), val.libObjPtr());
843 this->_handleAppendLibStatus(status);
846 void append(const bool rawVal) const
848 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
851 bt_value_array_append_bool_element(this->libObjPtr(), static_cast<bt_bool>(rawVal));
853 this->_handleAppendLibStatus(status);
856 void append(const std::uint64_t rawVal) const
858 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
861 bt_value_array_append_unsigned_integer_element(this->libObjPtr(), rawVal);
863 this->_handleAppendLibStatus(status);
866 void append(const std::int64_t rawVal) const
868 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
870 const auto status = bt_value_array_append_signed_integer_element(this->libObjPtr(), rawVal);
872 this->_handleAppendLibStatus(status);
875 void append(const double rawVal) const
877 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
879 const auto status = bt_value_array_append_real_element(this->libObjPtr(), rawVal);
881 this->_handleAppendLibStatus(status);
884 void append(const char * const rawVal) const
886 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
888 const auto status = bt_value_array_append_string_element(this->libObjPtr(), rawVal);
890 this->_handleAppendLibStatus(status);
893 void append(const std::string& rawVal) const
895 this->append(rawVal.data());
898 CommonArrayValue<bt_value> appendEmptyArray() const;
899 CommonMapValue<bt_value> appendEmptyMap() const;
901 void operator+=(const Value val) const
906 void operator+=(const bool rawVal) const
908 this->append(rawVal);
911 void operator+=(const std::uint64_t rawVal) const
913 this->append(rawVal);
916 void operator+=(const std::int64_t rawVal) const
918 this->append(rawVal);
921 void operator+=(const double rawVal) const
923 this->append(rawVal);
926 void operator+=(const char * const rawVal) const
928 this->append(rawVal);
931 void operator+=(const std::string& rawVal) const
933 this->append(rawVal);
936 Shared shared() const noexcept
938 return Shared::createWithRef(*this);
942 void _handleAppendLibStatus(const bt_value_array_append_element_status status) const
944 if (status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR) {
945 throw MemoryError {};
950 using ArrayValue = CommonArrayValue<bt_value>;
951 using ConstArrayValue = CommonArrayValue<const bt_value>;
955 struct ArrayValueTypeDescr
957 using Const = ConstArrayValue;
958 using NonConst = ArrayValue;
962 struct TypeDescr<ArrayValue> : public ArrayValueTypeDescr
967 struct TypeDescr<ConstArrayValue> : public ArrayValueTypeDescr
972 * Type of a user function passed to `CommonMapValue<ObjT>::forEach()`.
974 * First argument is the entry's key, second is its value.
976 template <typename ObjT>
977 using CommonMapValueForEachUserFunc = std::function<void(const bpstd::string_view&, ObjT)>;
980 * Template of a function to be passed to bt_value_map_foreach_entry()
981 * for bt_value_map_foreach_entry_const() which calls a user function.
983 * `userData` is casted to a `const` pointer to
984 * `CommonMapValueForEachUserFunc<ObjT>` (the user function to call).
986 * This function catches any exception which the user function throws
987 * and returns the `ErrorStatus` value. If there's no execption, this
988 * function returns the `OkStatus` value.
990 template <typename ObjT, typename LibObjT, typename LibStatusT, int OkStatus, int ErrorStatus>
991 LibStatusT mapValueForEachLibFunc(const char * const key, LibObjT * const libObjPtr,
992 void * const userData)
994 const auto& userFunc = *reinterpret_cast<const CommonMapValueForEachUserFunc<ObjT> *>(userData);
997 userFunc(key, ObjT {libObjPtr});
999 return static_cast<LibStatusT>(ErrorStatus);
1002 return static_cast<LibStatusT>(OkStatus);
1005 template <typename LibObjT>
1006 struct CommonMapValueSpec;
1008 /* Functions specific to mutable map values */
1010 struct CommonMapValueSpec<bt_value> final
1012 static bt_value *entryByKey(bt_value * const libValPtr, const char * const key) noexcept
1014 return bt_value_map_borrow_entry_value(libValPtr, key);
1017 static void forEach(bt_value * const libValPtr,
1018 const CommonMapValueForEachUserFunc<Value>& func)
1020 const auto status = bt_value_map_foreach_entry(
1022 mapValueForEachLibFunc<Value, bt_value, bt_value_map_foreach_entry_func_status,
1023 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK,
1024 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR>,
1025 const_cast<void *>(reinterpret_cast<const void *>(&func)));
1028 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_OK:
1030 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR:
1031 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR:
1039 /* Functions specific to constant map values */
1041 struct CommonMapValueSpec<const bt_value> final
1043 static const bt_value *entryByKey(const bt_value * const libValPtr,
1044 const char * const key) noexcept
1046 return bt_value_map_borrow_entry_value_const(libValPtr, key);
1049 static void forEach(const bt_value * const libValPtr,
1050 const CommonMapValueForEachUserFunc<ConstValue>& func)
1052 const auto status = bt_value_map_foreach_entry_const(
1054 mapValueForEachLibFunc<ConstValue, const bt_value,
1055 bt_value_map_foreach_entry_const_func_status,
1056 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK,
1057 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_ERROR>,
1058 const_cast<void *>(reinterpret_cast<const void *>(&func)));
1061 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK:
1063 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_USER_ERROR:
1064 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR:
1072 } /* namespace internal */
1074 template <typename LibObjT>
1075 class CommonMapValue final : public CommonValue<LibObjT>
1078 using typename CommonValue<LibObjT>::_LibObjPtr;
1079 using typename CommonValue<LibObjT>::_ThisCommonValue;
1082 using Shared = SharedValue<CommonMapValue<LibObjT>, LibObjT>;
1084 explicit CommonMapValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
1086 BT_ASSERT_DBG(this->isMap());
1089 static Shared create()
1091 const auto libObjPtr = bt_value_map_create();
1093 internal::validateCreatedObjPtr(libObjPtr);
1094 return CommonMapValue::Shared::createWithoutRef(libObjPtr);
1097 template <typename OtherLibObjT>
1098 CommonMapValue(const CommonMapValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
1102 template <typename OtherLibObjT>
1103 CommonMapValue<LibObjT>& operator=(const CommonMapValue<OtherLibObjT> val) noexcept
1105 _ThisCommonValue::operator=(val);
1109 std::uint64_t size() const noexcept
1111 return bt_value_map_get_size(this->libObjPtr());
1114 bool isEmpty() const noexcept
1116 return this->size() == 0;
1119 nonstd::optional<CommonValue<LibObjT>> operator[](const char * const key) const noexcept
1121 const auto libObjPtr =
1122 internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key);
1125 return nonstd::nullopt;
1128 return CommonValue<LibObjT> {libObjPtr};
1131 nonstd::optional<CommonValue<LibObjT>> operator[](const std::string& key) const noexcept
1133 return (*this)[key.data()];
1136 bool hasEntry(const char * const key) const noexcept
1138 return static_cast<bool>(bt_value_map_has_entry(this->libObjPtr(), key));
1141 bool hasEntry(const std::string& key) const noexcept
1143 return this->hasEntry(key.data());
1146 void insert(const char * const key, const Value val) const
1148 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1150 const auto status = bt_value_map_insert_entry(this->libObjPtr(), key, val.libObjPtr());
1152 this->_handleInsertLibStatus(status);
1155 void insert(const std::string& key, const Value val) const
1157 this->insert(key.data(), val);
1160 void insert(const char * const key, const bool rawVal) const
1162 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1165 bt_value_map_insert_bool_entry(this->libObjPtr(), key, static_cast<bt_bool>(rawVal));
1167 this->_handleInsertLibStatus(status);
1170 void insert(const std::string& key, const bool rawVal) const
1172 this->insert(key.data(), rawVal);
1175 void insert(const char * const key, const std::uint64_t rawVal) const
1177 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1180 bt_value_map_insert_unsigned_integer_entry(this->libObjPtr(), key, rawVal);
1182 this->_handleInsertLibStatus(status);
1185 void insert(const std::string& key, const std::uint64_t rawVal) const
1187 this->insert(key.data(), rawVal);
1190 void insert(const char * const key, const std::int64_t rawVal) const
1192 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1195 bt_value_map_insert_signed_integer_entry(this->libObjPtr(), key, rawVal);
1197 this->_handleInsertLibStatus(status);
1200 void insert(const std::string& key, const std::int64_t rawVal) const
1202 this->insert(key.data(), rawVal);
1205 void insert(const char * const key, const double rawVal) const
1207 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1209 const auto status = bt_value_map_insert_real_entry(this->libObjPtr(), key, rawVal);
1211 this->_handleInsertLibStatus(status);
1214 void insert(const std::string& key, const double rawVal) const
1216 this->insert(key.data(), rawVal);
1219 void insert(const char * const key, const char * const rawVal) const
1221 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1223 const auto status = bt_value_map_insert_string_entry(this->libObjPtr(), key, rawVal);
1225 this->_handleInsertLibStatus(status);
1228 void insert(const char * const key, const std::string& rawVal) const
1230 this->insert(key, rawVal.data());
1233 void insert(const std::string& key, const char * const rawVal) const
1235 this->insert(key.data(), rawVal);
1238 void insert(const std::string& key, const std::string& rawVal) const
1240 this->insert(key.data(), rawVal.data());
1243 CommonArrayValue<bt_value> insertEmptyArray(const char *key) const;
1244 CommonArrayValue<bt_value> insertEmptyArray(const std::string& key) const;
1245 CommonMapValue<bt_value> insertEmptyMap(const char *key) const;
1246 CommonMapValue<bt_value> insertEmptyMap(const std::string& key) const;
1248 void forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func) const
1250 internal::CommonMapValueSpec<LibObjT>::forEach(this->libObjPtr(), func);
1253 Shared shared() const noexcept
1255 return Shared::createWithRef(*this);
1259 void _handleInsertLibStatus(const bt_value_map_insert_entry_status status) const
1261 if (status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_MEMORY_ERROR) {
1262 throw MemoryError {};
1267 using MapValue = CommonMapValue<bt_value>;
1268 using ConstMapValue = CommonMapValue<const bt_value>;
1270 namespace internal {
1272 struct MapValueTypeDescr
1274 using Const = ConstMapValue;
1275 using NonConst = MapValue;
1279 struct TypeDescr<MapValue> : public MapValueTypeDescr
1284 struct TypeDescr<ConstMapValue> : public MapValueTypeDescr
1288 } /* namespace internal */
1290 template <typename LibObjT>
1291 CommonNullValue<LibObjT> CommonValue<LibObjT>::asNull() const noexcept
1293 BT_ASSERT_DBG(this->isNull());
1294 return CommonNullValue<LibObjT> {this->libObjPtr()};
1297 template <typename LibObjT>
1298 CommonBoolValue<LibObjT> CommonValue<LibObjT>::asBool() const noexcept
1300 BT_ASSERT_DBG(this->isBool());
1301 return CommonBoolValue<LibObjT> {this->libObjPtr()};
1304 template <typename LibObjT>
1305 CommonSignedIntegerValue<LibObjT> CommonValue<LibObjT>::asSignedInteger() const noexcept
1307 BT_ASSERT_DBG(this->isSignedInteger());
1308 return CommonSignedIntegerValue<LibObjT> {this->libObjPtr()};
1311 template <typename LibObjT>
1312 CommonUnsignedIntegerValue<LibObjT> CommonValue<LibObjT>::asUnsignedInteger() const noexcept
1314 BT_ASSERT_DBG(this->isUnsignedInteger());
1315 return CommonUnsignedIntegerValue<LibObjT> {this->libObjPtr()};
1318 template <typename LibObjT>
1319 CommonRealValue<LibObjT> CommonValue<LibObjT>::asReal() const noexcept
1321 BT_ASSERT_DBG(this->isReal());
1322 return CommonRealValue<LibObjT> {this->libObjPtr()};
1325 template <typename LibObjT>
1326 CommonStringValue<LibObjT> CommonValue<LibObjT>::asString() const noexcept
1328 BT_ASSERT_DBG(this->isString());
1329 return CommonStringValue<LibObjT> {this->libObjPtr()};
1332 template <typename LibObjT>
1333 CommonArrayValue<LibObjT> CommonValue<LibObjT>::asArray() const noexcept
1335 BT_ASSERT_DBG(this->isArray());
1336 return CommonArrayValue<LibObjT> {this->libObjPtr()};
1339 template <typename LibObjT>
1340 CommonMapValue<LibObjT> CommonValue<LibObjT>::asMap() const noexcept
1342 BT_ASSERT_DBG(this->isMap());
1343 return CommonMapValue<LibObjT> {this->libObjPtr()};
1346 template <typename LibObjT>
1347 ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray() const
1349 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1351 bt_value *libElemPtr;
1352 const auto status = bt_value_array_append_empty_array_element(this->libObjPtr(), &libElemPtr);
1354 this->_handleAppendLibStatus(status);
1355 return ArrayValue {libElemPtr};
1358 template <typename LibObjT>
1359 MapValue CommonArrayValue<LibObjT>::appendEmptyMap() const
1361 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1363 bt_value *libElemPtr;
1364 const auto status = bt_value_array_append_empty_map_element(this->libObjPtr(), &libElemPtr);
1366 this->_handleAppendLibStatus(status);
1367 return MapValue {libElemPtr};
1370 template <typename LibObjT>
1371 ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key) const
1373 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1375 bt_value *libEntryPtr;
1376 const auto status = bt_value_map_insert_empty_array_entry(this->libObjPtr(), key, &libEntryPtr);
1378 this->_handleInsertLibStatus(status);
1379 return ArrayValue {libEntryPtr};
1382 template <typename LibObjT>
1383 ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const std::string& key) const
1385 return this->insertEmptyArray(key.data());
1388 template <typename LibObjT>
1389 MapValue CommonMapValue<LibObjT>::insertEmptyMap(const char * const key) const
1391 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1393 bt_value *libEntryPtr;
1394 const auto status = bt_value_map_insert_empty_map_entry(this->libObjPtr(), key, &libEntryPtr);
1396 this->_handleInsertLibStatus(status);
1397 return MapValue {libEntryPtr};
1400 template <typename LibObjT>
1401 MapValue CommonMapValue<LibObjT>::insertEmptyMap(const std::string& key) const
1403 return this->insertEmptyMap(key.data());
1406 inline BoolValue::Shared createValue(const bool rawVal)
1408 return BoolValue::create(rawVal);
1411 inline UnsignedIntegerValue::Shared createValue(const std::uint64_t rawVal)
1413 return UnsignedIntegerValue::create(rawVal);
1416 inline SignedIntegerValue::Shared createValue(const std::int64_t rawVal)
1418 return SignedIntegerValue::create(rawVal);
1421 inline RealValue::Shared createValue(const double rawVal)
1423 return RealValue::create(rawVal);
1426 inline StringValue::Shared createValue(const char * const rawVal)
1428 return StringValue::create(rawVal);
1431 inline StringValue::Shared createValue(const std::string& rawVal)
1433 return StringValue::create(rawVal);
1436 } /* namespace bt2 */
1438 #endif /* BABELTRACE_CPP_COMMON_BT2_VALUE_HPP */