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