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 ClockClassOffset(const ClockClassOffset&) noexcept = default;
76 ClockClassOffset& operator=(const ClockClassOffset&) noexcept = default;
78 std::int64_t seconds() const noexcept
83 std::uint64_t cycles() const noexcept
89 std::int64_t _mSeconds;
90 std::uint64_t _mCycles;
93 template <typename LibObjT>
94 class CommonClockClass final : public BorrowedObject<LibObjT>
97 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
98 using typename BorrowedObject<LibObjT>::_LibObjPtr;
99 using _ThisCommonClockClass = CommonClockClass<LibObjT>;
102 using Shared = SharedObject<_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 : _ThisBorrowedObject {libObjPtr}
111 template <typename OtherLibObjT>
112 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
113 _ThisBorrowedObject {clkClass}
117 template <typename OtherLibObjT>
118 _ThisCommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
120 _ThisBorrowedObject::operator=(clkClass);
124 CommonClockClass<const bt_clock_class> asConst() const noexcept
126 return CommonClockClass<const bt_clock_class> {*this};
129 void frequency(const std::uint64_t frequency) const noexcept
131 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
133 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
136 std::uint64_t frequency() const noexcept
138 return bt_clock_class_get_frequency(this->libObjPtr());
141 void offset(const ClockClassOffset& offset) const noexcept
143 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
145 bt_clock_class_set_offset(this->libObjPtr(), offset.seconds(), offset.cycles());
148 ClockClassOffset offset() const noexcept
150 std::int64_t seconds;
151 std::uint64_t cycles;
153 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
154 return ClockClassOffset {seconds, cycles};
157 void precision(const std::uint64_t precision) const noexcept
159 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
161 bt_clock_class_set_precision(this->libObjPtr(), precision);
164 std::uint64_t precision() const noexcept
166 return bt_clock_class_get_precision(this->libObjPtr());
169 void originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept
171 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
173 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
174 static_cast<bt_bool>(originIsUnixEpoch));
177 bool originIsUnixEpoch() const noexcept
179 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
182 void name(const char * const name) const
184 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
186 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
188 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
189 throw MemoryError {};
193 void name(const std::string& name) const
195 this->name(name.data());
198 nonstd::optional<bpstd::string_view> name() const noexcept
200 const auto name = bt_clock_class_get_name(this->libObjPtr());
206 return nonstd::nullopt;
209 void description(const char * const description) const
211 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
213 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
215 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
216 throw MemoryError {};
220 void description(const std::string& description) const
222 this->description(description.data());
225 nonstd::optional<bpstd::string_view> description() const noexcept
227 const auto description = bt_clock_class_get_description(this->libObjPtr());
233 return nonstd::nullopt;
236 void uuid(const std::uint8_t * const uuid) const noexcept
238 bt_clock_class_set_uuid(this->libObjPtr(), uuid);
241 nonstd::optional<bt2_common::UuidView> uuid() const noexcept
243 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
246 return bt2_common::UuidView {uuid};
249 return nonstd::nullopt;
252 template <typename LibValT>
253 void userAttributes(const CommonMapValue<LibValT> userAttrs) const noexcept
255 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
257 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
260 UserAttributes userAttributes() const 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 OverflowError {};
279 Shared shared() const noexcept
281 return Shared::createWithRef(*this);
285 using ClockClass = CommonClockClass<bt_clock_class>;
286 using ConstClockClass = CommonClockClass<const bt_clock_class>;
290 struct ClockClassTypeDescr
292 using Const = ConstClockClass;
293 using NonConst = ClockClass;
297 struct TypeDescr<ClockClass> : public ClockClassTypeDescr
302 struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
306 } /* namespace internal */
307 } /* namespace bt2 */
309 #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */