cpp-common/bt2/clock-class.hpp: adapt to latest libbabeltrace2 API
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 17 Apr 2024 19:28:23 +0000 (15:28 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 4 Sep 2024 19:05:14 +0000 (15:05 -0400)
Read anything related to the origin with the new origin() method which
returns a clock origin view from which you may get the namespace, name,
UID, and whether or not it's unknown or the Unix epoch.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ib44547fd60580ab9aea3c0cb3492542749d3a5da
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12729

src/clock-correlation-validator/clock-correlation-validator.cpp
src/cpp-common/bt2/clock-class.hpp

index 4bb1a725bcddf4a4c85d4f008e9585301bb200f5..b5c9c21b74431eb3098ac52c4f244b064c36385e 100644 (file)
@@ -41,7 +41,7 @@ void ClockCorrelationValidator::_validate(const bt2::ConstMessage msg)
          */
         if (!clockCls) {
             _mExpectation = PropsExpectation::None;
-        } else if (clockCls->originIsUnixEpoch()) {
+        } else if (clockCls->origin().isUnixEpoch()) {
             _mExpectation = PropsExpectation::OriginUnix;
         } else if (const auto uuid = clockCls->uuid()) {
             _mExpectation = PropsExpectation::OriginOtherUuid;
@@ -73,7 +73,7 @@ void ClockCorrelationValidator::_validate(const bt2::ConstMessage msg)
                                          streamCls};
         }
 
-        if (!clockCls->originIsUnixEpoch()) {
+        if (!clockCls->origin().isUnixEpoch()) {
             throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginUnixGotOther,
                                          bt2s::nullopt,
                                          *clockCls,
@@ -93,7 +93,7 @@ void ClockCorrelationValidator::_validate(const bt2::ConstMessage msg)
                                          streamCls};
         }
 
-        if (clockCls->originIsUnixEpoch()) {
+        if (clockCls->origin().isUnixEpoch()) {
             throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginUuidGotUnix,
                                          bt2s::nullopt,
                                          *clockCls,
index 5b80890d2e7deaf936bf64872afa35f54e04cbf8..76af9e4c50156ee060f6f7365df5adb24bae8391 100644 (file)
@@ -66,7 +66,7 @@ struct CommonClockClassSpec<const bt_clock_class> final
 class ClockOffset final
 {
 public:
-    explicit ClockOffset(const std::int64_t seconds, const std::uint64_t cycles) :
+    explicit ClockOffset(const std::int64_t seconds, const std::uint64_t cycles) noexcept :
         _mSeconds {seconds}, _mCycles {cycles}
     {
     }
@@ -86,6 +86,8 @@ private:
     std::uint64_t _mCycles;
 };
 
+class ClockOriginView;
+
 template <typename LibObjT>
 class CommonClockClass final : public BorrowedObject<LibObjT>
 {
@@ -158,9 +160,36 @@ public:
         return *this;
     }
 
-    std::uint64_t precision() const noexcept
+    bt2s::optional<std::uint64_t> precision() const noexcept
+    {
+        std::uint64_t prec;
+
+        if (bt_clock_class_get_opt_precision(this->libObjPtr(), &prec) ==
+            BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
+            return bt2s::nullopt;
+        }
+
+        return prec;
+    }
+
+    CommonClockClass accuracy(const std::uint64_t accuracy) const noexcept
     {
-        return bt_clock_class_get_precision(this->libObjPtr());
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
+
+        bt_clock_class_set_accuracy(this->libObjPtr(), accuracy);
+        return *this;
+    }
+
+    bt2s::optional<std::uint64_t> accuracy() const noexcept
+    {
+        std::uint64_t accuracy;
+
+        if (bt_clock_class_get_accuracy(this->libObjPtr(), &accuracy) ==
+            BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
+            return bt2s::nullopt;
+        }
+
+        return accuracy;
     }
 
     CommonClockClass originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept
@@ -172,9 +201,52 @@ public:
         return *this;
     }
 
-    bool originIsUnixEpoch() const noexcept
+    CommonClockClass setOriginIsUnixEpoch() const noexcept
+    {
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
+
+        bt_clock_class_set_origin_unix_epoch(this->libObjPtr());
+        return *this;
+    }
+
+    CommonClockClass setOriginIsUnknown() const noexcept
+    {
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
+
+        bt_clock_class_set_origin_unknown(this->libObjPtr());
+        return *this;
+    }
+
+    CommonClockClass origin(const bt2c::CStringView nameSpace, const bt2c::CStringView name,
+                            const bt2c::CStringView uid) const
+    {
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
+
+        if (bt_clock_class_set_origin(this->libObjPtr(), nameSpace, name, uid) ==
+            BT_CLOCK_CLASS_SET_ORIGIN_STATUS_MEMORY_ERROR) {
+            throw MemoryError {};
+        }
+
+        return *this;
+    }
+
+    ClockOriginView origin() const noexcept;
+
+    CommonClockClass nameSpace(const bt2c::CStringView nameSpace) const
+    {
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
+
+        if (bt_clock_class_set_namespace(this->libObjPtr(), nameSpace) ==
+            BT_CLOCK_CLASS_SET_NAMESPACE_STATUS_MEMORY_ERROR) {
+            throw MemoryError {};
+        }
+
+        return *this;
+    }
+
+    bt2c::CStringView nameSpace() const noexcept
     {
-        return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
+        return bt_clock_class_get_namespace(this->libObjPtr());
     }
 
     CommonClockClass name(const bt2c::CStringView name) const
@@ -195,6 +267,29 @@ public:
         return bt_clock_class_get_name(this->libObjPtr());
     }
 
+    CommonClockClass uid(const bt2c::CStringView uid) const
+    {
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
+
+        if (bt_clock_class_set_uid(this->libObjPtr(), uid) ==
+            BT_CLOCK_CLASS_SET_UID_STATUS_MEMORY_ERROR) {
+            throw MemoryError {};
+        }
+
+        return *this;
+    }
+
+    bt2c::CStringView uid() const noexcept
+    {
+        return bt_clock_class_get_uid(this->libObjPtr());
+    }
+
+    bool hasSameIdentity(const CommonClockClass<const bt_clock_class> other) const noexcept
+    {
+        return static_cast<bool>(
+            bt_clock_class_has_same_identity(this->libObjPtr(), other.libObjPtr()));
+    }
+
     CommonClockClass description(const bt2c::CStringView description) const
     {
         static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
@@ -288,6 +383,49 @@ struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
 };
 
 } /* namespace internal */
+
+class ClockOriginView final
+{
+public:
+    explicit ClockOriginView(const ConstClockClass clockClass) noexcept : _mClkCls {clockClass}
+    {
+    }
+
+    bt2c::CStringView nameSpace() const noexcept
+    {
+        return bt_clock_class_get_origin_namespace(_mClkCls.libObjPtr());
+    }
+
+    bt2c::CStringView name() const noexcept
+    {
+        return bt_clock_class_get_origin_name(_mClkCls.libObjPtr());
+    }
+
+    bt2c::CStringView uid() const noexcept
+    {
+        return bt_clock_class_get_origin_uid(_mClkCls.libObjPtr());
+    }
+
+    bool isUnknown() const noexcept
+    {
+        return bt_clock_class_origin_is_unknown(_mClkCls.libObjPtr());
+    }
+
+    bool isUnixEpoch() const noexcept
+    {
+        return bt_clock_class_origin_is_unix_epoch(_mClkCls.libObjPtr());
+    }
+
+private:
+    ConstClockClass _mClkCls;
+};
+
+template <typename LibObjT>
+ClockOriginView CommonClockClass<LibObjT>::origin() const noexcept
+{
+    return ClockOriginView {*this};
+}
+
 } /* namespace bt2 */
 
 #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */
This page took 0.027189 seconds and 4 git commands to generate.