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