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