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