cpp-common: add `bt2s::optional`, alias of `nonstd::optional`
[babeltrace.git] / src / cpp-common / bt2 / message.hpp
index 5812b06d6b0d73603b7aad42872fe8785ea7b625..afc751c5df1b50275363b9c655ecbe7e92d81519 100644 (file)
@@ -15,7 +15,7 @@
 #include "common/assert.h"
 #include "cpp-common/bt2/clock-snapshot.hpp"
 #include "cpp-common/bt2/trace-ir.hpp"
-#include "cpp-common/optional.hpp"
+#include "cpp-common/bt2s/optional.hpp"
 
 #include "borrowed-object.hpp"
 #include "internal/utils.hpp"
@@ -101,12 +101,17 @@ public:
     }
 
     template <typename OtherLibObjT>
-    _ThisCommonMessage& operator=(const CommonMessage<OtherLibObjT> val) noexcept
+    _ThisCommonMessage operator=(const CommonMessage<OtherLibObjT> val) noexcept
     {
         _ThisBorrowedObject::operator=(val);
         return *this;
     }
 
+    CommonMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonMessage<const bt_message> {*this};
+    }
+
     MessageType type() const noexcept
     {
         return static_cast<MessageType>(bt_message_get_type(this->libObjPtr()));
@@ -225,10 +230,7 @@ class CommonStreamBeginningMessage final : public CommonMessage<LibObjT>
 private:
     using typename CommonMessage<LibObjT>::_LibObjPtr;
     using typename CommonMessage<LibObjT>::_ThisCommonMessage;
-
-    using _Stream =
-        typename std::conditional<std::is_const<LibObjT>::value, CommonStream<const bt_stream>,
-                                  CommonStream<bt_stream>>::type;
+    using _Stream = internal::DepStream<LibObjT>;
 
 public:
     using Shared = SharedMessage<CommonStreamBeginningMessage<LibObjT>, LibObjT>;
@@ -246,13 +248,18 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonStreamBeginningMessage<LibObjT>&
+    CommonStreamBeginningMessage<LibObjT>
     operator=(const CommonStreamBeginningMessage<OtherLibObjT> val) noexcept
     {
         _ThisCommonMessage::operator=(val);
         return *this;
     }
 
+    CommonStreamBeginningMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonStreamBeginningMessage<const bt_message> {*this};
+    }
+
     _Stream stream() const noexcept
     {
         return _Stream {
@@ -261,12 +268,13 @@ public:
 
     void defaultClockSnapshot(const std::uint64_t val) const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstStreamBeginningMessage`.");
 
         bt_message_stream_beginning_set_default_clock_snapshot(this->libObjPtr(), val);
     }
 
-    nonstd::optional<ConstClockSnapshot> defaultClockSnapshot() const noexcept
+    bt2s::optional<ConstClockSnapshot> defaultClockSnapshot() const noexcept
     {
         const bt_clock_snapshot *libObjPtr;
         const auto state = bt_message_stream_beginning_borrow_default_clock_snapshot_const(
@@ -276,7 +284,7 @@ public:
             return ConstClockSnapshot {libObjPtr};
         }
 
-        return nonstd::nullopt;
+        return bt2s::nullopt;
     }
 
     Shared shared() const noexcept
@@ -337,10 +345,7 @@ class CommonStreamEndMessage final : public CommonMessage<LibObjT>
 private:
     using typename CommonMessage<LibObjT>::_LibObjPtr;
     using typename CommonMessage<LibObjT>::_ThisCommonMessage;
-
-    using _Stream =
-        typename std::conditional<std::is_const<LibObjT>::value, CommonStream<const bt_stream>,
-                                  CommonStream<bt_stream>>::type;
+    using _Stream = internal::DepStream<LibObjT>;
 
 public:
     using Shared = SharedMessage<CommonStreamEndMessage<LibObjT>, LibObjT>;
@@ -358,13 +363,18 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonStreamEndMessage<LibObjT>&
+    CommonStreamEndMessage<LibObjT>
     operator=(const CommonStreamEndMessage<OtherLibObjT> val) noexcept
     {
         _ThisCommonMessage::operator=(val);
         return *this;
     }
 
+    CommonStreamEndMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonStreamEndMessage<const bt_message> {*this};
+    }
+
     _Stream stream() const noexcept
     {
         return _Stream {internal::CommonStreamEndMessageSpec<LibObjT>::stream(this->libObjPtr())};
@@ -372,12 +382,13 @@ public:
 
     void defaultClockSnapshot(const std::uint64_t val) const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstStreamEndMessage`.");
 
         bt_message_stream_end_set_default_clock_snapshot(this->libObjPtr(), val);
     }
 
-    nonstd::optional<ConstClockSnapshot> defaultClockSnapshot() const noexcept
+    bt2s::optional<ConstClockSnapshot> defaultClockSnapshot() const noexcept
     {
         const bt_clock_snapshot *libObjPtr;
         const auto state = bt_message_stream_end_borrow_default_clock_snapshot_const(
@@ -387,7 +398,7 @@ public:
             return ConstClockSnapshot {libObjPtr};
         }
 
-        return nonstd::nullopt;
+        return bt2s::nullopt;
     }
 
     Shared shared() const noexcept
@@ -448,10 +459,7 @@ class CommonPacketBeginningMessage final : public CommonMessage<LibObjT>
 private:
     using typename CommonMessage<LibObjT>::_LibObjPtr;
     using typename CommonMessage<LibObjT>::_ThisCommonMessage;
-
-    using _Packet =
-        typename std::conditional<std::is_const<LibObjT>::value, CommonPacket<const bt_packet>,
-                                  CommonPacket<bt_packet>>::type;
+    using _Packet = internal::DepPacket<LibObjT>;
 
 public:
     using Shared = SharedMessage<CommonPacketBeginningMessage<LibObjT>, LibObjT>;
@@ -469,13 +477,18 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonPacketBeginningMessage<LibObjT>&
+    CommonPacketBeginningMessage<LibObjT>
     operator=(const CommonPacketBeginningMessage<OtherLibObjT> val) noexcept
     {
         _ThisCommonMessage::operator=(val);
         return *this;
     }
 
+    CommonPacketBeginningMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonPacketBeginningMessage<const bt_message> {*this};
+    }
+
     _Packet packet() const noexcept
     {
         return _Packet {
@@ -484,7 +497,8 @@ public:
 
     void defaultClockSnapshot(const std::uint64_t val) const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstPacketBeginningMessage`.");
 
         bt_message_packet_beginning_set_default_clock_snapshot(this->libObjPtr(), val);
     }
@@ -555,10 +569,7 @@ class CommonPacketEndMessage final : public CommonMessage<LibObjT>
 private:
     using typename CommonMessage<LibObjT>::_LibObjPtr;
     using typename CommonMessage<LibObjT>::_ThisCommonMessage;
-
-    using _Packet =
-        typename std::conditional<std::is_const<LibObjT>::value, CommonPacket<const bt_packet>,
-                                  CommonPacket<bt_packet>>::type;
+    using _Packet = internal::DepPacket<LibObjT>;
 
 public:
     using Shared = SharedMessage<CommonPacketEndMessage<LibObjT>, LibObjT>;
@@ -576,13 +587,18 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonPacketEndMessage<LibObjT>&
+    CommonPacketEndMessage<LibObjT>
     operator=(const CommonPacketEndMessage<OtherLibObjT> val) noexcept
     {
         _ThisCommonMessage::operator=(val);
         return *this;
     }
 
+    CommonPacketEndMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonPacketEndMessage<const bt_message> {*this};
+    }
+
     _Packet packet() const noexcept
     {
         return _Packet {internal::CommonPacketEndMessageSpec<LibObjT>::packet(this->libObjPtr())};
@@ -590,7 +606,8 @@ public:
 
     void defaultClockSnapshot(const std::uint64_t val) const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstPacketEndMessage`.");
 
         bt_message_packet_end_set_default_clock_snapshot(this->libObjPtr(), val);
     }
@@ -661,10 +678,7 @@ class CommonEventMessage final : public CommonMessage<LibObjT>
 private:
     using typename CommonMessage<LibObjT>::_LibObjPtr;
     using typename CommonMessage<LibObjT>::_ThisCommonMessage;
-
-    using _Event =
-        typename std::conditional<std::is_const<LibObjT>::value, CommonEvent<const bt_event>,
-                                  CommonEvent<bt_event>>::type;
+    using _Event = internal::DepType<LibObjT, CommonEvent<bt_event>, CommonEvent<const bt_event>>;
 
 public:
     using Shared = SharedMessage<CommonEventMessage<LibObjT>, LibObjT>;
@@ -682,12 +696,17 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonEventMessage<LibObjT>& operator=(const CommonEventMessage<OtherLibObjT> val) noexcept
+    CommonEventMessage<LibObjT> operator=(const CommonEventMessage<OtherLibObjT> val) noexcept
     {
         _ThisCommonMessage::operator=(val);
         return *this;
     }
 
+    CommonEventMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonEventMessage<const bt_message> {*this};
+    }
+
     _Event event() const noexcept
     {
         return _Event {internal::CommonEventMessageSpec<LibObjT>::event(this->libObjPtr())};
@@ -759,10 +778,7 @@ class CommonDiscardedEventsMessage final : public CommonMessage<LibObjT>
 private:
     using typename CommonMessage<LibObjT>::_LibObjPtr;
     using typename CommonMessage<LibObjT>::_ThisCommonMessage;
-
-    using _Stream =
-        typename std::conditional<std::is_const<LibObjT>::value, CommonStream<const bt_stream>,
-                                  CommonStream<bt_stream>>::type;
+    using _Stream = internal::DepStream<LibObjT>;
 
 public:
     using Shared = SharedMessage<CommonDiscardedEventsMessage<LibObjT>, LibObjT>;
@@ -780,13 +796,18 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonDiscardedEventsMessage<LibObjT>&
+    CommonDiscardedEventsMessage<LibObjT>
     operator=(const CommonDiscardedEventsMessage<OtherLibObjT> val) noexcept
     {
         _ThisCommonMessage::operator=(val);
         return *this;
     }
 
+    CommonDiscardedEventsMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonDiscardedEventsMessage<const bt_message> {*this};
+    }
+
     _Stream stream() const noexcept
     {
         return _Stream {
@@ -812,12 +833,13 @@ public:
 
     void count(const std::uint64_t count) const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstDiscardedEventsMessage`.");
 
         bt_message_discarded_events_set_count(this->libObjPtr(), count);
     }
 
-    nonstd::optional<std::uint64_t> count() const noexcept
+    bt2s::optional<std::uint64_t> count() const noexcept
     {
         std::uint64_t count;
         const auto avail = bt_message_discarded_events_get_count(this->libObjPtr(), &count);
@@ -826,7 +848,7 @@ public:
             return count;
         }
 
-        return nonstd::nullopt;
+        return bt2s::nullopt;
     }
 
     Shared shared() const noexcept
@@ -887,10 +909,7 @@ class CommonDiscardedPacketsMessage final : public CommonMessage<LibObjT>
 private:
     using typename CommonMessage<LibObjT>::_LibObjPtr;
     using typename CommonMessage<LibObjT>::_ThisCommonMessage;
-
-    using _Stream =
-        typename std::conditional<std::is_const<LibObjT>::value, CommonStream<const bt_stream>,
-                                  CommonStream<bt_stream>>::type;
+    using _Stream = internal::DepStream<LibObjT>;
 
 public:
     using Shared = SharedMessage<CommonDiscardedPacketsMessage<LibObjT>, LibObjT>;
@@ -908,13 +927,18 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonDiscardedPacketsMessage<LibObjT>&
+    CommonDiscardedPacketsMessage<LibObjT>
     operator=(const CommonDiscardedPacketsMessage<OtherLibObjT> val) noexcept
     {
         _ThisCommonMessage::operator=(val);
         return *this;
     }
 
+    CommonDiscardedPacketsMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonDiscardedPacketsMessage<const bt_message> {*this};
+    }
+
     _Stream stream() const noexcept
     {
         return _Stream {
@@ -940,12 +964,13 @@ public:
 
     void count(const std::uint64_t count) const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstDiscardedPacketsMessage`.");
 
         bt_message_discarded_packets_set_count(this->libObjPtr(), count);
     }
 
-    nonstd::optional<std::uint64_t> count() const noexcept
+    bt2s::optional<std::uint64_t> count() const noexcept
     {
         std::uint64_t count;
         const auto avail = bt_message_discarded_packets_get_count(this->libObjPtr(), &count);
@@ -954,7 +979,7 @@ public:
             return count;
         }
 
-        return nonstd::nullopt;
+        return bt2s::nullopt;
     }
 
     Shared shared() const noexcept
@@ -1010,13 +1035,18 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonMessageIteratorInactivityMessage<LibObjT>&
+    CommonMessageIteratorInactivityMessage<LibObjT>
     operator=(const CommonMessageIteratorInactivityMessage<OtherLibObjT> val) noexcept
     {
         _ThisCommonMessage::operator=(val);
         return *this;
     }
 
+    CommonMessageIteratorInactivityMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonMessageIteratorInactivityMessage<const bt_message> {*this};
+    }
+
     ConstClockSnapshot clockSnapshot() const noexcept
     {
         const auto libObjPtr =
This page took 0.029135 seconds and 4 git commands to generate.