cpp-common/bt2: use `bt2::OptionalBorrowedObject` where possible
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Sun, 26 Nov 2023 01:04:47 +0000 (20:04 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Thu, 14 Dec 2023 15:57:04 +0000 (10:57 -0500)
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Iba2056e520bbe4ad4843a51e51f99173e4929c4f
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11439
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

src/cpp-common/bt2/field-class.hpp
src/cpp-common/bt2/field.hpp
src/cpp-common/bt2/message.hpp
src/cpp-common/bt2/shared-object.hpp
src/cpp-common/bt2/trace-ir.hpp
src/cpp-common/bt2/value.hpp

index 2cef729057d2621ba0e6b7ffae9a3f27f6c448d1..fe4cea469ffd33da534558f649fcbf725267f0a2 100644 (file)
@@ -21,6 +21,7 @@
 #include "field-path.hpp"
 #include "integer-range-set.hpp"
 #include "internal/utils.hpp"
+#include "optional-borrowed-object.hpp"
 #include "shared-object.hpp"
 #include "value.hpp"
 
@@ -851,19 +852,13 @@ public:
             this->libObjPtr(), index)};
     }
 
-    bt2s::optional<Mapping> operator[](const char * const label) const noexcept
+    OptionalBorrowedObject<Mapping> operator[](const char * const label) const noexcept
     {
-        const auto libObjPtr = internal::CommonEnumerationFieldClassSpec<MappingT>::mappingByLabel(
+        return internal::CommonEnumerationFieldClassSpec<MappingT>::mappingByLabel(
             this->libObjPtr(), label);
-
-        if (libObjPtr) {
-            return Mapping {libObjPtr};
-        }
-
-        return bt2s::nullopt;
     }
 
-    bt2s::optional<Mapping> operator[](const std::string& label) const noexcept
+    OptionalBorrowedObject<Mapping> operator[](const std::string& label) const noexcept
     {
         return (*this)[label.data()];
     }
@@ -1189,19 +1184,13 @@ public:
             this->libObjPtr(), index)};
     }
 
-    bt2s::optional<Member> operator[](const char * const name) const noexcept
+    OptionalBorrowedObject<Member> operator[](const char * const name) const noexcept
     {
-        const auto libObjPtr =
-            internal::CommonStructureFieldClassSpec<LibObjT>::memberByName(this->libObjPtr(), name);
-
-        if (libObjPtr) {
-            return Member {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return internal::CommonStructureFieldClassSpec<LibObjT>::memberByName(this->libObjPtr(),
+                                                                              name);
     }
 
-    bt2s::optional<Member> operator[](const std::string& name) const noexcept
+    OptionalBorrowedObject<Member> operator[](const std::string& name) const noexcept
     {
         return (*this)[name.data()];
     }
@@ -2186,19 +2175,13 @@ public:
             this->libObjPtr(), index)};
     }
 
-    bt2s::optional<Option> operator[](const char * const name) const noexcept
+    OptionalBorrowedObject<Option> operator[](const char * const name) const noexcept
     {
-        const auto libObjPtr =
-            internal::CommonVariantFieldClassSpec<LibObjT>::optionByName(this->libObjPtr(), name);
-
-        if (libObjPtr) {
-            return Option {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return internal::CommonVariantFieldClassSpec<LibObjT>::optionByName(this->libObjPtr(),
+                                                                            name);
     }
 
-    bt2s::optional<Option> operator[](const std::string& name) const noexcept
+    OptionalBorrowedObject<Option> operator[](const std::string& name) const noexcept
     {
         return (*this)[name.data()];
     }
@@ -2482,18 +2465,12 @@ public:
         return Option {_Spec::optionByIndex(this->libObjPtr(), index)};
     }
 
-    bt2s::optional<Option> operator[](const char * const name) const noexcept
+    OptionalBorrowedObject<Option> operator[](const char * const name) const noexcept
     {
-        const auto libObjPtr = _Spec::optionByName(this->libObjPtr(), name);
-
-        if (libObjPtr) {
-            return Option {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::optionByName(this->libObjPtr(), name);
     }
 
-    bt2s::optional<Option> operator[](const std::string& name) const noexcept
+    OptionalBorrowedObject<Option> operator[](const std::string& name) const noexcept
     {
         return (*this)[name.data()];
     }
index cbc5fedec506316861697f31638d6850cfa94d6b..2c40ae3307a46a3420faa29e20c184606eb32e0f 100644 (file)
 #include <babeltrace2/babeltrace.h>
 
 #include "common/assert.h"
-#include "cpp-common/bt2s/optional.hpp"
 
 #include "borrowed-object.hpp"
 #include "field-class.hpp"
 #include "internal/utils.hpp"
+#include "optional-borrowed-object.hpp"
 #include "raw-value-proxy.hpp"
 
 namespace bt2 {
@@ -1117,18 +1117,12 @@ public:
         return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)};
     }
 
-    bt2s::optional<CommonField<LibObjT>> operator[](const char * const name) const noexcept
+    OptionalBorrowedObject<CommonField<LibObjT>> operator[](const char * const name) const noexcept
     {
-        const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name);
-
-        if (libObjPtr) {
-            return CommonField<LibObjT> {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::memberFieldByName(this->libObjPtr(), name);
     }
 
-    bt2s::optional<CommonField<LibObjT>> operator[](const std::string& name) const noexcept
+    OptionalBorrowedObject<CommonField<LibObjT>> operator[](const std::string& name) const noexcept
     {
         return (*this)[name.data()];
     }
@@ -1406,15 +1400,9 @@ public:
         return this->field().has_value();
     }
 
-    bt2s::optional<CommonField<LibObjT>> field() const noexcept
+    OptionalBorrowedObject<CommonField<LibObjT>> field() const noexcept
     {
-        const auto libObjPtr = _Spec::field(this->libObjPtr());
-
-        if (libObjPtr) {
-            return CommonField<LibObjT> {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::field(this->libObjPtr());
     }
 };
 
index fd2a8d2bb087a66c557726b86667e6e43358a397..30cf5b31174c3ff9a7cf2f6afd04a91726826166 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "borrowed-object.hpp"
 #include "internal/utils.hpp"
+#include "optional-borrowed-object.hpp"
 #include "shared-object.hpp"
 
 namespace bt2 {
@@ -274,17 +275,17 @@ public:
         bt_message_stream_beginning_set_default_clock_snapshot(this->libObjPtr(), val);
     }
 
-    bt2s::optional<ConstClockSnapshot> defaultClockSnapshot() const noexcept
+    OptionalBorrowedObject<ConstClockSnapshot> defaultClockSnapshot() const noexcept
     {
         const bt_clock_snapshot *libObjPtr;
         const auto state = bt_message_stream_beginning_borrow_default_clock_snapshot_const(
             this->libObjPtr(), &libObjPtr);
 
         if (state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
-            return ConstClockSnapshot {libObjPtr};
+            return libObjPtr;
         }
 
-        return bt2s::nullopt;
+        return {};
     }
 
     Shared shared() const noexcept
@@ -388,17 +389,17 @@ public:
         bt_message_stream_end_set_default_clock_snapshot(this->libObjPtr(), val);
     }
 
-    bt2s::optional<ConstClockSnapshot> defaultClockSnapshot() const noexcept
+    OptionalBorrowedObject<ConstClockSnapshot> defaultClockSnapshot() const noexcept
     {
         const bt_clock_snapshot *libObjPtr;
         const auto state = bt_message_stream_end_borrow_default_clock_snapshot_const(
             this->libObjPtr(), &libObjPtr);
 
         if (state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
-            return ConstClockSnapshot {libObjPtr};
+            return libObjPtr;
         }
 
-        return bt2s::nullopt;
+        return {};
     }
 
     Shared shared() const noexcept
@@ -711,14 +712,9 @@ public:
         return _Event {internal::CommonEventMessageSpec<LibObjT>::event(this->libObjPtr())};
     }
 
-    bt2s::optional<ConstClockClass> streamClassDefaultClockClass() const noexcept
+    OptionalBorrowedObject<ConstClockClass> streamClassDefaultClockClass() const noexcept
     {
-        if (const auto libClkClsPtr =
-                bt_message_event_borrow_stream_class_default_clock_class_const(this->libObjPtr())) {
-            return ConstClockClass {libClkClsPtr};
-        }
-
-        return bt2s::nullopt;
+        return bt_message_event_borrow_stream_class_default_clock_class_const(this->libObjPtr());
     }
 
     ConstClockSnapshot defaultClockSnapshot() const noexcept
index 3b5ccfa259d93996a7c1b925385608d8180d1146..406172d80577b929ea0a8745e6544425fd6a7791 100644 (file)
@@ -7,8 +7,11 @@
 #ifndef BABELTRACE_CPP_COMMON_BT2_SHARED_OBJECT_HPP
 #define BABELTRACE_CPP_COMMON_BT2_SHARED_OBJECT_HPP
 
+#include <utility>
+
 #include "common/assert.h"
-#include "cpp-common/bt2s/optional.hpp"
+
+#include "optional-borrowed-object.hpp"
 
 namespace bt2 {
 
@@ -230,33 +233,21 @@ public:
         this->_putRef();
     }
 
-    ObjT& operator*() noexcept
-    {
-        BT_ASSERT_DBG(_mObj);
-        return *_mObj;
-    }
-
-    const ObjT& operator*() const noexcept
+    ObjT operator*() const noexcept
     {
         BT_ASSERT_DBG(_mObj);
         return *_mObj;
     }
 
-    ObjT *operator->() noexcept
+    BorrowedObjectProxy<ObjT> operator->() const noexcept
     {
         BT_ASSERT_DBG(_mObj);
-        return &*_mObj;
-    }
-
-    const ObjT *operator->() const noexcept
-    {
-        BT_ASSERT_DBG(_mObj);
-        return &*_mObj;
+        return _mObj.operator->();
     }
 
     operator bool() const noexcept
     {
-        return _mObj.has_value();
+        return _mObj.hasObject();
     }
 
     /*
@@ -304,9 +295,7 @@ private:
      */
     void _getRef() const noexcept
     {
-        if (_mObj) {
-            RefFuncsT::get(_mObj->libObjPtr());
-        }
+        RefFuncsT::get(_mObj.libObjPtr());
     }
 
     /*
@@ -315,12 +304,10 @@ private:
      */
     void _putRef() const noexcept
     {
-        if (_mObj) {
-            RefFuncsT::put(_mObj->libObjPtr());
-        }
+        RefFuncsT::put(_mObj.libObjPtr());
     }
 
-    bt2s::optional<ObjT> _mObj;
+    OptionalBorrowedObject<ObjT> _mObj;
 };
 
 } /* namespace bt2 */
index c4a08f8b5c90b7db593f9e5a48dba8a05bb66cd9..06d4ca53478bdaeca2b204e31ac21693d7e163aa 100644 (file)
@@ -19,6 +19,7 @@
 #include "field-class.hpp"
 #include "field.hpp"
 #include "internal/utils.hpp"
+#include "optional-borrowed-object.hpp"
 #include "shared-object.hpp"
 #include "value.hpp"
 
@@ -164,39 +165,21 @@ public:
 
     Class cls() const noexcept;
     _Stream stream() const noexcept;
-    bt2s::optional<_Packet> packet() const noexcept;
+    OptionalBorrowedObject<_Packet> packet() const noexcept;
 
-    bt2s::optional<_StructureField> payloadField() const noexcept
+    OptionalBorrowedObject<_StructureField> payloadField() const noexcept
     {
-        const auto libObjPtr = _Spec::payloadField(this->libObjPtr());
-
-        if (libObjPtr) {
-            return _StructureField {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::payloadField(this->libObjPtr());
     }
 
-    bt2s::optional<_StructureField> specificContextField() const noexcept
+    OptionalBorrowedObject<_StructureField> specificContextField() const noexcept
     {
-        const auto libObjPtr = _Spec::specificContextField(this->libObjPtr());
-
-        if (libObjPtr) {
-            return _StructureField {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::specificContextField(this->libObjPtr());
     }
 
-    bt2s::optional<_StructureField> commonContextField() const noexcept
+    OptionalBorrowedObject<_StructureField> commonContextField() const noexcept
     {
-        const auto libObjPtr = _Spec::commonContextField(this->libObjPtr());
-
-        if (libObjPtr) {
-            return _StructureField {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::commonContextField(this->libObjPtr());
     }
 };
 
@@ -305,15 +288,9 @@ public:
 
     _Stream stream() const noexcept;
 
-    bt2s::optional<_StructureField> contextField() const noexcept
+    OptionalBorrowedObject<_StructureField> contextField() const noexcept
     {
-        const auto libObjPtr = _Spec::contextField(this->libObjPtr());
-
-        if (libObjPtr) {
-            return _StructureField {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::contextField(this->libObjPtr());
     }
 
     Shared shared() const noexcept
@@ -346,15 +323,10 @@ struct TypeDescr<ConstPacket> : public PacketTypeDescr
 } /* namespace internal */
 
 template <typename LibObjT>
-bt2s::optional<typename CommonEvent<LibObjT>::_Packet> CommonEvent<LibObjT>::packet() const noexcept
+OptionalBorrowedObject<typename CommonEvent<LibObjT>::_Packet>
+CommonEvent<LibObjT>::packet() const noexcept
 {
-    const auto libObjPtr = _Spec::packet(this->libObjPtr());
-
-    if (libObjPtr) {
-        return _Packet {libObjPtr};
-    }
-
-    return bt2s::nullopt;
+    return _Spec::packet(this->libObjPtr());
 }
 
 namespace internal {
@@ -711,15 +683,9 @@ public:
         return _Stream {_Spec::streamByIndex(this->libObjPtr(), index)};
     }
 
-    bt2s::optional<_Stream> streamById(const std::uint64_t id) const noexcept
+    OptionalBorrowedObject<_Stream> streamById(const std::uint64_t id) const noexcept
     {
-        const auto libObjPtr = _Spec::streamById(this->libObjPtr(), id);
-
-        if (libObjPtr) {
-            return _Stream {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::streamById(this->libObjPtr(), id);
     }
 
     void environmentEntry(const char * const name, const std::int64_t val) const
@@ -779,19 +745,12 @@ public:
         return ConstEnvironmentEntry {name, ConstValue {libObjPtr}};
     }
 
-    bt2s::optional<ConstValue> environmentEntry(const char * const name) const noexcept
+    OptionalBorrowedObject<ConstValue> environmentEntry(const char * const name) const noexcept
     {
-        const auto libObjPtr =
-            bt_trace_borrow_environment_entry_value_by_name_const(this->libObjPtr(), name);
-
-        if (libObjPtr) {
-            return ConstValue {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return bt_trace_borrow_environment_entry_value_by_name_const(this->libObjPtr(), name);
     }
 
-    bt2s::optional<ConstValue> environmentEntry(const std::string& name) const noexcept
+    OptionalBorrowedObject<ConstValue> environmentEntry(const std::string& name) const noexcept
     {
         return this->environmentEntry(name.data());
     }
@@ -1056,15 +1015,9 @@ public:
         }
     }
 
-    bt2s::optional<_StructureFieldClass> payloadFieldClass() const noexcept
+    OptionalBorrowedObject<_StructureFieldClass> payloadFieldClass() const noexcept
     {
-        const auto libObjPtr = _Spec::payloadFieldClass(this->libObjPtr());
-
-        if (libObjPtr) {
-            return _StructureFieldClass {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::payloadFieldClass(this->libObjPtr());
     }
 
     void specificContextFieldClass(const StructureFieldClass fc) const
@@ -1079,15 +1032,9 @@ public:
         }
     }
 
-    bt2s::optional<_StructureFieldClass> specificContextFieldClass() const noexcept
+    OptionalBorrowedObject<_StructureFieldClass> specificContextFieldClass() const noexcept
     {
-        const auto libObjPtr = _Spec::specificContextFieldClass(this->libObjPtr());
-
-        if (libObjPtr) {
-            return _StructureFieldClass {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::specificContextFieldClass(this->libObjPtr());
     }
 
     template <typename LibValT>
@@ -1473,15 +1420,9 @@ public:
         BT_ASSERT(status == BT_STREAM_CLASS_SET_DEFAULT_CLOCK_CLASS_STATUS_OK);
     }
 
-    bt2s::optional<_ClockClass> defaultClockClass() const noexcept
+    OptionalBorrowedObject<_ClockClass> defaultClockClass() const noexcept
     {
-        const auto libObjPtr = _Spec::defaultClockClass(this->libObjPtr());
-
-        if (libObjPtr) {
-            return _ClockClass {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::defaultClockClass(this->libObjPtr());
     }
 
     std::uint64_t length() const noexcept
@@ -1494,15 +1435,9 @@ public:
         return _EventClass {_Spec::eventClassByIndex(this->libObjPtr(), index)};
     }
 
-    bt2s::optional<_EventClass> eventClassById(const std::uint64_t id) const noexcept
+    OptionalBorrowedObject<_EventClass> eventClassById(const std::uint64_t id) const noexcept
     {
-        const auto libObjPtr = _Spec::eventClassById(this->libObjPtr(), id);
-
-        if (libObjPtr) {
-            return _EventClass {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::eventClassById(this->libObjPtr(), id);
     }
 
     void packetContextFieldClass(const StructureFieldClass fc) const
@@ -1518,15 +1453,9 @@ public:
         }
     }
 
-    bt2s::optional<_StructureFieldClass> packetContextFieldClass() const noexcept
+    OptionalBorrowedObject<_StructureFieldClass> packetContextFieldClass() const noexcept
     {
-        const auto libObjPtr = _Spec::packetContextFieldClass(this->libObjPtr());
-
-        if (libObjPtr) {
-            return _StructureFieldClass {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::packetContextFieldClass(this->libObjPtr());
     }
 
     void eventCommonContextFieldClass(const StructureFieldClass fc) const
@@ -1542,15 +1471,9 @@ public:
         }
     }
 
-    bt2s::optional<_StructureFieldClass> eventCommonContextFieldClass() const noexcept
+    OptionalBorrowedObject<_StructureFieldClass> eventCommonContextFieldClass() const noexcept
     {
-        const auto libObjPtr = _Spec::eventCommonContextFieldClass(this->libObjPtr());
-
-        if (libObjPtr) {
-            return _StructureFieldClass {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::eventCommonContextFieldClass(this->libObjPtr());
     }
 
     template <typename LibValT>
@@ -1981,15 +1904,9 @@ public:
         return _StreamClass {_Spec::streamClassByIndex(this->libObjPtr(), index)};
     }
 
-    bt2s::optional<_StreamClass> streamClassById(const std::uint64_t id) const noexcept
+    OptionalBorrowedObject<_StreamClass> streamClassById(const std::uint64_t id) const noexcept
     {
-        const auto libObjPtr = _Spec::streamClassById(this->libObjPtr(), id);
-
-        if (libObjPtr) {
-            return _StreamClass {libObjPtr};
-        }
-
-        return bt2s::nullopt;
+        return _Spec::streamClassById(this->libObjPtr(), id);
     }
 
     template <typename LibValT>
index 13d30a98c450bbadf1f8f60d0ec43670e27294c0..4dc4019a8677ffab3d36f35043217331eddbcc05 100644 (file)
 
 #include "common/assert.h"
 #include "common/common.h"
-#include "cpp-common/bt2s/optional.hpp"
 
 #include "borrowed-object-iterator.hpp"
 #include "borrowed-object.hpp"
 #include "exc.hpp"
 #include "internal/utils.hpp"
+#include "optional-borrowed-object.hpp"
 #include "raw-value-proxy.hpp"
 #include "shared-object.hpp"
 
@@ -241,7 +241,7 @@ public:
     }
 
     template <typename KeyT>
-    bt2s::optional<CommonValue<LibObjT>> operator[](KeyT&& key) const noexcept
+    OptionalBorrowedObject<CommonValue<LibObjT>> operator[](KeyT&& key) const noexcept
     {
         return this->asMap()[std::forward<KeyT>(key)];
     }
@@ -1285,19 +1285,12 @@ public:
         return this->length() == 0;
     }
 
-    bt2s::optional<CommonValue<LibObjT>> operator[](const char * const key) const noexcept
+    OptionalBorrowedObject<CommonValue<LibObjT>> operator[](const char * const key) const noexcept
     {
-        const auto libObjPtr =
-            internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key);
-
-        if (!libObjPtr) {
-            return bt2s::nullopt;
-        }
-
-        return CommonValue<LibObjT> {libObjPtr};
+        return internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key);
     }
 
-    bt2s::optional<CommonValue<LibObjT>> operator[](const std::string& key) const noexcept
+    OptionalBorrowedObject<CommonValue<LibObjT>> operator[](const std::string& key) const noexcept
     {
         return (*this)[key.data()];
     }
This page took 0.036322 seconds and 4 git commands to generate.