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