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