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