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
10 #include <type_traits>
13 #include <babeltrace2/babeltrace.h>
15 #include "internal/borrowed-obj.hpp"
16 #include "internal/shared-obj.hpp"
17 #include "internal/utils.hpp"
18 #include "cpp-common/optional.hpp"
19 #include "cpp-common/string_view.hpp"
20 #include "cpp-common/uuid-view.hpp"
28 struct ClockClassRefFuncs final
30 static void get(const bt_clock_class * const libObjPtr)
32 bt_clock_class_get_ref(libObjPtr);
35 static void put(const bt_clock_class * const libObjPtr)
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 ClockClassOffset final
69 explicit ClockClassOffset(const std::int64_t seconds, const std::uint64_t cycles) :
70 _mSeconds {seconds}, _mCycles {cycles}
74 ClockClassOffset(const ClockClassOffset&) noexcept = default;
75 ClockClassOffset& operator=(const ClockClassOffset&) noexcept = default;
77 std::int64_t seconds() const noexcept
82 std::uint64_t cycles() const noexcept
88 std::int64_t _mSeconds;
89 std::uint64_t _mCycles;
92 template <typename LibObjT>
93 class CommonClockClass final : public internal::BorrowedObj<LibObjT>
96 using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
97 using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
98 using _ThisCommonClockClass = CommonClockClass<LibObjT>;
102 internal::SharedObj<_ThisCommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
104 using UserAttributes =
105 typename std::conditional<std::is_const<LibObjT>::value, ConstMapValue, MapValue>::type;
107 explicit CommonClockClass(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
111 template <typename OtherLibObjT>
112 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
113 _ThisBorrowedObj {clkClass}
117 template <typename OtherLibObjT>
118 _ThisCommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
120 _ThisBorrowedObj::operator=(clkClass);
124 void frequency(const std::uint64_t frequency) noexcept
126 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
128 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
131 std::uint64_t frequency() const noexcept
133 return bt_clock_class_get_frequency(this->libObjPtr());
136 void offset(const ClockClassOffset& offset) noexcept
138 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
140 bt_clock_class_set_offset(this->libObjPtr(), offset.seconds(), offset.cycles());
143 ClockClassOffset offset() const noexcept
145 std::int64_t seconds;
146 std::uint64_t cycles;
148 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
149 return ClockClassOffset {seconds, cycles};
152 void precision(const std::uint64_t precision) noexcept
154 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
156 bt_clock_class_set_precision(this->libObjPtr(), precision);
159 std::uint64_t precision() const noexcept
161 return bt_clock_class_get_precision(this->libObjPtr());
164 void originIsUnixEpoch(const bool originIsUnixEpoch) noexcept
166 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
168 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
169 static_cast<bt_bool>(originIsUnixEpoch));
172 bool originIsUnixEpoch() const noexcept
174 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
177 void name(const char * const name)
179 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
181 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
183 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
184 throw MemoryError {};
188 void name(const std::string& name)
190 this->name(name.data());
193 nonstd::optional<bpstd::string_view> name() const noexcept
195 const auto name = bt_clock_class_get_name(this->libObjPtr());
201 return nonstd::nullopt;
204 void description(const char * const description)
206 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
208 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
210 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
211 throw MemoryError {};
215 void description(const std::string& description)
217 this->description(description.data());
220 nonstd::optional<bpstd::string_view> description() const noexcept
222 const auto description = bt_clock_class_get_description(this->libObjPtr());
228 return nonstd::nullopt;
231 void uuid(const std::uint8_t * const uuid) noexcept
233 bt_clock_class_set_uuid(this->libObjPtr(), uuid);
236 nonstd::optional<bt2_common::UuidView> uuid() const noexcept
238 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
241 return bt2_common::UuidView {uuid};
244 return nonstd::nullopt;
247 template <typename LibValT>
248 void userAttributes(const CommonMapValue<LibValT> userAttrs)
250 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
252 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
255 ConstMapValue userAttributes() const noexcept
257 return ConstMapValue {internal::CommonClockClassSpec<const bt_clock_class>::userAttributes(
261 UserAttributes userAttributes() noexcept
263 return UserAttributes {
264 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
267 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
269 std::int64_t nsFromOrigin;
271 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
273 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
274 throw OverflowError {};
280 Shared shared() const noexcept
282 return Shared::createWithRef(*this);
286 using ClockClass = CommonClockClass<bt_clock_class>;
287 using ConstClockClass = CommonClockClass<const bt_clock_class>;
291 struct ClockClassTypeDescr
293 using Const = ConstClockClass;
294 using NonConst = ClockClass;
298 struct TypeDescr<ClockClass> : public ClockClassTypeDescr
303 struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
307 } /* namespace internal */
308 } /* namespace bt2 */
310 #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */