Commit | Line | Data |
---|---|---|
cfc44919 PP |
1 | /* |
2 | * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
7 | #ifndef BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP | |
9 | ||
cfc44919 PP |
10 | #include <cstdint> |
11 | #include <string> | |
c802cacb SM |
12 | #include <type_traits> |
13 | ||
cfc44919 PP |
14 | #include <babeltrace2/babeltrace.h> |
15 | ||
cfc44919 PP |
16 | #include "cpp-common/optional.hpp" |
17 | #include "cpp-common/string_view.hpp" | |
18 | #include "cpp-common/uuid-view.hpp" | |
c802cacb | 19 | |
0d218157 | 20 | #include "borrowed-object.hpp" |
39278ebc | 21 | #include "exc.hpp" |
c802cacb | 22 | #include "internal/utils.hpp" |
7f5cdaf0 | 23 | #include "shared-object.hpp" |
cfc44919 PP |
24 | #include "value.hpp" |
25 | ||
26 | namespace bt2 { | |
cfc44919 PP |
27 | namespace internal { |
28 | ||
29 | struct ClockClassRefFuncs final | |
30 | { | |
c677c492 | 31 | static void get(const bt_clock_class * const libObjPtr) noexcept |
cfc44919 PP |
32 | { |
33 | bt_clock_class_get_ref(libObjPtr); | |
34 | } | |
35 | ||
c677c492 | 36 | static void put(const bt_clock_class * const libObjPtr) noexcept |
cfc44919 PP |
37 | { |
38 | bt_clock_class_put_ref(libObjPtr); | |
39 | } | |
40 | }; | |
41 | ||
42 | template <typename LibObjT> | |
43 | struct CommonClockClassSpec; | |
44 | ||
b5f55e9f | 45 | /* Functions specific to mutable clock classes */ |
cfc44919 PP |
46 | template <> |
47 | struct CommonClockClassSpec<bt_clock_class> final | |
48 | { | |
49 | static bt_value *userAttributes(bt_clock_class * const libObjPtr) noexcept | |
50 | { | |
51 | return bt_clock_class_borrow_user_attributes(libObjPtr); | |
52 | } | |
53 | }; | |
54 | ||
b5f55e9f | 55 | /* Functions specific to constant clock classes */ |
cfc44919 PP |
56 | template <> |
57 | struct CommonClockClassSpec<const bt_clock_class> final | |
58 | { | |
59 | static const bt_value *userAttributes(const bt_clock_class * const libObjPtr) noexcept | |
60 | { | |
61 | return bt_clock_class_borrow_user_attributes_const(libObjPtr); | |
62 | } | |
63 | }; | |
64 | ||
b5f55e9f | 65 | } /* namespace internal */ |
cfc44919 PP |
66 | |
67 | class ClockClassOffset final | |
68 | { | |
69 | public: | |
70 | explicit ClockClassOffset(const std::int64_t seconds, const std::uint64_t cycles) : | |
71 | _mSeconds {seconds}, _mCycles {cycles} | |
72 | { | |
73 | } | |
74 | ||
75 | ClockClassOffset(const ClockClassOffset&) noexcept = default; | |
76 | ClockClassOffset& operator=(const ClockClassOffset&) noexcept = default; | |
77 | ||
78 | std::int64_t seconds() const noexcept | |
79 | { | |
80 | return _mSeconds; | |
81 | } | |
82 | ||
83 | std::uint64_t cycles() const noexcept | |
84 | { | |
85 | return _mCycles; | |
86 | } | |
87 | ||
88 | private: | |
89 | std::int64_t _mSeconds; | |
90 | std::uint64_t _mCycles; | |
91 | }; | |
92 | ||
93 | template <typename LibObjT> | |
0d218157 | 94 | class CommonClockClass final : public BorrowedObject<LibObjT> |
cfc44919 PP |
95 | { |
96 | private: | |
0d218157 PP |
97 | using typename BorrowedObject<LibObjT>::_ThisBorrowedObject; |
98 | using typename BorrowedObject<LibObjT>::_LibObjPtr; | |
cfc44919 PP |
99 | |
100 | public: | |
ac19444e | 101 | using Shared = SharedObject<CommonClockClass, LibObjT, internal::ClockClassRefFuncs>; |
8047a175 | 102 | using UserAttributes = internal::DepUserAttrs<LibObjT>; |
cfc44919 | 103 | |
0d218157 | 104 | explicit CommonClockClass(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} |
cfc44919 PP |
105 | { |
106 | } | |
107 | ||
108 | template <typename OtherLibObjT> | |
100fa861 | 109 | CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept : |
0d218157 | 110 | _ThisBorrowedObject {clkClass} |
cfc44919 PP |
111 | { |
112 | } | |
113 | ||
114 | template <typename OtherLibObjT> | |
ac19444e | 115 | CommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept |
cfc44919 | 116 | { |
0d218157 | 117 | _ThisBorrowedObject::operator=(clkClass); |
cfc44919 PP |
118 | return *this; |
119 | } | |
120 | ||
328a274a PP |
121 | CommonClockClass<const bt_clock_class> asConst() const noexcept |
122 | { | |
123 | return CommonClockClass<const bt_clock_class> {*this}; | |
124 | } | |
125 | ||
dcb8ae9b | 126 | void frequency(const std::uint64_t frequency) const noexcept |
cfc44919 | 127 | { |
5c895f64 | 128 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 129 | |
341a67c4 | 130 | bt_clock_class_set_frequency(this->libObjPtr(), frequency); |
cfc44919 PP |
131 | } |
132 | ||
133 | std::uint64_t frequency() const noexcept | |
134 | { | |
341a67c4 | 135 | return bt_clock_class_get_frequency(this->libObjPtr()); |
cfc44919 PP |
136 | } |
137 | ||
dcb8ae9b | 138 | void offset(const ClockClassOffset& offset) const noexcept |
cfc44919 | 139 | { |
5c895f64 | 140 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 141 | |
341a67c4 | 142 | bt_clock_class_set_offset(this->libObjPtr(), offset.seconds(), offset.cycles()); |
cfc44919 PP |
143 | } |
144 | ||
145 | ClockClassOffset offset() const noexcept | |
146 | { | |
147 | std::int64_t seconds; | |
148 | std::uint64_t cycles; | |
149 | ||
341a67c4 | 150 | bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles); |
cfc44919 PP |
151 | return ClockClassOffset {seconds, cycles}; |
152 | } | |
153 | ||
dcb8ae9b | 154 | void precision(const std::uint64_t precision) const noexcept |
cfc44919 | 155 | { |
5c895f64 | 156 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 157 | |
341a67c4 | 158 | bt_clock_class_set_precision(this->libObjPtr(), precision); |
cfc44919 PP |
159 | } |
160 | ||
161 | std::uint64_t precision() const noexcept | |
162 | { | |
341a67c4 | 163 | return bt_clock_class_get_precision(this->libObjPtr()); |
cfc44919 PP |
164 | } |
165 | ||
dcb8ae9b | 166 | void originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept |
cfc44919 | 167 | { |
5c895f64 | 168 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 169 | |
341a67c4 | 170 | bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(), |
cfc44919 PP |
171 | static_cast<bt_bool>(originIsUnixEpoch)); |
172 | } | |
173 | ||
174 | bool originIsUnixEpoch() const noexcept | |
175 | { | |
341a67c4 | 176 | return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr())); |
cfc44919 PP |
177 | } |
178 | ||
dcb8ae9b | 179 | void name(const char * const name) const |
cfc44919 | 180 | { |
5c895f64 | 181 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 182 | |
341a67c4 | 183 | const auto status = bt_clock_class_set_name(this->libObjPtr(), name); |
cfc44919 PP |
184 | |
185 | if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) { | |
39278ebc | 186 | throw MemoryError {}; |
cfc44919 PP |
187 | } |
188 | } | |
189 | ||
dcb8ae9b | 190 | void name(const std::string& name) const |
cfc44919 PP |
191 | { |
192 | this->name(name.data()); | |
193 | } | |
194 | ||
195 | nonstd::optional<bpstd::string_view> name() const noexcept | |
196 | { | |
341a67c4 | 197 | const auto name = bt_clock_class_get_name(this->libObjPtr()); |
cfc44919 PP |
198 | |
199 | if (name) { | |
200 | return name; | |
201 | } | |
202 | ||
203 | return nonstd::nullopt; | |
204 | } | |
205 | ||
dcb8ae9b | 206 | void description(const char * const description) const |
cfc44919 | 207 | { |
5c895f64 | 208 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 209 | |
341a67c4 | 210 | const auto status = bt_clock_class_set_description(this->libObjPtr(), description); |
cfc44919 PP |
211 | |
212 | if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) { | |
39278ebc | 213 | throw MemoryError {}; |
cfc44919 PP |
214 | } |
215 | } | |
216 | ||
dcb8ae9b | 217 | void description(const std::string& description) const |
cfc44919 PP |
218 | { |
219 | this->description(description.data()); | |
220 | } | |
221 | ||
222 | nonstd::optional<bpstd::string_view> description() const noexcept | |
223 | { | |
341a67c4 | 224 | const auto description = bt_clock_class_get_description(this->libObjPtr()); |
cfc44919 PP |
225 | |
226 | if (description) { | |
227 | return description; | |
228 | } | |
229 | ||
230 | return nonstd::nullopt; | |
231 | } | |
232 | ||
dcb8ae9b | 233 | void uuid(const std::uint8_t * const uuid) const noexcept |
cfc44919 | 234 | { |
341a67c4 | 235 | bt_clock_class_set_uuid(this->libObjPtr(), uuid); |
cfc44919 PP |
236 | } |
237 | ||
238 | nonstd::optional<bt2_common::UuidView> uuid() const noexcept | |
239 | { | |
341a67c4 | 240 | const auto uuid = bt_clock_class_get_uuid(this->libObjPtr()); |
cfc44919 PP |
241 | |
242 | if (uuid) { | |
243 | return bt2_common::UuidView {uuid}; | |
244 | } | |
245 | ||
246 | return nonstd::nullopt; | |
247 | } | |
248 | ||
249 | template <typename LibValT> | |
b7ffa6f0 | 250 | void userAttributes(const CommonMapValue<LibValT> userAttrs) const noexcept |
cfc44919 | 251 | { |
5c895f64 | 252 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 253 | |
341a67c4 | 254 | bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr()); |
cfc44919 PP |
255 | } |
256 | ||
dcb8ae9b | 257 | UserAttributes userAttributes() const noexcept |
cfc44919 PP |
258 | { |
259 | return UserAttributes { | |
341a67c4 | 260 | internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())}; |
cfc44919 PP |
261 | } |
262 | ||
263 | std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const | |
264 | { | |
265 | std::int64_t nsFromOrigin; | |
266 | const auto status = | |
341a67c4 | 267 | bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin); |
cfc44919 PP |
268 | |
269 | if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) { | |
39278ebc | 270 | throw OverflowError {}; |
cfc44919 PP |
271 | } |
272 | ||
273 | return nsFromOrigin; | |
274 | } | |
275 | ||
276 | Shared shared() const noexcept | |
277 | { | |
c9c0b6e2 | 278 | return Shared::createWithRef(*this); |
cfc44919 PP |
279 | } |
280 | }; | |
281 | ||
282 | using ClockClass = CommonClockClass<bt_clock_class>; | |
283 | using ConstClockClass = CommonClockClass<const bt_clock_class>; | |
284 | ||
4927bae7 PP |
285 | namespace internal { |
286 | ||
287 | struct ClockClassTypeDescr | |
288 | { | |
289 | using Const = ConstClockClass; | |
290 | using NonConst = ClockClass; | |
291 | }; | |
292 | ||
293 | template <> | |
294 | struct TypeDescr<ClockClass> : public ClockClassTypeDescr | |
295 | { | |
296 | }; | |
297 | ||
298 | template <> | |
299 | struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr | |
300 | { | |
301 | }; | |
302 | ||
303 | } /* namespace internal */ | |
b5f55e9f | 304 | } /* namespace bt2 */ |
cfc44919 | 305 | |
b5f55e9f | 306 | #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */ |