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