Add C++ interface for the libbabeltrace2 `bt_clock_class` API
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 17 Dec 2020 04:33:50 +0000 (23:33 -0500)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 28 Jan 2022 16:22:26 +0000 (11:22 -0500)
This patch adds C++ wrappers for Babeltrace 2 clock class objects.

The two new available types are `bt2::ClockClass` and
`bt2::ConstClockClass`.

This new template class follows the approach of other wrappers in
`src/cpp-common/bt2`.

Because `bt2::ClockClass::userAttributes()` needs to access the
libbabeltrace2 pointer of the map value, `bt2::CommonValue` makes
`bt2::ClockClass` a friend.

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

src/cpp-common/bt2/clock-class.hpp [new file with mode: 0644]
src/cpp-common/bt2/value.hpp

diff --git a/src/cpp-common/bt2/clock-class.hpp b/src/cpp-common/bt2/clock-class.hpp
new file mode 100644 (file)
index 0000000..e552209
--- /dev/null
@@ -0,0 +1,290 @@
+/*
+ * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP
+#define BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP
+
+#include <type_traits>
+#include <cstdint>
+#include <string>
+#include <babeltrace2/babeltrace.h>
+
+#include "internal/borrowed-obj.hpp"
+#include "internal/shared-obj.hpp"
+#include "cpp-common/optional.hpp"
+#include "cpp-common/string_view.hpp"
+#include "cpp-common/uuid-view.hpp"
+#include "lib-error.hpp"
+#include "value.hpp"
+
+namespace bt2 {
+
+namespace internal {
+
+struct ClockClassRefFuncs final
+{
+    static void get(const bt_clock_class * const libObjPtr)
+    {
+        bt_clock_class_get_ref(libObjPtr);
+    }
+
+    static void put(const bt_clock_class * const libObjPtr)
+    {
+        bt_clock_class_put_ref(libObjPtr);
+    }
+};
+
+template <typename LibObjT>
+struct CommonClockClassSpec;
+
+// Functions specific to mutable clock classes
+template <>
+struct CommonClockClassSpec<bt_clock_class> final
+{
+    static bt_value *userAttributes(bt_clock_class * const libObjPtr) noexcept
+    {
+        return bt_clock_class_borrow_user_attributes(libObjPtr);
+    }
+};
+
+// Functions specific to constant clock classes
+template <>
+struct CommonClockClassSpec<const bt_clock_class> final
+{
+    static const bt_value *userAttributes(const bt_clock_class * const libObjPtr) noexcept
+    {
+        return bt_clock_class_borrow_user_attributes_const(libObjPtr);
+    }
+};
+
+} // namespace internal
+
+class ClockClassOffset final
+{
+public:
+    explicit ClockClassOffset(const std::int64_t seconds, const std::uint64_t cycles) :
+        _mSeconds {seconds}, _mCycles {cycles}
+    {
+    }
+
+    ClockClassOffset(const ClockClassOffset&) noexcept = default;
+    ClockClassOffset& operator=(const ClockClassOffset&) noexcept = default;
+
+    std::int64_t seconds() const noexcept
+    {
+        return _mSeconds;
+    }
+
+    std::uint64_t cycles() const noexcept
+    {
+        return _mCycles;
+    }
+
+private:
+    std::int64_t _mSeconds;
+    std::uint64_t _mCycles;
+};
+
+template <typename LibObjT>
+class CommonClockClass final : public internal::BorrowedObj<LibObjT>
+{
+private:
+    using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
+    using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
+    using _ThisCommonClockClass = CommonClockClass<LibObjT>;
+
+public:
+    using Shared =
+        internal::SharedObj<_ThisCommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
+
+    using UserAttributes =
+        typename std::conditional<std::is_const<LibObjT>::value, ConstMapValue, MapValue>::type;
+
+    explicit CommonClockClass(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
+    {
+    }
+
+    template <typename OtherLibObjT>
+    CommonClockClass(const CommonClockClass<OtherLibObjT>& clkClass) noexcept :
+        _ThisBorrowedObj {clkClass}
+    {
+    }
+
+    template <typename OtherLibObjT>
+    _ThisCommonClockClass& operator=(const CommonClockClass<OtherLibObjT>& clkClass) noexcept
+    {
+        _ThisBorrowedObj::operator=(clkClass);
+        return *this;
+    }
+
+    void frequency(const std::uint64_t frequency) noexcept
+    {
+        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+
+        bt_clock_class_set_frequency(this->_libObjPtr(), frequency);
+    }
+
+    std::uint64_t frequency() const noexcept
+    {
+        return bt_clock_class_get_frequency(this->_libObjPtr());
+    }
+
+    void offset(const ClockClassOffset& offset) noexcept
+    {
+        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+
+        bt_clock_class_set_offset(this->_libObjPtr(), offset.seconds(), offset.cycles());
+    }
+
+    ClockClassOffset offset() const noexcept
+    {
+        std::int64_t seconds;
+        std::uint64_t cycles;
+
+        bt_clock_class_get_offset(this->_libObjPtr(), &seconds, &cycles);
+        return ClockClassOffset {seconds, cycles};
+    }
+
+    void precision(const std::uint64_t precision) noexcept
+    {
+        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+
+        bt_clock_class_set_precision(this->_libObjPtr(), precision);
+    }
+
+    std::uint64_t precision() const noexcept
+    {
+        return bt_clock_class_get_precision(this->_libObjPtr());
+    }
+
+    void originIsUnixEpoch(const bool originIsUnixEpoch) noexcept
+    {
+        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+
+        bt_clock_class_set_origin_is_unix_epoch(this->_libObjPtr(),
+                                                static_cast<bt_bool>(originIsUnixEpoch));
+    }
+
+    bool originIsUnixEpoch() const noexcept
+    {
+        return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->_libObjPtr()));
+    }
+
+    void name(const char * const name)
+    {
+        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+
+        const auto status = bt_clock_class_set_name(this->_libObjPtr(), name);
+
+        if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
+            throw LibMemoryError {};
+        }
+    }
+
+    void name(const std::string& name)
+    {
+        this->name(name.data());
+    }
+
+    nonstd::optional<bpstd::string_view> name() const noexcept
+    {
+        const auto name = bt_clock_class_get_name(this->_libObjPtr());
+
+        if (name) {
+            return name;
+        }
+
+        return nonstd::nullopt;
+    }
+
+    void description(const char * const description)
+    {
+        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+
+        const auto status = bt_clock_class_set_description(this->_libObjPtr(), description);
+
+        if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
+            throw LibMemoryError {};
+        }
+    }
+
+    void description(const std::string& description)
+    {
+        this->description(description.data());
+    }
+
+    nonstd::optional<bpstd::string_view> description() const noexcept
+    {
+        const auto description = bt_clock_class_get_description(this->_libObjPtr());
+
+        if (description) {
+            return description;
+        }
+
+        return nonstd::nullopt;
+    }
+
+    void uuid(const std::uint8_t * const uuid) noexcept
+    {
+        bt_clock_class_set_uuid(this->_libObjPtr(), uuid);
+    }
+
+    nonstd::optional<bt2_common::UuidView> uuid() const noexcept
+    {
+        const auto uuid = bt_clock_class_get_uuid(this->_libObjPtr());
+
+        if (uuid) {
+            return bt2_common::UuidView {uuid};
+        }
+
+        return nonstd::nullopt;
+    }
+
+    template <typename LibValT>
+    void userAttributes(const CommonMapValue<LibValT>& userAttrs)
+    {
+        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+
+        bt_clock_class_set_user_attributes(this->_libObjPtr(), userAttrs._libObjPtr());
+    }
+
+    ConstMapValue userAttributes() const noexcept
+    {
+        return ConstMapValue {internal::CommonClockClassSpec<const bt_clock_class>::userAttributes(
+            this->_libObjPtr())};
+    }
+
+    UserAttributes userAttributes() noexcept
+    {
+        return UserAttributes {
+            internal::CommonClockClassSpec<LibObjT>::userAttributes(this->_libObjPtr())};
+    }
+
+    std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
+    {
+        std::int64_t nsFromOrigin;
+        const auto status =
+            bt_clock_class_cycles_to_ns_from_origin(this->_libObjPtr(), value, &nsFromOrigin);
+
+        if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
+            throw LibOverflowError {};
+        }
+
+        return nsFromOrigin;
+    }
+
+    Shared shared() const noexcept
+    {
+        return Shared {*this};
+    }
+};
+
+using ClockClass = CommonClockClass<bt_clock_class>;
+using ConstClockClass = CommonClockClass<const bt_clock_class>;
+
+} // namespace bt2
+
+#endif // BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP
index 314111836c96157dcf8acc4a037bd3b112e2397d..2c4d4dabfa463ce1d931f9e721478a351afe003b 100644 (file)
@@ -79,6 +79,9 @@ enum class ValueType
     MAP = BT_VALUE_TYPE_MAP,
 };
 
     MAP = BT_VALUE_TYPE_MAP,
 };
 
+template <typename LibObjT>
+class CommonClockClass;
+
 template <typename LibObjT>
 class CommonValue : public internal::BorrowedObj<LibObjT>
 {
 template <typename LibObjT>
 class CommonValue : public internal::BorrowedObj<LibObjT>
 {
@@ -88,6 +91,9 @@ class CommonValue : public internal::BorrowedObj<LibObjT>
     // Allow insert() to call `val._libObjPtr()`
     friend class CommonMapValue<bt_value>;
 
     // Allow insert() to call `val._libObjPtr()`
     friend class CommonMapValue<bt_value>;
 
+    // Allow userAttributes() to call `val._libObjPtr()`
+    friend class CommonClockClass<bt_clock_class>;
+
     // Allow operator==() to call `other._libObjPtr()`
     friend class CommonValue<bt_value>;
     friend class CommonValue<const bt_value>;
     // Allow operator==() to call `other._libObjPtr()`
     friend class CommonValue<bt_value>;
     friend class CommonValue<const bt_value>;
This page took 0.027872 seconds and 4 git commands to generate.