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"
30 struct ClockClassRefFuncs final
32 static void get(const bt_clock_class * const libObjPtr)
34 bt_clock_class_get_ref(libObjPtr);
37 static void put(const bt_clock_class * const libObjPtr)
39 bt_clock_class_put_ref(libObjPtr);
43 template <typename LibObjT>
44 struct CommonClockClassSpec;
46 /* Functions specific to mutable clock classes */
48 struct CommonClockClassSpec<bt_clock_class> final
50 static bt_value *userAttributes(bt_clock_class * const libObjPtr) noexcept
52 return bt_clock_class_borrow_user_attributes(libObjPtr);
56 /* Functions specific to constant clock classes */
58 struct CommonClockClassSpec<const bt_clock_class> final
60 static const bt_value *userAttributes(const bt_clock_class * const libObjPtr) noexcept
62 return bt_clock_class_borrow_user_attributes_const(libObjPtr);
66 } /* namespace internal */
68 class ClockClassOffset final
71 explicit ClockClassOffset(const std::int64_t seconds, const std::uint64_t cycles) :
72 _mSeconds {seconds}, _mCycles {cycles}
76 ClockClassOffset(const ClockClassOffset&) noexcept = default;
77 ClockClassOffset& operator=(const ClockClassOffset&) noexcept = default;
79 std::int64_t seconds() const noexcept
84 std::uint64_t cycles() const noexcept
90 std::int64_t _mSeconds;
91 std::uint64_t _mCycles;
94 template <typename LibObjT>
95 class CommonClockClass final : public BorrowedObject<LibObjT>
98 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
99 using typename BorrowedObject<LibObjT>::_LibObjPtr;
100 using _ThisCommonClockClass = CommonClockClass<LibObjT>;
103 using Shared = SharedObject<_ThisCommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
105 using UserAttributes =
106 typename std::conditional<std::is_const<LibObjT>::value, ConstMapValue, MapValue>::type;
108 explicit CommonClockClass(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
112 template <typename OtherLibObjT>
113 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
114 _ThisBorrowedObject {clkClass}
118 template <typename OtherLibObjT>
119 _ThisCommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
121 _ThisBorrowedObject::operator=(clkClass);
125 void frequency(const std::uint64_t frequency) noexcept
127 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
129 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
132 std::uint64_t frequency() const noexcept
134 return bt_clock_class_get_frequency(this->libObjPtr());
137 void offset(const ClockClassOffset& offset) noexcept
139 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
141 bt_clock_class_set_offset(this->libObjPtr(), offset.seconds(), offset.cycles());
144 ClockClassOffset offset() const noexcept
146 std::int64_t seconds;
147 std::uint64_t cycles;
149 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
150 return ClockClassOffset {seconds, cycles};
153 void precision(const std::uint64_t precision) noexcept
155 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
157 bt_clock_class_set_precision(this->libObjPtr(), precision);
160 std::uint64_t precision() const noexcept
162 return bt_clock_class_get_precision(this->libObjPtr());
165 void originIsUnixEpoch(const bool originIsUnixEpoch) noexcept
167 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
169 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
170 static_cast<bt_bool>(originIsUnixEpoch));
173 bool originIsUnixEpoch() const noexcept
175 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
178 void name(const char * const name)
180 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
182 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
184 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
185 throw MemoryError {};
189 void name(const std::string& name)
191 this->name(name.data());
194 nonstd::optional<bpstd::string_view> name() const noexcept
196 const auto name = bt_clock_class_get_name(this->libObjPtr());
202 return nonstd::nullopt;
205 void description(const char * const description)
207 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
209 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
211 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
212 throw MemoryError {};
216 void description(const std::string& description)
218 this->description(description.data());
221 nonstd::optional<bpstd::string_view> description() const noexcept
223 const auto description = bt_clock_class_get_description(this->libObjPtr());
229 return nonstd::nullopt;
232 void uuid(const std::uint8_t * const uuid) noexcept
234 bt_clock_class_set_uuid(this->libObjPtr(), uuid);
237 nonstd::optional<bt2_common::UuidView> uuid() const noexcept
239 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
242 return bt2_common::UuidView {uuid};
245 return nonstd::nullopt;
248 template <typename LibValT>
249 void userAttributes(const CommonMapValue<LibValT> userAttrs)
251 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
253 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
256 ConstMapValue userAttributes() const noexcept
258 return ConstMapValue {internal::CommonClockClassSpec<const bt_clock_class>::userAttributes(
262 UserAttributes userAttributes() noexcept
264 return UserAttributes {
265 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
268 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
270 std::int64_t nsFromOrigin;
272 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
274 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
275 throw OverflowError {};
281 Shared shared() const noexcept
283 return Shared::createWithRef(*this);
287 using ClockClass = CommonClockClass<bt_clock_class>;
288 using ConstClockClass = CommonClockClass<const bt_clock_class>;
292 struct ClockClassTypeDescr
294 using Const = ConstClockClass;
295 using NonConst = ClockClass;
299 struct TypeDescr<ClockClass> : public ClockClassTypeDescr
304 struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
308 } /* namespace internal */
309 } /* namespace bt2 */
311 #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */