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