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