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 CommonValue : public internal::BorrowedObj<LibObjT>
85 // Allow append() to call `val._libObjPtr()`
86 friend class CommonArrayValue<bt_value>;
88 // Allow insert() to call `val._libObjPtr()`
89 friend class CommonMapValue<bt_value>;
91 // Allow operator==() to call `other._libObjPtr()`
92 friend class CommonValue<bt_value>;
93 friend class CommonValue<const bt_value>;
96 using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
99 using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
100 using _ThisCommonValue = CommonValue<LibObjT>;
103 using Shared = internal::SharedValue<CommonValue<LibObjT>, LibObjT>;
105 explicit CommonValue(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
109 template <typename OtherLibObjT>
110 CommonValue(const CommonValue<OtherLibObjT>& val) noexcept : _ThisBorrowedObj {val}
114 template <typename OtherLibObjT>
115 _ThisCommonValue& operator=(const CommonValue<OtherLibObjT>& val) noexcept
117 _ThisBorrowedObj::operator=(val);
121 ValueType type() const noexcept
123 return static_cast<ValueType>(bt_value_get_type(this->_libObjPtr()));
126 bool isNull() const noexcept
128 return this->_libTypeIs(BT_VALUE_TYPE_NULL);
131 bool isBool() const noexcept
133 return this->_libTypeIs(BT_VALUE_TYPE_BOOL);
136 bool isInteger() const noexcept
138 return this->_libTypeIs(BT_VALUE_TYPE_INTEGER);
141 bool isUnsignedInteger() const noexcept
143 return this->_libTypeIs(BT_VALUE_TYPE_UNSIGNED_INTEGER);
146 bool isSignedInteger() const noexcept
148 return this->_libTypeIs(BT_VALUE_TYPE_SIGNED_INTEGER);
151 bool isReal() const noexcept
153 return this->_libTypeIs(BT_VALUE_TYPE_REAL);
156 bool isString() const noexcept
158 return this->_libTypeIs(BT_VALUE_TYPE_STRING);
161 bool isArray() const noexcept
163 return this->_libTypeIs(BT_VALUE_TYPE_ARRAY);
166 bool isMap() const noexcept
168 return this->_libTypeIs(BT_VALUE_TYPE_MAP);
171 template <typename OtherLibObjT>
172 bool operator==(const CommonValue<OtherLibObjT>& other) const noexcept
174 return static_cast<bool>(bt_value_is_equal(this->_libObjPtr(), other._libObjPtr()));
177 template <typename OtherLibObjT>
178 bool operator!=(const CommonValue<OtherLibObjT>& other) const noexcept
180 return !(*this == other);
183 Shared shared() const noexcept
185 return Shared {*this};
188 CommonNullValue<LibObjT> asNull() const noexcept;
189 CommonBoolValue<LibObjT> asBool() const noexcept;
190 CommonSignedIntegerValue<LibObjT> asSignedInteger() const noexcept;
191 CommonUnsignedIntegerValue<LibObjT> asUnsignedInteger() const noexcept;
192 CommonRealValue<LibObjT> asReal() const noexcept;
193 CommonStringValue<LibObjT> asString() const noexcept;
194 CommonArrayValue<LibObjT> asArray() const noexcept;
195 CommonMapValue<LibObjT> asMap() const noexcept;
198 bool _libTypeIs(const bt_value_type type) const noexcept
200 return bt_value_type_is(bt_value_get_type(this->_libObjPtr()), type);
204 using Value = CommonValue<bt_value>;
205 using ConstValue = CommonValue<const bt_value>;
207 template <typename LibObjT>
208 class CommonNullValue final : public CommonValue<LibObjT>
211 using typename CommonValue<LibObjT>::_ThisCommonValue;
214 using Shared = internal::SharedValue<CommonNullValue<LibObjT>, LibObjT>;
216 CommonNullValue() noexcept : _ThisCommonValue {bt_value_null}
220 template <typename OtherLibObjT>
221 CommonNullValue(const CommonNullValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
225 template <typename OtherLibObjT>
226 CommonNullValue<LibObjT>& operator=(const CommonNullValue<OtherLibObjT>& val) noexcept
228 _ThisCommonValue::operator=(val);
232 Shared shared() const noexcept
234 return Shared {*this};
238 using NullValue = CommonNullValue<bt_value>;
239 using ConstNullValue = CommonNullValue<const bt_value>;
241 template <typename LibObjT>
242 class CommonBoolValue final : public CommonValue<LibObjT>
245 using typename CommonValue<LibObjT>::_LibObjPtr;
246 using typename CommonValue<LibObjT>::_ThisCommonValue;
249 using Shared = internal::SharedValue<CommonBoolValue<LibObjT>, LibObjT>;
252 explicit CommonBoolValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
254 BT_ASSERT_DBG(this->isBool());
257 template <typename OtherLibObjT>
258 CommonBoolValue(const CommonBoolValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
262 static Shared create(const Value rawVal = false)
264 const auto libObjPtr = bt_value_bool_create_init(static_cast<bt_bool>(rawVal));
266 internal::validateCreatedObjPtr(libObjPtr);
267 return Shared {CommonBoolValue<LibObjT> {libObjPtr}};
270 template <typename OtherLibObjT>
271 CommonBoolValue<LibObjT>& operator=(const CommonBoolValue<OtherLibObjT>& val) noexcept
273 _ThisCommonValue::operator=(val);
277 CommonBoolValue<LibObjT>& operator=(const Value rawVal) noexcept
279 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
281 bt_value_bool_set(this->_libObjPtr(), static_cast<bt_bool>(rawVal));
285 Value value() const noexcept
287 return static_cast<Value>(bt_value_bool_get(this->_libObjPtr()));
290 operator Value() const noexcept
292 return this->value();
295 Shared shared() const noexcept
297 return Shared {*this};
301 using BoolValue = CommonBoolValue<bt_value>;
302 using ConstBoolValue = CommonBoolValue<const bt_value>;
304 template <typename LibObjT>
305 class CommonUnsignedIntegerValue final : public CommonValue<LibObjT>
308 using typename CommonValue<LibObjT>::_LibObjPtr;
309 using typename CommonValue<LibObjT>::_ThisCommonValue;
312 using Shared = internal::SharedValue<CommonUnsignedIntegerValue<LibObjT>, LibObjT>;
313 using Value = std::uint64_t;
315 explicit CommonUnsignedIntegerValue(const _LibObjPtr libObjPtr) noexcept :
316 _ThisCommonValue {libObjPtr}
318 BT_ASSERT_DBG(this->isUnsignedInteger());
321 static Shared create(const Value rawVal = 0)
323 const auto libObjPtr = bt_value_integer_unsigned_create_init(rawVal);
325 internal::validateCreatedObjPtr(libObjPtr);
326 return Shared {CommonUnsignedIntegerValue<LibObjT> {libObjPtr}};
329 template <typename OtherLibObjT>
330 CommonUnsignedIntegerValue(const CommonUnsignedIntegerValue<OtherLibObjT>& val) noexcept :
331 _ThisCommonValue {val}
335 template <typename OtherLibObjT>
336 CommonUnsignedIntegerValue<LibObjT>&
337 operator=(const CommonUnsignedIntegerValue<OtherLibObjT>& val) noexcept
339 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
341 _ThisCommonValue::operator=(val);
345 CommonUnsignedIntegerValue<LibObjT>& operator=(const Value rawVal) noexcept
347 bt_value_integer_unsigned_set(this->_libObjPtr(), rawVal);
351 Value value() const noexcept
353 return bt_value_integer_unsigned_get(this->_libObjPtr());
356 operator Value() const noexcept
358 return this->value();
361 Shared shared() const noexcept
363 return Shared {*this};
367 using UnsignedIntegerValue = CommonUnsignedIntegerValue<bt_value>;
368 using ConstUnsignedIntegerValue = CommonUnsignedIntegerValue<const bt_value>;
370 template <typename LibObjT>
371 class CommonSignedIntegerValue final : public CommonValue<LibObjT>
374 using typename CommonValue<LibObjT>::_LibObjPtr;
375 using typename CommonValue<LibObjT>::_ThisCommonValue;
378 using Shared = internal::SharedValue<CommonSignedIntegerValue<LibObjT>, LibObjT>;
379 using Value = std::int64_t;
381 explicit CommonSignedIntegerValue(const _LibObjPtr libObjPtr) noexcept :
382 _ThisCommonValue {libObjPtr}
384 BT_ASSERT_DBG(this->isSignedInteger());
387 static Shared create(const Value rawVal = 0)
389 const auto libObjPtr = bt_value_integer_signed_create_init(rawVal);
391 internal::validateCreatedObjPtr(libObjPtr);
392 return Shared {CommonSignedIntegerValue<LibObjT> {libObjPtr}};
395 template <typename OtherLibObjT>
396 CommonSignedIntegerValue(const CommonSignedIntegerValue<OtherLibObjT>& val) noexcept :
397 _ThisCommonValue {val}
401 template <typename OtherLibObjT>
402 CommonSignedIntegerValue<LibObjT>&
403 operator=(const CommonSignedIntegerValue<OtherLibObjT>& val) noexcept
405 _ThisCommonValue::operator=(val);
409 CommonSignedIntegerValue<LibObjT>& operator=(const Value rawVal) noexcept
411 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
413 bt_value_integer_signed_set(this->_libObjPtr(), rawVal);
417 Value value() const noexcept
419 return bt_value_integer_signed_get(this->_libObjPtr());
422 operator Value() const noexcept
424 return this->value();
427 Shared shared() const noexcept
429 return Shared {*this};
433 using SignedIntegerValue = CommonSignedIntegerValue<bt_value>;
434 using ConstSignedIntegerValue = CommonSignedIntegerValue<const bt_value>;
436 template <typename LibObjT>
437 class CommonRealValue final : public CommonValue<LibObjT>
440 using typename CommonValue<LibObjT>::_LibObjPtr;
441 using typename CommonValue<LibObjT>::_ThisCommonValue;
444 using Shared = internal::SharedValue<CommonRealValue<LibObjT>, LibObjT>;
445 using Value = double;
447 explicit CommonRealValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
449 BT_ASSERT_DBG(this->isReal());
452 static Shared create(const Value rawVal = 0)
454 const auto libObjPtr = bt_value_real_create_init(rawVal);
456 internal::validateCreatedObjPtr(libObjPtr);
457 return Shared {CommonRealValue<LibObjT> {libObjPtr}};
460 template <typename OtherLibObjT>
461 CommonRealValue(const CommonRealValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
465 template <typename OtherLibObjT>
466 CommonRealValue<LibObjT>& operator=(const CommonRealValue<OtherLibObjT>& val) noexcept
468 _ThisCommonValue::operator=(val);
472 CommonRealValue<LibObjT>& operator=(const Value rawVal) noexcept
474 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
476 bt_value_real_set(this->_libObjPtr(), rawVal);
480 Value value() const noexcept
482 return bt_value_real_get(this->_libObjPtr());
485 operator Value() const noexcept
487 return this->value();
490 Shared shared() const noexcept
492 return Shared {*this};
496 using RealValue = CommonRealValue<bt_value>;
497 using ConstRealValue = CommonRealValue<const bt_value>;
499 template <typename LibObjT>
500 class CommonStringValue final : public CommonValue<LibObjT>
503 using typename CommonValue<LibObjT>::_LibObjPtr;
504 using typename CommonValue<LibObjT>::_ThisCommonValue;
507 using Shared = internal::SharedValue<CommonStringValue<LibObjT>, LibObjT>;
509 explicit CommonStringValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
511 BT_ASSERT_DBG(this->isString());
514 static Shared create(const char * const rawVal = "")
516 const auto libObjPtr = bt_value_string_create_init(rawVal);
518 internal::validateCreatedObjPtr(libObjPtr);
519 return Shared {CommonStringValue<LibObjT> {libObjPtr}};
522 static Shared create(const std::string& rawVal)
524 return CommonStringValue<LibObjT>::create(rawVal.data());
527 template <typename OtherLibObjT>
528 CommonStringValue(const CommonStringValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
532 template <typename OtherLibObjT>
533 CommonStringValue<LibObjT>& operator=(const CommonStringValue<OtherLibObjT>& val) noexcept
535 _ThisCommonValue::operator=(val);
539 CommonStringValue<LibObjT>& operator=(const char * const rawVal)
541 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
543 const auto status = bt_value_string_set(this->_libObjPtr(), rawVal);
545 if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) {
546 throw LibMemoryError {};
552 CommonStringValue<LibObjT>& operator=(const std::string& rawVal) noexcept
554 return *this = rawVal.data();
557 bpstd::string_view value() const noexcept
559 return bt_value_string_get(this->_libObjPtr());
562 Shared shared() const noexcept
564 return Shared {*this};
568 using StringValue = CommonStringValue<bt_value>;
569 using ConstStringValue = CommonStringValue<const bt_value>;
573 template <typename LibObjT>
574 struct CommonArrayValueSpec;
576 // Functions specific to mutable array values
578 struct CommonArrayValueSpec<bt_value> final
580 static bt_value *elementByIndex(bt_value * const libValPtr, const std::uint64_t index) noexcept
582 return bt_value_array_borrow_element_by_index(libValPtr, index);
586 // Functions specific to constant array values
588 struct CommonArrayValueSpec<const bt_value> final
590 static const bt_value *elementByIndex(const bt_value * const libValPtr,
591 const std::uint64_t index) noexcept
593 return bt_value_array_borrow_element_by_index_const(libValPtr, index);
597 } // namespace internal
599 template <typename LibObjT>
600 class CommonArrayValue final : public CommonValue<LibObjT>
603 using typename CommonValue<LibObjT>::_LibObjPtr;
604 using typename CommonValue<LibObjT>::_ThisCommonValue;
607 using Shared = internal::SharedValue<CommonArrayValue<LibObjT>, LibObjT>;
609 explicit CommonArrayValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
611 BT_ASSERT_DBG(this->isArray());
614 static Shared create()
616 const auto libObjPtr = bt_value_array_create();
618 internal::validateCreatedObjPtr(libObjPtr);
619 return Shared {CommonArrayValue<LibObjT> {libObjPtr}};
622 template <typename OtherLibObjT>
623 CommonArrayValue(const CommonArrayValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
627 template <typename OtherLibObjT>
628 CommonArrayValue<LibObjT>& operator=(const CommonArrayValue<OtherLibObjT>& val) noexcept
630 _ThisCommonValue::operator=(val);
634 std::uint64_t length() const noexcept
636 return bt_value_array_get_length(this->_libObjPtr());
639 bool isEmpty() const noexcept
641 return this->length() == 0;
644 ConstValue operator[](const std::uint64_t index) const noexcept
646 return ConstValue {internal::CommonArrayValueSpec<const bt_value>::elementByIndex(
647 this->_libObjPtr(), index)};
650 CommonValue<LibObjT> operator[](const std::uint64_t index) noexcept
652 return CommonValue<LibObjT> {
653 internal::CommonArrayValueSpec<LibObjT>::elementByIndex(this->_libObjPtr(), index)};
656 void append(const Value& val)
658 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
660 const auto status = bt_value_array_append_element(this->_libObjPtr(), val._libObjPtr());
662 this->_handleAppendLibStatus(status);
665 void append(const bool rawVal)
667 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
670 bt_value_array_append_bool_element(this->_libObjPtr(), static_cast<bt_bool>(rawVal));
672 this->_handleAppendLibStatus(status);
675 void append(const std::uint64_t rawVal)
677 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
680 bt_value_array_append_unsigned_integer_element(this->_libObjPtr(), rawVal);
682 this->_handleAppendLibStatus(status);
685 void append(const std::int64_t rawVal)
687 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
690 bt_value_array_append_signed_integer_element(this->_libObjPtr(), rawVal);
692 this->_handleAppendLibStatus(status);
695 void append(const double rawVal)
697 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
699 const auto status = bt_value_array_append_real_element(this->_libObjPtr(), rawVal);
701 this->_handleAppendLibStatus(status);
704 void append(const char * const rawVal)
706 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
708 const auto status = bt_value_array_append_string_element(this->_libObjPtr(), rawVal);
710 this->_handleAppendLibStatus(status);
713 void append(const std::string& rawVal)
715 this->append(rawVal.data());
718 CommonArrayValue<bt_value> appendEmptyArray();
719 CommonMapValue<bt_value> appendEmptyMap();
721 void operator+=(const Value& val)
726 void operator+=(const bool rawVal)
728 this->append(rawVal);
731 void operator+=(const std::uint64_t rawVal)
733 this->append(rawVal);
736 void operator+=(const std::int64_t rawVal)
738 this->append(rawVal);
741 void operator+=(const double rawVal)
743 this->append(rawVal);
746 void operator+=(const char * const rawVal)
748 this->append(rawVal);
751 void operator+=(const std::string& rawVal)
753 this->append(rawVal);
756 Shared shared() const noexcept
758 return Shared {*this};
762 void _handleAppendLibStatus(const bt_value_array_append_element_status status) const
764 if (status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR) {
765 throw LibMemoryError {};
770 using ArrayValue = CommonArrayValue<bt_value>;
771 using ConstArrayValue = CommonArrayValue<const bt_value>;
776 * Type of a user function passed to `CommonMapValue<ObjT>::forEach()`.
778 * First argument is the entry's key, second is its value.
780 template <typename ObjT>
781 using CommonMapValueForEachUserFunc = std::function<void(const bpstd::string_view&, ObjT)>;
784 * Template of a function to be passed to bt_value_map_foreach_entry()
785 * for bt_value_map_foreach_entry_const() which calls a user function.
787 * `userData` is casted to a `const` pointer to
788 * `CommonMapValueForEachUserFunc<ObjT>` (the user function to call).
790 * This function catches any exception which the user function throws
791 * and returns the `ErrorStatus` value. If there's no execption, this
792 * function returns the `OkStatus` value.
794 template <typename ObjT, typename LibObjT, typename LibStatusT, int OkStatus, int ErrorStatus>
795 LibStatusT mapValueForEachLibFunc(const char * const key, LibObjT * const libObjPtr,
796 void * const userData)
798 const auto& userFunc = *reinterpret_cast<const CommonMapValueForEachUserFunc<ObjT> *>(userData);
801 userFunc(key, ObjT {libObjPtr});
803 return static_cast<LibStatusT>(ErrorStatus);
806 return static_cast<LibStatusT>(OkStatus);
809 template <typename LibObjT>
810 struct CommonMapValueSpec;
812 // Functions specific to mutable map values
814 struct CommonMapValueSpec<bt_value> final
816 static bt_value *entryByKey(bt_value * const libValPtr, const char * const key) noexcept
818 return bt_value_map_borrow_entry_value(libValPtr, key);
821 static void forEach(bt_value * const libValPtr,
822 const CommonMapValueForEachUserFunc<Value>& func)
824 const auto status = bt_value_map_foreach_entry(
826 mapValueForEachLibFunc<Value, bt_value, bt_value_map_foreach_entry_func_status,
827 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK,
828 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR>,
829 const_cast<void *>(reinterpret_cast<const void *>(&func)));
832 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_OK:
834 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR:
835 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR:
843 // Functions specific to constant map values
845 struct CommonMapValueSpec<const bt_value> final
847 static const bt_value *entryByKey(const bt_value * const libValPtr,
848 const char * const key) noexcept
850 return bt_value_map_borrow_entry_value_const(libValPtr, key);
853 static void forEach(const bt_value * const libValPtr,
854 const CommonMapValueForEachUserFunc<ConstValue>& func)
856 const auto status = bt_value_map_foreach_entry_const(
858 mapValueForEachLibFunc<ConstValue, const bt_value,
859 bt_value_map_foreach_entry_const_func_status,
860 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK,
861 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_ERROR>,
862 const_cast<void *>(reinterpret_cast<const void *>(&func)));
865 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK:
867 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_USER_ERROR:
868 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR:
876 } // namespace internal
878 template <typename LibObjT>
879 class CommonMapValue final : public CommonValue<LibObjT>
882 using typename CommonValue<LibObjT>::_LibObjPtr;
883 using typename CommonValue<LibObjT>::_ThisCommonValue;
886 using Shared = internal::SharedValue<CommonMapValue<LibObjT>, LibObjT>;
888 explicit CommonMapValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
890 BT_ASSERT_DBG(this->isMap());
893 static Shared create()
895 const auto libObjPtr = bt_value_map_create();
897 internal::validateCreatedObjPtr(libObjPtr);
898 return Shared {CommonMapValue<LibObjT> {libObjPtr}};
901 template <typename OtherLibObjT>
902 CommonMapValue(const CommonMapValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
906 template <typename OtherLibObjT>
907 CommonMapValue<LibObjT>& operator=(const CommonMapValue<OtherLibObjT>& val) noexcept
909 _ThisCommonValue::operator=(val);
913 std::uint64_t size() const noexcept
915 return bt_value_map_get_size(this->_libObjPtr());
918 bool isEmpty() const noexcept
920 return this->size() == 0;
923 nonstd::optional<ConstValue> operator[](const char * const key) const noexcept
925 const auto libObjPtr =
926 internal::CommonMapValueSpec<const bt_value>::entryByKey(this->_libObjPtr(), key);
929 return nonstd::nullopt;
932 return ConstValue {libObjPtr};
935 nonstd::optional<ConstValue> operator[](const std::string& key) const noexcept
937 return (*this)[key.data()];
940 nonstd::optional<CommonValue<LibObjT>> operator[](const char * const key) noexcept
942 const auto libObjPtr =
943 internal::CommonMapValueSpec<LibObjT>::entryByKey(this->_libObjPtr(), key);
946 return nonstd::nullopt;
949 return CommonValue<LibObjT> {libObjPtr};
952 nonstd::optional<CommonValue<LibObjT>> operator[](const std::string& key) noexcept
954 return (*this)[key.data()];
957 bool hasEntry(const char * const key) const noexcept
959 return static_cast<bool>(bt_value_map_has_entry(this->_libObjPtr(), key));
962 bool hasEntry(const std::string& key) const noexcept
964 return this->hasEntry(key.data());
967 void insert(const char * const key, const Value& val)
969 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
971 const auto status = bt_value_map_insert_entry(this->_libObjPtr(), key, val._libObjPtr());
973 this->_handleInsertLibStatus(status);
976 void insert(const std::string& key, const Value& val)
978 this->insert(key.data(), val);
981 void insert(const char * const key, const bool rawVal)
983 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
986 bt_value_map_insert_bool_entry(this->_libObjPtr(), key, static_cast<bt_bool>(rawVal));
988 this->_handleInsertLibStatus(status);
991 void insert(const std::string& key, const bool rawVal)
993 this->insert(key.data(), rawVal);
996 void insert(const char * const key, const std::uint64_t rawVal)
998 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1001 bt_value_map_insert_unsigned_integer_entry(this->_libObjPtr(), key, rawVal);
1003 this->_handleInsertLibStatus(status);
1006 void insert(const std::string& key, const std::uint64_t rawVal)
1008 this->insert(key.data(), rawVal);
1011 void insert(const char * const key, const std::int64_t rawVal)
1013 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1016 bt_value_map_insert_signed_integer_entry(this->_libObjPtr(), key, rawVal);
1018 this->_handleInsertLibStatus(status);
1021 void insert(const std::string& key, const std::int64_t rawVal)
1023 this->insert(key.data(), rawVal);
1026 void insert(const char * const key, const double rawVal)
1028 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1030 const auto status = bt_value_map_insert_real_entry(this->_libObjPtr(), key, rawVal);
1032 this->_handleInsertLibStatus(status);
1035 void insert(const std::string& key, const double rawVal)
1037 this->insert(key.data(), rawVal);
1040 void insert(const char * const key, const char * const rawVal)
1042 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1044 const auto status = bt_value_map_insert_string_entry(this->_libObjPtr(), key, rawVal);
1046 this->_handleInsertLibStatus(status);
1049 void insert(const char * const key, const std::string& rawVal)
1051 this->insert(key, rawVal.data());
1054 void insert(const std::string& key, const char * const rawVal)
1056 this->insert(key.data(), rawVal);
1059 void insert(const std::string& key, const std::string& rawVal)
1061 this->insert(key.data(), rawVal.data());
1064 CommonArrayValue<bt_value> insertEmptyArray(const char *key);
1065 CommonArrayValue<bt_value> insertEmptyArray(const std::string& key);
1066 CommonMapValue<bt_value> insertEmptyMap(const char *key);
1067 CommonMapValue<bt_value> insertEmptyMap(const std::string& key);
1069 void forEach(const internal::CommonMapValueForEachUserFunc<ConstValue>& func) const
1071 internal::CommonMapValueSpec<const bt_value>::forEach(this->_libObjPtr(), func);
1074 void forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func)
1076 internal::CommonMapValueSpec<LibObjT>::forEach(this->_libObjPtr(), func);
1079 Shared shared() const noexcept
1081 return Shared {*this};
1085 void _handleInsertLibStatus(const bt_value_map_insert_entry_status status) const
1087 if (status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_MEMORY_ERROR) {
1088 throw LibMemoryError {};
1093 using MapValue = CommonMapValue<bt_value>;
1094 using ConstMapValue = CommonMapValue<const bt_value>;
1096 template <typename LibObjT>
1097 CommonNullValue<LibObjT> CommonValue<LibObjT>::asNull() const noexcept
1099 BT_ASSERT_DBG(this->isNull());
1100 return CommonNullValue<LibObjT> {this->_libObjPtr()};
1103 template <typename LibObjT>
1104 CommonBoolValue<LibObjT> CommonValue<LibObjT>::asBool() const noexcept
1106 BT_ASSERT_DBG(this->isBool());
1107 return CommonBoolValue<LibObjT> {this->_libObjPtr()};
1110 template <typename LibObjT>
1111 CommonSignedIntegerValue<LibObjT> CommonValue<LibObjT>::asSignedInteger() const noexcept
1113 BT_ASSERT_DBG(this->isSignedInteger());
1114 return CommonSignedIntegerValue<LibObjT> {this->_libObjPtr()};
1117 template <typename LibObjT>
1118 CommonUnsignedIntegerValue<LibObjT> CommonValue<LibObjT>::asUnsignedInteger() const noexcept
1120 BT_ASSERT_DBG(this->isUnsignedInteger());
1121 return CommonUnsignedIntegerValue<LibObjT> {this->_libObjPtr()};
1124 template <typename LibObjT>
1125 CommonRealValue<LibObjT> CommonValue<LibObjT>::asReal() const noexcept
1127 BT_ASSERT_DBG(this->isReal());
1128 return CommonRealValue<LibObjT> {this->_libObjPtr()};
1131 template <typename LibObjT>
1132 CommonStringValue<LibObjT> CommonValue<LibObjT>::asString() const noexcept
1134 BT_ASSERT_DBG(this->isString());
1135 return CommonStringValue<LibObjT> {this->_libObjPtr()};
1138 template <typename LibObjT>
1139 CommonArrayValue<LibObjT> CommonValue<LibObjT>::asArray() const noexcept
1141 BT_ASSERT_DBG(this->isArray());
1142 return CommonArrayValue<LibObjT> {this->_libObjPtr()};
1145 template <typename LibObjT>
1146 CommonMapValue<LibObjT> CommonValue<LibObjT>::asMap() const noexcept
1148 BT_ASSERT_DBG(this->isMap());
1149 return CommonMapValue<LibObjT> {this->_libObjPtr()};
1152 template <typename LibObjT>
1153 ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray()
1155 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1157 bt_value *libElemPtr;
1158 const auto status = bt_value_array_append_empty_array_element(this->_libObjPtr(), &libElemPtr);
1160 this->_handleAppendLibStatus(status);
1161 return ArrayValue {libElemPtr};
1164 template <typename LibObjT>
1165 MapValue CommonArrayValue<LibObjT>::appendEmptyMap()
1167 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1169 bt_value *libElemPtr;
1170 const auto status = bt_value_array_append_empty_map_element(this->_libObjPtr(), &libElemPtr);
1172 this->_handleAppendLibStatus(status);
1173 return MapValue {libElemPtr};
1176 template <typename LibObjT>
1177 ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key)
1179 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1181 bt_value *libEntryPtr;
1183 bt_value_map_insert_empty_array_entry(this->_libObjPtr(), key, &libEntryPtr);
1185 this->_handleInsertLibStatus(status);
1186 return ArrayValue {libEntryPtr};
1189 template <typename LibObjT>
1190 ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const std::string& key)
1192 return this->insertEmptyArray(key.data());
1195 template <typename LibObjT>
1196 MapValue CommonMapValue<LibObjT>::insertEmptyMap(const char * const key)
1198 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1200 bt_value *libEntryPtr;
1201 const auto status = bt_value_map_insert_empty_map_entry(this->_libObjPtr(), key, &libEntryPtr);
1203 this->_handleInsertLibStatus(status);
1204 return MapValue {libEntryPtr};
1207 template <typename LibObjT>
1208 MapValue CommonMapValue<LibObjT>::insertEmptyMap(const std::string& key)
1210 return this->insertEmptyMap(key.data());
1213 inline BoolValue::Shared createValue(const bool rawVal)
1215 return BoolValue::create(rawVal);
1218 inline UnsignedIntegerValue::Shared createValue(const std::uint64_t rawVal)
1220 return UnsignedIntegerValue::create(rawVal);
1223 inline SignedIntegerValue::Shared createValue(const std::int64_t rawVal)
1225 return SignedIntegerValue::create(rawVal);
1228 inline RealValue::Shared createValue(const double rawVal)
1230 return RealValue::create(rawVal);
1233 inline StringValue::Shared createValue(const char * const rawVal)
1235 return StringValue::create(rawVal);
1238 inline StringValue::Shared createValue(const std::string& rawVal)
1240 return StringValue::create(rawVal);
1245 #endif // BABELTRACE_CPP_COMMON_BT2_VALUE_HPP