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 "cpp-common/optional.hpp"
18 #include "cpp-common/string_view.hpp"
19 #include "cpp-common/uuid-view.hpp"
20 #include "lib-error.hpp"
27 struct ClockClassRefFuncs final
29 static void get(const bt_clock_class * const libObjPtr)
31 bt_clock_class_get_ref(libObjPtr);
34 static void put(const bt_clock_class * const libObjPtr)
36 bt_clock_class_put_ref(libObjPtr);
40 template <typename LibObjT>
41 struct CommonClockClassSpec;
43 /* Functions specific to mutable clock classes */
45 struct CommonClockClassSpec<bt_clock_class> final
47 static bt_value *userAttributes(bt_clock_class * const libObjPtr) noexcept
49 return bt_clock_class_borrow_user_attributes(libObjPtr);
53 /* Functions specific to constant clock classes */
55 struct CommonClockClassSpec<const bt_clock_class> final
57 static const bt_value *userAttributes(const bt_clock_class * const libObjPtr) noexcept
59 return bt_clock_class_borrow_user_attributes_const(libObjPtr);
63 } /* namespace internal */
65 class ClockClassOffset final
68 explicit ClockClassOffset(const std::int64_t seconds, const std::uint64_t cycles) :
69 _mSeconds {seconds}, _mCycles {cycles}
73 ClockClassOffset(const ClockClassOffset&) noexcept = default;
74 ClockClassOffset& operator=(const ClockClassOffset&) noexcept = default;
76 std::int64_t seconds() const noexcept
81 std::uint64_t cycles() const noexcept
87 std::int64_t _mSeconds;
88 std::uint64_t _mCycles;
91 template <typename LibObjT>
92 class CommonClockClass final : public internal::BorrowedObj<LibObjT>
95 using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
96 using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
97 using _ThisCommonClockClass = CommonClockClass<LibObjT>;
101 internal::SharedObj<_ThisCommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
103 using UserAttributes =
104 typename std::conditional<std::is_const<LibObjT>::value, ConstMapValue, MapValue>::type;
106 explicit CommonClockClass(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
110 template <typename OtherLibObjT>
111 CommonClockClass(const CommonClockClass<OtherLibObjT>& clkClass) noexcept :
112 _ThisBorrowedObj {clkClass}
116 template <typename OtherLibObjT>
117 _ThisCommonClockClass& operator=(const CommonClockClass<OtherLibObjT>& clkClass) noexcept
119 _ThisBorrowedObj::operator=(clkClass);
123 void frequency(const std::uint64_t frequency) noexcept
125 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
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) noexcept
137 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
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) noexcept
153 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
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) noexcept
165 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
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)
178 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
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 LibMemoryError {};
187 void name(const std::string& name)
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)
205 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
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 LibMemoryError {};
214 void description(const std::string& description)
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) 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)
249 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
251 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
254 ConstMapValue userAttributes() const noexcept
256 return ConstMapValue {internal::CommonClockClassSpec<const bt_clock_class>::userAttributes(
260 UserAttributes userAttributes() noexcept
262 return UserAttributes {
263 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
266 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
268 std::int64_t nsFromOrigin;
270 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
272 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
273 throw LibOverflowError {};
279 Shared shared() const noexcept
281 return Shared {*this};
285 using ClockClass = CommonClockClass<bt_clock_class>;
286 using ConstClockClass = CommonClockClass<const bt_clock_class>;
288 } /* namespace bt2 */
290 #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */