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