cpp-common/bt2: use raw value proxy for scalar value classes
[babeltrace.git] / src / cpp-common / bt2 / value.hpp
1 /*
2 * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 #ifndef BABELTRACE_CPP_COMMON_BT2_VALUE_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_VALUE_HPP
9
10 #include <cstdint>
11 #include <functional>
12 #include <type_traits>
13
14 #include <babeltrace2/babeltrace.h>
15
16 #include "common/assert.h"
17 #include "common/common.h"
18 #include "cpp-common/optional.hpp"
19 #include "cpp-common/string_view.hpp"
20
21 #include "borrowed-object-iterator.hpp"
22 #include "borrowed-object.hpp"
23 #include "exc.hpp"
24 #include "internal/utils.hpp"
25 #include "raw-value-proxy.hpp"
26 #include "shared-object.hpp"
27
28 namespace bt2 {
29 namespace internal {
30
31 struct ValueRefFuncs final
32 {
33 static void get(const bt_value * const libObjPtr) noexcept
34 {
35 bt_value_get_ref(libObjPtr);
36 }
37
38 static void put(const bt_value * const libObjPtr) noexcept
39 {
40 bt_value_put_ref(libObjPtr);
41 }
42 };
43
44 } /* namespace internal */
45
46 template <typename ObjT, typename LibObjT>
47 using SharedValue = SharedObject<ObjT, LibObjT, internal::ValueRefFuncs>;
48
49 template <typename LibObjT>
50 class CommonNullValue;
51
52 template <typename LibObjT>
53 class CommonBoolValue;
54
55 template <typename LibObjT>
56 class CommonUnsignedIntegerValue;
57
58 template <typename LibObjT>
59 class CommonSignedIntegerValue;
60
61 template <typename LibObjT>
62 class CommonRealValue;
63
64 template <typename LibObjT>
65 class CommonStringValue;
66
67 template <typename LibObjT>
68 class CommonArrayValue;
69
70 template <typename LibObjT>
71 class CommonMapValue;
72
73 enum class ValueType
74 {
75 NUL = BT_VALUE_TYPE_NULL,
76 BOOL = BT_VALUE_TYPE_BOOL,
77 UNSIGNED_INTEGER = BT_VALUE_TYPE_UNSIGNED_INTEGER,
78 SIGNED_INTEGER = BT_VALUE_TYPE_SIGNED_INTEGER,
79 REAL = BT_VALUE_TYPE_REAL,
80 STRING = BT_VALUE_TYPE_STRING,
81 ARRAY = BT_VALUE_TYPE_ARRAY,
82 MAP = BT_VALUE_TYPE_MAP,
83 };
84
85 template <typename ValueObjT>
86 class CommonValueRawValueProxy final
87 {
88 public:
89 explicit CommonValueRawValueProxy(const ValueObjT obj) : _mObj {obj}
90 {
91 }
92
93 CommonValueRawValueProxy& operator=(bool rawVal) noexcept;
94 CommonValueRawValueProxy& operator=(std::int64_t rawVal) noexcept;
95 CommonValueRawValueProxy& operator=(std::uint64_t rawVal) noexcept;
96 CommonValueRawValueProxy& operator=(double rawVal) noexcept;
97 CommonValueRawValueProxy& operator=(const char *rawVal);
98 CommonValueRawValueProxy& operator=(const std::string& rawVal);
99 operator bool() const noexcept;
100 operator std::int64_t() const noexcept;
101 operator std::uint64_t() const noexcept;
102 operator double() const noexcept;
103 operator const char *() const noexcept;
104
105 private:
106 ValueObjT _mObj;
107 };
108
109 template <typename LibObjT>
110 class CommonValue : public BorrowedObject<LibObjT>
111 {
112 private:
113 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
114
115 protected:
116 using typename BorrowedObject<LibObjT>::_LibObjPtr;
117 using _ThisCommonValue = CommonValue<LibObjT>;
118
119 public:
120 using Shared = SharedValue<CommonValue<LibObjT>, LibObjT>;
121
122 explicit CommonValue(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
123 {
124 }
125
126 template <typename OtherLibObjT>
127 CommonValue(const CommonValue<OtherLibObjT> val) noexcept : _ThisBorrowedObject {val}
128 {
129 }
130
131 template <typename OtherLibObjT>
132 _ThisCommonValue& operator=(const CommonValue<OtherLibObjT> val) noexcept
133 {
134 _ThisBorrowedObject::operator=(val);
135 return *this;
136 }
137
138 CommonValue<const bt_value> asConst() const noexcept
139 {
140 return CommonValue<const bt_value> {*this};
141 }
142
143 ValueType type() const noexcept
144 {
145 return static_cast<ValueType>(bt_value_get_type(this->libObjPtr()));
146 }
147
148 bool isNull() const noexcept
149 {
150 return this->_libTypeIs(BT_VALUE_TYPE_NULL);
151 }
152
153 bool isBool() const noexcept
154 {
155 return this->_libTypeIs(BT_VALUE_TYPE_BOOL);
156 }
157
158 bool isInteger() const noexcept
159 {
160 return this->_libTypeIs(BT_VALUE_TYPE_INTEGER);
161 }
162
163 bool isUnsignedInteger() const noexcept
164 {
165 return this->_libTypeIs(BT_VALUE_TYPE_UNSIGNED_INTEGER);
166 }
167
168 bool isSignedInteger() const noexcept
169 {
170 return this->_libTypeIs(BT_VALUE_TYPE_SIGNED_INTEGER);
171 }
172
173 bool isReal() const noexcept
174 {
175 return this->_libTypeIs(BT_VALUE_TYPE_REAL);
176 }
177
178 bool isString() const noexcept
179 {
180 return this->_libTypeIs(BT_VALUE_TYPE_STRING);
181 }
182
183 bool isArray() const noexcept
184 {
185 return this->_libTypeIs(BT_VALUE_TYPE_ARRAY);
186 }
187
188 bool isMap() const noexcept
189 {
190 return this->_libTypeIs(BT_VALUE_TYPE_MAP);
191 }
192
193 template <typename OtherLibObjT>
194 bool operator==(const CommonValue<OtherLibObjT> other) const noexcept
195 {
196 return static_cast<bool>(bt_value_is_equal(this->libObjPtr(), other.libObjPtr()));
197 }
198
199 template <typename OtherLibObjT>
200 bool operator!=(const CommonValue<OtherLibObjT> other) const noexcept
201 {
202 return !(*this == other);
203 }
204
205 CommonValueRawValueProxy<CommonValue> operator*() const noexcept
206 {
207 return CommonValueRawValueProxy<CommonValue> {*this};
208 }
209
210 std::uint64_t arrayLength() const noexcept
211 {
212 return this->asArray().length();
213 }
214
215 bool arrayIsEmpty() const noexcept
216 {
217 return this->asArray().isEmpty();
218 }
219
220 CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept
221 {
222 return this->asArray()[index];
223 }
224
225 template <typename T>
226 void append(T&& elem) const
227 {
228 this->asArray().append(std::forward<T>(elem));
229 }
230
231 CommonArrayValue<bt_value> appendEmptyArray() const;
232 CommonMapValue<bt_value> appendEmptyMap() const;
233
234 std::uint64_t mapLength() const noexcept
235 {
236 return this->asMap().length();
237 }
238
239 bool mapIsEmpty() const noexcept
240 {
241 return this->asMap().isEmpty();
242 }
243
244 template <typename KeyT>
245 nonstd::optional<CommonValue<LibObjT>> operator[](KeyT&& key) const noexcept
246 {
247 return this->asMap()[std::forward<KeyT>(key)];
248 }
249
250 template <typename KeyT>
251 bool hasEntry(KeyT&& key) const noexcept
252 {
253 return this->asMap().hasEntry(std::forward<KeyT>(key));
254 }
255
256 template <typename KeyT, typename ValT>
257 void insert(KeyT&& key, ValT&& val) const
258 {
259 this->asMap().insert(std::forward<KeyT>(key), std::forward<ValT>(val));
260 }
261
262 CommonArrayValue<bt_value> insertEmptyArray(const char *key) const;
263 CommonArrayValue<bt_value> insertEmptyArray(const std::string& key) const;
264 CommonMapValue<bt_value> insertEmptyMap(const char *key) const;
265 CommonMapValue<bt_value> insertEmptyMap(const std::string& key) const;
266
267 Shared shared() const noexcept
268 {
269 return Shared::createWithRef(*this);
270 }
271
272 template <typename ValueT>
273 ValueT as() const noexcept
274 {
275 return ValueT {this->libObjPtr()};
276 }
277
278 CommonNullValue<LibObjT> asNull() const noexcept;
279 CommonBoolValue<LibObjT> asBool() const noexcept;
280 CommonSignedIntegerValue<LibObjT> asSignedInteger() const noexcept;
281 CommonUnsignedIntegerValue<LibObjT> asUnsignedInteger() const noexcept;
282 CommonRealValue<LibObjT> asReal() const noexcept;
283 CommonStringValue<LibObjT> asString() const noexcept;
284 CommonArrayValue<LibObjT> asArray() const noexcept;
285 CommonMapValue<LibObjT> asMap() const noexcept;
286
287 protected:
288 bool _libTypeIs(const bt_value_type type) const noexcept
289 {
290 return bt_value_type_is(bt_value_get_type(this->libObjPtr()), type);
291 }
292 };
293
294 using Value = CommonValue<bt_value>;
295 using ConstValue = CommonValue<const bt_value>;
296
297 template <typename ValueObjT>
298 CommonValueRawValueProxy<ValueObjT>&
299 CommonValueRawValueProxy<ValueObjT>::operator=(const bool rawVal) noexcept
300 {
301 _mObj.asBool().value(rawVal);
302 return *this;
303 }
304
305 template <typename ValueObjT>
306 CommonValueRawValueProxy<ValueObjT>&
307 CommonValueRawValueProxy<ValueObjT>::operator=(const std::int64_t rawVal) noexcept
308 {
309 _mObj.asSignedInteger().value(rawVal);
310 return *this;
311 }
312
313 template <typename ValueObjT>
314 CommonValueRawValueProxy<ValueObjT>&
315 CommonValueRawValueProxy<ValueObjT>::operator=(const std::uint64_t rawVal) noexcept
316 {
317 _mObj.asUnsignedInteger().value(rawVal);
318 return *this;
319 }
320
321 template <typename ValueObjT>
322 CommonValueRawValueProxy<ValueObjT>&
323 CommonValueRawValueProxy<ValueObjT>::operator=(const double rawVal) noexcept
324 {
325 _mObj.asReal().value(rawVal);
326 return *this;
327 }
328
329 template <typename ValueObjT>
330 CommonValueRawValueProxy<ValueObjT>&
331 CommonValueRawValueProxy<ValueObjT>::operator=(const char * const rawVal)
332 {
333 _mObj.asString().value(rawVal);
334 return *this;
335 }
336
337 template <typename ValueObjT>
338 CommonValueRawValueProxy<ValueObjT>&
339 CommonValueRawValueProxy<ValueObjT>::operator=(const std::string& rawVal)
340 {
341 return *this = rawVal.data();
342 }
343
344 template <typename ValueObjT>
345 CommonValueRawValueProxy<ValueObjT>::operator bool() const noexcept
346 {
347 return _mObj.asBool().value();
348 }
349
350 template <typename ValueObjT>
351 CommonValueRawValueProxy<ValueObjT>::operator std::int64_t() const noexcept
352 {
353 return _mObj.asSignedInteger().value();
354 }
355
356 template <typename ValueObjT>
357 CommonValueRawValueProxy<ValueObjT>::operator std::uint64_t() const noexcept
358 {
359 return _mObj.asUnsignedInteger().value();
360 }
361
362 template <typename ValueObjT>
363 CommonValueRawValueProxy<ValueObjT>::operator double() const noexcept
364 {
365 return _mObj.asReal().value();
366 }
367
368 template <typename ValueObjT>
369 CommonValueRawValueProxy<ValueObjT>::operator const char *() const noexcept
370 {
371 return _mObj.asString().value();
372 }
373
374 namespace internal {
375
376 struct ValueTypeDescr
377 {
378 using Const = ConstValue;
379 using NonConst = Value;
380 };
381
382 template <>
383 struct TypeDescr<Value> : public ValueTypeDescr
384 {
385 };
386
387 template <>
388 struct TypeDescr<ConstValue> : public ValueTypeDescr
389 {
390 };
391
392 } /* namespace internal */
393
394 template <typename LibObjT>
395 class CommonNullValue final : public CommonValue<LibObjT>
396 {
397 private:
398 using typename CommonValue<LibObjT>::_ThisCommonValue;
399
400 public:
401 using Shared = SharedValue<CommonNullValue<LibObjT>, LibObjT>;
402
403 CommonNullValue() noexcept : _ThisCommonValue {bt_value_null}
404 {
405 }
406
407 template <typename OtherLibObjT>
408 CommonNullValue(const CommonNullValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
409 {
410 }
411
412 template <typename OtherLibObjT>
413 CommonNullValue<LibObjT>& operator=(const CommonNullValue<OtherLibObjT> val) noexcept
414 {
415 _ThisCommonValue::operator=(val);
416 return *this;
417 }
418
419 CommonNullValue<const bt_value> asConst() const noexcept
420 {
421 return CommonNullValue<const bt_value> {*this};
422 }
423
424 Shared shared() const noexcept
425 {
426 return Shared::createWithRef(*this);
427 }
428 };
429
430 using NullValue = CommonNullValue<bt_value>;
431 using ConstNullValue = CommonNullValue<const bt_value>;
432
433 namespace internal {
434
435 struct NullValueTypeDescr
436 {
437 using Const = ConstNullValue;
438 using NonConst = NullValue;
439 };
440
441 template <>
442 struct TypeDescr<NullValue> : public NullValueTypeDescr
443 {
444 };
445
446 template <>
447 struct TypeDescr<ConstNullValue> : public NullValueTypeDescr
448 {
449 };
450
451 } /* namespace internal */
452
453 template <typename LibObjT>
454 class CommonBoolValue final : public CommonValue<LibObjT>
455 {
456 private:
457 using typename CommonValue<LibObjT>::_LibObjPtr;
458 using typename CommonValue<LibObjT>::_ThisCommonValue;
459
460 public:
461 using Shared = SharedValue<CommonBoolValue<LibObjT>, LibObjT>;
462 using Value = bool;
463
464 explicit CommonBoolValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
465 {
466 BT_ASSERT_DBG(this->isBool());
467 }
468
469 template <typename OtherLibObjT>
470 CommonBoolValue(const CommonBoolValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
471 {
472 }
473
474 static Shared create(const Value rawVal = false)
475 {
476 const auto libObjPtr = bt_value_bool_create_init(static_cast<bt_bool>(rawVal));
477
478 internal::validateCreatedObjPtr(libObjPtr);
479 return CommonBoolValue::Shared::createWithoutRef(libObjPtr);
480 }
481
482 template <typename OtherLibObjT>
483 CommonBoolValue<LibObjT>& operator=(const CommonBoolValue<OtherLibObjT> val) noexcept
484 {
485 _ThisCommonValue::operator=(val);
486 return *this;
487 }
488
489 CommonBoolValue<const bt_value> asConst() const noexcept
490 {
491 return CommonBoolValue<const bt_value> {*this};
492 }
493
494 RawValueProxy<CommonBoolValue> operator*() const noexcept
495 {
496 return RawValueProxy<CommonBoolValue> {*this};
497 }
498
499 Value value() const noexcept
500 {
501 return static_cast<Value>(bt_value_bool_get(this->libObjPtr()));
502 }
503
504 void value(const Value val) const noexcept
505 {
506 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstBoolValue`.");
507
508 bt_value_bool_set(this->libObjPtr(), static_cast<bt_bool>(val));
509 }
510
511 Shared shared() const noexcept
512 {
513 return Shared::createWithRef(*this);
514 }
515 };
516
517 using BoolValue = CommonBoolValue<bt_value>;
518 using ConstBoolValue = CommonBoolValue<const bt_value>;
519
520 namespace internal {
521
522 struct BoolValueTypeDescr
523 {
524 using Const = ConstBoolValue;
525 using NonConst = BoolValue;
526 };
527
528 template <>
529 struct TypeDescr<BoolValue> : public BoolValueTypeDescr
530 {
531 };
532
533 template <>
534 struct TypeDescr<ConstBoolValue> : public BoolValueTypeDescr
535 {
536 };
537
538 } /* namespace internal */
539
540 template <typename LibObjT>
541 class CommonUnsignedIntegerValue final : public CommonValue<LibObjT>
542 {
543 private:
544 using typename CommonValue<LibObjT>::_LibObjPtr;
545 using typename CommonValue<LibObjT>::_ThisCommonValue;
546
547 public:
548 using Shared = SharedValue<CommonUnsignedIntegerValue<LibObjT>, LibObjT>;
549 using Value = std::uint64_t;
550
551 explicit CommonUnsignedIntegerValue(const _LibObjPtr libObjPtr) noexcept :
552 _ThisCommonValue {libObjPtr}
553 {
554 BT_ASSERT_DBG(this->isUnsignedInteger());
555 }
556
557 static Shared create(const Value rawVal = 0)
558 {
559 const auto libObjPtr = bt_value_integer_unsigned_create_init(rawVal);
560
561 internal::validateCreatedObjPtr(libObjPtr);
562 return CommonUnsignedIntegerValue::Shared::createWithoutRef(libObjPtr);
563 }
564
565 template <typename OtherLibObjT>
566 CommonUnsignedIntegerValue(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept :
567 _ThisCommonValue {val}
568 {
569 }
570
571 template <typename OtherLibObjT>
572 CommonUnsignedIntegerValue<LibObjT>&
573 operator=(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept
574 {
575 _ThisCommonValue::operator=(val);
576 return *this;
577 }
578
579 CommonUnsignedIntegerValue<const bt_value> asConst() const noexcept
580 {
581 return CommonUnsignedIntegerValue<const bt_value> {*this};
582 }
583
584 RawValueProxy<CommonUnsignedIntegerValue> operator*() const noexcept
585 {
586 return RawValueProxy<CommonUnsignedIntegerValue> {*this};
587 }
588
589 void value(const Value val) const noexcept
590 {
591 static_assert(!std::is_const<LibObjT>::value,
592 "Not available with `bt2::ConstUnsignedIntegerValue`.");
593
594 bt_value_integer_unsigned_set(this->libObjPtr(), val);
595 }
596
597 Value value() const noexcept
598 {
599 return bt_value_integer_unsigned_get(this->libObjPtr());
600 }
601
602 Shared shared() const noexcept
603 {
604 return Shared::createWithRef(*this);
605 }
606 };
607
608 using UnsignedIntegerValue = CommonUnsignedIntegerValue<bt_value>;
609 using ConstUnsignedIntegerValue = CommonUnsignedIntegerValue<const bt_value>;
610
611 namespace internal {
612
613 struct UnsignedIntegerValueTypeDescr
614 {
615 using Const = ConstUnsignedIntegerValue;
616 using NonConst = UnsignedIntegerValue;
617 };
618
619 template <>
620 struct TypeDescr<UnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
621 {
622 };
623
624 template <>
625 struct TypeDescr<ConstUnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
626 {
627 };
628
629 } /* namespace internal */
630
631 template <typename LibObjT>
632 class CommonSignedIntegerValue final : public CommonValue<LibObjT>
633 {
634 private:
635 using typename CommonValue<LibObjT>::_LibObjPtr;
636 using typename CommonValue<LibObjT>::_ThisCommonValue;
637
638 public:
639 using Shared = SharedValue<CommonSignedIntegerValue<LibObjT>, LibObjT>;
640 using Value = std::int64_t;
641
642 explicit CommonSignedIntegerValue(const _LibObjPtr libObjPtr) noexcept :
643 _ThisCommonValue {libObjPtr}
644 {
645 BT_ASSERT_DBG(this->isSignedInteger());
646 }
647
648 static Shared create(const Value rawVal = 0)
649 {
650 const auto libObjPtr = bt_value_integer_signed_create_init(rawVal);
651
652 internal::validateCreatedObjPtr(libObjPtr);
653 return CommonSignedIntegerValue::Shared::createWithoutRef(libObjPtr);
654 }
655
656 template <typename OtherLibObjT>
657 CommonSignedIntegerValue(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept :
658 _ThisCommonValue {val}
659 {
660 }
661
662 template <typename OtherLibObjT>
663 CommonSignedIntegerValue<LibObjT>
664 operator=(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept
665 {
666 _ThisCommonValue::operator=(val);
667 return *this;
668 }
669
670 CommonSignedIntegerValue<const bt_value> asConst() const noexcept
671 {
672 return CommonSignedIntegerValue<const bt_value> {*this};
673 }
674
675 RawValueProxy<CommonSignedIntegerValue> operator*() const noexcept
676 {
677 return RawValueProxy<CommonSignedIntegerValue> {*this};
678 }
679
680 void value(const Value val) const noexcept
681 {
682 static_assert(!std::is_const<LibObjT>::value,
683 "Not available with `bt2::ConstSignedIntegerValue`.");
684
685 bt_value_integer_signed_set(this->libObjPtr(), val);
686 }
687
688 Value value() const noexcept
689 {
690 return bt_value_integer_signed_get(this->libObjPtr());
691 }
692
693 Shared shared() const noexcept
694 {
695 return Shared::createWithRef(*this);
696 }
697 };
698
699 using SignedIntegerValue = CommonSignedIntegerValue<bt_value>;
700 using ConstSignedIntegerValue = CommonSignedIntegerValue<const bt_value>;
701
702 namespace internal {
703
704 struct SignedIntegerValueTypeDescr
705 {
706 using Const = ConstSignedIntegerValue;
707 using NonConst = SignedIntegerValue;
708 };
709
710 template <>
711 struct TypeDescr<SignedIntegerValue> : public SignedIntegerValueTypeDescr
712 {
713 };
714
715 template <>
716 struct TypeDescr<ConstSignedIntegerValue> : public SignedIntegerValueTypeDescr
717 {
718 };
719
720 } /* namespace internal */
721
722 template <typename LibObjT>
723 class CommonRealValue final : public CommonValue<LibObjT>
724 {
725 private:
726 using typename CommonValue<LibObjT>::_LibObjPtr;
727 using typename CommonValue<LibObjT>::_ThisCommonValue;
728
729 public:
730 using Shared = SharedValue<CommonRealValue<LibObjT>, LibObjT>;
731 using Value = double;
732
733 explicit CommonRealValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
734 {
735 BT_ASSERT_DBG(this->isReal());
736 }
737
738 static Shared create(const Value rawVal = 0)
739 {
740 const auto libObjPtr = bt_value_real_create_init(rawVal);
741
742 internal::validateCreatedObjPtr(libObjPtr);
743 return CommonRealValue::Shared::createWithoutRef(libObjPtr);
744 }
745
746 template <typename OtherLibObjT>
747 CommonRealValue(const CommonRealValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
748 {
749 }
750
751 template <typename OtherLibObjT>
752 CommonRealValue<LibObjT>& operator=(const CommonRealValue<OtherLibObjT> val) noexcept
753 {
754 _ThisCommonValue::operator=(val);
755 return *this;
756 }
757
758 CommonRealValue<const bt_value> asConst() const noexcept
759 {
760 return CommonRealValue<const bt_value> {*this};
761 }
762
763 RawValueProxy<CommonRealValue> operator*() const noexcept
764 {
765 return RawValueProxy<CommonRealValue> {*this};
766 }
767
768 void value(const Value val) const noexcept
769 {
770 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstRealValue`.");
771
772 bt_value_real_set(this->libObjPtr(), val);
773 }
774
775 Value value() const noexcept
776 {
777 return bt_value_real_get(this->libObjPtr());
778 }
779
780 Shared shared() const noexcept
781 {
782 return Shared::createWithRef(*this);
783 }
784 };
785
786 using RealValue = CommonRealValue<bt_value>;
787 using ConstRealValue = CommonRealValue<const bt_value>;
788
789 namespace internal {
790
791 struct RealValueTypeDescr
792 {
793 using Const = ConstRealValue;
794 using NonConst = RealValue;
795 };
796
797 template <>
798 struct TypeDescr<RealValue> : public RealValueTypeDescr
799 {
800 };
801
802 template <>
803 struct TypeDescr<ConstRealValue> : public RealValueTypeDescr
804 {
805 };
806
807 } /* namespace internal */
808
809 template <typename LibObjT>
810 class CommonStringValue final : public CommonValue<LibObjT>
811 {
812 private:
813 using typename CommonValue<LibObjT>::_LibObjPtr;
814 using typename CommonValue<LibObjT>::_ThisCommonValue;
815
816 public:
817 using Shared = SharedValue<CommonStringValue<LibObjT>, LibObjT>;
818 using Value = const char *;
819
820 explicit CommonStringValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
821 {
822 BT_ASSERT_DBG(this->isString());
823 }
824
825 static Shared create(const char * const rawVal = "")
826 {
827 const auto libObjPtr = bt_value_string_create_init(rawVal);
828
829 internal::validateCreatedObjPtr(libObjPtr);
830 return CommonStringValue::Shared::createWithoutRef(libObjPtr);
831 }
832
833 static Shared create(const std::string& rawVal)
834 {
835 return CommonStringValue::create(rawVal.data());
836 }
837
838 template <typename OtherLibObjT>
839 CommonStringValue(const CommonStringValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
840 {
841 }
842
843 template <typename OtherLibObjT>
844 CommonStringValue<LibObjT>& operator=(const CommonStringValue<OtherLibObjT> val) noexcept
845 {
846 _ThisCommonValue::operator=(val);
847 return *this;
848 }
849
850 CommonStringValue<const bt_value> asConst() const noexcept
851 {
852 return CommonStringValue<const bt_value> {*this};
853 }
854
855 RawStringValueProxy<CommonStringValue> operator*() const noexcept
856 {
857 return RawStringValueProxy<CommonStringValue> {*this};
858 }
859
860 void value(const Value val) const
861 {
862 static_assert(!std::is_const<LibObjT>::value,
863 "Not available with `bt2::ConstStringValue`.");
864
865 const auto status = bt_value_string_set(this->libObjPtr(), val);
866
867 if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) {
868 throw MemoryError {};
869 }
870 }
871
872 void value(const std::string& val) const
873 {
874 this->value(val.data());
875 }
876
877 const char *value() const noexcept
878 {
879 return bt_value_string_get(this->libObjPtr());
880 }
881
882 Shared shared() const noexcept
883 {
884 return Shared::createWithRef(*this);
885 }
886 };
887
888 using StringValue = CommonStringValue<bt_value>;
889 using ConstStringValue = CommonStringValue<const bt_value>;
890
891 namespace internal {
892
893 struct StringValueTypeDescr
894 {
895 using Const = ConstStringValue;
896 using NonConst = StringValue;
897 };
898
899 template <>
900 struct TypeDescr<StringValue> : public StringValueTypeDescr
901 {
902 };
903
904 template <>
905 struct TypeDescr<ConstStringValue> : public StringValueTypeDescr
906 {
907 };
908
909 template <typename LibObjT>
910 struct CommonArrayValueSpec;
911
912 /* Functions specific to mutable array values */
913 template <>
914 struct CommonArrayValueSpec<bt_value> final
915 {
916 static bt_value *elementByIndex(bt_value * const libValPtr, const std::uint64_t index) noexcept
917 {
918 return bt_value_array_borrow_element_by_index(libValPtr, index);
919 }
920 };
921
922 /* Functions specific to constant array values */
923 template <>
924 struct CommonArrayValueSpec<const bt_value> final
925 {
926 static const bt_value *elementByIndex(const bt_value * const libValPtr,
927 const std::uint64_t index) noexcept
928 {
929 return bt_value_array_borrow_element_by_index_const(libValPtr, index);
930 }
931 };
932
933 } /* namespace internal */
934
935 template <typename LibObjT>
936 class CommonArrayValue final : public CommonValue<LibObjT>
937 {
938 private:
939 using typename CommonValue<LibObjT>::_LibObjPtr;
940 using typename CommonValue<LibObjT>::_ThisCommonValue;
941
942 public:
943 using Shared = SharedValue<CommonArrayValue<LibObjT>, LibObjT>;
944 using Iterator = BorrowedObjectIterator<CommonArrayValue<LibObjT>>;
945
946 explicit CommonArrayValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
947 {
948 BT_ASSERT_DBG(this->isArray());
949 }
950
951 static Shared create()
952 {
953 const auto libObjPtr = bt_value_array_create();
954
955 internal::validateCreatedObjPtr(libObjPtr);
956 return CommonArrayValue::Shared::createWithoutRef(libObjPtr);
957 }
958
959 template <typename OtherLibObjT>
960 CommonArrayValue(const CommonArrayValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
961 {
962 }
963
964 template <typename OtherLibObjT>
965 CommonArrayValue<LibObjT>& operator=(const CommonArrayValue<OtherLibObjT> val) noexcept
966 {
967 _ThisCommonValue::operator=(val);
968 return *this;
969 }
970
971 CommonArrayValue<const bt_value> asConst() const noexcept
972 {
973 return CommonArrayValue<const bt_value> {*this};
974 }
975
976 std::uint64_t length() const noexcept
977 {
978 return bt_value_array_get_length(this->libObjPtr());
979 }
980
981 Iterator begin() const noexcept
982 {
983 return Iterator {*this, 0};
984 }
985
986 Iterator end() const noexcept
987 {
988 return Iterator {*this, this->length()};
989 }
990
991 bool isEmpty() const noexcept
992 {
993 return this->length() == 0;
994 }
995
996 CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept
997 {
998 return CommonValue<LibObjT> {
999 internal::CommonArrayValueSpec<LibObjT>::elementByIndex(this->libObjPtr(), index)};
1000 }
1001
1002 void append(const Value val) const
1003 {
1004 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1005
1006 const auto status = bt_value_array_append_element(this->libObjPtr(), val.libObjPtr());
1007
1008 this->_handleAppendLibStatus(status);
1009 }
1010
1011 void append(const bool rawVal) const
1012 {
1013 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1014
1015 const auto status =
1016 bt_value_array_append_bool_element(this->libObjPtr(), static_cast<bt_bool>(rawVal));
1017
1018 this->_handleAppendLibStatus(status);
1019 }
1020
1021 void append(const std::uint64_t rawVal) const
1022 {
1023 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1024
1025 const auto status =
1026 bt_value_array_append_unsigned_integer_element(this->libObjPtr(), rawVal);
1027
1028 this->_handleAppendLibStatus(status);
1029 }
1030
1031 void append(const std::int64_t rawVal) const
1032 {
1033 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1034
1035 const auto status = bt_value_array_append_signed_integer_element(this->libObjPtr(), rawVal);
1036
1037 this->_handleAppendLibStatus(status);
1038 }
1039
1040 void append(const double rawVal) const
1041 {
1042 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1043
1044 const auto status = bt_value_array_append_real_element(this->libObjPtr(), rawVal);
1045
1046 this->_handleAppendLibStatus(status);
1047 }
1048
1049 void append(const char * const rawVal) const
1050 {
1051 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1052
1053 const auto status = bt_value_array_append_string_element(this->libObjPtr(), rawVal);
1054
1055 this->_handleAppendLibStatus(status);
1056 }
1057
1058 void append(const std::string& rawVal) const
1059 {
1060 this->append(rawVal.data());
1061 }
1062
1063 CommonArrayValue<bt_value> appendEmptyArray() const;
1064 CommonMapValue<bt_value> appendEmptyMap() const;
1065
1066 void operator+=(const Value val) const
1067 {
1068 this->append(val);
1069 }
1070
1071 void operator+=(const bool rawVal) const
1072 {
1073 this->append(rawVal);
1074 }
1075
1076 void operator+=(const std::uint64_t rawVal) const
1077 {
1078 this->append(rawVal);
1079 }
1080
1081 void operator+=(const std::int64_t rawVal) const
1082 {
1083 this->append(rawVal);
1084 }
1085
1086 void operator+=(const double rawVal) const
1087 {
1088 this->append(rawVal);
1089 }
1090
1091 void operator+=(const char * const rawVal) const
1092 {
1093 this->append(rawVal);
1094 }
1095
1096 void operator+=(const std::string& rawVal) const
1097 {
1098 this->append(rawVal);
1099 }
1100
1101 Shared shared() const noexcept
1102 {
1103 return Shared::createWithRef(*this);
1104 }
1105
1106 private:
1107 void _handleAppendLibStatus(const bt_value_array_append_element_status status) const
1108 {
1109 if (status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR) {
1110 throw MemoryError {};
1111 }
1112 }
1113 };
1114
1115 using ArrayValue = CommonArrayValue<bt_value>;
1116 using ConstArrayValue = CommonArrayValue<const bt_value>;
1117
1118 namespace internal {
1119
1120 struct ArrayValueTypeDescr
1121 {
1122 using Const = ConstArrayValue;
1123 using NonConst = ArrayValue;
1124 };
1125
1126 template <>
1127 struct TypeDescr<ArrayValue> : public ArrayValueTypeDescr
1128 {
1129 };
1130
1131 template <>
1132 struct TypeDescr<ConstArrayValue> : public ArrayValueTypeDescr
1133 {
1134 };
1135
1136 /*
1137 * Type of a user function passed to `CommonMapValue<ObjT>::forEach()`.
1138 *
1139 * First argument is the entry's key, second is its value.
1140 */
1141 template <typename ObjT>
1142 using CommonMapValueForEachUserFunc = std::function<void(const bpstd::string_view&, ObjT)>;
1143
1144 /*
1145 * Template of a function to be passed to bt_value_map_foreach_entry()
1146 * for bt_value_map_foreach_entry_const() which calls a user function.
1147 *
1148 * `userData` is casted to a `const` pointer to
1149 * `CommonMapValueForEachUserFunc<ObjT>` (the user function to call).
1150 *
1151 * This function catches any exception which the user function throws
1152 * and returns the `ErrorStatus` value. If there's no execption, this
1153 * function returns the `OkStatus` value.
1154 */
1155 template <typename ObjT, typename LibObjT, typename LibStatusT, int OkStatus, int ErrorStatus>
1156 LibStatusT mapValueForEachLibFunc(const char * const key, LibObjT * const libObjPtr,
1157 void * const userData)
1158 {
1159 const auto& userFunc = *reinterpret_cast<const CommonMapValueForEachUserFunc<ObjT> *>(userData);
1160
1161 try {
1162 userFunc(key, ObjT {libObjPtr});
1163 } catch (...) {
1164 return static_cast<LibStatusT>(ErrorStatus);
1165 }
1166
1167 return static_cast<LibStatusT>(OkStatus);
1168 }
1169
1170 template <typename LibObjT>
1171 struct CommonMapValueSpec;
1172
1173 /* Functions specific to mutable map values */
1174 template <>
1175 struct CommonMapValueSpec<bt_value> final
1176 {
1177 static bt_value *entryByKey(bt_value * const libValPtr, const char * const key) noexcept
1178 {
1179 return bt_value_map_borrow_entry_value(libValPtr, key);
1180 }
1181
1182 static void forEach(bt_value * const libValPtr,
1183 const CommonMapValueForEachUserFunc<Value>& func)
1184 {
1185 const auto status = bt_value_map_foreach_entry(
1186 libValPtr,
1187 mapValueForEachLibFunc<Value, bt_value, bt_value_map_foreach_entry_func_status,
1188 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK,
1189 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR>,
1190 const_cast<void *>(reinterpret_cast<const void *>(&func)));
1191
1192 switch (status) {
1193 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_OK:
1194 return;
1195 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR:
1196 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR:
1197 throw Error {};
1198 default:
1199 bt_common_abort();
1200 }
1201 }
1202 };
1203
1204 /* Functions specific to constant map values */
1205 template <>
1206 struct CommonMapValueSpec<const bt_value> final
1207 {
1208 static const bt_value *entryByKey(const bt_value * const libValPtr,
1209 const char * const key) noexcept
1210 {
1211 return bt_value_map_borrow_entry_value_const(libValPtr, key);
1212 }
1213
1214 static void forEach(const bt_value * const libValPtr,
1215 const CommonMapValueForEachUserFunc<ConstValue>& func)
1216 {
1217 const auto status = bt_value_map_foreach_entry_const(
1218 libValPtr,
1219 mapValueForEachLibFunc<ConstValue, const bt_value,
1220 bt_value_map_foreach_entry_const_func_status,
1221 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK,
1222 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_ERROR>,
1223 const_cast<void *>(reinterpret_cast<const void *>(&func)));
1224
1225 switch (status) {
1226 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK:
1227 return;
1228 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_USER_ERROR:
1229 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR:
1230 throw Error {};
1231 default:
1232 bt_common_abort();
1233 }
1234 }
1235 };
1236
1237 } /* namespace internal */
1238
1239 template <typename LibObjT>
1240 class CommonMapValue final : public CommonValue<LibObjT>
1241 {
1242 private:
1243 using typename CommonValue<LibObjT>::_LibObjPtr;
1244 using typename CommonValue<LibObjT>::_ThisCommonValue;
1245
1246 public:
1247 using Shared = SharedValue<CommonMapValue<LibObjT>, LibObjT>;
1248
1249 explicit CommonMapValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
1250 {
1251 BT_ASSERT_DBG(this->isMap());
1252 }
1253
1254 static Shared create()
1255 {
1256 const auto libObjPtr = bt_value_map_create();
1257
1258 internal::validateCreatedObjPtr(libObjPtr);
1259 return CommonMapValue::Shared::createWithoutRef(libObjPtr);
1260 }
1261
1262 template <typename OtherLibObjT>
1263 CommonMapValue(const CommonMapValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
1264 {
1265 }
1266
1267 template <typename OtherLibObjT>
1268 CommonMapValue<LibObjT>& operator=(const CommonMapValue<OtherLibObjT> val) noexcept
1269 {
1270 _ThisCommonValue::operator=(val);
1271 return *this;
1272 }
1273
1274 CommonMapValue<const bt_value> asConst() const noexcept
1275 {
1276 return CommonMapValue<const bt_value> {*this};
1277 }
1278
1279 std::uint64_t length() const noexcept
1280 {
1281 return bt_value_map_get_size(this->libObjPtr());
1282 }
1283
1284 bool isEmpty() const noexcept
1285 {
1286 return this->length() == 0;
1287 }
1288
1289 nonstd::optional<CommonValue<LibObjT>> operator[](const char * const key) const noexcept
1290 {
1291 const auto libObjPtr =
1292 internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key);
1293
1294 if (!libObjPtr) {
1295 return nonstd::nullopt;
1296 }
1297
1298 return CommonValue<LibObjT> {libObjPtr};
1299 }
1300
1301 nonstd::optional<CommonValue<LibObjT>> operator[](const std::string& key) const noexcept
1302 {
1303 return (*this)[key.data()];
1304 }
1305
1306 bool hasEntry(const char * const key) const noexcept
1307 {
1308 return static_cast<bool>(bt_value_map_has_entry(this->libObjPtr(), key));
1309 }
1310
1311 bool hasEntry(const std::string& key) const noexcept
1312 {
1313 return this->hasEntry(key.data());
1314 }
1315
1316 void insert(const char * const key, const Value val) const
1317 {
1318 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1319
1320 const auto status = bt_value_map_insert_entry(this->libObjPtr(), key, val.libObjPtr());
1321
1322 this->_handleInsertLibStatus(status);
1323 }
1324
1325 void insert(const std::string& key, const Value val) const
1326 {
1327 this->insert(key.data(), val);
1328 }
1329
1330 void insert(const char * const key, const bool rawVal) const
1331 {
1332 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1333
1334 const auto status =
1335 bt_value_map_insert_bool_entry(this->libObjPtr(), key, static_cast<bt_bool>(rawVal));
1336
1337 this->_handleInsertLibStatus(status);
1338 }
1339
1340 void insert(const std::string& key, const bool rawVal) const
1341 {
1342 this->insert(key.data(), rawVal);
1343 }
1344
1345 void insert(const char * const key, const std::uint64_t rawVal) const
1346 {
1347 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1348
1349 const auto status =
1350 bt_value_map_insert_unsigned_integer_entry(this->libObjPtr(), key, rawVal);
1351
1352 this->_handleInsertLibStatus(status);
1353 }
1354
1355 void insert(const std::string& key, const std::uint64_t rawVal) const
1356 {
1357 this->insert(key.data(), rawVal);
1358 }
1359
1360 void insert(const char * const key, const std::int64_t rawVal) const
1361 {
1362 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1363
1364 const auto status =
1365 bt_value_map_insert_signed_integer_entry(this->libObjPtr(), key, rawVal);
1366
1367 this->_handleInsertLibStatus(status);
1368 }
1369
1370 void insert(const std::string& key, const std::int64_t rawVal) const
1371 {
1372 this->insert(key.data(), rawVal);
1373 }
1374
1375 void insert(const char * const key, const double rawVal) const
1376 {
1377 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1378
1379 const auto status = bt_value_map_insert_real_entry(this->libObjPtr(), key, rawVal);
1380
1381 this->_handleInsertLibStatus(status);
1382 }
1383
1384 void insert(const std::string& key, const double rawVal) const
1385 {
1386 this->insert(key.data(), rawVal);
1387 }
1388
1389 void insert(const char * const key, const char * const rawVal) const
1390 {
1391 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1392
1393 const auto status = bt_value_map_insert_string_entry(this->libObjPtr(), key, rawVal);
1394
1395 this->_handleInsertLibStatus(status);
1396 }
1397
1398 void insert(const char * const key, const std::string& rawVal) const
1399 {
1400 this->insert(key, rawVal.data());
1401 }
1402
1403 void insert(const std::string& key, const char * const rawVal) const
1404 {
1405 this->insert(key.data(), rawVal);
1406 }
1407
1408 void insert(const std::string& key, const std::string& rawVal) const
1409 {
1410 this->insert(key.data(), rawVal.data());
1411 }
1412
1413 CommonArrayValue<bt_value> insertEmptyArray(const char *key) const;
1414 CommonArrayValue<bt_value> insertEmptyArray(const std::string& key) const;
1415 CommonMapValue<bt_value> insertEmptyMap(const char *key) const;
1416 CommonMapValue<bt_value> insertEmptyMap(const std::string& key) const;
1417
1418 void forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func) const
1419 {
1420 internal::CommonMapValueSpec<LibObjT>::forEach(this->libObjPtr(), func);
1421 }
1422
1423 Shared shared() const noexcept
1424 {
1425 return Shared::createWithRef(*this);
1426 }
1427
1428 private:
1429 void _handleInsertLibStatus(const bt_value_map_insert_entry_status status) const
1430 {
1431 if (status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_MEMORY_ERROR) {
1432 throw MemoryError {};
1433 }
1434 }
1435 };
1436
1437 using MapValue = CommonMapValue<bt_value>;
1438 using ConstMapValue = CommonMapValue<const bt_value>;
1439
1440 namespace internal {
1441
1442 struct MapValueTypeDescr
1443 {
1444 using Const = ConstMapValue;
1445 using NonConst = MapValue;
1446 };
1447
1448 template <>
1449 struct TypeDescr<MapValue> : public MapValueTypeDescr
1450 {
1451 };
1452
1453 template <>
1454 struct TypeDescr<ConstMapValue> : public MapValueTypeDescr
1455 {
1456 };
1457
1458 } /* namespace internal */
1459
1460 template <typename LibObjT>
1461 ArrayValue CommonValue<LibObjT>::appendEmptyArray() const
1462 {
1463 return this->asArray().appendEmptyArray();
1464 }
1465
1466 template <typename LibObjT>
1467 MapValue CommonValue<LibObjT>::appendEmptyMap() const
1468 {
1469 return this->asArray().appendEmptyMap();
1470 }
1471
1472 template <typename LibObjT>
1473 ArrayValue CommonValue<LibObjT>::insertEmptyArray(const char * const key) const
1474 {
1475 return this->asMap().insertEmptyArray(key);
1476 }
1477
1478 template <typename LibObjT>
1479 ArrayValue CommonValue<LibObjT>::insertEmptyArray(const std::string& key) const
1480 {
1481 return this->asMap().insertEmptyArray(key);
1482 }
1483
1484 template <typename LibObjT>
1485 MapValue CommonValue<LibObjT>::insertEmptyMap(const char * const key) const
1486 {
1487 return this->asMap().insertEmptyMap(key);
1488 }
1489
1490 template <typename LibObjT>
1491 MapValue CommonValue<LibObjT>::insertEmptyMap(const std::string& key) const
1492 {
1493 return this->asMap().insertEmptyMap(key);
1494 }
1495
1496 template <typename LibObjT>
1497 CommonNullValue<LibObjT> CommonValue<LibObjT>::asNull() const noexcept
1498 {
1499 BT_ASSERT_DBG(this->isNull());
1500 return CommonNullValue<LibObjT> {this->libObjPtr()};
1501 }
1502
1503 template <typename LibObjT>
1504 CommonBoolValue<LibObjT> CommonValue<LibObjT>::asBool() const noexcept
1505 {
1506 BT_ASSERT_DBG(this->isBool());
1507 return CommonBoolValue<LibObjT> {this->libObjPtr()};
1508 }
1509
1510 template <typename LibObjT>
1511 CommonSignedIntegerValue<LibObjT> CommonValue<LibObjT>::asSignedInteger() const noexcept
1512 {
1513 BT_ASSERT_DBG(this->isSignedInteger());
1514 return CommonSignedIntegerValue<LibObjT> {this->libObjPtr()};
1515 }
1516
1517 template <typename LibObjT>
1518 CommonUnsignedIntegerValue<LibObjT> CommonValue<LibObjT>::asUnsignedInteger() const noexcept
1519 {
1520 BT_ASSERT_DBG(this->isUnsignedInteger());
1521 return CommonUnsignedIntegerValue<LibObjT> {this->libObjPtr()};
1522 }
1523
1524 template <typename LibObjT>
1525 CommonRealValue<LibObjT> CommonValue<LibObjT>::asReal() const noexcept
1526 {
1527 BT_ASSERT_DBG(this->isReal());
1528 return CommonRealValue<LibObjT> {this->libObjPtr()};
1529 }
1530
1531 template <typename LibObjT>
1532 CommonStringValue<LibObjT> CommonValue<LibObjT>::asString() const noexcept
1533 {
1534 BT_ASSERT_DBG(this->isString());
1535 return CommonStringValue<LibObjT> {this->libObjPtr()};
1536 }
1537
1538 template <typename LibObjT>
1539 CommonArrayValue<LibObjT> CommonValue<LibObjT>::asArray() const noexcept
1540 {
1541 BT_ASSERT_DBG(this->isArray());
1542 return CommonArrayValue<LibObjT> {this->libObjPtr()};
1543 }
1544
1545 template <typename LibObjT>
1546 CommonMapValue<LibObjT> CommonValue<LibObjT>::asMap() const noexcept
1547 {
1548 BT_ASSERT_DBG(this->isMap());
1549 return CommonMapValue<LibObjT> {this->libObjPtr()};
1550 }
1551
1552 template <typename LibObjT>
1553 ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray() const
1554 {
1555 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1556
1557 bt_value *libElemPtr;
1558 const auto status = bt_value_array_append_empty_array_element(this->libObjPtr(), &libElemPtr);
1559
1560 this->_handleAppendLibStatus(status);
1561 return ArrayValue {libElemPtr};
1562 }
1563
1564 template <typename LibObjT>
1565 MapValue CommonArrayValue<LibObjT>::appendEmptyMap() const
1566 {
1567 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
1568
1569 bt_value *libElemPtr;
1570 const auto status = bt_value_array_append_empty_map_element(this->libObjPtr(), &libElemPtr);
1571
1572 this->_handleAppendLibStatus(status);
1573 return MapValue {libElemPtr};
1574 }
1575
1576 template <typename LibObjT>
1577 ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key) const
1578 {
1579 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1580
1581 bt_value *libEntryPtr;
1582 const auto status = bt_value_map_insert_empty_array_entry(this->libObjPtr(), key, &libEntryPtr);
1583
1584 this->_handleInsertLibStatus(status);
1585 return ArrayValue {libEntryPtr};
1586 }
1587
1588 template <typename LibObjT>
1589 ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const std::string& key) const
1590 {
1591 return this->insertEmptyArray(key.data());
1592 }
1593
1594 template <typename LibObjT>
1595 MapValue CommonMapValue<LibObjT>::insertEmptyMap(const char * const key) const
1596 {
1597 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
1598
1599 bt_value *libEntryPtr;
1600 const auto status = bt_value_map_insert_empty_map_entry(this->libObjPtr(), key, &libEntryPtr);
1601
1602 this->_handleInsertLibStatus(status);
1603 return MapValue {libEntryPtr};
1604 }
1605
1606 template <typename LibObjT>
1607 MapValue CommonMapValue<LibObjT>::insertEmptyMap(const std::string& key) const
1608 {
1609 return this->insertEmptyMap(key.data());
1610 }
1611
1612 inline BoolValue::Shared createValue(const bool rawVal)
1613 {
1614 return BoolValue::create(rawVal);
1615 }
1616
1617 inline UnsignedIntegerValue::Shared createValue(const std::uint64_t rawVal)
1618 {
1619 return UnsignedIntegerValue::create(rawVal);
1620 }
1621
1622 inline SignedIntegerValue::Shared createValue(const std::int64_t rawVal)
1623 {
1624 return SignedIntegerValue::create(rawVal);
1625 }
1626
1627 inline RealValue::Shared createValue(const double rawVal)
1628 {
1629 return RealValue::create(rawVal);
1630 }
1631
1632 inline StringValue::Shared createValue(const char * const rawVal)
1633 {
1634 return StringValue::create(rawVal);
1635 }
1636
1637 inline StringValue::Shared createValue(const std::string& rawVal)
1638 {
1639 return StringValue::create(rawVal);
1640 }
1641
1642 } /* namespace bt2 */
1643
1644 #endif /* BABELTRACE_CPP_COMMON_BT2_VALUE_HPP */
This page took 0.085929 seconds and 4 git commands to generate.