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" |
8aee46a3 | 22 | #include "common-iterator.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 | { | |
c677c492 | 32 | static void get(const bt_value * const libObjPtr) noexcept |
33a17831 PP |
33 | { |
34 | bt_value_get_ref(libObjPtr); | |
35 | } | |
36 | ||
c677c492 | 37 | static void put(const bt_value * const libObjPtr) noexcept |
33a17831 PP |
38 | { |
39 | bt_value_put_ref(libObjPtr); | |
40 | } | |
41 | }; | |
42 | ||
b5f55e9f | 43 | } /* namespace internal */ |
33a17831 | 44 | |
26b9d24c | 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 | ||
dcb8ae9b | 351 | CommonBoolValue<LibObjT> operator=(const Value rawVal) const noexcept |
33a17831 PP |
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 | ||
dcb8ae9b | 439 | CommonUnsignedIntegerValue<LibObjT> operator=(const Value rawVal) const noexcept |
33a17831 | 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> | |
dcb8ae9b | 516 | CommonSignedIntegerValue<LibObjT> |
100fa861 | 517 | operator=(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept |
33a17831 PP |
518 | { |
519 | _ThisCommonValue::operator=(val); | |
520 | return *this; | |
521 | } | |
522 | ||
dcb8ae9b | 523 | CommonSignedIntegerValue<LibObjT> operator=(const Value rawVal) const noexcept |
33a17831 PP |
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 | ||
dcb8ae9b | 606 | CommonRealValue<LibObjT> operator=(const Value rawVal) const noexcept |
33a17831 PP |
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 | ||
dcb8ae9b | 693 | CommonStringValue<LibObjT> operator=(const char * const rawVal) const |
33a17831 PP |
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 | ||
dcb8ae9b | 706 | CommonStringValue<LibObjT> operator=(const std::string& rawVal) const noexcept |
33a17831 PP |
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 | ||
dcb8ae9b | 831 | CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept |
33a17831 PP |
832 | { |
833 | return CommonValue<LibObjT> { | |
341a67c4 | 834 | internal::CommonArrayValueSpec<LibObjT>::elementByIndex(this->libObjPtr(), index)}; |
33a17831 PP |
835 | } |
836 | ||
dcb8ae9b | 837 | void append(const Value val) const |
33a17831 PP |
838 | { |
839 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
840 | ||
341a67c4 | 841 | const auto status = bt_value_array_append_element(this->libObjPtr(), val.libObjPtr()); |
33a17831 PP |
842 | |
843 | this->_handleAppendLibStatus(status); | |
844 | } | |
845 | ||
dcb8ae9b | 846 | void append(const bool rawVal) const |
33a17831 PP |
847 | { |
848 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
849 | ||
850 | const auto status = | |
341a67c4 | 851 | bt_value_array_append_bool_element(this->libObjPtr(), static_cast<bt_bool>(rawVal)); |
33a17831 PP |
852 | |
853 | this->_handleAppendLibStatus(status); | |
854 | } | |
855 | ||
dcb8ae9b | 856 | void append(const std::uint64_t rawVal) const |
33a17831 PP |
857 | { |
858 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
859 | ||
860 | const auto status = | |
341a67c4 | 861 | bt_value_array_append_unsigned_integer_element(this->libObjPtr(), rawVal); |
33a17831 PP |
862 | |
863 | this->_handleAppendLibStatus(status); | |
864 | } | |
865 | ||
dcb8ae9b | 866 | void append(const std::int64_t rawVal) const |
33a17831 PP |
867 | { |
868 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
869 | ||
341a67c4 | 870 | const auto status = bt_value_array_append_signed_integer_element(this->libObjPtr(), rawVal); |
33a17831 PP |
871 | |
872 | this->_handleAppendLibStatus(status); | |
873 | } | |
874 | ||
dcb8ae9b | 875 | void append(const double rawVal) const |
33a17831 PP |
876 | { |
877 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
878 | ||
341a67c4 | 879 | const auto status = bt_value_array_append_real_element(this->libObjPtr(), rawVal); |
33a17831 PP |
880 | |
881 | this->_handleAppendLibStatus(status); | |
882 | } | |
883 | ||
dcb8ae9b | 884 | void append(const char * const rawVal) const |
33a17831 PP |
885 | { |
886 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
887 | ||
341a67c4 | 888 | const auto status = bt_value_array_append_string_element(this->libObjPtr(), rawVal); |
33a17831 PP |
889 | |
890 | this->_handleAppendLibStatus(status); | |
891 | } | |
892 | ||
dcb8ae9b | 893 | void append(const std::string& rawVal) const |
33a17831 PP |
894 | { |
895 | this->append(rawVal.data()); | |
896 | } | |
897 | ||
dcb8ae9b PP |
898 | CommonArrayValue<bt_value> appendEmptyArray() const; |
899 | CommonMapValue<bt_value> appendEmptyMap() const; | |
33a17831 | 900 | |
dcb8ae9b | 901 | void operator+=(const Value val) const |
33a17831 PP |
902 | { |
903 | this->append(val); | |
904 | } | |
905 | ||
dcb8ae9b | 906 | void operator+=(const bool rawVal) const |
33a17831 PP |
907 | { |
908 | this->append(rawVal); | |
909 | } | |
910 | ||
dcb8ae9b | 911 | void operator+=(const std::uint64_t rawVal) const |
33a17831 PP |
912 | { |
913 | this->append(rawVal); | |
914 | } | |
915 | ||
dcb8ae9b | 916 | void operator+=(const std::int64_t rawVal) const |
33a17831 PP |
917 | { |
918 | this->append(rawVal); | |
919 | } | |
920 | ||
dcb8ae9b | 921 | void operator+=(const double rawVal) const |
33a17831 PP |
922 | { |
923 | this->append(rawVal); | |
924 | } | |
925 | ||
dcb8ae9b | 926 | void operator+=(const char * const rawVal) const |
33a17831 PP |
927 | { |
928 | this->append(rawVal); | |
929 | } | |
930 | ||
dcb8ae9b | 931 | void operator+=(const std::string& rawVal) const |
33a17831 PP |
932 | { |
933 | this->append(rawVal); | |
934 | } | |
935 | ||
936 | Shared shared() const noexcept | |
937 | { | |
c9c0b6e2 | 938 | return Shared::createWithRef(*this); |
33a17831 PP |
939 | } |
940 | ||
941 | private: | |
942 | void _handleAppendLibStatus(const bt_value_array_append_element_status status) const | |
943 | { | |
944 | if (status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR) { | |
39278ebc | 945 | throw MemoryError {}; |
33a17831 PP |
946 | } |
947 | } | |
948 | }; | |
949 | ||
950 | using ArrayValue = CommonArrayValue<bt_value>; | |
951 | using ConstArrayValue = CommonArrayValue<const bt_value>; | |
952 | ||
953 | namespace internal { | |
954 | ||
4927bae7 PP |
955 | struct ArrayValueTypeDescr |
956 | { | |
957 | using Const = ConstArrayValue; | |
958 | using NonConst = ArrayValue; | |
959 | }; | |
960 | ||
961 | template <> | |
962 | struct TypeDescr<ArrayValue> : public ArrayValueTypeDescr | |
963 | { | |
964 | }; | |
965 | ||
966 | template <> | |
967 | struct TypeDescr<ConstArrayValue> : public ArrayValueTypeDescr | |
968 | { | |
969 | }; | |
970 | ||
33a17831 PP |
971 | /* |
972 | * Type of a user function passed to `CommonMapValue<ObjT>::forEach()`. | |
973 | * | |
974 | * First argument is the entry's key, second is its value. | |
975 | */ | |
976 | template <typename ObjT> | |
977 | using CommonMapValueForEachUserFunc = std::function<void(const bpstd::string_view&, ObjT)>; | |
978 | ||
979 | /* | |
980 | * Template of a function to be passed to bt_value_map_foreach_entry() | |
981 | * for bt_value_map_foreach_entry_const() which calls a user function. | |
982 | * | |
983 | * `userData` is casted to a `const` pointer to | |
984 | * `CommonMapValueForEachUserFunc<ObjT>` (the user function to call). | |
985 | * | |
986 | * This function catches any exception which the user function throws | |
987 | * and returns the `ErrorStatus` value. If there's no execption, this | |
988 | * function returns the `OkStatus` value. | |
989 | */ | |
990 | template <typename ObjT, typename LibObjT, typename LibStatusT, int OkStatus, int ErrorStatus> | |
991 | LibStatusT mapValueForEachLibFunc(const char * const key, LibObjT * const libObjPtr, | |
992 | void * const userData) | |
993 | { | |
994 | const auto& userFunc = *reinterpret_cast<const CommonMapValueForEachUserFunc<ObjT> *>(userData); | |
995 | ||
996 | try { | |
997 | userFunc(key, ObjT {libObjPtr}); | |
998 | } catch (...) { | |
999 | return static_cast<LibStatusT>(ErrorStatus); | |
1000 | } | |
1001 | ||
1002 | return static_cast<LibStatusT>(OkStatus); | |
1003 | } | |
1004 | ||
1005 | template <typename LibObjT> | |
1006 | struct CommonMapValueSpec; | |
1007 | ||
b5f55e9f | 1008 | /* Functions specific to mutable map values */ |
33a17831 PP |
1009 | template <> |
1010 | struct CommonMapValueSpec<bt_value> final | |
1011 | { | |
1012 | static bt_value *entryByKey(bt_value * const libValPtr, const char * const key) noexcept | |
1013 | { | |
1014 | return bt_value_map_borrow_entry_value(libValPtr, key); | |
1015 | } | |
1016 | ||
1017 | static void forEach(bt_value * const libValPtr, | |
1018 | const CommonMapValueForEachUserFunc<Value>& func) | |
1019 | { | |
1020 | const auto status = bt_value_map_foreach_entry( | |
1021 | libValPtr, | |
1022 | mapValueForEachLibFunc<Value, bt_value, bt_value_map_foreach_entry_func_status, | |
1023 | BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_OK, | |
1024 | BT_VALUE_MAP_FOREACH_ENTRY_FUNC_STATUS_ERROR>, | |
1025 | const_cast<void *>(reinterpret_cast<const void *>(&func))); | |
1026 | ||
1027 | switch (status) { | |
1028 | case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_OK: | |
1029 | return; | |
1030 | case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR: | |
1031 | case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR: | |
39278ebc | 1032 | throw Error {}; |
33a17831 PP |
1033 | default: |
1034 | bt_common_abort(); | |
1035 | } | |
1036 | } | |
1037 | }; | |
1038 | ||
b5f55e9f | 1039 | /* Functions specific to constant map values */ |
33a17831 PP |
1040 | template <> |
1041 | struct CommonMapValueSpec<const bt_value> final | |
1042 | { | |
1043 | static const bt_value *entryByKey(const bt_value * const libValPtr, | |
1044 | const char * const key) noexcept | |
1045 | { | |
1046 | return bt_value_map_borrow_entry_value_const(libValPtr, key); | |
1047 | } | |
1048 | ||
1049 | static void forEach(const bt_value * const libValPtr, | |
1050 | const CommonMapValueForEachUserFunc<ConstValue>& func) | |
1051 | { | |
1052 | const auto status = bt_value_map_foreach_entry_const( | |
1053 | libValPtr, | |
1054 | mapValueForEachLibFunc<ConstValue, const bt_value, | |
1055 | bt_value_map_foreach_entry_const_func_status, | |
1056 | BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK, | |
1057 | BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_ERROR>, | |
1058 | const_cast<void *>(reinterpret_cast<const void *>(&func))); | |
1059 | ||
1060 | switch (status) { | |
1061 | case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK: | |
1062 | return; | |
1063 | case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_USER_ERROR: | |
1064 | case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR: | |
39278ebc | 1065 | throw Error {}; |
33a17831 PP |
1066 | default: |
1067 | bt_common_abort(); | |
1068 | } | |
1069 | } | |
1070 | }; | |
1071 | ||
b5f55e9f | 1072 | } /* namespace internal */ |
33a17831 PP |
1073 | |
1074 | template <typename LibObjT> | |
1075 | class CommonMapValue final : public CommonValue<LibObjT> | |
1076 | { | |
1077 | private: | |
1078 | using typename CommonValue<LibObjT>::_LibObjPtr; | |
1079 | using typename CommonValue<LibObjT>::_ThisCommonValue; | |
1080 | ||
1081 | public: | |
26b9d24c | 1082 | using Shared = SharedValue<CommonMapValue<LibObjT>, LibObjT>; |
33a17831 PP |
1083 | |
1084 | explicit CommonMapValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} | |
1085 | { | |
1086 | BT_ASSERT_DBG(this->isMap()); | |
1087 | } | |
1088 | ||
1089 | static Shared create() | |
1090 | { | |
1091 | const auto libObjPtr = bt_value_map_create(); | |
1092 | ||
1093 | internal::validateCreatedObjPtr(libObjPtr); | |
c9c0b6e2 | 1094 | return CommonMapValue::Shared::createWithoutRef(libObjPtr); |
33a17831 PP |
1095 | } |
1096 | ||
1097 | template <typename OtherLibObjT> | |
100fa861 | 1098 | CommonMapValue(const CommonMapValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val} |
33a17831 PP |
1099 | { |
1100 | } | |
1101 | ||
1102 | template <typename OtherLibObjT> | |
100fa861 | 1103 | CommonMapValue<LibObjT>& operator=(const CommonMapValue<OtherLibObjT> val) noexcept |
33a17831 PP |
1104 | { |
1105 | _ThisCommonValue::operator=(val); | |
1106 | return *this; | |
1107 | } | |
1108 | ||
1109 | std::uint64_t size() const noexcept | |
1110 | { | |
341a67c4 | 1111 | return bt_value_map_get_size(this->libObjPtr()); |
33a17831 PP |
1112 | } |
1113 | ||
1114 | bool isEmpty() const noexcept | |
1115 | { | |
1116 | return this->size() == 0; | |
1117 | } | |
1118 | ||
dcb8ae9b | 1119 | nonstd::optional<CommonValue<LibObjT>> operator[](const char * const key) const noexcept |
33a17831 PP |
1120 | { |
1121 | const auto libObjPtr = | |
341a67c4 | 1122 | internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key); |
33a17831 PP |
1123 | |
1124 | if (!libObjPtr) { | |
1125 | return nonstd::nullopt; | |
1126 | } | |
1127 | ||
1128 | return CommonValue<LibObjT> {libObjPtr}; | |
1129 | } | |
1130 | ||
dcb8ae9b | 1131 | nonstd::optional<CommonValue<LibObjT>> operator[](const std::string& key) const noexcept |
33a17831 PP |
1132 | { |
1133 | return (*this)[key.data()]; | |
1134 | } | |
1135 | ||
1136 | bool hasEntry(const char * const key) const noexcept | |
1137 | { | |
341a67c4 | 1138 | return static_cast<bool>(bt_value_map_has_entry(this->libObjPtr(), key)); |
33a17831 PP |
1139 | } |
1140 | ||
1141 | bool hasEntry(const std::string& key) const noexcept | |
1142 | { | |
1143 | return this->hasEntry(key.data()); | |
1144 | } | |
1145 | ||
dcb8ae9b | 1146 | void insert(const char * const key, const Value val) const |
33a17831 PP |
1147 | { |
1148 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1149 | ||
341a67c4 | 1150 | const auto status = bt_value_map_insert_entry(this->libObjPtr(), key, val.libObjPtr()); |
33a17831 PP |
1151 | |
1152 | this->_handleInsertLibStatus(status); | |
1153 | } | |
1154 | ||
dcb8ae9b | 1155 | void insert(const std::string& key, const Value val) const |
33a17831 PP |
1156 | { |
1157 | this->insert(key.data(), val); | |
1158 | } | |
1159 | ||
dcb8ae9b | 1160 | void insert(const char * const key, const bool rawVal) const |
33a17831 PP |
1161 | { |
1162 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1163 | ||
1164 | const auto status = | |
341a67c4 | 1165 | bt_value_map_insert_bool_entry(this->libObjPtr(), key, static_cast<bt_bool>(rawVal)); |
33a17831 PP |
1166 | |
1167 | this->_handleInsertLibStatus(status); | |
1168 | } | |
1169 | ||
dcb8ae9b | 1170 | void insert(const std::string& key, const bool rawVal) const |
33a17831 PP |
1171 | { |
1172 | this->insert(key.data(), rawVal); | |
1173 | } | |
1174 | ||
dcb8ae9b | 1175 | void insert(const char * const key, const std::uint64_t rawVal) const |
33a17831 PP |
1176 | { |
1177 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1178 | ||
1179 | const auto status = | |
341a67c4 | 1180 | bt_value_map_insert_unsigned_integer_entry(this->libObjPtr(), key, rawVal); |
33a17831 PP |
1181 | |
1182 | this->_handleInsertLibStatus(status); | |
1183 | } | |
1184 | ||
dcb8ae9b | 1185 | void insert(const std::string& key, const std::uint64_t rawVal) const |
33a17831 PP |
1186 | { |
1187 | this->insert(key.data(), rawVal); | |
1188 | } | |
1189 | ||
dcb8ae9b | 1190 | void insert(const char * const key, const std::int64_t rawVal) const |
33a17831 PP |
1191 | { |
1192 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1193 | ||
1194 | const auto status = | |
341a67c4 | 1195 | bt_value_map_insert_signed_integer_entry(this->libObjPtr(), key, rawVal); |
33a17831 PP |
1196 | |
1197 | this->_handleInsertLibStatus(status); | |
1198 | } | |
1199 | ||
dcb8ae9b | 1200 | void insert(const std::string& key, const std::int64_t rawVal) const |
33a17831 PP |
1201 | { |
1202 | this->insert(key.data(), rawVal); | |
1203 | } | |
1204 | ||
dcb8ae9b | 1205 | void insert(const char * const key, const double rawVal) const |
33a17831 PP |
1206 | { |
1207 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1208 | ||
341a67c4 | 1209 | const auto status = bt_value_map_insert_real_entry(this->libObjPtr(), key, rawVal); |
33a17831 PP |
1210 | |
1211 | this->_handleInsertLibStatus(status); | |
1212 | } | |
1213 | ||
dcb8ae9b | 1214 | void insert(const std::string& key, const double rawVal) const |
33a17831 PP |
1215 | { |
1216 | this->insert(key.data(), rawVal); | |
1217 | } | |
1218 | ||
dcb8ae9b | 1219 | void insert(const char * const key, const char * const rawVal) const |
33a17831 PP |
1220 | { |
1221 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1222 | ||
341a67c4 | 1223 | const auto status = bt_value_map_insert_string_entry(this->libObjPtr(), key, rawVal); |
33a17831 PP |
1224 | |
1225 | this->_handleInsertLibStatus(status); | |
1226 | } | |
1227 | ||
dcb8ae9b | 1228 | void insert(const char * const key, const std::string& rawVal) const |
33a17831 PP |
1229 | { |
1230 | this->insert(key, rawVal.data()); | |
1231 | } | |
1232 | ||
dcb8ae9b | 1233 | void insert(const std::string& key, const char * const rawVal) const |
33a17831 PP |
1234 | { |
1235 | this->insert(key.data(), rawVal); | |
1236 | } | |
1237 | ||
dcb8ae9b | 1238 | void insert(const std::string& key, const std::string& rawVal) const |
33a17831 PP |
1239 | { |
1240 | this->insert(key.data(), rawVal.data()); | |
1241 | } | |
1242 | ||
dcb8ae9b PP |
1243 | CommonArrayValue<bt_value> insertEmptyArray(const char *key) const; |
1244 | CommonArrayValue<bt_value> insertEmptyArray(const std::string& key) const; | |
1245 | CommonMapValue<bt_value> insertEmptyMap(const char *key) const; | |
1246 | CommonMapValue<bt_value> insertEmptyMap(const std::string& key) const; | |
33a17831 | 1247 | |
dcb8ae9b | 1248 | void forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func) const |
33a17831 | 1249 | { |
341a67c4 | 1250 | internal::CommonMapValueSpec<LibObjT>::forEach(this->libObjPtr(), func); |
33a17831 PP |
1251 | } |
1252 | ||
1253 | Shared shared() const noexcept | |
1254 | { | |
c9c0b6e2 | 1255 | return Shared::createWithRef(*this); |
33a17831 PP |
1256 | } |
1257 | ||
1258 | private: | |
1259 | void _handleInsertLibStatus(const bt_value_map_insert_entry_status status) const | |
1260 | { | |
1261 | if (status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_MEMORY_ERROR) { | |
39278ebc | 1262 | throw MemoryError {}; |
33a17831 PP |
1263 | } |
1264 | } | |
1265 | }; | |
1266 | ||
1267 | using MapValue = CommonMapValue<bt_value>; | |
1268 | using ConstMapValue = CommonMapValue<const bt_value>; | |
1269 | ||
4927bae7 PP |
1270 | namespace internal { |
1271 | ||
1272 | struct MapValueTypeDescr | |
1273 | { | |
1274 | using Const = ConstMapValue; | |
1275 | using NonConst = MapValue; | |
1276 | }; | |
1277 | ||
1278 | template <> | |
1279 | struct TypeDescr<MapValue> : public MapValueTypeDescr | |
1280 | { | |
1281 | }; | |
1282 | ||
1283 | template <> | |
1284 | struct TypeDescr<ConstMapValue> : public MapValueTypeDescr | |
1285 | { | |
1286 | }; | |
1287 | ||
1288 | } /* namespace internal */ | |
1289 | ||
33a17831 PP |
1290 | template <typename LibObjT> |
1291 | CommonNullValue<LibObjT> CommonValue<LibObjT>::asNull() const noexcept | |
1292 | { | |
1293 | BT_ASSERT_DBG(this->isNull()); | |
341a67c4 | 1294 | return CommonNullValue<LibObjT> {this->libObjPtr()}; |
33a17831 PP |
1295 | } |
1296 | ||
1297 | template <typename LibObjT> | |
1298 | CommonBoolValue<LibObjT> CommonValue<LibObjT>::asBool() const noexcept | |
1299 | { | |
1300 | BT_ASSERT_DBG(this->isBool()); | |
341a67c4 | 1301 | return CommonBoolValue<LibObjT> {this->libObjPtr()}; |
33a17831 PP |
1302 | } |
1303 | ||
1304 | template <typename LibObjT> | |
1305 | CommonSignedIntegerValue<LibObjT> CommonValue<LibObjT>::asSignedInteger() const noexcept | |
1306 | { | |
1307 | BT_ASSERT_DBG(this->isSignedInteger()); | |
341a67c4 | 1308 | return CommonSignedIntegerValue<LibObjT> {this->libObjPtr()}; |
33a17831 PP |
1309 | } |
1310 | ||
1311 | template <typename LibObjT> | |
1312 | CommonUnsignedIntegerValue<LibObjT> CommonValue<LibObjT>::asUnsignedInteger() const noexcept | |
1313 | { | |
1314 | BT_ASSERT_DBG(this->isUnsignedInteger()); | |
341a67c4 | 1315 | return CommonUnsignedIntegerValue<LibObjT> {this->libObjPtr()}; |
33a17831 PP |
1316 | } |
1317 | ||
1318 | template <typename LibObjT> | |
1319 | CommonRealValue<LibObjT> CommonValue<LibObjT>::asReal() const noexcept | |
1320 | { | |
1321 | BT_ASSERT_DBG(this->isReal()); | |
341a67c4 | 1322 | return CommonRealValue<LibObjT> {this->libObjPtr()}; |
33a17831 PP |
1323 | } |
1324 | ||
1325 | template <typename LibObjT> | |
1326 | CommonStringValue<LibObjT> CommonValue<LibObjT>::asString() const noexcept | |
1327 | { | |
1328 | BT_ASSERT_DBG(this->isString()); | |
341a67c4 | 1329 | return CommonStringValue<LibObjT> {this->libObjPtr()}; |
33a17831 PP |
1330 | } |
1331 | ||
1332 | template <typename LibObjT> | |
1333 | CommonArrayValue<LibObjT> CommonValue<LibObjT>::asArray() const noexcept | |
1334 | { | |
1335 | BT_ASSERT_DBG(this->isArray()); | |
341a67c4 | 1336 | return CommonArrayValue<LibObjT> {this->libObjPtr()}; |
33a17831 PP |
1337 | } |
1338 | ||
1339 | template <typename LibObjT> | |
1340 | CommonMapValue<LibObjT> CommonValue<LibObjT>::asMap() const noexcept | |
1341 | { | |
1342 | BT_ASSERT_DBG(this->isMap()); | |
341a67c4 | 1343 | return CommonMapValue<LibObjT> {this->libObjPtr()}; |
33a17831 PP |
1344 | } |
1345 | ||
1346 | template <typename LibObjT> | |
dcb8ae9b | 1347 | ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray() const |
33a17831 PP |
1348 | { |
1349 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1350 | ||
1351 | bt_value *libElemPtr; | |
341a67c4 | 1352 | const auto status = bt_value_array_append_empty_array_element(this->libObjPtr(), &libElemPtr); |
33a17831 PP |
1353 | |
1354 | this->_handleAppendLibStatus(status); | |
1355 | return ArrayValue {libElemPtr}; | |
1356 | } | |
1357 | ||
1358 | template <typename LibObjT> | |
dcb8ae9b | 1359 | MapValue CommonArrayValue<LibObjT>::appendEmptyMap() const |
33a17831 PP |
1360 | { |
1361 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1362 | ||
1363 | bt_value *libElemPtr; | |
341a67c4 | 1364 | const auto status = bt_value_array_append_empty_map_element(this->libObjPtr(), &libElemPtr); |
33a17831 PP |
1365 | |
1366 | this->_handleAppendLibStatus(status); | |
1367 | return MapValue {libElemPtr}; | |
1368 | } | |
1369 | ||
1370 | template <typename LibObjT> | |
dcb8ae9b | 1371 | ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key) const |
33a17831 PP |
1372 | { |
1373 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1374 | ||
1375 | bt_value *libEntryPtr; | |
341a67c4 | 1376 | const auto status = bt_value_map_insert_empty_array_entry(this->libObjPtr(), key, &libEntryPtr); |
33a17831 PP |
1377 | |
1378 | this->_handleInsertLibStatus(status); | |
1379 | return ArrayValue {libEntryPtr}; | |
1380 | } | |
1381 | ||
1382 | template <typename LibObjT> | |
dcb8ae9b | 1383 | ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const std::string& key) const |
33a17831 PP |
1384 | { |
1385 | return this->insertEmptyArray(key.data()); | |
1386 | } | |
1387 | ||
1388 | template <typename LibObjT> | |
dcb8ae9b | 1389 | MapValue CommonMapValue<LibObjT>::insertEmptyMap(const char * const key) const |
33a17831 PP |
1390 | { |
1391 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1392 | ||
1393 | bt_value *libEntryPtr; | |
341a67c4 | 1394 | const auto status = bt_value_map_insert_empty_map_entry(this->libObjPtr(), key, &libEntryPtr); |
33a17831 PP |
1395 | |
1396 | this->_handleInsertLibStatus(status); | |
1397 | return MapValue {libEntryPtr}; | |
1398 | } | |
1399 | ||
1400 | template <typename LibObjT> | |
dcb8ae9b | 1401 | MapValue CommonMapValue<LibObjT>::insertEmptyMap(const std::string& key) const |
33a17831 PP |
1402 | { |
1403 | return this->insertEmptyMap(key.data()); | |
1404 | } | |
1405 | ||
1406 | inline BoolValue::Shared createValue(const bool rawVal) | |
1407 | { | |
1408 | return BoolValue::create(rawVal); | |
1409 | } | |
1410 | ||
1411 | inline UnsignedIntegerValue::Shared createValue(const std::uint64_t rawVal) | |
1412 | { | |
1413 | return UnsignedIntegerValue::create(rawVal); | |
1414 | } | |
1415 | ||
1416 | inline SignedIntegerValue::Shared createValue(const std::int64_t rawVal) | |
1417 | { | |
1418 | return SignedIntegerValue::create(rawVal); | |
1419 | } | |
1420 | ||
1421 | inline RealValue::Shared createValue(const double rawVal) | |
1422 | { | |
1423 | return RealValue::create(rawVal); | |
1424 | } | |
1425 | ||
1426 | inline StringValue::Shared createValue(const char * const rawVal) | |
1427 | { | |
1428 | return StringValue::create(rawVal); | |
1429 | } | |
1430 | ||
1431 | inline StringValue::Shared createValue(const std::string& rawVal) | |
1432 | { | |
1433 | return StringValue::create(rawVal); | |
1434 | } | |
1435 | ||
b5f55e9f | 1436 | } /* namespace bt2 */ |
33a17831 | 1437 | |
b5f55e9f | 1438 | #endif /* BABELTRACE_CPP_COMMON_BT2_VALUE_HPP */ |