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"
21 #include "internal/borrowed-obj.hpp"
22 #include "internal/shared-obj.hpp"
23 #include "internal/utils.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 internal::BorrowedObj<LibObjT>
98 using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
99 using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
100 using _ThisCommonClockClass = CommonClockClass<LibObjT>;
104 internal::SharedObj<_ThisCommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
106 using UserAttributes =
107 typename std::conditional<std::is_const<LibObjT>::value, ConstMapValue, MapValue>::type;
109 explicit CommonClockClass(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
113 template <typename OtherLibObjT>
114 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
115 _ThisBorrowedObj {clkClass}
119 template <typename OtherLibObjT>
120 _ThisCommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
122 _ThisBorrowedObj::operator=(clkClass);
126 void frequency(const std::uint64_t frequency) noexcept
128 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
130 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
133 std::uint64_t frequency() const noexcept
135 return bt_clock_class_get_frequency(this->libObjPtr());
138 void offset(const ClockClassOffset& offset) noexcept
140 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
142 bt_clock_class_set_offset(this->libObjPtr(), offset.seconds(), offset.cycles());
145 ClockClassOffset offset() const noexcept
147 std::int64_t seconds;
148 std::uint64_t cycles;
150 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
151 return ClockClassOffset {seconds, cycles};
154 void precision(const std::uint64_t precision) noexcept
156 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
158 bt_clock_class_set_precision(this->libObjPtr(), precision);
161 std::uint64_t precision() const noexcept
163 return bt_clock_class_get_precision(this->libObjPtr());
166 void originIsUnixEpoch(const bool originIsUnixEpoch) noexcept
168 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
170 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
171 static_cast<bt_bool>(originIsUnixEpoch));
174 bool originIsUnixEpoch() const noexcept
176 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
179 void name(const char * const name)
181 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
183 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
185 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
186 throw MemoryError {};
190 void name(const std::string& name)
192 this->name(name.data());
195 nonstd::optional<bpstd::string_view> name() const noexcept
197 const auto name = bt_clock_class_get_name(this->libObjPtr());
203 return nonstd::nullopt;
206 void description(const char * const description)
208 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
210 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
212 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
213 throw MemoryError {};
217 void description(const std::string& description)
219 this->description(description.data());
222 nonstd::optional<bpstd::string_view> description() const noexcept
224 const auto description = bt_clock_class_get_description(this->libObjPtr());
230 return nonstd::nullopt;
233 void uuid(const std::uint8_t * const uuid) noexcept
235 bt_clock_class_set_uuid(this->libObjPtr(), uuid);
238 nonstd::optional<bt2_common::UuidView> uuid() const noexcept
240 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
243 return bt2_common::UuidView {uuid};
246 return nonstd::nullopt;
249 template <typename LibValT>
250 void userAttributes(const CommonMapValue<LibValT> userAttrs)
252 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
254 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
257 ConstMapValue userAttributes() const noexcept
259 return ConstMapValue {internal::CommonClockClassSpec<const bt_clock_class>::userAttributes(
263 UserAttributes userAttributes() noexcept
265 return UserAttributes {
266 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
269 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
271 std::int64_t nsFromOrigin;
273 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
275 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
276 throw OverflowError {};
282 Shared shared() const noexcept
284 return Shared::createWithRef(*this);
288 using ClockClass = CommonClockClass<bt_clock_class>;
289 using ConstClockClass = CommonClockClass<const bt_clock_class>;
293 struct ClockClassTypeDescr
295 using Const = ConstClockClass;
296 using NonConst = ClockClass;
300 struct TypeDescr<ClockClass> : public ClockClassTypeDescr
305 struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
309 } /* namespace internal */
310 } /* namespace bt2 */
312 #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */