2 * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP
11 #include <type_traits>
13 #include <babeltrace2/babeltrace.h>
15 #include "cpp-common/bt2c/c-string-view.hpp"
16 #include "cpp-common/bt2c/uuid.hpp"
17 #include "cpp-common/bt2s/optional.hpp"
19 #include "borrowed-object.hpp"
21 #include "internal/utils.hpp"
22 #include "shared-object.hpp"
28 struct ClockClassRefFuncs final
30 static void get(const bt_clock_class * const libObjPtr) noexcept
32 bt_clock_class_get_ref(libObjPtr);
35 static void put(const bt_clock_class * const libObjPtr) noexcept
37 bt_clock_class_put_ref(libObjPtr);
41 template <typename LibObjT>
42 struct CommonClockClassSpec;
44 /* Functions specific to mutable clock classes */
46 struct CommonClockClassSpec<bt_clock_class> final
48 static bt_value *userAttributes(bt_clock_class * const libObjPtr) noexcept
50 return bt_clock_class_borrow_user_attributes(libObjPtr);
54 /* Functions specific to constant clock classes */
56 struct CommonClockClassSpec<const bt_clock_class> final
58 static const bt_value *userAttributes(const bt_clock_class * const libObjPtr) noexcept
60 return bt_clock_class_borrow_user_attributes_const(libObjPtr);
64 } /* namespace internal */
66 class ClockOffset final
69 explicit ClockOffset(const std::int64_t seconds, const std::uint64_t cycles) :
70 _mSeconds {seconds}, _mCycles {cycles}
74 std::int64_t seconds() const noexcept
79 std::uint64_t cycles() const noexcept
85 std::int64_t _mSeconds;
86 std::uint64_t _mCycles;
89 template <typename LibObjT>
90 class CommonClockClass final : public BorrowedObject<LibObjT>
93 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
96 using typename BorrowedObject<LibObjT>::LibObjPtr;
97 using Shared = SharedObject<CommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
98 using UserAttributes = internal::DepUserAttrs<LibObjT>;
100 explicit CommonClockClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
104 template <typename OtherLibObjT>
105 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
106 _ThisBorrowedObject {clkClass}
110 template <typename OtherLibObjT>
111 CommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
113 _ThisBorrowedObject::operator=(clkClass);
117 CommonClockClass<const bt_clock_class> asConst() const noexcept
119 return CommonClockClass<const bt_clock_class> {*this};
122 CommonClockClass frequency(const std::uint64_t frequency) const noexcept
124 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
126 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
130 std::uint64_t frequency() const noexcept
132 return bt_clock_class_get_frequency(this->libObjPtr());
135 CommonClockClass offsetFromOrigin(const ClockOffset& offsetFromOrigin) const noexcept
137 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
139 bt_clock_class_set_offset(this->libObjPtr(), offsetFromOrigin.seconds(),
140 offsetFromOrigin.cycles());
144 ClockOffset offsetFromOrigin() const noexcept
146 std::int64_t seconds;
147 std::uint64_t cycles;
149 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
150 return ClockOffset {seconds, cycles};
153 CommonClockClass precision(const std::uint64_t precision) const noexcept
155 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
157 bt_clock_class_set_precision(this->libObjPtr(), precision);
161 std::uint64_t precision() const noexcept
163 return bt_clock_class_get_precision(this->libObjPtr());
166 CommonClockClass originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept
168 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
170 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
171 static_cast<bt_bool>(originIsUnixEpoch));
175 bool originIsUnixEpoch() const noexcept
177 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
180 CommonClockClass name(const bt2c::CStringView name) const
182 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
184 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
186 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
187 throw MemoryError {};
193 bt2c::CStringView name() const noexcept
195 return bt_clock_class_get_name(this->libObjPtr());
198 CommonClockClass description(const bt2c::CStringView description) const
200 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
202 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
204 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
205 throw MemoryError {};
211 bt2c::CStringView description() const noexcept
213 return bt_clock_class_get_description(this->libObjPtr());
216 CommonClockClass uuid(const bt2c::UuidView uuid) const noexcept
218 bt_clock_class_set_uuid(this->libObjPtr(), uuid.data());
222 bt2s::optional<bt2c::UuidView> uuid() const noexcept
224 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
227 return bt2c::UuidView {uuid};
230 return bt2s::nullopt;
233 template <typename LibValT>
234 CommonClockClass userAttributes(const CommonMapValue<LibValT> userAttrs) const noexcept
236 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
238 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
242 UserAttributes userAttributes() const noexcept
244 return UserAttributes {
245 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
248 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
250 std::int64_t nsFromOrigin;
252 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
254 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
255 throw OverflowError {};
261 Shared shared() const noexcept
263 return Shared::createWithRef(*this);
267 using ClockClass = CommonClockClass<bt_clock_class>;
268 using ConstClockClass = CommonClockClass<const bt_clock_class>;
272 struct ClockClassTypeDescr
274 using Const = ConstClockClass;
275 using NonConst = ClockClass;
279 struct TypeDescr<ClockClass> : public ClockClassTypeDescr
284 struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
288 } /* namespace internal */
289 } /* namespace bt2 */
291 #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */