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