cpp-common/bt2: make setters return `*this`
[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"
e7f0f07b 18#include "cpp-common/bt2c/c-string-view.hpp"
c802cacb 19
56862ee2 20#include "borrowed-object-iterator.hpp"
0d218157 21#include "borrowed-object.hpp"
c802cacb 22#include "exc.hpp"
33a17831 23#include "internal/utils.hpp"
ca61ecbc 24#include "optional-borrowed-object.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);
15030982 98 CommonValueRawValueProxy& operator=(bt2c::CStringView rawVal);
b3f060ed
PP
99 operator bool() const noexcept;
100 operator std::int64_t() const noexcept;
101 operator std::uint64_t() const noexcept;
102 operator double() const noexcept;
e7f0f07b 103 operator bt2c::CStringView() const noexcept;
b3f060ed
PP
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:
33a17831
PP
116 using _ThisCommonValue = CommonValue<LibObjT>;
117
118public:
d246c457 119 using typename BorrowedObject<LibObjT>::LibObjPtr;
26b9d24c 120 using Shared = SharedValue<CommonValue<LibObjT>, LibObjT>;
33a17831 121
d246c457 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>
ac30a470 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>
ca61ecbc 245 OptionalBorrowedObject<CommonValue<LibObjT>> operator[](KeyT&& key) const noexcept
a699e23c
PP
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
15030982
SM
262 CommonArrayValue<bt_value> insertEmptyArray(bt2c::CStringView key) const;
263 CommonMapValue<bt_value> insertEmptyMap(bt2c::CStringView key) const;
a699e23c 264
33a17831
PP
265 Shared shared() const noexcept
266 {
c9c0b6e2 267 return Shared::createWithRef(*this);
33a17831
PP
268 }
269
45e0ded5
PP
270 template <typename ValueT>
271 ValueT as() const noexcept
272 {
273 return ValueT {this->libObjPtr()};
274 }
275
33a17831
PP
276 CommonNullValue<LibObjT> asNull() const noexcept;
277 CommonBoolValue<LibObjT> asBool() const noexcept;
278 CommonSignedIntegerValue<LibObjT> asSignedInteger() const noexcept;
279 CommonUnsignedIntegerValue<LibObjT> asUnsignedInteger() const noexcept;
280 CommonRealValue<LibObjT> asReal() const noexcept;
281 CommonStringValue<LibObjT> asString() const noexcept;
282 CommonArrayValue<LibObjT> asArray() const noexcept;
283 CommonMapValue<LibObjT> asMap() const noexcept;
284
285protected:
286 bool _libTypeIs(const bt_value_type type) const noexcept
287 {
341a67c4 288 return bt_value_type_is(bt_value_get_type(this->libObjPtr()), type);
33a17831
PP
289 }
290};
291
292using Value = CommonValue<bt_value>;
293using ConstValue = CommonValue<const bt_value>;
294
b3f060ed
PP
295template <typename ValueObjT>
296CommonValueRawValueProxy<ValueObjT>&
297CommonValueRawValueProxy<ValueObjT>::operator=(const bool rawVal) noexcept
298{
299 _mObj.asBool().value(rawVal);
300 return *this;
301}
302
303template <typename ValueObjT>
304CommonValueRawValueProxy<ValueObjT>&
305CommonValueRawValueProxy<ValueObjT>::operator=(const std::int64_t rawVal) noexcept
306{
307 _mObj.asSignedInteger().value(rawVal);
308 return *this;
309}
310
311template <typename ValueObjT>
312CommonValueRawValueProxy<ValueObjT>&
313CommonValueRawValueProxy<ValueObjT>::operator=(const std::uint64_t rawVal) noexcept
314{
315 _mObj.asUnsignedInteger().value(rawVal);
316 return *this;
317}
318
319template <typename ValueObjT>
320CommonValueRawValueProxy<ValueObjT>&
321CommonValueRawValueProxy<ValueObjT>::operator=(const double rawVal) noexcept
322{
323 _mObj.asReal().value(rawVal);
324 return *this;
325}
326
327template <typename ValueObjT>
328CommonValueRawValueProxy<ValueObjT>&
329CommonValueRawValueProxy<ValueObjT>::operator=(const char * const rawVal)
330{
331 _mObj.asString().value(rawVal);
332 return *this;
333}
334
335template <typename ValueObjT>
336CommonValueRawValueProxy<ValueObjT>&
15030982 337CommonValueRawValueProxy<ValueObjT>::operator=(const bt2c::CStringView rawVal)
b3f060ed 338{
e7f0f07b
SM
339 _mObj.asString().value(rawVal);
340 return *this;
b3f060ed
PP
341}
342
343template <typename ValueObjT>
344CommonValueRawValueProxy<ValueObjT>::operator bool() const noexcept
345{
346 return _mObj.asBool().value();
347}
348
349template <typename ValueObjT>
350CommonValueRawValueProxy<ValueObjT>::operator std::int64_t() const noexcept
351{
352 return _mObj.asSignedInteger().value();
353}
354
355template <typename ValueObjT>
356CommonValueRawValueProxy<ValueObjT>::operator std::uint64_t() const noexcept
357{
358 return _mObj.asUnsignedInteger().value();
359}
360
361template <typename ValueObjT>
362CommonValueRawValueProxy<ValueObjT>::operator double() const noexcept
363{
364 return _mObj.asReal().value();
365}
366
e7f0f07b
SM
367template <typename ValueObjT>
368CommonValueRawValueProxy<ValueObjT>::operator bt2c::CStringView() const noexcept
369{
370 return _mObj.asString().value();
371}
372
4927bae7
PP
373namespace internal {
374
375struct ValueTypeDescr
376{
377 using Const = ConstValue;
378 using NonConst = Value;
379};
380
381template <>
382struct TypeDescr<Value> : public ValueTypeDescr
383{
384};
385
386template <>
387struct TypeDescr<ConstValue> : public ValueTypeDescr
388{
389};
390
391} /* namespace internal */
392
33a17831
PP
393template <typename LibObjT>
394class CommonNullValue final : public CommonValue<LibObjT>
395{
396private:
397 using typename CommonValue<LibObjT>::_ThisCommonValue;
398
399public:
26b9d24c 400 using Shared = SharedValue<CommonNullValue<LibObjT>, LibObjT>;
33a17831
PP
401
402 CommonNullValue() noexcept : _ThisCommonValue {bt_value_null}
403 {
404 }
405
406 template <typename OtherLibObjT>
100fa861 407 CommonNullValue(const CommonNullValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
33a17831
PP
408 {
409 }
410
411 template <typename OtherLibObjT>
ac30a470 412 CommonNullValue<LibObjT> operator=(const CommonNullValue<OtherLibObjT> val) noexcept
33a17831
PP
413 {
414 _ThisCommonValue::operator=(val);
415 return *this;
416 }
417
328a274a
PP
418 CommonNullValue<const bt_value> asConst() const noexcept
419 {
420 return CommonNullValue<const bt_value> {*this};
421 }
422
33a17831
PP
423 Shared shared() const noexcept
424 {
c9c0b6e2 425 return Shared::createWithRef(*this);
33a17831
PP
426 }
427};
428
429using NullValue = CommonNullValue<bt_value>;
430using ConstNullValue = CommonNullValue<const bt_value>;
431
4927bae7
PP
432namespace internal {
433
434struct NullValueTypeDescr
435{
436 using Const = ConstNullValue;
437 using NonConst = NullValue;
438};
439
440template <>
441struct TypeDescr<NullValue> : public NullValueTypeDescr
442{
443};
444
445template <>
446struct TypeDescr<ConstNullValue> : public NullValueTypeDescr
447{
448};
449
450} /* namespace internal */
451
33a17831
PP
452template <typename LibObjT>
453class CommonBoolValue final : public CommonValue<LibObjT>
454{
455private:
33a17831
PP
456 using typename CommonValue<LibObjT>::_ThisCommonValue;
457
458public:
d246c457 459 using typename CommonValue<LibObjT>::LibObjPtr;
26b9d24c 460 using Shared = SharedValue<CommonBoolValue<LibObjT>, LibObjT>;
33a17831
PP
461 using Value = bool;
462
d246c457 463 explicit CommonBoolValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
33a17831
PP
464 {
465 BT_ASSERT_DBG(this->isBool());
466 }
467
468 template <typename OtherLibObjT>
100fa861 469 CommonBoolValue(const CommonBoolValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
33a17831
PP
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);
c9c0b6e2 478 return CommonBoolValue::Shared::createWithoutRef(libObjPtr);
33a17831
PP
479 }
480
481 template <typename OtherLibObjT>
ac30a470 482 CommonBoolValue<LibObjT> operator=(const CommonBoolValue<OtherLibObjT> val) noexcept
33a17831
PP
483 {
484 _ThisCommonValue::operator=(val);
485 return *this;
486 }
487
328a274a
PP
488 CommonBoolValue<const bt_value> asConst() const noexcept
489 {
490 return CommonBoolValue<const bt_value> {*this};
491 }
492
b3f060ed 493 RawValueProxy<CommonBoolValue> operator*() const noexcept
33a17831 494 {
b3f060ed 495 return RawValueProxy<CommonBoolValue> {*this};
33a17831
PP
496 }
497
498 Value value() const noexcept
499 {
341a67c4 500 return static_cast<Value>(bt_value_bool_get(this->libObjPtr()));
33a17831
PP
501 }
502
2a24eba8 503 CommonBoolValue value(const Value val) const noexcept
33a17831 504 {
b3f060ed
PP
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));
2a24eba8 508 return *this;
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:
33a17831
PP
544 using typename CommonValue<LibObjT>::_ThisCommonValue;
545
546public:
d246c457 547 using typename CommonValue<LibObjT>::LibObjPtr;
26b9d24c 548 using Shared = SharedValue<CommonUnsignedIntegerValue<LibObjT>, LibObjT>;
33a17831
PP
549 using Value = std::uint64_t;
550
d246c457 551 explicit CommonUnsignedIntegerValue(const LibObjPtr libObjPtr) noexcept :
33a17831
PP
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>
ac30a470 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
2a24eba8 589 CommonUnsignedIntegerValue 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);
2a24eba8 595 return *this;
33a17831
PP
596 }
597
598 Value value() const noexcept
599 {
341a67c4 600 return bt_value_integer_unsigned_get(this->libObjPtr());
33a17831
PP
601 }
602
33a17831
PP
603 Shared shared() const noexcept
604 {
c9c0b6e2 605 return Shared::createWithRef(*this);
33a17831
PP
606 }
607};
608
609using UnsignedIntegerValue = CommonUnsignedIntegerValue<bt_value>;
610using ConstUnsignedIntegerValue = CommonUnsignedIntegerValue<const bt_value>;
611
4927bae7
PP
612namespace internal {
613
614struct UnsignedIntegerValueTypeDescr
615{
616 using Const = ConstUnsignedIntegerValue;
617 using NonConst = UnsignedIntegerValue;
618};
619
620template <>
621struct TypeDescr<UnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
622{
623};
624
625template <>
626struct TypeDescr<ConstUnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
627{
628};
629
630} /* namespace internal */
631
33a17831
PP
632template <typename LibObjT>
633class CommonSignedIntegerValue final : public CommonValue<LibObjT>
634{
635private:
33a17831
PP
636 using typename CommonValue<LibObjT>::_ThisCommonValue;
637
638public:
d246c457 639 using typename CommonValue<LibObjT>::LibObjPtr;
26b9d24c 640 using Shared = SharedValue<CommonSignedIntegerValue<LibObjT>, LibObjT>;
33a17831
PP
641 using Value = std::int64_t;
642
d246c457 643 explicit CommonSignedIntegerValue(const LibObjPtr libObjPtr) noexcept :
33a17831
PP
644 _ThisCommonValue {libObjPtr}
645 {
646 BT_ASSERT_DBG(this->isSignedInteger());
647 }
648
649 static Shared create(const Value rawVal = 0)
650 {
651 const auto libObjPtr = bt_value_integer_signed_create_init(rawVal);
652
653 internal::validateCreatedObjPtr(libObjPtr);
c9c0b6e2 654 return CommonSignedIntegerValue::Shared::createWithoutRef(libObjPtr);
33a17831
PP
655 }
656
657 template <typename OtherLibObjT>
100fa861 658 CommonSignedIntegerValue(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept :
33a17831
PP
659 _ThisCommonValue {val}
660 {
661 }
662
663 template <typename OtherLibObjT>
dcb8ae9b 664 CommonSignedIntegerValue<LibObjT>
100fa861 665 operator=(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept
33a17831
PP
666 {
667 _ThisCommonValue::operator=(val);
668 return *this;
669 }
670
328a274a
PP
671 CommonSignedIntegerValue<const bt_value> asConst() const noexcept
672 {
673 return CommonSignedIntegerValue<const bt_value> {*this};
674 }
675
b3f060ed
PP
676 RawValueProxy<CommonSignedIntegerValue> operator*() const noexcept
677 {
678 return RawValueProxy<CommonSignedIntegerValue> {*this};
679 }
680
2a24eba8 681 CommonSignedIntegerValue value(const Value val) const noexcept
33a17831 682 {
5c895f64
PP
683 static_assert(!std::is_const<LibObjT>::value,
684 "Not available with `bt2::ConstSignedIntegerValue`.");
33a17831 685
b3f060ed 686 bt_value_integer_signed_set(this->libObjPtr(), val);
2a24eba8 687 return *this;
33a17831
PP
688 }
689
690 Value value() const noexcept
691 {
341a67c4 692 return bt_value_integer_signed_get(this->libObjPtr());
33a17831
PP
693 }
694
33a17831
PP
695 Shared shared() const noexcept
696 {
c9c0b6e2 697 return Shared::createWithRef(*this);
33a17831
PP
698 }
699};
700
701using SignedIntegerValue = CommonSignedIntegerValue<bt_value>;
702using ConstSignedIntegerValue = CommonSignedIntegerValue<const bt_value>;
703
4927bae7
PP
704namespace internal {
705
706struct SignedIntegerValueTypeDescr
707{
708 using Const = ConstSignedIntegerValue;
709 using NonConst = SignedIntegerValue;
710};
711
712template <>
713struct TypeDescr<SignedIntegerValue> : public SignedIntegerValueTypeDescr
714{
715};
716
717template <>
718struct TypeDescr<ConstSignedIntegerValue> : public SignedIntegerValueTypeDescr
719{
720};
721
722} /* namespace internal */
723
33a17831
PP
724template <typename LibObjT>
725class CommonRealValue final : public CommonValue<LibObjT>
726{
727private:
33a17831
PP
728 using typename CommonValue<LibObjT>::_ThisCommonValue;
729
730public:
d246c457 731 using typename CommonValue<LibObjT>::LibObjPtr;
26b9d24c 732 using Shared = SharedValue<CommonRealValue<LibObjT>, LibObjT>;
33a17831
PP
733 using Value = double;
734
d246c457 735 explicit CommonRealValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
33a17831
PP
736 {
737 BT_ASSERT_DBG(this->isReal());
738 }
739
740 static Shared create(const Value rawVal = 0)
741 {
742 const auto libObjPtr = bt_value_real_create_init(rawVal);
743
744 internal::validateCreatedObjPtr(libObjPtr);
c9c0b6e2 745 return CommonRealValue::Shared::createWithoutRef(libObjPtr);
33a17831
PP
746 }
747
748 template <typename OtherLibObjT>
100fa861 749 CommonRealValue(const CommonRealValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
33a17831
PP
750 {
751 }
752
753 template <typename OtherLibObjT>
ac30a470 754 CommonRealValue<LibObjT> operator=(const CommonRealValue<OtherLibObjT> val) noexcept
33a17831
PP
755 {
756 _ThisCommonValue::operator=(val);
757 return *this;
758 }
759
328a274a
PP
760 CommonRealValue<const bt_value> asConst() const noexcept
761 {
762 return CommonRealValue<const bt_value> {*this};
763 }
764
b3f060ed
PP
765 RawValueProxy<CommonRealValue> operator*() const noexcept
766 {
767 return RawValueProxy<CommonRealValue> {*this};
768 }
769
2a24eba8 770 CommonRealValue value(const Value val) const noexcept
33a17831 771 {
5c895f64 772 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstRealValue`.");
33a17831 773
b3f060ed 774 bt_value_real_set(this->libObjPtr(), val);
2a24eba8 775 return *this;
33a17831
PP
776 }
777
778 Value value() const noexcept
779 {
341a67c4 780 return bt_value_real_get(this->libObjPtr());
33a17831
PP
781 }
782
33a17831
PP
783 Shared shared() const noexcept
784 {
c9c0b6e2 785 return Shared::createWithRef(*this);
33a17831
PP
786 }
787};
788
789using RealValue = CommonRealValue<bt_value>;
790using ConstRealValue = CommonRealValue<const bt_value>;
791
4927bae7
PP
792namespace internal {
793
794struct RealValueTypeDescr
795{
796 using Const = ConstRealValue;
797 using NonConst = RealValue;
798};
799
800template <>
801struct TypeDescr<RealValue> : public RealValueTypeDescr
802{
803};
804
805template <>
806struct TypeDescr<ConstRealValue> : public RealValueTypeDescr
807{
808};
809
810} /* namespace internal */
811
33a17831
PP
812template <typename LibObjT>
813class CommonStringValue final : public CommonValue<LibObjT>
814{
815private:
33a17831
PP
816 using typename CommonValue<LibObjT>::_ThisCommonValue;
817
818public:
d246c457 819 using typename CommonValue<LibObjT>::LibObjPtr;
26b9d24c 820 using Shared = SharedValue<CommonStringValue<LibObjT>, LibObjT>;
e7f0f07b 821 using Value = bt2c::CStringView;
33a17831 822
d246c457 823 explicit CommonStringValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
33a17831
PP
824 {
825 BT_ASSERT_DBG(this->isString());
826 }
827
15030982 828 static Shared create(const bt2c::CStringView rawVal = "")
33a17831
PP
829 {
830 const auto libObjPtr = bt_value_string_create_init(rawVal);
831
832 internal::validateCreatedObjPtr(libObjPtr);
c9c0b6e2 833 return CommonStringValue::Shared::createWithoutRef(libObjPtr);
33a17831
PP
834 }
835
33a17831 836 template <typename OtherLibObjT>
100fa861 837 CommonStringValue(const CommonStringValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
33a17831
PP
838 {
839 }
840
841 template <typename OtherLibObjT>
ac30a470 842 CommonStringValue<LibObjT> operator=(const CommonStringValue<OtherLibObjT> val) noexcept
33a17831
PP
843 {
844 _ThisCommonValue::operator=(val);
845 return *this;
846 }
847
328a274a
PP
848 CommonStringValue<const bt_value> asConst() const noexcept
849 {
850 return CommonStringValue<const bt_value> {*this};
851 }
852
15030982 853 RawValueProxy<CommonStringValue> operator*() const noexcept
b3f060ed 854 {
15030982 855 return RawValueProxy<CommonStringValue> {*this};
b3f060ed
PP
856 }
857
2a24eba8 858 CommonStringValue value(const Value val) const
33a17831 859 {
5c895f64
PP
860 static_assert(!std::is_const<LibObjT>::value,
861 "Not available with `bt2::ConstStringValue`.");
33a17831 862
e7f0f07b 863 const auto status = bt_value_string_set(this->libObjPtr(), *val);
33a17831
PP
864
865 if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) {
39278ebc 866 throw MemoryError {};
33a17831 867 }
2a24eba8
SM
868
869 return *this;
33a17831
PP
870 }
871
e7f0f07b 872 Value value() const noexcept
33a17831 873 {
341a67c4 874 return bt_value_string_get(this->libObjPtr());
33a17831
PP
875 }
876
877 Shared shared() const noexcept
878 {
c9c0b6e2 879 return Shared::createWithRef(*this);
33a17831
PP
880 }
881};
882
883using StringValue = CommonStringValue<bt_value>;
884using ConstStringValue = CommonStringValue<const bt_value>;
885
886namespace internal {
887
4927bae7
PP
888struct StringValueTypeDescr
889{
890 using Const = ConstStringValue;
891 using NonConst = StringValue;
892};
893
894template <>
895struct TypeDescr<StringValue> : public StringValueTypeDescr
896{
897};
898
899template <>
900struct TypeDescr<ConstStringValue> : public StringValueTypeDescr
901{
902};
903
33a17831
PP
904template <typename LibObjT>
905struct CommonArrayValueSpec;
906
b5f55e9f 907/* Functions specific to mutable array values */
33a17831
PP
908template <>
909struct CommonArrayValueSpec<bt_value> final
910{
911 static bt_value *elementByIndex(bt_value * const libValPtr, const std::uint64_t index) noexcept
912 {
913 return bt_value_array_borrow_element_by_index(libValPtr, index);
914 }
915};
916
b5f55e9f 917/* Functions specific to constant array values */
33a17831
PP
918template <>
919struct CommonArrayValueSpec<const bt_value> final
920{
921 static const bt_value *elementByIndex(const bt_value * const libValPtr,
922 const std::uint64_t index) noexcept
923 {
924 return bt_value_array_borrow_element_by_index_const(libValPtr, index);
925 }
926};
927
b5f55e9f 928} /* namespace internal */
33a17831
PP
929
930template <typename LibObjT>
931class CommonArrayValue final : public CommonValue<LibObjT>
932{
933private:
33a17831
PP
934 using typename CommonValue<LibObjT>::_ThisCommonValue;
935
936public:
d246c457 937 using typename CommonValue<LibObjT>::LibObjPtr;
26b9d24c 938 using Shared = SharedValue<CommonArrayValue<LibObjT>, LibObjT>;
56862ee2 939 using Iterator = BorrowedObjectIterator<CommonArrayValue<LibObjT>>;
33a17831 940
d246c457 941 explicit CommonArrayValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
33a17831
PP
942 {
943 BT_ASSERT_DBG(this->isArray());
944 }
945
946 static Shared create()
947 {
948 const auto libObjPtr = bt_value_array_create();
949
950 internal::validateCreatedObjPtr(libObjPtr);
c9c0b6e2 951 return CommonArrayValue::Shared::createWithoutRef(libObjPtr);
33a17831
PP
952 }
953
954 template <typename OtherLibObjT>
100fa861 955 CommonArrayValue(const CommonArrayValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
33a17831
PP
956 {
957 }
958
959 template <typename OtherLibObjT>
ac30a470 960 CommonArrayValue<LibObjT> operator=(const CommonArrayValue<OtherLibObjT> val) noexcept
33a17831
PP
961 {
962 _ThisCommonValue::operator=(val);
963 return *this;
964 }
965
328a274a
PP
966 CommonArrayValue<const bt_value> asConst() const noexcept
967 {
968 return CommonArrayValue<const bt_value> {*this};
969 }
970
33a17831
PP
971 std::uint64_t length() const noexcept
972 {
341a67c4 973 return bt_value_array_get_length(this->libObjPtr());
33a17831
PP
974 }
975
3a343611
FD
976 Iterator begin() const noexcept
977 {
978 return Iterator {*this, 0};
979 }
980
981 Iterator end() const noexcept
982 {
983 return Iterator {*this, this->length()};
984 }
985
33a17831
PP
986 bool isEmpty() const noexcept
987 {
988 return this->length() == 0;
989 }
990
dcb8ae9b 991 CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept
33a17831
PP
992 {
993 return CommonValue<LibObjT> {
341a67c4 994 internal::CommonArrayValueSpec<LibObjT>::elementByIndex(this->libObjPtr(), index)};
33a17831
PP
995 }
996
2a24eba8 997 CommonArrayValue append(const Value val) const
33a17831 998 {
5c895f64 999 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
33a17831 1000
341a67c4 1001 const auto status = bt_value_array_append_element(this->libObjPtr(), val.libObjPtr());
33a17831
PP
1002
1003 this->_handleAppendLibStatus(status);
2a24eba8 1004 return *this;
33a17831
PP
1005 }
1006
2a24eba8 1007 CommonArrayValue append(const bool rawVal) const
33a17831 1008 {
5c895f64 1009 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
33a17831
PP
1010
1011 const auto status =
341a67c4 1012 bt_value_array_append_bool_element(this->libObjPtr(), static_cast<bt_bool>(rawVal));
33a17831
PP
1013
1014 this->_handleAppendLibStatus(status);
2a24eba8 1015 return *this;
33a17831
PP
1016 }
1017
2a24eba8 1018 CommonArrayValue append(const std::uint64_t rawVal) const
33a17831 1019 {
5c895f64 1020 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
33a17831
PP
1021
1022 const auto status =
341a67c4 1023 bt_value_array_append_unsigned_integer_element(this->libObjPtr(), rawVal);
33a17831
PP
1024
1025 this->_handleAppendLibStatus(status);
2a24eba8 1026 return *this;
33a17831
PP
1027 }
1028
2a24eba8 1029 CommonArrayValue append(const std::int64_t rawVal) const
33a17831 1030 {
5c895f64 1031 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
33a17831 1032
341a67c4 1033 const auto status = bt_value_array_append_signed_integer_element(this->libObjPtr(), rawVal);
33a17831
PP
1034
1035 this->_handleAppendLibStatus(status);
2a24eba8 1036 return *this;
33a17831
PP
1037 }
1038
2a24eba8 1039 CommonArrayValue append(const double rawVal) const
33a17831 1040 {
5c895f64 1041 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
33a17831 1042
341a67c4 1043 const auto status = bt_value_array_append_real_element(this->libObjPtr(), rawVal);
33a17831
PP
1044
1045 this->_handleAppendLibStatus(status);
2a24eba8 1046 return *this;
33a17831
PP
1047 }
1048
2a24eba8 1049 CommonArrayValue 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);
2a24eba8 1056 return *this;
33a17831
PP
1057 }
1058
2a24eba8 1059 CommonArrayValue append(const bt2c::CStringView rawVal) const
33a17831 1060 {
2a24eba8 1061 return this->append(rawVal.data());
33a17831
PP
1062 }
1063
dcb8ae9b
PP
1064 CommonArrayValue<bt_value> appendEmptyArray() const;
1065 CommonMapValue<bt_value> appendEmptyMap() const;
33a17831 1066
dcb8ae9b 1067 void operator+=(const Value val) const
33a17831
PP
1068 {
1069 this->append(val);
1070 }
1071
dcb8ae9b 1072 void operator+=(const bool rawVal) const
33a17831
PP
1073 {
1074 this->append(rawVal);
1075 }
1076
dcb8ae9b 1077 void operator+=(const std::uint64_t rawVal) const
33a17831
PP
1078 {
1079 this->append(rawVal);
1080 }
1081
dcb8ae9b 1082 void operator+=(const std::int64_t rawVal) const
33a17831
PP
1083 {
1084 this->append(rawVal);
1085 }
1086
dcb8ae9b 1087 void operator+=(const double rawVal) const
33a17831
PP
1088 {
1089 this->append(rawVal);
1090 }
1091
dcb8ae9b 1092 void operator+=(const char * const rawVal) const
33a17831
PP
1093 {
1094 this->append(rawVal);
1095 }
1096
15030982 1097 void operator+=(const bt2c::CStringView rawVal) const
33a17831
PP
1098 {
1099 this->append(rawVal);
1100 }
1101
1102 Shared shared() const noexcept
1103 {
c9c0b6e2 1104 return Shared::createWithRef(*this);
33a17831
PP
1105 }
1106
1107private:
1108 void _handleAppendLibStatus(const bt_value_array_append_element_status status) const
1109 {
1110 if (status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR) {
39278ebc 1111 throw MemoryError {};
33a17831
PP
1112 }
1113 }
1114};
1115
1116using ArrayValue = CommonArrayValue<bt_value>;
1117using ConstArrayValue = CommonArrayValue<const bt_value>;
1118
1119namespace internal {
1120
4927bae7
PP
1121struct ArrayValueTypeDescr
1122{
1123 using Const = ConstArrayValue;
1124 using NonConst = ArrayValue;
1125};
1126
1127template <>
1128struct TypeDescr<ArrayValue> : public ArrayValueTypeDescr
1129{
1130};
1131
1132template <>
1133struct TypeDescr<ConstArrayValue> : public ArrayValueTypeDescr
1134{
1135};
1136
33a17831
PP
1137/*
1138 * Type of a user function passed to `CommonMapValue<ObjT>::forEach()`.
1139 *
1140 * First argument is the entry's key, second is its value.
1141 */
1142template <typename ObjT>
e7f0f07b 1143using CommonMapValueForEachUserFunc = std::function<void(bt2c::CStringView, ObjT)>;
33a17831
PP
1144
1145/*
1146 * Template of a function to be passed to bt_value_map_foreach_entry()
1147 * for bt_value_map_foreach_entry_const() which calls a user function.
1148 *
1149 * `userData` is casted to a `const` pointer to
1150 * `CommonMapValueForEachUserFunc<ObjT>` (the user function to call).
1151 *
1152 * This function catches any exception which the user function throws
e7401568 1153 * and returns the `ErrorStatus` value. If there's no exception, this
33a17831
PP
1154 * function returns the `OkStatus` value.
1155 */
1156template <typename ObjT, typename LibObjT, typename LibStatusT, int OkStatus, int ErrorStatus>
1157LibStatusT mapValueForEachLibFunc(const char * const key, LibObjT * const libObjPtr,
1158 void * const userData)
1159{
1160 const auto& userFunc = *reinterpret_cast<const CommonMapValueForEachUserFunc<ObjT> *>(userData);
1161
1162 try {
1163 userFunc(key, ObjT {libObjPtr});
1164 } catch (...) {
1165 return static_cast<LibStatusT>(ErrorStatus);
1166 }
1167
1168 return static_cast<LibStatusT>(OkStatus);
1169}
1170
1171template <typename LibObjT>
1172struct CommonMapValueSpec;
1173
b5f55e9f 1174/* Functions specific to mutable map values */
33a17831
PP
1175template <>
1176struct CommonMapValueSpec<bt_value> final
1177{
1178 static bt_value *entryByKey(bt_value * const libValPtr, const char * const key) noexcept
1179 {
1180 return bt_value_map_borrow_entry_value(libValPtr, key);
1181 }
1182
1183 static void forEach(bt_value * const libValPtr,
1184 const CommonMapValueForEachUserFunc<Value>& func)
1185 {
1186 const auto status = bt_value_map_foreach_entry(
1187 libValPtr,
1188 mapValueForEachLibFunc<Value, bt_value, bt_value_map_foreach_entry_func_status,
1189 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK,
1190 BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR>,
1191 const_cast<void *>(reinterpret_cast<const void *>(&func)));
1192
1193 switch (status) {
1194 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_OK:
1195 return;
1196 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR:
1197 case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR:
39278ebc 1198 throw Error {};
33a17831
PP
1199 default:
1200 bt_common_abort();
1201 }
1202 }
1203};
1204
b5f55e9f 1205/* Functions specific to constant map values */
33a17831
PP
1206template <>
1207struct CommonMapValueSpec<const bt_value> final
1208{
1209 static const bt_value *entryByKey(const bt_value * const libValPtr,
1210 const char * const key) noexcept
1211 {
1212 return bt_value_map_borrow_entry_value_const(libValPtr, key);
1213 }
1214
1215 static void forEach(const bt_value * const libValPtr,
1216 const CommonMapValueForEachUserFunc<ConstValue>& func)
1217 {
1218 const auto status = bt_value_map_foreach_entry_const(
1219 libValPtr,
1220 mapValueForEachLibFunc<ConstValue, const bt_value,
1221 bt_value_map_foreach_entry_const_func_status,
1222 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK,
1223 BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_ERROR>,
1224 const_cast<void *>(reinterpret_cast<const void *>(&func)));
1225
1226 switch (status) {
1227 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK:
1228 return;
1229 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_USER_ERROR:
1230 case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR:
39278ebc 1231 throw Error {};
33a17831
PP
1232 default:
1233 bt_common_abort();
1234 }
1235 }
1236};
1237
b5f55e9f 1238} /* namespace internal */
33a17831
PP
1239
1240template <typename LibObjT>
1241class CommonMapValue final : public CommonValue<LibObjT>
1242{
1243private:
33a17831
PP
1244 using typename CommonValue<LibObjT>::_ThisCommonValue;
1245
1246public:
d246c457 1247 using typename CommonValue<LibObjT>::LibObjPtr;
26b9d24c 1248 using Shared = SharedValue<CommonMapValue<LibObjT>, LibObjT>;
33a17831 1249
d246c457 1250 explicit CommonMapValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
33a17831
PP
1251 {
1252 BT_ASSERT_DBG(this->isMap());
1253 }
1254
1255 static Shared create()
1256 {
1257 const auto libObjPtr = bt_value_map_create();
1258
1259 internal::validateCreatedObjPtr(libObjPtr);
c9c0b6e2 1260 return CommonMapValue::Shared::createWithoutRef(libObjPtr);
33a17831
PP
1261 }
1262
1263 template <typename OtherLibObjT>
100fa861 1264 CommonMapValue(const CommonMapValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
33a17831
PP
1265 {
1266 }
1267
1268 template <typename OtherLibObjT>
ac30a470 1269 CommonMapValue<LibObjT> operator=(const CommonMapValue<OtherLibObjT> val) noexcept
33a17831
PP
1270 {
1271 _ThisCommonValue::operator=(val);
1272 return *this;
1273 }
1274
328a274a
PP
1275 CommonMapValue<const bt_value> asConst() const noexcept
1276 {
1277 return CommonMapValue<const bt_value> {*this};
1278 }
1279
c0b73c63 1280 std::uint64_t length() const noexcept
33a17831 1281 {
341a67c4 1282 return bt_value_map_get_size(this->libObjPtr());
33a17831
PP
1283 }
1284
1285 bool isEmpty() const noexcept
1286 {
c0b73c63 1287 return this->length() == 0;
33a17831
PP
1288 }
1289
15030982
SM
1290 OptionalBorrowedObject<CommonValue<LibObjT>>
1291 operator[](const bt2c::CStringView key) const noexcept
33a17831 1292 {
ca61ecbc 1293 return internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key);
33a17831
PP
1294 }
1295
15030982 1296 bool hasEntry(const bt2c::CStringView key) const noexcept
33a17831 1297 {
341a67c4 1298 return static_cast<bool>(bt_value_map_has_entry(this->libObjPtr(), key));
33a17831
PP
1299 }
1300
2a24eba8 1301 CommonMapValue insert(const bt2c::CStringView key, const Value val) const
33a17831 1302 {
5c895f64 1303 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
33a17831 1304
341a67c4 1305 const auto status = bt_value_map_insert_entry(this->libObjPtr(), key, val.libObjPtr());
33a17831
PP
1306
1307 this->_handleInsertLibStatus(status);
2a24eba8 1308 return *this;
33a17831
PP
1309 }
1310
2a24eba8 1311 CommonMapValue insert(const bt2c::CStringView key, const bool rawVal) const
33a17831 1312 {
5c895f64 1313 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
33a17831
PP
1314
1315 const auto status =
341a67c4 1316 bt_value_map_insert_bool_entry(this->libObjPtr(), key, static_cast<bt_bool>(rawVal));
33a17831
PP
1317
1318 this->_handleInsertLibStatus(status);
2a24eba8 1319 return *this;
33a17831
PP
1320 }
1321
2a24eba8 1322 CommonMapValue insert(const bt2c::CStringView key, const std::uint64_t rawVal) const
33a17831 1323 {
5c895f64 1324 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
33a17831
PP
1325
1326 const auto status =
341a67c4 1327 bt_value_map_insert_unsigned_integer_entry(this->libObjPtr(), key, rawVal);
33a17831
PP
1328
1329 this->_handleInsertLibStatus(status);
2a24eba8 1330 return *this;
33a17831 1331 }
2a24eba8
SM
1332
1333 CommonMapValue insert(const bt2c::CStringView key, const std::int64_t rawVal) const
33a17831 1334 {
5c895f64 1335 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
33a17831
PP
1336
1337 const auto status =
341a67c4 1338 bt_value_map_insert_signed_integer_entry(this->libObjPtr(), key, rawVal);
33a17831
PP
1339
1340 this->_handleInsertLibStatus(status);
2a24eba8 1341 return *this;
33a17831
PP
1342 }
1343
2a24eba8 1344 CommonMapValue insert(const bt2c::CStringView key, const double rawVal) const
33a17831 1345 {
5c895f64 1346 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
33a17831 1347
341a67c4 1348 const auto status = bt_value_map_insert_real_entry(this->libObjPtr(), key, rawVal);
33a17831
PP
1349
1350 this->_handleInsertLibStatus(status);
2a24eba8 1351 return *this;
33a17831
PP
1352 }
1353
2a24eba8 1354 CommonMapValue insert(const bt2c::CStringView key, const char *rawVal) const
33a17831 1355 {
5c895f64 1356 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
33a17831 1357
341a67c4 1358 const auto status = bt_value_map_insert_string_entry(this->libObjPtr(), key, rawVal);
33a17831
PP
1359
1360 this->_handleInsertLibStatus(status);
2a24eba8 1361 return *this;
33a17831
PP
1362 }
1363
2a24eba8 1364 CommonMapValue insert(const bt2c::CStringView key, const bt2c::CStringView rawVal) const
33a17831 1365 {
15030982 1366 return this->insert(key, rawVal.data());
33a17831
PP
1367 }
1368
15030982
SM
1369 CommonArrayValue<bt_value> insertEmptyArray(bt2c::CStringView key) const;
1370 CommonMapValue<bt_value> insertEmptyMap(bt2c::CStringView key) const;
33a17831 1371
2a24eba8
SM
1372 CommonMapValue
1373 forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func) const
33a17831 1374 {
341a67c4 1375 internal::CommonMapValueSpec<LibObjT>::forEach(this->libObjPtr(), func);
2a24eba8 1376 return *this;
33a17831
PP
1377 }
1378
1379 Shared shared() const noexcept
1380 {
c9c0b6e2 1381 return Shared::createWithRef(*this);
33a17831
PP
1382 }
1383
1384private:
1385 void _handleInsertLibStatus(const bt_value_map_insert_entry_status status) const
1386 {
1387 if (status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_MEMORY_ERROR) {
39278ebc 1388 throw MemoryError {};
33a17831
PP
1389 }
1390 }
1391};
1392
1393using MapValue = CommonMapValue<bt_value>;
1394using ConstMapValue = CommonMapValue<const bt_value>;
1395
4927bae7
PP
1396namespace internal {
1397
1398struct MapValueTypeDescr
1399{
1400 using Const = ConstMapValue;
1401 using NonConst = MapValue;
1402};
1403
1404template <>
1405struct TypeDescr<MapValue> : public MapValueTypeDescr
1406{
1407};
1408
1409template <>
1410struct TypeDescr<ConstMapValue> : public MapValueTypeDescr
1411{
1412};
1413
1414} /* namespace internal */
1415
a699e23c
PP
1416template <typename LibObjT>
1417ArrayValue CommonValue<LibObjT>::appendEmptyArray() const
1418{
1419 return this->asArray().appendEmptyArray();
1420}
1421
1422template <typename LibObjT>
1423MapValue CommonValue<LibObjT>::appendEmptyMap() const
1424{
1425 return this->asArray().appendEmptyMap();
1426}
1427
1428template <typename LibObjT>
15030982 1429ArrayValue CommonValue<LibObjT>::insertEmptyArray(const bt2c::CStringView key) const
a699e23c
PP
1430{
1431 return this->asMap().insertEmptyArray(key);
1432}
1433
1434template <typename LibObjT>
15030982 1435MapValue CommonValue<LibObjT>::insertEmptyMap(const bt2c::CStringView key) const
a699e23c
PP
1436{
1437 return this->asMap().insertEmptyMap(key);
1438}
1439
33a17831
PP
1440template <typename LibObjT>
1441CommonNullValue<LibObjT> CommonValue<LibObjT>::asNull() const noexcept
1442{
1443 BT_ASSERT_DBG(this->isNull());
7eb1d0da 1444 return CommonNullValue<LibObjT> {};
33a17831
PP
1445}
1446
1447template <typename LibObjT>
1448CommonBoolValue<LibObjT> CommonValue<LibObjT>::asBool() const noexcept
1449{
341a67c4 1450 return CommonBoolValue<LibObjT> {this->libObjPtr()};
33a17831
PP
1451}
1452
1453template <typename LibObjT>
1454CommonSignedIntegerValue<LibObjT> CommonValue<LibObjT>::asSignedInteger() const noexcept
1455{
341a67c4 1456 return CommonSignedIntegerValue<LibObjT> {this->libObjPtr()};
33a17831
PP
1457}
1458
1459template <typename LibObjT>
1460CommonUnsignedIntegerValue<LibObjT> CommonValue<LibObjT>::asUnsignedInteger() const noexcept
1461{
341a67c4 1462 return CommonUnsignedIntegerValue<LibObjT> {this->libObjPtr()};
33a17831
PP
1463}
1464
1465template <typename LibObjT>
1466CommonRealValue<LibObjT> CommonValue<LibObjT>::asReal() const noexcept
1467{
341a67c4 1468 return CommonRealValue<LibObjT> {this->libObjPtr()};
33a17831
PP
1469}
1470
1471template <typename LibObjT>
1472CommonStringValue<LibObjT> CommonValue<LibObjT>::asString() const noexcept
1473{
341a67c4 1474 return CommonStringValue<LibObjT> {this->libObjPtr()};
33a17831
PP
1475}
1476
1477template <typename LibObjT>
1478CommonArrayValue<LibObjT> CommonValue<LibObjT>::asArray() const noexcept
1479{
341a67c4 1480 return CommonArrayValue<LibObjT> {this->libObjPtr()};
33a17831
PP
1481}
1482
1483template <typename LibObjT>
1484CommonMapValue<LibObjT> CommonValue<LibObjT>::asMap() const noexcept
1485{
341a67c4 1486 return CommonMapValue<LibObjT> {this->libObjPtr()};
33a17831
PP
1487}
1488
1489template <typename LibObjT>
dcb8ae9b 1490ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray() const
33a17831 1491{
5c895f64 1492 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
33a17831
PP
1493
1494 bt_value *libElemPtr;
341a67c4 1495 const auto status = bt_value_array_append_empty_array_element(this->libObjPtr(), &libElemPtr);
33a17831
PP
1496
1497 this->_handleAppendLibStatus(status);
1498 return ArrayValue {libElemPtr};
1499}
1500
1501template <typename LibObjT>
dcb8ae9b 1502MapValue CommonArrayValue<LibObjT>::appendEmptyMap() const
33a17831 1503{
5c895f64 1504 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
33a17831
PP
1505
1506 bt_value *libElemPtr;
341a67c4 1507 const auto status = bt_value_array_append_empty_map_element(this->libObjPtr(), &libElemPtr);
33a17831
PP
1508
1509 this->_handleAppendLibStatus(status);
1510 return MapValue {libElemPtr};
1511}
1512
1513template <typename LibObjT>
15030982 1514ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const bt2c::CStringView key) const
33a17831 1515{
5c895f64 1516 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
33a17831
PP
1517
1518 bt_value *libEntryPtr;
341a67c4 1519 const auto status = bt_value_map_insert_empty_array_entry(this->libObjPtr(), key, &libEntryPtr);
33a17831
PP
1520
1521 this->_handleInsertLibStatus(status);
1522 return ArrayValue {libEntryPtr};
1523}
1524
1525template <typename LibObjT>
15030982 1526MapValue CommonMapValue<LibObjT>::insertEmptyMap(const bt2c::CStringView key) const
33a17831 1527{
5c895f64 1528 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
33a17831
PP
1529
1530 bt_value *libEntryPtr;
341a67c4 1531 const auto status = bt_value_map_insert_empty_map_entry(this->libObjPtr(), key, &libEntryPtr);
33a17831
PP
1532
1533 this->_handleInsertLibStatus(status);
1534 return MapValue {libEntryPtr};
1535}
1536
33a17831
PP
1537inline BoolValue::Shared createValue(const bool rawVal)
1538{
1539 return BoolValue::create(rawVal);
1540}
1541
1542inline UnsignedIntegerValue::Shared createValue(const std::uint64_t rawVal)
1543{
1544 return UnsignedIntegerValue::create(rawVal);
1545}
1546
1547inline SignedIntegerValue::Shared createValue(const std::int64_t rawVal)
1548{
1549 return SignedIntegerValue::create(rawVal);
1550}
1551
1552inline RealValue::Shared createValue(const double rawVal)
1553{
1554 return RealValue::create(rawVal);
1555}
1556
1557inline StringValue::Shared createValue(const char * const rawVal)
1558{
1559 return StringValue::create(rawVal);
1560}
1561
15030982 1562inline StringValue::Shared createValue(const bt2c::CStringView rawVal)
33a17831
PP
1563{
1564 return StringValue::create(rawVal);
1565}
1566
b5f55e9f 1567} /* namespace bt2 */
33a17831 1568
b5f55e9f 1569#endif /* BABELTRACE_CPP_COMMON_BT2_VALUE_HPP */
This page took 0.11228 seconds and 4 git commands to generate.