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/optional.hpp"
17 #include "cpp-common/string_view.hpp"
18 #include "cpp-common/uuid-view.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;
95 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 char * const 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 void name(const std::string& name) const
189 this->name(name.data());
192 nonstd::optional<bpstd::string_view> name() const noexcept
194 const auto name = bt_clock_class_get_name(this->libObjPtr());
200 return nonstd::nullopt;
203 void description(const char * const description) const
205 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
207 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
209 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
210 throw MemoryError {};
214 void description(const std::string& description) const
216 this->description(description.data());
219 nonstd::optional<bpstd::string_view> description() const noexcept
221 const auto description = bt_clock_class_get_description(this->libObjPtr());
227 return nonstd::nullopt;
230 void uuid(const std::uint8_t * const uuid) const noexcept
232 bt_clock_class_set_uuid(this->libObjPtr(), uuid);
235 nonstd::optional<bt2_common::UuidView> uuid() const noexcept
237 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
240 return bt2_common::UuidView {uuid};
243 return nonstd::nullopt;
246 template <typename LibValT>
247 void userAttributes(const CommonMapValue<LibValT> userAttrs) const noexcept
249 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
251 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
254 UserAttributes userAttributes() const noexcept
256 return UserAttributes {
257 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
260 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
262 std::int64_t nsFromOrigin;
264 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
266 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
267 throw OverflowError {};
273 Shared shared() const noexcept
275 return Shared::createWithRef(*this);
279 using ClockClass = CommonClockClass<bt_clock_class>;
280 using ConstClockClass = CommonClockClass<const bt_clock_class>;
284 struct ClockClassTypeDescr
286 using Const = ConstClockClass;
287 using NonConst = ClockClass;
291 struct TypeDescr<ClockClass> : public ClockClassTypeDescr
296 struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
300 } /* namespace internal */
301 } /* namespace bt2 */
303 #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */