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
10 #include <type_traits>
13 #include <babeltrace2/babeltrace.h>
15 #include "common/assert.h"
16 #include "common/common.h"
17 #include "internal/borrowed-obj.hpp"
18 #include "internal/shared-obj.hpp"
19 #include "internal/utils.hpp"
20 #include "cpp-common/optional.hpp"
21 #include "cpp-common/string_view.hpp"
22 #include "lib-error.hpp"
28 struct ValueRefFuncs final
30 static void get(const bt_value * const libObjPtr)
32 bt_value_get_ref(libObjPtr);
35 static void put(const bt_value * const libObjPtr)
37 bt_value_put_ref(libObjPtr);
41 template <typename ObjT, typename LibObjT>
42 using SharedValue = internal::SharedObj<ObjT, LibObjT, internal::ValueRefFuncs>;
44 } // namespace internal
46 template <typename LibObjT>
47 class CommonNullValue;
49 template <typename LibObjT>
50 class CommonBoolValue;
52 template <typename LibObjT>
53 class CommonUnsignedIntegerValue;
55 template <typename LibObjT>
56 class CommonSignedIntegerValue;
58 template <typename LibObjT>
59 class CommonRealValue;
61 template <typename LibObjT>
62 class CommonStringValue;
64 template <typename LibObjT>
65 class CommonArrayValue;
67 template <typename LibObjT>
72 NUL = BT_VALUE_TYPE_NULL,
73 BOOL = BT_VALUE_TYPE_BOOL,
74 UNSIGNED_INTEGER = BT_VALUE_TYPE_UNSIGNED_INTEGER,
75 SIGNED_INTEGER = BT_VALUE_TYPE_SIGNED_INTEGER,
76 REAL = BT_VALUE_TYPE_REAL,
77 STRING = BT_VALUE_TYPE_STRING,
78 ARRAY = BT_VALUE_TYPE_ARRAY,
79 MAP = BT_VALUE_TYPE_MAP,
82 template <typename LibObjT>
83 class CommonClockClass;
85 template <typename LibObjT>
86 class CommonValue : public internal::BorrowedObj<LibObjT>
88 // Allow append() to call `val._libObjPtr()`
89 friend class CommonArrayValue<bt_value>;
91 // Allow insert() to call `val._libObjPtr()`
92 friend class CommonMapValue<bt_value>;
94 // Allow userAttributes() to call `val._libObjPtr()`
95 friend class CommonClockClass<bt_clock_class>;
97 // Allow operator==() to call `other._libObjPtr()`
98 friend class CommonValue<bt_value>;
99 friend class CommonValue<const bt_value>;
102 using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
105 using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
106 using _ThisCommonValue = CommonValue<LibObjT>;
109 using Shared = internal::SharedValue<CommonValue<LibObjT>, LibObjT>;
111 explicit CommonValue(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
115 template <typename OtherLibObjT>
116 CommonValue(const CommonValue<OtherLibObjT>& val) noexcept : _ThisBorrowedObj {val}
120 template <typename OtherLibObjT>
121 _ThisCommonValue& operator=(const CommonValue<OtherLibObjT>& val) noexcept
123 _ThisBorrowedObj::operator=(val);
127 ValueType type() const noexcept
129 return static_cast<ValueType>(bt_value_get_type(this->_libObjPtr()));
132 bool isNull() const noexcept
134 return this->_libTypeIs(BT_VALUE_TYPE_NULL);
137 bool isBool() const noexcept
139 return this->_libTypeIs(BT_VALUE_TYPE_BOOL);
142 bool isInteger() const noexcept
144 return this->_libTypeIs(BT_VALUE_TYPE_INTEGER);
147 bool isUnsignedInteger() const noexcept
149 return this->_libTypeIs(BT_VALUE_TYPE_UNSIGNED_INTEGER);
152 bool isSignedInteger() const noexcept
154 return this->_libTypeIs(BT_VALUE_TYPE_SIGNED_INTEGER);
157 bool isReal() const noexcept
159 return this->_libTypeIs(BT_VALUE_TYPE_REAL);
162 bool isString() const noexcept
164 return this->_libTypeIs(BT_VALUE_TYPE_STRING);
167 bool isArray() const noexcept
169 return this->_libTypeIs(BT_VALUE_TYPE_ARRAY);
172 bool isMap() const noexcept
174 return this->_libTypeIs(BT_VALUE_TYPE_MAP);
177 template <typename OtherLibObjT>
178 bool operator==(const CommonValue<OtherLibObjT>& other) const noexcept
180 return static_cast<bool>(bt_value_is_equal(this->_libObjPtr(), other._libObjPtr()));
183 template <typename OtherLibObjT>
184 bool operator!=(const CommonValue<OtherLibObjT>& other) const noexcept
186 return !(*this == other);
189 Shared shared() const noexcept
191 return Shared {*this};
194 CommonNullValue<LibObjT> asNull() const noexcept;
195 CommonBoolValue<LibObjT> asBool() const noexcept;
196 CommonSignedIntegerValue<LibObjT> asSignedInteger() const noexcept;
197 CommonUnsignedIntegerValue<LibObjT> asUnsignedInteger() const noexcept;
198 CommonRealValue<LibObjT> asReal() const noexcept;
199 CommonStringValue<LibObjT> asString() const noexcept;
200 CommonArrayValue<LibObjT> asArray() const noexcept;
201 CommonMapValue<LibObjT> asMap() const noexcept;
204 bool _libTypeIs(const bt_value_type type) const noexcept
206 return bt_value_type_is(bt_value_get_type(this->_libObjPtr()), type);
210 using Value = CommonValue<bt_value>;
211 using ConstValue = CommonValue<const bt_value>;
213 template <typename LibObjT>
214 class CommonNullValue final : public CommonValue<LibObjT>
217 using typename CommonValue<LibObjT>::_ThisCommonValue;
220 using Shared = internal::SharedValue<CommonNullValue<LibObjT>, LibObjT>;
222 CommonNullValue() noexcept : _ThisCommonValue {bt_value_null}
226 template <typename OtherLibObjT>
227 CommonNullValue(const CommonNullValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
231 template <typename OtherLibObjT>
232 CommonNullValue<LibObjT>& operator=(const CommonNullValue<OtherLibObjT>& val) noexcept
234 _ThisCommonValue::operator=(val);
238 Shared shared() const noexcept
240 return Shared {*this};
244 using NullValue = CommonNullValue<bt_value>;
245 using ConstNullValue = CommonNullValue<const bt_value>;
247 template <typename LibObjT>
248 class CommonBoolValue final : public CommonValue<LibObjT>
251 using typename CommonValue<LibObjT>::_LibObjPtr;
252 using typename CommonValue<LibObjT>::_ThisCommonValue;
255 using Shared = internal::SharedValue<CommonBoolValue<LibObjT>, LibObjT>;
258 explicit CommonBoolValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
260 BT_ASSERT_DBG(this->isBool());
263 template <typename OtherLibObjT>
264 CommonBoolValue(const CommonBoolValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
268 static Shared create(const Value rawVal = false)
270 const auto libObjPtr = bt_value_bool_create_init(static_cast<bt_bool>(rawVal));
272 internal::validateCreatedObjPtr(libObjPtr);
273 return Shared {CommonBoolValue<LibObjT> {libObjPtr}};
276 template <typename OtherLibObjT>
277 CommonBoolValue<LibObjT>& operator=(const CommonBoolValue<OtherLibObjT>& val) noexcept
279 _ThisCommonValue::operator=(val);
283 CommonBoolValue<LibObjT>& operator=(const Value rawVal) noexcept
285 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
287 bt_value_bool_set(this->_libObjPtr(), static_cast<bt_bool>(rawVal));
291 Value value() const noexcept
293 return static_cast<Value>(bt_value_bool_get(this->_libObjPtr()));
296 operator Value() const noexcept
298 return this->value();
301 Shared shared() const noexcept
303 return Shared {*this};
307 using BoolValue = CommonBoolValue<bt_value>;
308 using ConstBoolValue = CommonBoolValue<const bt_value>;
310 template <typename LibObjT>
311 class CommonUnsignedIntegerValue final : public CommonValue<LibObjT>
314 using typename CommonValue<LibObjT>::_LibObjPtr;
315 using typename CommonValue<LibObjT>::_ThisCommonValue;
318 using Shared = internal::SharedValue<CommonUnsignedIntegerValue<LibObjT>, LibObjT>;
319 using Value = std::uint64_t;
321 explicit CommonUnsignedIntegerValue(const _LibObjPtr libObjPtr) noexcept :
322 _ThisCommonValue {libObjPtr}
324 BT_ASSERT_DBG(this->isUnsignedInteger());
327 static Shared create(const Value rawVal = 0)
329 const auto libObjPtr = bt_value_integer_unsigned_create_init(rawVal);
331 internal::validateCreatedObjPtr(libObjPtr);
332 return Shared {CommonUnsignedIntegerValue<LibObjT> {libObjPtr}};
335 template <typename OtherLibObjT>
336 CommonUnsignedIntegerValue(const CommonUnsignedIntegerValue<OtherLibObjT>& val) noexcept :
337 _ThisCommonValue {val}
341 template <typename OtherLibObjT>
342 CommonUnsignedIntegerValue<LibObjT>&
343 operator=(const CommonUnsignedIntegerValue<OtherLibObjT>& val) noexcept
345 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
347 _ThisCommonValue::operator=(val);
351 CommonUnsignedIntegerValue<LibObjT>& operator=(const Value rawVal) noexcept
353 bt_value_integer_unsigned_set(this->_libObjPtr(), rawVal);
357 Value value() const noexcept
359 return bt_value_integer_unsigned_get(this->_libObjPtr());
362 operator Value() const noexcept
364 return this->value();
367 Shared shared() const noexcept
369 return Shared {*this};
373 using UnsignedIntegerValue = CommonUnsignedIntegerValue<bt_value>;
374 using ConstUnsignedIntegerValue = CommonUnsignedIntegerValue<const bt_value>;
376 template <typename LibObjT>
377 class CommonSignedIntegerValue final : public CommonValue<LibObjT>
380 using typename CommonValue<LibObjT>::_LibObjPtr;
381 using typename CommonValue<LibObjT>::_ThisCommonValue;
384 using Shared = internal::SharedValue<CommonSignedIntegerValue<LibObjT>, LibObjT>;
385 using Value = std::int64_t;
387 explicit CommonSignedIntegerValue(const _LibObjPtr libObjPtr) noexcept :
388 _ThisCommonValue {libObjPtr}
390 BT_ASSERT_DBG(this->isSignedInteger());
393 static Shared create(const Value rawVal = 0)
395 const auto libObjPtr = bt_value_integer_signed_create_init(rawVal);
397 internal::validateCreatedObjPtr(libObjPtr);
398 return Shared {CommonSignedIntegerValue<LibObjT> {libObjPtr}};
401 template <typename OtherLibObjT>
402 CommonSignedIntegerValue(const CommonSignedIntegerValue<OtherLibObjT>& val) noexcept :
403 _ThisCommonValue {val}
407 template <typename OtherLibObjT>
408 CommonSignedIntegerValue<LibObjT>&
409 operator=(const CommonSignedIntegerValue<OtherLibObjT>& val) noexcept
411 _ThisCommonValue::operator=(val);
415 CommonSignedIntegerValue<LibObjT>& operator=(const Value rawVal) noexcept
417 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
419 bt_value_integer_signed_set(this->_libObjPtr(), rawVal);
423 Value value() const noexcept
425 return bt_value_integer_signed_get(this->_libObjPtr());
428 operator Value() const noexcept
430 return this->value();
433 Shared shared() const noexcept
435 return Shared {*this};
439 using SignedIntegerValue = CommonSignedIntegerValue<bt_value>;
440 using ConstSignedIntegerValue = CommonSignedIntegerValue<const bt_value>;
442 template <typename LibObjT>
443 class CommonRealValue final : public CommonValue<LibObjT>
446 using typename CommonValue<LibObjT>::_LibObjPtr;
447 using typename CommonValue<LibObjT>::_ThisCommonValue;
450 using Shared = internal::SharedValue<CommonRealValue<LibObjT>, LibObjT>;
451 using Value = double;
453 explicit CommonRealValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
455 BT_ASSERT_DBG(this->isReal());
458 static Shared create(const Value rawVal = 0)
460 const auto libObjPtr = bt_value_real_create_init(rawVal);
462 internal::validateCreatedObjPtr(libObjPtr);
463 return Shared {CommonRealValue<LibObjT> {libObjPtr}};
466 template <typename OtherLibObjT>
467 CommonRealValue(const CommonRealValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
471 template <typename OtherLibObjT>
472 CommonRealValue<LibObjT>& operator=(const CommonRealValue<OtherLibObjT>& val) noexcept
474 _ThisCommonValue::operator=(val);
478 CommonRealValue<LibObjT>& operator=(const Value rawVal) noexcept
480 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
482 bt_value_real_set(this->_libObjPtr(), rawVal);
486 Value value() const noexcept
488 return bt_value_real_get(this->_libObjPtr());
491 operator Value() const noexcept
493 return this->value();
496 Shared shared() const noexcept
498 return Shared {*this};
502 using RealValue = CommonRealValue<bt_value>;
503 using ConstRealValue = CommonRealValue<const bt_value>;
505 template <typename LibObjT>
506 class CommonStringValue final : public CommonValue<LibObjT>
509 using typename CommonValue<LibObjT>::_LibObjPtr;
510 using typename CommonValue<LibObjT>::_ThisCommonValue;
513 using Shared = internal::SharedValue<CommonStringValue<LibObjT>, LibObjT>;
515 explicit CommonStringValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
517 BT_ASSERT_DBG(this->isString());
520 static Shared create(const char * const rawVal = "")
522 const auto libObjPtr = bt_value_string_create_init(rawVal);
524 internal::validateCreatedObjPtr(libObjPtr);
525 return Shared {CommonStringValue<LibObjT> {libObjPtr}};
528 static Shared create(const std::string& rawVal)
530 return CommonStringValue<LibObjT>::create(rawVal.data());
533 template <typename OtherLibObjT>
534 CommonStringValue(const CommonStringValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
538 template <typename OtherLibObjT>
539 CommonStringValue<LibObjT>& operator=(const CommonStringValue<OtherLibObjT>& val) noexcept
541 _ThisCommonValue::operator=(val);
545 CommonStringValue<LibObjT>& operator=(const char * const rawVal)
547 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
549 const auto status = bt_value_string_set(this->_libObjPtr(), rawVal);
551 if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) {
552 throw LibMemoryError {};
558 CommonStringValue<LibObjT>& operator=(const std::string& rawVal) noexcept
560 return *this = rawVal.data();
563 bpstd::string_view value() const noexcept
565 return bt_value_string_get(this->_libObjPtr());
568 Shared shared() const noexcept
570 return Shared {*this};
574 using StringValue = CommonStringValue<bt_value>;
575 using ConstStringValue = CommonStringValue<const bt_value>;
579 template <typename LibObjT>
580 struct CommonArrayValueSpec;
582 // Functions specific to mutable array values
584 struct CommonArrayValueSpec<bt_value> final
586 static bt_value *elementByIndex(bt_value * const libValPtr, const std::uint64_t index) noexcept
588 return bt_value_array_borrow_element_by_index(libValPtr, index);
592 // Functions specific to constant array values
594 struct CommonArrayValueSpec<const bt_value> final
596 static const bt_value *elementByIndex(const bt_value * const libValPtr,
597 const std::uint64_t index) noexcept
599 return bt_value_array_borrow_element_by_index_const(libValPtr, index);
603 } // namespace internal
605 template <typename LibObjT>
606 class CommonArrayValue final : public CommonValue<LibObjT>
609 using typename CommonValue<LibObjT>::_LibObjPtr;
610 using typename CommonValue<LibObjT>::_ThisCommonValue;
613 using Shared = internal::SharedValue<CommonArrayValue<LibObjT>, LibObjT>;
615 explicit CommonArrayValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
617 BT_ASSERT_DBG(this->isArray());
620 static Shared create()
622 const auto libObjPtr = bt_value_array_create();
624 internal::validateCreatedObjPtr(libObjPtr);
625 return Shared {CommonArrayValue<LibObjT> {libObjPtr}};
628 template <typename OtherLibObjT>
629 CommonArrayValue(const CommonArrayValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
633 template <typename OtherLibObjT>
634 CommonArrayValue<LibObjT>& operator=(const CommonArrayValue<OtherLibObjT>& val) noexcept
636 _ThisCommonValue::operator=(val);
640 std::uint64_t length() const noexcept
642 return bt_value_array_get_length(this->_libObjPtr());
645 bool isEmpty() const noexcept
647 return this->length() == 0;
650 ConstValue operator[](const std::uint64_t index) const noexcept
652 return ConstValue {internal::CommonArrayValueSpec<const bt_value>::elementByIndex(
653 this->_libObjPtr(), index)};
656 CommonValue<LibObjT> operator[](const std::uint64_t index) noexcept
658 return CommonValue<LibObjT> {
659 internal::CommonArrayValueSpec<LibObjT>::elementByIndex(this->_libObjPtr(), index)};
662 void append(const Value& val)
664 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
666 const auto status = bt_value_array_append_element(this->_libObjPtr(), val._libObjPtr());
668 this->_handleAppendLibStatus(status);
671 void append(const bool rawVal)
673 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
676 bt_value_array_append_bool_element(this->_libObjPtr(), static_cast<bt_bool>(rawVal));
678 this->_handleAppendLibStatus(status);
681 void append(const std::uint64_t rawVal)
683 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
686 bt_value_array_append_unsigned_integer_element(this->_libObjPtr(), rawVal);
688 this->_handleAppendLibStatus(status);
691 void append(const std::int64_t rawVal)
693 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
696 bt_value_array_append_signed_integer_element(this->_libObjPtr(), rawVal);
698 this->_handleAppendLibStatus(status);
701 void append(const double rawVal)
703 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
705 const auto status = bt_value_array_append_real_element(this->_libObjPtr(), rawVal);
707 this->_handleAppendLibStatus(status);
710 void append(const char * const rawVal)
712 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
714 const auto status = bt_value_array_append_string_element(this->_libObjPtr(), rawVal);
716 this->_handleAppendLibStatus(status);
719 void append(const std::string& rawVal)
721 this->append(rawVal.data());
724 CommonArrayValue<bt_value> appendEmptyArray();
725 CommonMapValue<bt_value> appendEmptyMap();
727 void operator+=(const Value& val)
732 void operator+=(const bool rawVal)
734 this->append(rawVal);
737 void operator+=(const std::uint64_t rawVal)
739 this->append(rawVal);
742 void operator+=(const std::int64_t rawVal)
744 this->append(rawVal);
747 void operator+=(const double rawVal)
749 this->append(rawVal);
752 void operator+=(const char * const rawVal)
754 this->append(rawVal);
757 void operator+=(const std::string& rawVal)
759 this->append(rawVal);
762 Shared shared() const noexcept
764 return Shared {*this};
768 void _handleAppendLibStatus(const bt_value_array_append_element_status status) const
770 if (status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR) {
771 throw LibMemoryError {};
776 using ArrayValue = CommonArrayValue<bt_value>;
777 using ConstArrayValue = CommonArrayValue<const bt_value>;
782 * Type of a user function passed to `CommonMapValue<ObjT>::forEach()`.
784 * First argument is the entry's key, second is its value.
786 template <typename ObjT>
787 using CommonMapValueForEachUserFunc = std::function<void(const bpstd::string_view&, ObjT)>;
790 * Template of a function to be passed to bt_value_map_foreach_entry()
791 * for bt_value_map_foreach_entry_const() which calls a user function.
793 * `userData` is casted to a `const` pointer to
794 * `CommonMapValueForEachUserFunc<ObjT>` (the user function to call).
796 * This function catches any exception which the user function throws
797 * and returns the `ErrorStatus` value. If there's no execption, this
798 * function returns the `OkStatus` value.
800 template <typename ObjT, typename LibObjT, typename LibStatusT, int OkStatus, int ErrorStatus>
801 LibStatusT mapValueForEachLibFunc(const char * const key, LibObjT * const libObjPtr,
802 void * const userData)
804 const auto& userFunc = *reinterpret_cast<const CommonMapValueForEachUserFunc<ObjT> *>(userData);
807 userFunc(key, ObjT {libObjPtr});
809 return static_cast<LibStatusT>(ErrorStatus);
812 return static_cast<LibStatusT>(OkStatus);
815 template <typename LibObjT>
816 struct CommonMapValueSpec;
818 // Functions specific to mutable map values
820 struct CommonMapValueSpec<bt_value> final
822 static bt_value *entryByKey(bt_value * const libValPtr, const char * const key) noexcept
824 return bt_value_map_borrow_entry_value(libValPtr, key);
827 static void forEach(bt_value * const libValPtr,
828 const CommonMapValueForEachUserFunc<Value>& func)
830 const auto status = bt_value_map_foreach_entry(
832 mapValueForEachLibFunc<Value, bt_value, bt_value_map_foreach_entry_func_status,
833 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK,
834 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR>,
835 const_cast<void *>(reinterpret_cast<const void *>(&func)));
838 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_OK:
840 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR:
841 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR:
849 // Functions specific to constant map values
851 struct CommonMapValueSpec<const bt_value> final
853 static const bt_value *entryByKey(const bt_value * const libValPtr,
854 const char * const key) noexcept
856 return bt_value_map_borrow_entry_value_const(libValPtr, key);
859 static void forEach(const bt_value * const libValPtr,
860 const CommonMapValueForEachUserFunc<ConstValue>& func)
862 const auto status = bt_value_map_foreach_entry_const(
864 mapValueForEachLibFunc<ConstValue, const bt_value,
865 bt_value_map_foreach_entry_const_func_status,
866 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK,
867 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_ERROR>,
868 const_cast<void *>(reinterpret_cast<const void *>(&func)));
871 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK:
873 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_USER_ERROR:
874 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR:
882 } // namespace internal
884 template <typename LibObjT>
885 class CommonMapValue final : public CommonValue<LibObjT>
888 using typename CommonValue<LibObjT>::_LibObjPtr;
889 using typename CommonValue<LibObjT>::_ThisCommonValue;
892 using Shared = internal::SharedValue<CommonMapValue<LibObjT>, LibObjT>;
894 explicit CommonMapValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
896 BT_ASSERT_DBG(this->isMap());
899 static Shared create()
901 const auto libObjPtr = bt_value_map_create();
903 internal::validateCreatedObjPtr(libObjPtr);
904 return Shared {CommonMapValue<LibObjT> {libObjPtr}};
907 template <typename OtherLibObjT>
908 CommonMapValue(const CommonMapValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
912 template <typename OtherLibObjT>
913 CommonMapValue<LibObjT>& operator=(const CommonMapValue<OtherLibObjT>& val) noexcept
915 _ThisCommonValue::operator=(val);
919 std::uint64_t size() const noexcept
921 return bt_value_map_get_size(this->_libObjPtr());
924 bool isEmpty() const noexcept
926 return this->size() == 0;
929 nonstd::optional<ConstValue> operator[](const char * const key) const noexcept
931 const auto libObjPtr =
932 internal::CommonMapValueSpec<const bt_value>::entryByKey(this->_libObjPtr(), key);
935 return nonstd::nullopt;
938 return ConstValue {libObjPtr};
941 nonstd::optional<ConstValue> operator[](const std::string& key) const noexcept
943 return (*this)[key.data()];
946 nonstd::optional<CommonValue<LibObjT>> operator[](const char * const key) noexcept
948 const auto libObjPtr =
949 internal::CommonMapValueSpec<LibObjT>::entryByKey(this->_libObjPtr(), key);
952 return nonstd::nullopt;
955 return CommonValue<LibObjT> {libObjPtr};
958 nonstd::optional<CommonValue<LibObjT>> operator[](const std::string& key) noexcept
960 return (*this)[key.data()];
963 bool hasEntry(const char * const key) const noexcept
965 return static_cast<bool>(bt_value_map_has_entry(this->_libObjPtr(), key));
968 bool hasEntry(const std::string& key) const noexcept
970 return this->hasEntry(key.data());
973 void insert(const char * const key, const Value& val)
975 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
977 const auto status = bt_value_map_insert_entry(this->_libObjPtr(), key, val._libObjPtr());
979 this->_handleInsertLibStatus(status);
982 void insert(const std::string& key, const Value& val)
984 this->insert(key.data(), val);
987 void insert(const char * const key, const bool rawVal)
989 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
992 bt_value_map_insert_bool_entry(this->_libObjPtr(), key, static_cast<bt_bool>(rawVal));
994 this->_handleInsertLibStatus(status);
997 void insert(const std::string& key, const bool rawVal)
999 this->insert(key.data(), rawVal);
1002 void insert(const char * const key, const std::uint64_t rawVal)
1004 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1007 bt_value_map_insert_unsigned_integer_entry(this->_libObjPtr(), key, rawVal);
1009 this->_handleInsertLibStatus(status);
1012 void insert(const std::string& key, const std::uint64_t rawVal)
1014 this->insert(key.data(), rawVal);
1017 void insert(const char * const key, const std::int64_t rawVal)
1019 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1022 bt_value_map_insert_signed_integer_entry(this->_libObjPtr(), key, rawVal);
1024 this->_handleInsertLibStatus(status);
1027 void insert(const std::string& key, const std::int64_t rawVal)
1029 this->insert(key.data(), rawVal);
1032 void insert(const char * const key, const double rawVal)
1034 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1036 const auto status = bt_value_map_insert_real_entry(this->_libObjPtr(), key, rawVal);
1038 this->_handleInsertLibStatus(status);
1041 void insert(const std::string& key, const double rawVal)
1043 this->insert(key.data(), rawVal);
1046 void insert(const char * const key, const char * const rawVal)
1048 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1050 const auto status = bt_value_map_insert_string_entry(this->_libObjPtr(), key, rawVal);
1052 this->_handleInsertLibStatus(status);
1055 void insert(const char * const key, const std::string& rawVal)
1057 this->insert(key, rawVal.data());
1060 void insert(const std::string& key, const char * const rawVal)
1062 this->insert(key.data(), rawVal);
1065 void insert(const std::string& key, const std::string& rawVal)
1067 this->insert(key.data(), rawVal.data());
1070 CommonArrayValue<bt_value> insertEmptyArray(const char *key);
1071 CommonArrayValue<bt_value> insertEmptyArray(const std::string& key);
1072 CommonMapValue<bt_value> insertEmptyMap(const char *key);
1073 CommonMapValue<bt_value> insertEmptyMap(const std::string& key);
1075 void forEach(const internal::CommonMapValueForEachUserFunc<ConstValue>& func) const
1077 internal::CommonMapValueSpec<const bt_value>::forEach(this->_libObjPtr(), func);
1080 void forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func)
1082 internal::CommonMapValueSpec<LibObjT>::forEach(this->_libObjPtr(), func);
1085 Shared shared() const noexcept
1087 return Shared {*this};
1091 void _handleInsertLibStatus(const bt_value_map_insert_entry_status status) const
1093 if (status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_MEMORY_ERROR) {
1094 throw LibMemoryError {};
1099 using MapValue = CommonMapValue<bt_value>;
1100 using ConstMapValue = CommonMapValue<const bt_value>;
1102 template <typename LibObjT>
1103 CommonNullValue<LibObjT> CommonValue<LibObjT>::asNull() const noexcept
1105 BT_ASSERT_DBG(this->isNull());
1106 return CommonNullValue<LibObjT> {this->_libObjPtr()};
1109 template <typename LibObjT>
1110 CommonBoolValue<LibObjT> CommonValue<LibObjT>::asBool() const noexcept
1112 BT_ASSERT_DBG(this->isBool());
1113 return CommonBoolValue<LibObjT> {this->_libObjPtr()};
1116 template <typename LibObjT>
1117 CommonSignedIntegerValue<LibObjT> CommonValue<LibObjT>::asSignedInteger() const noexcept
1119 BT_ASSERT_DBG(this->isSignedInteger());
1120 return CommonSignedIntegerValue<LibObjT> {this->_libObjPtr()};
1123 template <typename LibObjT>
1124 CommonUnsignedIntegerValue<LibObjT> CommonValue<LibObjT>::asUnsignedInteger() const noexcept
1126 BT_ASSERT_DBG(this->isUnsignedInteger());
1127 return CommonUnsignedIntegerValue<LibObjT> {this->_libObjPtr()};
1130 template <typename LibObjT>
1131 CommonRealValue<LibObjT> CommonValue<LibObjT>::asReal() const noexcept
1133 BT_ASSERT_DBG(this->isReal());
1134 return CommonRealValue<LibObjT> {this->_libObjPtr()};
1137 template <typename LibObjT>
1138 CommonStringValue<LibObjT> CommonValue<LibObjT>::asString() const noexcept
1140 BT_ASSERT_DBG(this->isString());
1141 return CommonStringValue<LibObjT> {this->_libObjPtr()};
1144 template <typename LibObjT>
1145 CommonArrayValue<LibObjT> CommonValue<LibObjT>::asArray() const noexcept
1147 BT_ASSERT_DBG(this->isArray());
1148 return CommonArrayValue<LibObjT> {this->_libObjPtr()};
1151 template <typename LibObjT>
1152 CommonMapValue<LibObjT> CommonValue<LibObjT>::asMap() const noexcept
1154 BT_ASSERT_DBG(this->isMap());
1155 return CommonMapValue<LibObjT> {this->_libObjPtr()};
1158 template <typename LibObjT>
1159 ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray()
1161 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1163 bt_value *libElemPtr;
1164 const auto status = bt_value_array_append_empty_array_element(this->_libObjPtr(), &libElemPtr);
1166 this->_handleAppendLibStatus(status);
1167 return ArrayValue {libElemPtr};
1170 template <typename LibObjT>
1171 MapValue CommonArrayValue<LibObjT>::appendEmptyMap()
1173 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1175 bt_value *libElemPtr;
1176 const auto status = bt_value_array_append_empty_map_element(this->_libObjPtr(), &libElemPtr);
1178 this->_handleAppendLibStatus(status);
1179 return MapValue {libElemPtr};
1182 template <typename LibObjT>
1183 ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key)
1185 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1187 bt_value *libEntryPtr;
1189 bt_value_map_insert_empty_array_entry(this->_libObjPtr(), key, &libEntryPtr);
1191 this->_handleInsertLibStatus(status);
1192 return ArrayValue {libEntryPtr};
1195 template <typename LibObjT>
1196 ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const std::string& key)
1198 return this->insertEmptyArray(key.data());
1201 template <typename LibObjT>
1202 MapValue CommonMapValue<LibObjT>::insertEmptyMap(const char * const key)
1204 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1206 bt_value *libEntryPtr;
1207 const auto status = bt_value_map_insert_empty_map_entry(this->_libObjPtr(), key, &libEntryPtr);
1209 this->_handleInsertLibStatus(status);
1210 return MapValue {libEntryPtr};
1213 template <typename LibObjT>
1214 MapValue CommonMapValue<LibObjT>::insertEmptyMap(const std::string& key)
1216 return this->insertEmptyMap(key.data());
1219 inline BoolValue::Shared createValue(const bool rawVal)
1221 return BoolValue::create(rawVal);
1224 inline UnsignedIntegerValue::Shared createValue(const std::uint64_t rawVal)
1226 return UnsignedIntegerValue::create(rawVal);
1229 inline SignedIntegerValue::Shared createValue(const std::int64_t rawVal)
1231 return SignedIntegerValue::create(rawVal);
1234 inline RealValue::Shared createValue(const double rawVal)
1236 return RealValue::create(rawVal);
1239 inline StringValue::Shared createValue(const char * const rawVal)
1241 return StringValue::create(rawVal);
1244 inline StringValue::Shared createValue(const std::string& rawVal)
1246 return StringValue::create(rawVal);
1251 #endif // BABELTRACE_CPP_COMMON_BT2_VALUE_HPP