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