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