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