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
12 #include <type_traits>
14 #include <babeltrace2/babeltrace.h>
16 #include "cpp-common/bt2c/c-string-view.hpp"
17 #include "cpp-common/bt2c/uuid.hpp"
18 #include "cpp-common/bt2s/optional.hpp"
20 #include "borrowed-object.hpp"
22 #include "internal/utils.hpp"
23 #include "shared-object.hpp"
29 struct ClockClassRefFuncs final
31 static void get(const bt_clock_class * const libObjPtr) noexcept
33 bt_clock_class_get_ref(libObjPtr);
36 static void put(const bt_clock_class * const libObjPtr) noexcept
38 bt_clock_class_put_ref(libObjPtr);
42 template <typename LibObjT>
43 struct CommonClockClassSpec;
45 /* Functions specific to mutable clock classes */
47 struct CommonClockClassSpec<bt_clock_class> final
49 static bt_value *userAttributes(bt_clock_class * const libObjPtr) noexcept
51 return bt_clock_class_borrow_user_attributes(libObjPtr);
55 /* Functions specific to constant clock classes */
57 struct CommonClockClassSpec<const bt_clock_class> final
59 static const bt_value *userAttributes(const bt_clock_class * const libObjPtr) noexcept
61 return bt_clock_class_borrow_user_attributes_const(libObjPtr);
65 } /* namespace internal */
67 class ClockClassOffset final
70 explicit ClockClassOffset(const std::int64_t seconds, const std::uint64_t cycles) :
71 _mSeconds {seconds}, _mCycles {cycles}
75 std::int64_t seconds() const noexcept
80 std::uint64_t cycles() const noexcept
86 std::int64_t _mSeconds;
87 std::uint64_t _mCycles;
90 template <typename LibObjT>
91 class CommonClockClass final : public BorrowedObject<LibObjT>
94 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
97 using typename BorrowedObject<LibObjT>::LibObjPtr;
98 using Shared = SharedObject<CommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
99 using UserAttributes = internal::DepUserAttrs<LibObjT>;
101 explicit CommonClockClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
105 template <typename OtherLibObjT>
106 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
107 _ThisBorrowedObject {clkClass}
111 template <typename OtherLibObjT>
112 CommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
114 _ThisBorrowedObject::operator=(clkClass);
118 CommonClockClass<const bt_clock_class> asConst() const noexcept
120 return CommonClockClass<const bt_clock_class> {*this};
123 void frequency(const std::uint64_t frequency) const noexcept
125 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
127 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 void offset(const ClockClassOffset& offset) const noexcept
137 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
139 bt_clock_class_set_offset(this->libObjPtr(), offset.seconds(), offset.cycles());
142 ClockClassOffset offset() const noexcept
144 std::int64_t seconds;
145 std::uint64_t cycles;
147 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
148 return ClockClassOffset {seconds, cycles};
151 void precision(const std::uint64_t precision) const noexcept
153 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
155 bt_clock_class_set_precision(this->libObjPtr(), precision);
158 std::uint64_t precision() const noexcept
160 return bt_clock_class_get_precision(this->libObjPtr());
163 void originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept
165 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
167 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
168 static_cast<bt_bool>(originIsUnixEpoch));
171 bool originIsUnixEpoch() const noexcept
173 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
176 void name(const bt2c::CStringView name) const
178 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
180 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
182 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
183 throw MemoryError {};
187 bt2c::CStringView name() const noexcept
189 return bt_clock_class_get_name(this->libObjPtr());
192 void description(const bt2c::CStringView description) const
194 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
196 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
198 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
199 throw MemoryError {};
203 bt2c::CStringView description() const noexcept
205 return bt_clock_class_get_description(this->libObjPtr());
208 void uuid(const std::uint8_t * const uuid) const noexcept
210 bt_clock_class_set_uuid(this->libObjPtr(), uuid);
213 bt2s::optional<bt2c::UuidView> uuid() const noexcept
215 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
218 return bt2c::UuidView {uuid};
221 return bt2s::nullopt;
224 template <typename LibValT>
225 void userAttributes(const CommonMapValue<LibValT> userAttrs) const noexcept
227 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
229 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
232 UserAttributes userAttributes() const noexcept
234 return UserAttributes {
235 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
238 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
240 std::int64_t nsFromOrigin;
242 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
244 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
245 throw OverflowError {};
251 Shared shared() const noexcept
253 return Shared::createWithRef(*this);
257 using ClockClass = CommonClockClass<bt_clock_class>;
258 using ConstClockClass = CommonClockClass<const bt_clock_class>;
262 struct ClockClassTypeDescr
264 using Const = ConstClockClass;
265 using NonConst = ClockClass;
269 struct TypeDescr<ClockClass> : public ClockClassTypeDescr
274 struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
278 } /* namespace internal */
279 } /* namespace bt2 */
281 #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */