Commit | Line | Data |
---|---|---|
508053d4 PP |
1 | /* |
2 | * Copyright (c) 2021 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
7 | #ifndef BABELTRACE_CPP_COMMON_BT2_MESSAGE_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_MESSAGE_HPP | |
9 | ||
10 | #include <type_traits> | |
11 | #include <cstdint> | |
12 | #include <functional> | |
13 | #include <babeltrace2/babeltrace.h> | |
14 | ||
15 | #include "common/assert.h" | |
16 | #include "common/common.h" | |
17 | #include "internal/borrowed-obj.hpp" | |
18 | #include "internal/shared-obj.hpp" | |
19 | #include "internal/utils.hpp" | |
20 | #include "cpp-common/optional.hpp" | |
21 | #include "cpp-common/string_view.hpp" | |
22 | #include "lib-error.hpp" | |
23 | ||
24 | namespace bt2 { | |
25 | ||
26 | namespace internal { | |
27 | ||
28 | struct MessageRefFuncs final | |
29 | { | |
30 | static void get(const bt_message * const libObjPtr) | |
31 | { | |
32 | bt_message_get_ref(libObjPtr); | |
33 | } | |
34 | ||
35 | static void put(const bt_message * const libObjPtr) | |
36 | { | |
37 | bt_message_put_ref(libObjPtr); | |
38 | } | |
39 | }; | |
40 | ||
41 | template <typename ObjT, typename LibObjT> | |
42 | using SharedMessage = internal::SharedObj<ObjT, LibObjT, internal::MessageRefFuncs>; | |
43 | ||
44 | } // namespace internal | |
45 | ||
46 | template <typename LibObjT> | |
47 | class CommonStreamBeginningMessage; | |
48 | ||
49 | template <typename LibObjT> | |
50 | class CommonStreamEndMessage; | |
51 | ||
52 | template <typename LibObjT> | |
53 | class CommonEventMessage; | |
54 | ||
55 | template <typename LibObjT> | |
56 | class CommonPacketBeginningMessage; | |
57 | ||
58 | template <typename LibObjT> | |
59 | class CommonPacketEndMessage; | |
60 | ||
61 | template <typename LibObjT> | |
62 | class CommonDiscardedEventsMessage; | |
63 | ||
64 | template <typename LibObjT> | |
65 | class CommonDiscardedPacketsMessage; | |
66 | ||
67 | template <typename LibObjT> | |
68 | class CommonMessageIteratorInactivityMessage; | |
69 | ||
70 | enum class MessageType | |
71 | { | |
72 | STREAM_BEGINNING = BT_MESSAGE_TYPE_STREAM_BEGINNING, | |
73 | STREAM_END = BT_MESSAGE_TYPE_STREAM_END, | |
74 | EVENT = BT_MESSAGE_TYPE_EVENT, | |
75 | PACKET_BEGINNING = BT_MESSAGE_TYPE_PACKET_BEGINNING, | |
76 | PACKET_END = BT_MESSAGE_TYPE_PACKET_END, | |
77 | DISCARDED_EVENTS = BT_MESSAGE_TYPE_DISCARDED_EVENTS, | |
78 | DISCARDED_PACKETS = BT_MESSAGE_TYPE_DISCARDED_PACKETS, | |
79 | MESSAGE_ITERATOR_INACTIVITY = BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY, | |
80 | }; | |
81 | ||
82 | template <typename LibObjT> | |
83 | class CommonMessage : public internal::BorrowedObj<LibObjT> | |
84 | { | |
85 | private: | |
86 | using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj; | |
87 | ||
88 | protected: | |
89 | using typename internal::BorrowedObj<LibObjT>::_LibObjPtr; | |
90 | using _ThisCommonMessage = CommonMessage<LibObjT>; | |
91 | ||
92 | public: | |
93 | using Shared = internal::SharedMessage<CommonMessage<LibObjT>, LibObjT>; | |
94 | ||
95 | explicit CommonMessage(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr} | |
96 | { | |
97 | } | |
98 | ||
99 | template <typename OtherLibObjT> | |
100 | CommonMessage(const CommonMessage<OtherLibObjT>& val) noexcept : _ThisBorrowedObj {val} | |
101 | { | |
102 | } | |
103 | ||
104 | template <typename OtherLibObjT> | |
105 | _ThisCommonMessage& operator=(const CommonMessage<OtherLibObjT>& val) noexcept | |
106 | { | |
107 | _ThisBorrowedObj::operator=(val); | |
108 | return *this; | |
109 | } | |
110 | ||
111 | MessageType type() const noexcept | |
112 | { | |
113 | return static_cast<MessageType>(bt_message_get_type(this->_libObjPtr())); | |
114 | } | |
115 | ||
116 | bool isStreamBeginning() const noexcept | |
117 | { | |
118 | return this->type() == MessageType::STREAM_BEGINNING; | |
119 | } | |
120 | ||
121 | bool isStreamEnd() const noexcept | |
122 | { | |
123 | return this->type() == MessageType::STREAM_END; | |
124 | } | |
125 | ||
126 | bool isEvent() const noexcept | |
127 | { | |
128 | return this->type() == MessageType::EVENT; | |
129 | } | |
130 | ||
131 | bool isPacketBeginning() const noexcept | |
132 | { | |
133 | return this->type() == MessageType::PACKET_BEGINNING; | |
134 | } | |
135 | ||
136 | bool isPacketEnd() const noexcept | |
137 | { | |
138 | return this->type() == MessageType::PACKET_END; | |
139 | } | |
140 | ||
141 | bool isDiscardedEvents() const noexcept | |
142 | { | |
143 | return this->type() == MessageType::DISCARDED_EVENTS; | |
144 | } | |
145 | ||
146 | bool isDiscardedPackets() const noexcept | |
147 | { | |
148 | return this->type() == MessageType::DISCARDED_PACKETS; | |
149 | } | |
150 | ||
151 | bool isMessageIteratorInactivity() const noexcept | |
152 | { | |
153 | return this->type() == MessageType::MESSAGE_ITERATOR_INACTIVITY; | |
154 | } | |
155 | ||
156 | Shared shared() const noexcept | |
157 | { | |
158 | return Shared {*this}; | |
159 | } | |
160 | ||
161 | CommonStreamBeginningMessage<LibObjT> asStreamBeginning() const noexcept; | |
162 | CommonStreamEndMessage<LibObjT> asStreamEnd() const noexcept; | |
163 | CommonEventMessage<LibObjT> asEvent() const noexcept; | |
164 | CommonPacketBeginningMessage<LibObjT> asPacketBeginning() const noexcept; | |
165 | CommonPacketEndMessage<LibObjT> asPacketEnd() const noexcept; | |
166 | CommonDiscardedEventsMessage<LibObjT> asDiscardedEvents() const noexcept; | |
167 | CommonDiscardedPacketsMessage<LibObjT> asDiscardedPackets() const noexcept; | |
168 | CommonMessageIteratorInactivityMessage<LibObjT> asMessageIteratorInactivity() const noexcept; | |
169 | }; | |
170 | ||
171 | using Message = CommonMessage<bt_message>; | |
172 | using ConstMessage = CommonMessage<const bt_message>; | |
173 | ||
174 | namespace internal { | |
175 | ||
176 | template <typename LibObjT> | |
177 | struct CommonStreamBeginningMessageSpec; | |
178 | ||
179 | // Functions specific to mutable stream beginning messages | |
180 | template <> | |
181 | struct CommonStreamBeginningMessageSpec<bt_message> final | |
182 | { | |
183 | static bt_stream *stream(bt_message * const libObjPtr) noexcept | |
184 | { | |
185 | return bt_message_stream_beginning_borrow_stream(libObjPtr); | |
186 | } | |
187 | }; | |
188 | ||
189 | // Functions specific to constant stream beginning messages | |
190 | template <> | |
191 | struct CommonStreamBeginningMessageSpec<const bt_message> final | |
192 | { | |
193 | static const bt_stream *stream(const bt_message * const libObjPtr) noexcept | |
194 | { | |
195 | return bt_message_stream_beginning_borrow_stream_const(libObjPtr); | |
196 | } | |
197 | }; | |
198 | ||
199 | } // namespace internal | |
200 | ||
201 | template <typename LibObjT> | |
202 | class CommonStreamBeginningMessage final : public CommonMessage<LibObjT> | |
203 | { | |
204 | private: | |
205 | using typename CommonMessage<LibObjT>::_LibObjPtr; | |
206 | using typename CommonMessage<LibObjT>::_ThisCommonMessage; | |
207 | ||
208 | using _Stream = | |
209 | typename std::conditional<std::is_const<LibObjT>::value, CommonStream<const bt_stream>, | |
210 | CommonStream<bt_stream>>::type; | |
211 | ||
212 | public: | |
213 | using Shared = internal::SharedMessage<CommonStreamBeginningMessage<LibObjT>, LibObjT>; | |
214 | ||
215 | explicit CommonStreamBeginningMessage(const _LibObjPtr libObjPtr) noexcept : | |
216 | _ThisCommonMessage {libObjPtr} | |
217 | { | |
218 | BT_ASSERT_DBG(this->isStreamBeginning()); | |
219 | } | |
220 | ||
221 | template <typename OtherLibObjT> | |
222 | CommonStreamBeginningMessage(const CommonStreamBeginningMessage<OtherLibObjT>& val) noexcept : | |
223 | _ThisCommonMessage {val} | |
224 | { | |
225 | } | |
226 | ||
227 | template <typename OtherLibObjT> | |
228 | CommonStreamBeginningMessage<LibObjT>& | |
229 | operator=(const CommonStreamBeginningMessage<OtherLibObjT>& val) noexcept | |
230 | { | |
231 | _ThisCommonMessage::operator=(val); | |
232 | return *this; | |
233 | } | |
234 | ||
235 | ConstStream stream() const noexcept | |
236 | { | |
237 | return ConstStream {internal::CommonStreamBeginningMessageSpec<const bt_message>::stream( | |
238 | this->_libObjPtr())}; | |
239 | } | |
240 | ||
241 | _Stream stream() noexcept | |
242 | { | |
243 | return _Stream { | |
244 | internal::CommonStreamBeginningMessageSpec<LibObjT>::stream(this->_libObjPtr())}; | |
245 | } | |
246 | ||
247 | void defaultClockSnapshot(const std::uint64_t val) noexcept | |
248 | { | |
249 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
250 | ||
251 | bt_message_stream_beginning_set_default_clock_snapshot(this->_libObjPtr(), val); | |
252 | } | |
253 | ||
254 | nonstd::optional<ConstClockSnapshot> defaultClockSnapshot() const noexcept | |
255 | { | |
256 | const bt_clock_snapshot *libObjPtr; | |
257 | const auto state = bt_message_stream_beginning_borrow_default_clock_snapshot_const( | |
258 | this->_libObjPtr(), &libObjPtr); | |
259 | ||
260 | if (state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
261 | return ConstClockSnapshot {libObjPtr}; | |
262 | } | |
263 | ||
264 | return nonstd::nullopt; | |
265 | } | |
266 | ||
267 | Shared shared() const noexcept | |
268 | { | |
269 | return Shared {*this}; | |
270 | } | |
271 | }; | |
272 | ||
273 | using StreamBeginningMessage = CommonStreamBeginningMessage<bt_message>; | |
274 | using ConstStreamBeginningMessage = CommonStreamBeginningMessage<const bt_message>; | |
275 | ||
276 | namespace internal { | |
277 | ||
278 | template <typename LibObjT> | |
279 | struct CommonStreamEndMessageSpec; | |
280 | ||
281 | // Functions specific to mutable stream end messages | |
282 | template <> | |
283 | struct CommonStreamEndMessageSpec<bt_message> final | |
284 | { | |
285 | static bt_stream *stream(bt_message * const libObjPtr) noexcept | |
286 | { | |
287 | return bt_message_stream_end_borrow_stream(libObjPtr); | |
288 | } | |
289 | }; | |
290 | ||
291 | // Functions specific to constant stream end messages | |
292 | template <> | |
293 | struct CommonStreamEndMessageSpec<const bt_message> final | |
294 | { | |
295 | static const bt_stream *stream(const bt_message * const libObjPtr) noexcept | |
296 | { | |
297 | return bt_message_stream_end_borrow_stream_const(libObjPtr); | |
298 | } | |
299 | }; | |
300 | ||
301 | } // namespace internal | |
302 | ||
303 | template <typename LibObjT> | |
304 | class CommonStreamEndMessage final : public CommonMessage<LibObjT> | |
305 | { | |
306 | private: | |
307 | using typename CommonMessage<LibObjT>::_LibObjPtr; | |
308 | using typename CommonMessage<LibObjT>::_ThisCommonMessage; | |
309 | ||
310 | using _Stream = | |
311 | typename std::conditional<std::is_const<LibObjT>::value, CommonStream<const bt_stream>, | |
312 | CommonStream<bt_stream>>::type; | |
313 | ||
314 | public: | |
315 | using Shared = internal::SharedMessage<CommonStreamEndMessage<LibObjT>, LibObjT>; | |
316 | ||
317 | explicit CommonStreamEndMessage(const _LibObjPtr libObjPtr) noexcept : | |
318 | _ThisCommonMessage {libObjPtr} | |
319 | { | |
320 | BT_ASSERT_DBG(this->isStreamEnd()); | |
321 | } | |
322 | ||
323 | template <typename OtherLibObjT> | |
324 | CommonStreamEndMessage(const CommonStreamEndMessage<OtherLibObjT>& val) noexcept : | |
325 | _ThisCommonMessage {val} | |
326 | { | |
327 | } | |
328 | ||
329 | template <typename OtherLibObjT> | |
330 | CommonStreamEndMessage<LibObjT>& | |
331 | operator=(const CommonStreamEndMessage<OtherLibObjT>& val) noexcept | |
332 | { | |
333 | _ThisCommonMessage::operator=(val); | |
334 | return *this; | |
335 | } | |
336 | ||
337 | ConstStream stream() const noexcept | |
338 | { | |
339 | return ConstStream { | |
340 | internal::CommonStreamEndMessageSpec<const bt_message>::stream(this->_libObjPtr())}; | |
341 | } | |
342 | ||
343 | _Stream stream() noexcept | |
344 | { | |
345 | return _Stream {internal::CommonStreamEndMessageSpec<LibObjT>::stream(this->_libObjPtr())}; | |
346 | } | |
347 | ||
348 | void defaultClockSnapshot(const std::uint64_t val) noexcept | |
349 | { | |
350 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
351 | ||
352 | bt_message_stream_end_set_default_clock_snapshot(this->_libObjPtr(), val); | |
353 | } | |
354 | ||
355 | nonstd::optional<ConstClockSnapshot> defaultClockSnapshot() const noexcept | |
356 | { | |
357 | const bt_clock_snapshot *libObjPtr; | |
358 | const auto state = bt_message_stream_end_borrow_default_clock_snapshot_const( | |
359 | this->_libObjPtr(), &libObjPtr); | |
360 | ||
361 | if (state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
362 | return ConstClockSnapshot {libObjPtr}; | |
363 | } | |
364 | ||
365 | return nonstd::nullopt; | |
366 | } | |
367 | ||
368 | Shared shared() const noexcept | |
369 | { | |
370 | return Shared {*this}; | |
371 | } | |
372 | }; | |
373 | ||
374 | using StreamEndMessage = CommonStreamEndMessage<bt_message>; | |
375 | using ConstStreamEndMessage = CommonStreamEndMessage<const bt_message>; | |
376 | ||
377 | namespace internal { | |
378 | ||
379 | template <typename LibObjT> | |
380 | struct CommonPacketBeginningMessageSpec; | |
381 | ||
382 | // Functions specific to mutable packet beginning messages | |
383 | template <> | |
384 | struct CommonPacketBeginningMessageSpec<bt_message> final | |
385 | { | |
386 | static bt_packet *packet(bt_message * const libObjPtr) noexcept | |
387 | { | |
388 | return bt_message_packet_beginning_borrow_packet(libObjPtr); | |
389 | } | |
390 | }; | |
391 | ||
392 | // Functions specific to constant packet beginning messages | |
393 | template <> | |
394 | struct CommonPacketBeginningMessageSpec<const bt_message> final | |
395 | { | |
396 | static const bt_packet *packet(const bt_message * const libObjPtr) noexcept | |
397 | { | |
398 | return bt_message_packet_beginning_borrow_packet_const(libObjPtr); | |
399 | } | |
400 | }; | |
401 | ||
402 | } // namespace internal | |
403 | ||
404 | template <typename LibObjT> | |
405 | class CommonPacketBeginningMessage final : public CommonMessage<LibObjT> | |
406 | { | |
407 | private: | |
408 | using typename CommonMessage<LibObjT>::_LibObjPtr; | |
409 | using typename CommonMessage<LibObjT>::_ThisCommonMessage; | |
410 | ||
411 | using _Packet = | |
412 | typename std::conditional<std::is_const<LibObjT>::value, CommonPacket<const bt_packet>, | |
413 | CommonPacket<bt_packet>>::type; | |
414 | ||
415 | public: | |
416 | using Shared = internal::SharedMessage<CommonPacketBeginningMessage<LibObjT>, LibObjT>; | |
417 | ||
418 | explicit CommonPacketBeginningMessage(const _LibObjPtr libObjPtr) noexcept : | |
419 | _ThisCommonMessage {libObjPtr} | |
420 | { | |
421 | BT_ASSERT_DBG(this->isPacketBeginning()); | |
422 | } | |
423 | ||
424 | template <typename OtherLibObjT> | |
425 | CommonPacketBeginningMessage(const CommonPacketBeginningMessage<OtherLibObjT>& val) noexcept : | |
426 | _ThisCommonMessage {val} | |
427 | { | |
428 | } | |
429 | ||
430 | template <typename OtherLibObjT> | |
431 | CommonPacketBeginningMessage<LibObjT>& | |
432 | operator=(const CommonPacketBeginningMessage<OtherLibObjT>& val) noexcept | |
433 | { | |
434 | _ThisCommonMessage::operator=(val); | |
435 | return *this; | |
436 | } | |
437 | ||
438 | ConstPacket packet() const noexcept | |
439 | { | |
440 | return ConstPacket {internal::CommonPacketBeginningMessageSpec<const bt_message>::packet( | |
441 | this->_libObjPtr())}; | |
442 | } | |
443 | ||
444 | _Packet packet() noexcept | |
445 | { | |
446 | return _Packet { | |
447 | internal::CommonPacketBeginningMessageSpec<LibObjT>::packet(this->_libObjPtr())}; | |
448 | } | |
449 | ||
450 | void defaultClockSnapshot(const std::uint64_t val) noexcept | |
451 | { | |
452 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
453 | ||
454 | bt_message_packet_beginning_set_default_clock_snapshot(this->_libObjPtr(), val); | |
455 | } | |
456 | ||
457 | ConstClockSnapshot defaultClockSnapshot() const noexcept | |
458 | { | |
459 | const auto libObjPtr = | |
460 | bt_message_packet_beginning_borrow_default_clock_snapshot_const(this->_libObjPtr()); | |
461 | ||
462 | return ConstClockSnapshot {libObjPtr}; | |
463 | } | |
464 | ||
465 | Shared shared() const noexcept | |
466 | { | |
467 | return Shared {*this}; | |
468 | } | |
469 | }; | |
470 | ||
471 | using PacketBeginningMessage = CommonPacketBeginningMessage<bt_message>; | |
472 | using ConstPacketBeginningMessage = CommonPacketBeginningMessage<const bt_message>; | |
473 | ||
474 | namespace internal { | |
475 | ||
476 | template <typename LibObjT> | |
477 | struct CommonPacketEndMessageSpec; | |
478 | ||
479 | // Functions specific to mutable packet end messages | |
480 | template <> | |
481 | struct CommonPacketEndMessageSpec<bt_message> final | |
482 | { | |
483 | static bt_packet *packet(bt_message * const libObjPtr) noexcept | |
484 | { | |
485 | return bt_message_packet_end_borrow_packet(libObjPtr); | |
486 | } | |
487 | }; | |
488 | ||
489 | // Functions specific to constant packet end messages | |
490 | template <> | |
491 | struct CommonPacketEndMessageSpec<const bt_message> final | |
492 | { | |
493 | static const bt_packet *packet(const bt_message * const libObjPtr) noexcept | |
494 | { | |
495 | return bt_message_packet_end_borrow_packet_const(libObjPtr); | |
496 | } | |
497 | }; | |
498 | ||
499 | } // namespace internal | |
500 | ||
501 | template <typename LibObjT> | |
502 | class CommonPacketEndMessage final : public CommonMessage<LibObjT> | |
503 | { | |
504 | private: | |
505 | using typename CommonMessage<LibObjT>::_LibObjPtr; | |
506 | using typename CommonMessage<LibObjT>::_ThisCommonMessage; | |
507 | ||
508 | using _Packet = | |
509 | typename std::conditional<std::is_const<LibObjT>::value, CommonPacket<const bt_packet>, | |
510 | CommonPacket<bt_packet>>::type; | |
511 | ||
512 | public: | |
513 | using Shared = internal::SharedMessage<CommonPacketEndMessage<LibObjT>, LibObjT>; | |
514 | ||
515 | explicit CommonPacketEndMessage(const _LibObjPtr libObjPtr) noexcept : | |
516 | _ThisCommonMessage {libObjPtr} | |
517 | { | |
518 | BT_ASSERT_DBG(this->isPacketEnd()); | |
519 | } | |
520 | ||
521 | template <typename OtherLibObjT> | |
522 | CommonPacketEndMessage(const CommonPacketEndMessage<OtherLibObjT>& val) noexcept : | |
523 | _ThisCommonMessage {val} | |
524 | { | |
525 | } | |
526 | ||
527 | template <typename OtherLibObjT> | |
528 | CommonPacketEndMessage<LibObjT>& | |
529 | operator=(const CommonPacketEndMessage<OtherLibObjT>& val) noexcept | |
530 | { | |
531 | _ThisCommonMessage::operator=(val); | |
532 | return *this; | |
533 | } | |
534 | ||
535 | ConstPacket packet() const noexcept | |
536 | { | |
537 | return ConstPacket { | |
538 | internal::CommonPacketEndMessageSpec<const bt_message>::packet(this->_libObjPtr())}; | |
539 | } | |
540 | ||
541 | _Packet packet() noexcept | |
542 | { | |
543 | return _Packet {internal::CommonPacketEndMessageSpec<LibObjT>::packet(this->_libObjPtr())}; | |
544 | } | |
545 | ||
546 | void defaultClockSnapshot(const std::uint64_t val) noexcept | |
547 | { | |
548 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
549 | ||
550 | bt_message_packet_end_set_default_clock_snapshot(this->_libObjPtr(), val); | |
551 | } | |
552 | ||
553 | ConstClockSnapshot defaultClockSnapshot() const noexcept | |
554 | { | |
555 | const auto libObjPtr = | |
556 | bt_message_packet_end_borrow_default_clock_snapshot_const(this->_libObjPtr()); | |
557 | ||
558 | return ConstClockSnapshot {libObjPtr}; | |
559 | } | |
560 | ||
561 | Shared shared() const noexcept | |
562 | { | |
563 | return Shared {*this}; | |
564 | } | |
565 | }; | |
566 | ||
567 | using PacketEndMessage = CommonPacketEndMessage<bt_message>; | |
568 | using ConstPacketEndMessage = CommonPacketEndMessage<const bt_message>; | |
569 | ||
570 | namespace internal { | |
571 | ||
572 | template <typename LibObjT> | |
573 | struct CommonEventMessageSpec; | |
574 | ||
575 | // Functions specific to mutable event messages | |
576 | template <> | |
577 | struct CommonEventMessageSpec<bt_message> final | |
578 | { | |
579 | static bt_event *event(bt_message * const libObjPtr) noexcept | |
580 | { | |
581 | return bt_message_event_borrow_event(libObjPtr); | |
582 | } | |
583 | }; | |
584 | ||
585 | // Functions specific to constant event messages | |
586 | template <> | |
587 | struct CommonEventMessageSpec<const bt_message> final | |
588 | { | |
589 | static const bt_event *event(const bt_message * const libObjPtr) noexcept | |
590 | { | |
591 | return bt_message_event_borrow_event_const(libObjPtr); | |
592 | } | |
593 | }; | |
594 | ||
595 | } // namespace internal | |
596 | ||
597 | template <typename LibObjT> | |
598 | class CommonEventMessage final : public CommonMessage<LibObjT> | |
599 | { | |
600 | private: | |
601 | using typename CommonMessage<LibObjT>::_LibObjPtr; | |
602 | using typename CommonMessage<LibObjT>::_ThisCommonMessage; | |
603 | ||
604 | using _Event = | |
605 | typename std::conditional<std::is_const<LibObjT>::value, CommonEvent<const bt_event>, | |
606 | CommonEvent<bt_event>>::type; | |
607 | ||
608 | public: | |
609 | using Shared = internal::SharedMessage<CommonEventMessage<LibObjT>, LibObjT>; | |
610 | ||
611 | explicit CommonEventMessage(const _LibObjPtr libObjPtr) noexcept : | |
612 | _ThisCommonMessage {libObjPtr} | |
613 | { | |
614 | BT_ASSERT_DBG(this->isEvent()); | |
615 | } | |
616 | ||
617 | template <typename OtherLibObjT> | |
618 | CommonEventMessage(const CommonEventMessage<OtherLibObjT>& val) noexcept : | |
619 | _ThisCommonMessage {val} | |
620 | { | |
621 | } | |
622 | ||
623 | template <typename OtherLibObjT> | |
624 | CommonEventMessage<LibObjT>& operator=(const CommonEventMessage<OtherLibObjT>& val) noexcept | |
625 | { | |
626 | _ThisCommonMessage::operator=(val); | |
627 | return *this; | |
628 | } | |
629 | ||
630 | ConstEvent event() const noexcept | |
631 | { | |
632 | return ConstEvent { | |
633 | internal::CommonEventMessageSpec<const bt_message>::event(this->_libObjPtr())}; | |
634 | } | |
635 | ||
636 | _Event event() noexcept | |
637 | { | |
638 | return _Event {internal::CommonEventMessageSpec<LibObjT>::event(this->_libObjPtr())}; | |
639 | } | |
640 | ||
641 | ConstClockSnapshot defaultClockSnapshot() const noexcept | |
642 | { | |
643 | const auto libObjPtr = | |
644 | bt_message_event_borrow_default_clock_snapshot_const(this->_libObjPtr()); | |
645 | ||
646 | return ConstClockSnapshot {libObjPtr}; | |
647 | } | |
648 | ||
649 | Shared shared() const noexcept | |
650 | { | |
651 | return Shared {*this}; | |
652 | } | |
653 | }; | |
654 | ||
655 | using EventMessage = CommonEventMessage<bt_message>; | |
656 | using ConstEventMessage = CommonEventMessage<const bt_message>; | |
657 | ||
658 | namespace internal { | |
659 | ||
660 | template <typename LibObjT> | |
661 | struct CommonDiscardedEventsMessageSpec; | |
662 | ||
663 | // Functions specific to mutable discarded events messages | |
664 | template <> | |
665 | struct CommonDiscardedEventsMessageSpec<bt_message> final | |
666 | { | |
667 | static bt_stream *stream(bt_message * const libObjPtr) noexcept | |
668 | { | |
669 | return bt_message_discarded_events_borrow_stream(libObjPtr); | |
670 | } | |
671 | }; | |
672 | ||
673 | // Functions specific to constant discarded events messages | |
674 | template <> | |
675 | struct CommonDiscardedEventsMessageSpec<const bt_message> final | |
676 | { | |
677 | static const bt_stream *stream(const bt_message * const libObjPtr) noexcept | |
678 | { | |
679 | return bt_message_discarded_events_borrow_stream_const(libObjPtr); | |
680 | } | |
681 | }; | |
682 | ||
683 | } // namespace internal | |
684 | ||
685 | template <typename LibObjT> | |
686 | class CommonDiscardedEventsMessage final : public CommonMessage<LibObjT> | |
687 | { | |
688 | private: | |
689 | using typename CommonMessage<LibObjT>::_LibObjPtr; | |
690 | using typename CommonMessage<LibObjT>::_ThisCommonMessage; | |
691 | ||
692 | using _Stream = | |
693 | typename std::conditional<std::is_const<LibObjT>::value, CommonStream<const bt_stream>, | |
694 | CommonStream<bt_stream>>::type; | |
695 | ||
696 | public: | |
697 | using Shared = internal::SharedMessage<CommonDiscardedEventsMessage<LibObjT>, LibObjT>; | |
698 | ||
699 | explicit CommonDiscardedEventsMessage(const _LibObjPtr libObjPtr) noexcept : | |
700 | _ThisCommonMessage {libObjPtr} | |
701 | { | |
702 | BT_ASSERT_DBG(this->isDiscardedEvents()); | |
703 | } | |
704 | ||
705 | template <typename OtherLibObjT> | |
706 | CommonDiscardedEventsMessage(const CommonDiscardedEventsMessage<OtherLibObjT>& val) noexcept : | |
707 | _ThisCommonMessage {val} | |
708 | { | |
709 | } | |
710 | ||
711 | template <typename OtherLibObjT> | |
712 | CommonDiscardedEventsMessage<LibObjT>& | |
713 | operator=(const CommonDiscardedEventsMessage<OtherLibObjT>& val) noexcept | |
714 | { | |
715 | _ThisCommonMessage::operator=(val); | |
716 | return *this; | |
717 | } | |
718 | ||
719 | ConstStream stream() const noexcept | |
720 | { | |
721 | return ConstStream {internal::CommonDiscardedEventsMessageSpec<const bt_message>::stream( | |
722 | this->_libObjPtr())}; | |
723 | } | |
724 | ||
725 | _Stream stream() noexcept | |
726 | { | |
727 | return _Stream { | |
728 | internal::CommonDiscardedEventsMessageSpec<LibObjT>::stream(this->_libObjPtr())}; | |
729 | } | |
730 | ||
731 | ConstClockSnapshot beginningDefaultClockSnapshot() const noexcept | |
732 | { | |
733 | const auto libObjPtr = | |
734 | bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( | |
735 | this->_libObjPtr()); | |
736 | ||
737 | return ConstClockSnapshot {libObjPtr}; | |
738 | } | |
739 | ||
740 | ConstClockSnapshot endDefaultClockSnapshot() const noexcept | |
741 | { | |
742 | const auto libObjPtr = | |
743 | bt_message_discarded_events_borrow_end_default_clock_snapshot_const(this->_libObjPtr()); | |
744 | ||
745 | return ConstClockSnapshot {libObjPtr}; | |
746 | } | |
747 | ||
748 | void count(const std::uint64_t count) noexcept | |
749 | { | |
750 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
751 | ||
752 | bt_message_discarded_events_set_count(this->_libObjPtr(), count); | |
753 | } | |
754 | ||
755 | nonstd::optional<std::uint64_t> count() const noexcept | |
756 | { | |
757 | std::uint64_t count; | |
758 | const auto avail = bt_message_discarded_events_get_count(this->_libObjPtr(), &count); | |
759 | ||
760 | if (avail) { | |
761 | return count; | |
762 | } | |
763 | ||
764 | return nonstd::nullopt; | |
765 | } | |
766 | ||
767 | Shared shared() const noexcept | |
768 | { | |
769 | return Shared {*this}; | |
770 | } | |
771 | }; | |
772 | ||
773 | using DiscardedEventsMessage = CommonDiscardedEventsMessage<bt_message>; | |
774 | using ConstDiscardedEventsMessage = CommonDiscardedEventsMessage<const bt_message>; | |
775 | ||
776 | namespace internal { | |
777 | ||
778 | template <typename LibObjT> | |
779 | struct CommonDiscardedPacketsMessageSpec; | |
780 | ||
781 | // Functions specific to mutable discarded packets messages | |
782 | template <> | |
783 | struct CommonDiscardedPacketsMessageSpec<bt_message> final | |
784 | { | |
785 | static bt_stream *stream(bt_message * const libObjPtr) noexcept | |
786 | { | |
787 | return bt_message_discarded_packets_borrow_stream(libObjPtr); | |
788 | } | |
789 | }; | |
790 | ||
791 | // Functions specific to constant discarded packets messages | |
792 | template <> | |
793 | struct CommonDiscardedPacketsMessageSpec<const bt_message> final | |
794 | { | |
795 | static const bt_stream *stream(const bt_message * const libObjPtr) noexcept | |
796 | { | |
797 | return bt_message_discarded_packets_borrow_stream_const(libObjPtr); | |
798 | } | |
799 | }; | |
800 | ||
801 | } // namespace internal | |
802 | ||
803 | template <typename LibObjT> | |
804 | class CommonDiscardedPacketsMessage final : public CommonMessage<LibObjT> | |
805 | { | |
806 | private: | |
807 | using typename CommonMessage<LibObjT>::_LibObjPtr; | |
808 | using typename CommonMessage<LibObjT>::_ThisCommonMessage; | |
809 | ||
810 | using _Stream = | |
811 | typename std::conditional<std::is_const<LibObjT>::value, CommonStream<const bt_stream>, | |
812 | CommonStream<bt_stream>>::type; | |
813 | ||
814 | public: | |
815 | using Shared = internal::SharedMessage<CommonDiscardedPacketsMessage<LibObjT>, LibObjT>; | |
816 | ||
817 | explicit CommonDiscardedPacketsMessage(const _LibObjPtr libObjPtr) noexcept : | |
818 | _ThisCommonMessage {libObjPtr} | |
819 | { | |
820 | BT_ASSERT_DBG(this->isDiscardedPackets()); | |
821 | } | |
822 | ||
823 | template <typename OtherLibObjT> | |
824 | CommonDiscardedPacketsMessage(const CommonDiscardedPacketsMessage<OtherLibObjT>& val) noexcept : | |
825 | _ThisCommonMessage {val} | |
826 | { | |
827 | } | |
828 | ||
829 | template <typename OtherLibObjT> | |
830 | CommonDiscardedPacketsMessage<LibObjT>& | |
831 | operator=(const CommonDiscardedPacketsMessage<OtherLibObjT>& val) noexcept | |
832 | { | |
833 | _ThisCommonMessage::operator=(val); | |
834 | return *this; | |
835 | } | |
836 | ||
837 | ConstStream stream() const noexcept | |
838 | { | |
839 | return ConstStream {internal::CommonDiscardedPacketsMessageSpec<const bt_message>::stream( | |
840 | this->_libObjPtr())}; | |
841 | } | |
842 | ||
843 | _Stream stream() noexcept | |
844 | { | |
845 | return _Stream { | |
846 | internal::CommonDiscardedPacketsMessageSpec<LibObjT>::stream(this->_libObjPtr())}; | |
847 | } | |
848 | ||
849 | ConstClockSnapshot beginningDefaultClockSnapshot() const noexcept | |
850 | { | |
851 | const auto libObjPtr = | |
852 | bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( | |
853 | this->_libObjPtr()); | |
854 | ||
855 | return ConstClockSnapshot {libObjPtr}; | |
856 | } | |
857 | ||
858 | ConstClockSnapshot endDefaultClockSnapshot() const noexcept | |
859 | { | |
860 | const auto libObjPtr = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const( | |
861 | this->_libObjPtr()); | |
862 | ||
863 | return ConstClockSnapshot {libObjPtr}; | |
864 | } | |
865 | ||
866 | void count(const std::uint64_t count) noexcept | |
867 | { | |
868 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
869 | ||
870 | bt_message_discarded_packets_set_count(this->_libObjPtr(), count); | |
871 | } | |
872 | ||
873 | nonstd::optional<std::uint64_t> count() const noexcept | |
874 | { | |
875 | std::uint64_t count; | |
876 | const auto avail = bt_message_discarded_packets_get_count(this->_libObjPtr(), &count); | |
877 | ||
878 | if (avail) { | |
879 | return count; | |
880 | } | |
881 | ||
882 | return nonstd::nullopt; | |
883 | } | |
884 | ||
885 | Shared shared() const noexcept | |
886 | { | |
887 | return Shared {*this}; | |
888 | } | |
889 | }; | |
890 | ||
891 | using DiscardedPacketsMessage = CommonDiscardedPacketsMessage<bt_message>; | |
892 | using ConstDiscardedPacketsMessage = CommonDiscardedPacketsMessage<const bt_message>; | |
893 | ||
894 | template <typename LibObjT> | |
895 | class CommonMessageIteratorInactivityMessage final : public CommonMessage<LibObjT> | |
896 | { | |
897 | private: | |
898 | using typename CommonMessage<LibObjT>::_LibObjPtr; | |
899 | using typename CommonMessage<LibObjT>::_ThisCommonMessage; | |
900 | ||
901 | public: | |
902 | using Shared = | |
903 | internal::SharedMessage<CommonMessageIteratorInactivityMessage<LibObjT>, LibObjT>; | |
904 | ||
905 | explicit CommonMessageIteratorInactivityMessage(const _LibObjPtr libObjPtr) noexcept : | |
906 | _ThisCommonMessage {libObjPtr} | |
907 | { | |
908 | BT_ASSERT_DBG(this->isMessageIteratorInactivity()); | |
909 | } | |
910 | ||
911 | template <typename OtherLibObjT> | |
912 | CommonMessageIteratorInactivityMessage( | |
913 | const CommonMessageIteratorInactivityMessage<OtherLibObjT>& val) noexcept : | |
914 | _ThisCommonMessage {val} | |
915 | { | |
916 | } | |
917 | ||
918 | template <typename OtherLibObjT> | |
919 | CommonMessageIteratorInactivityMessage<LibObjT>& | |
920 | operator=(const CommonMessageIteratorInactivityMessage<OtherLibObjT>& val) noexcept | |
921 | { | |
922 | _ThisCommonMessage::operator=(val); | |
923 | return *this; | |
924 | } | |
925 | ||
926 | ConstClockSnapshot clockSnapshot() const noexcept | |
927 | { | |
928 | const auto libObjPtr = | |
929 | bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(this->_libObjPtr()); | |
930 | ||
931 | return ConstClockSnapshot {libObjPtr}; | |
932 | } | |
933 | ||
934 | Shared shared() const noexcept | |
935 | { | |
936 | return Shared {*this}; | |
937 | } | |
938 | }; | |
939 | ||
940 | using MessageIteratorInactivityMessage = CommonMessageIteratorInactivityMessage<bt_message>; | |
941 | using ConstMessageIteratorInactivityMessage = | |
942 | CommonMessageIteratorInactivityMessage<const bt_message>; | |
943 | ||
944 | template <typename LibObjT> | |
945 | CommonStreamBeginningMessage<LibObjT> CommonMessage<LibObjT>::asStreamBeginning() const noexcept | |
946 | { | |
947 | BT_ASSERT_DBG(this->isStreamBeginning()); | |
948 | return CommonStreamBeginningMessage<LibObjT> {this->_libObjPtr()}; | |
949 | } | |
950 | ||
951 | template <typename LibObjT> | |
952 | CommonStreamEndMessage<LibObjT> CommonMessage<LibObjT>::asStreamEnd() const noexcept | |
953 | { | |
954 | BT_ASSERT_DBG(this->isStreamEnd()); | |
955 | return CommonStreamEndMessage<LibObjT> {this->_libObjPtr()}; | |
956 | } | |
957 | ||
958 | template <typename LibObjT> | |
959 | CommonPacketBeginningMessage<LibObjT> CommonMessage<LibObjT>::asPacketBeginning() const noexcept | |
960 | { | |
961 | BT_ASSERT_DBG(this->isPacketBeginning()); | |
962 | return CommonPacketBeginningMessage<LibObjT> {this->_libObjPtr()}; | |
963 | } | |
964 | ||
965 | template <typename LibObjT> | |
966 | CommonPacketEndMessage<LibObjT> CommonMessage<LibObjT>::asPacketEnd() const noexcept | |
967 | { | |
968 | BT_ASSERT_DBG(this->isPacketEnd()); | |
969 | return CommonPacketEndMessage<LibObjT> {this->_libObjPtr()}; | |
970 | } | |
971 | ||
972 | template <typename LibObjT> | |
973 | CommonEventMessage<LibObjT> CommonMessage<LibObjT>::asEvent() const noexcept | |
974 | { | |
975 | BT_ASSERT_DBG(this->isEvent()); | |
976 | return CommonEventMessage<LibObjT> {this->_libObjPtr()}; | |
977 | } | |
978 | ||
979 | template <typename LibObjT> | |
980 | CommonDiscardedEventsMessage<LibObjT> CommonMessage<LibObjT>::asDiscardedEvents() const noexcept | |
981 | { | |
982 | BT_ASSERT_DBG(this->isDiscardedEvents()); | |
983 | return CommonDiscardedEventsMessage<LibObjT> {this->_libObjPtr()}; | |
984 | } | |
985 | ||
986 | template <typename LibObjT> | |
987 | CommonMessageIteratorInactivityMessage<LibObjT> | |
988 | CommonMessage<LibObjT>::asMessageIteratorInactivity() const noexcept | |
989 | { | |
990 | BT_ASSERT_DBG(this->isMessageIteratorInactivity()); | |
991 | return CommonMessageIteratorInactivityMessage<LibObjT> {this->_libObjPtr()}; | |
992 | } | |
993 | ||
994 | } // namespace bt2 | |
995 | ||
996 | #endif // BABELTRACE_CPP_COMMON_BT2_MESSAGE_HPP |