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 | ||
e7f0f07b | 16 | #include "cpp-common/bt2c/c-string-view.hpp" |
12958bb9 | 17 | #include "cpp-common/bt2c/uuid.hpp" |
c022776a | 18 | #include "cpp-common/bt2s/optional.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 | ||
cfc44919 PP |
75 | std::int64_t seconds() const noexcept |
76 | { | |
77 | return _mSeconds; | |
78 | } | |
79 | ||
80 | std::uint64_t cycles() const noexcept | |
81 | { | |
82 | return _mCycles; | |
83 | } | |
84 | ||
85 | private: | |
86 | std::int64_t _mSeconds; | |
87 | std::uint64_t _mCycles; | |
88 | }; | |
89 | ||
90 | template <typename LibObjT> | |
0d218157 | 91 | class CommonClockClass final : public BorrowedObject<LibObjT> |
cfc44919 PP |
92 | { |
93 | private: | |
0d218157 | 94 | using typename BorrowedObject<LibObjT>::_ThisBorrowedObject; |
cfc44919 PP |
95 | |
96 | public: | |
d246c457 | 97 | using typename BorrowedObject<LibObjT>::LibObjPtr; |
ac19444e | 98 | using Shared = SharedObject<CommonClockClass, LibObjT, internal::ClockClassRefFuncs>; |
8047a175 | 99 | using UserAttributes = internal::DepUserAttrs<LibObjT>; |
cfc44919 | 100 | |
d246c457 | 101 | explicit CommonClockClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} |
cfc44919 PP |
102 | { |
103 | } | |
104 | ||
105 | template <typename OtherLibObjT> | |
100fa861 | 106 | CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept : |
0d218157 | 107 | _ThisBorrowedObject {clkClass} |
cfc44919 PP |
108 | { |
109 | } | |
110 | ||
111 | template <typename OtherLibObjT> | |
ac19444e | 112 | CommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept |
cfc44919 | 113 | { |
0d218157 | 114 | _ThisBorrowedObject::operator=(clkClass); |
cfc44919 PP |
115 | return *this; |
116 | } | |
117 | ||
328a274a PP |
118 | CommonClockClass<const bt_clock_class> asConst() const noexcept |
119 | { | |
120 | return CommonClockClass<const bt_clock_class> {*this}; | |
121 | } | |
122 | ||
dcb8ae9b | 123 | void frequency(const std::uint64_t frequency) const noexcept |
cfc44919 | 124 | { |
5c895f64 | 125 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 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 | ||
dcb8ae9b | 135 | void offset(const ClockClassOffset& offset) const noexcept |
cfc44919 | 136 | { |
5c895f64 | 137 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 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 | ||
dcb8ae9b | 151 | void precision(const std::uint64_t precision) const noexcept |
cfc44919 | 152 | { |
5c895f64 | 153 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 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 | ||
dcb8ae9b | 163 | void originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept |
cfc44919 | 164 | { |
5c895f64 | 165 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 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 | ||
dcb8ae9b | 176 | void name(const char * const name) const |
cfc44919 | 177 | { |
5c895f64 | 178 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 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) { | |
39278ebc | 183 | throw MemoryError {}; |
cfc44919 PP |
184 | } |
185 | } | |
186 | ||
dcb8ae9b | 187 | void name(const std::string& name) const |
cfc44919 PP |
188 | { |
189 | this->name(name.data()); | |
190 | } | |
191 | ||
e7f0f07b | 192 | bt2c::CStringView name() const noexcept |
cfc44919 | 193 | { |
5cc5088c | 194 | return bt_clock_class_get_name(this->libObjPtr()); |
cfc44919 PP |
195 | } |
196 | ||
dcb8ae9b | 197 | void description(const char * const description) const |
cfc44919 | 198 | { |
5c895f64 | 199 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 200 | |
341a67c4 | 201 | const auto status = bt_clock_class_set_description(this->libObjPtr(), description); |
cfc44919 PP |
202 | |
203 | if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) { | |
39278ebc | 204 | throw MemoryError {}; |
cfc44919 PP |
205 | } |
206 | } | |
207 | ||
dcb8ae9b | 208 | void description(const std::string& description) const |
cfc44919 PP |
209 | { |
210 | this->description(description.data()); | |
211 | } | |
212 | ||
e7f0f07b | 213 | bt2c::CStringView description() const noexcept |
cfc44919 | 214 | { |
5cc5088c | 215 | return bt_clock_class_get_description(this->libObjPtr()); |
cfc44919 PP |
216 | } |
217 | ||
dcb8ae9b | 218 | void uuid(const std::uint8_t * const uuid) const noexcept |
cfc44919 | 219 | { |
341a67c4 | 220 | bt_clock_class_set_uuid(this->libObjPtr(), uuid); |
cfc44919 PP |
221 | } |
222 | ||
c022776a | 223 | bt2s::optional<bt2c::UuidView> uuid() const noexcept |
cfc44919 | 224 | { |
341a67c4 | 225 | const auto uuid = bt_clock_class_get_uuid(this->libObjPtr()); |
cfc44919 PP |
226 | |
227 | if (uuid) { | |
094bf3f2 | 228 | return bt2c::UuidView {uuid}; |
cfc44919 PP |
229 | } |
230 | ||
c022776a | 231 | return bt2s::nullopt; |
cfc44919 PP |
232 | } |
233 | ||
234 | template <typename LibValT> | |
b7ffa6f0 | 235 | void userAttributes(const CommonMapValue<LibValT> userAttrs) const noexcept |
cfc44919 | 236 | { |
5c895f64 | 237 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 238 | |
341a67c4 | 239 | bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr()); |
cfc44919 PP |
240 | } |
241 | ||
dcb8ae9b | 242 | UserAttributes userAttributes() const noexcept |
cfc44919 PP |
243 | { |
244 | return UserAttributes { | |
341a67c4 | 245 | internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())}; |
cfc44919 PP |
246 | } |
247 | ||
248 | std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const | |
249 | { | |
250 | std::int64_t nsFromOrigin; | |
251 | const auto status = | |
341a67c4 | 252 | bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin); |
cfc44919 PP |
253 | |
254 | if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) { | |
39278ebc | 255 | throw OverflowError {}; |
cfc44919 PP |
256 | } |
257 | ||
258 | return nsFromOrigin; | |
259 | } | |
260 | ||
261 | Shared shared() const noexcept | |
262 | { | |
c9c0b6e2 | 263 | return Shared::createWithRef(*this); |
cfc44919 PP |
264 | } |
265 | }; | |
266 | ||
267 | using ClockClass = CommonClockClass<bt_clock_class>; | |
268 | using ConstClockClass = CommonClockClass<const bt_clock_class>; | |
269 | ||
4927bae7 PP |
270 | namespace internal { |
271 | ||
272 | struct ClockClassTypeDescr | |
273 | { | |
274 | using Const = ConstClockClass; | |
275 | using NonConst = ClockClass; | |
276 | }; | |
277 | ||
278 | template <> | |
279 | struct TypeDescr<ClockClass> : public ClockClassTypeDescr | |
280 | { | |
281 | }; | |
282 | ||
283 | template <> | |
284 | struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr | |
285 | { | |
286 | }; | |
287 | ||
288 | } /* namespace internal */ | |
b5f55e9f | 289 | } /* namespace bt2 */ |
cfc44919 | 290 | |
b5f55e9f | 291 | #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */ |