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 | 10 | #include <cstdint> |
c802cacb SM |
11 | #include <type_traits> |
12 | ||
cfc44919 PP |
13 | #include <babeltrace2/babeltrace.h> |
14 | ||
e7f0f07b | 15 | #include "cpp-common/bt2c/c-string-view.hpp" |
12958bb9 | 16 | #include "cpp-common/bt2c/uuid.hpp" |
c022776a | 17 | #include "cpp-common/bt2s/optional.hpp" |
c802cacb | 18 | |
0d218157 | 19 | #include "borrowed-object.hpp" |
39278ebc | 20 | #include "exc.hpp" |
c802cacb | 21 | #include "internal/utils.hpp" |
7f5cdaf0 | 22 | #include "shared-object.hpp" |
cfc44919 PP |
23 | #include "value.hpp" |
24 | ||
25 | namespace bt2 { | |
cfc44919 PP |
26 | namespace internal { |
27 | ||
28 | struct ClockClassRefFuncs final | |
29 | { | |
c677c492 | 30 | static void get(const bt_clock_class * const libObjPtr) noexcept |
cfc44919 PP |
31 | { |
32 | bt_clock_class_get_ref(libObjPtr); | |
33 | } | |
34 | ||
c677c492 | 35 | static void put(const bt_clock_class * const libObjPtr) noexcept |
cfc44919 PP |
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 | ||
cfc44919 PP |
74 | std::int64_t seconds() const noexcept |
75 | { | |
76 | return _mSeconds; | |
77 | } | |
78 | ||
79 | std::uint64_t cycles() const noexcept | |
80 | { | |
81 | return _mCycles; | |
82 | } | |
83 | ||
84 | private: | |
85 | std::int64_t _mSeconds; | |
86 | std::uint64_t _mCycles; | |
87 | }; | |
88 | ||
89 | template <typename LibObjT> | |
0d218157 | 90 | class CommonClockClass final : public BorrowedObject<LibObjT> |
cfc44919 PP |
91 | { |
92 | private: | |
0d218157 | 93 | using typename BorrowedObject<LibObjT>::_ThisBorrowedObject; |
cfc44919 PP |
94 | |
95 | public: | |
d246c457 | 96 | using typename BorrowedObject<LibObjT>::LibObjPtr; |
ac19444e | 97 | using Shared = SharedObject<CommonClockClass, LibObjT, internal::ClockClassRefFuncs>; |
8047a175 | 98 | using UserAttributes = internal::DepUserAttrs<LibObjT>; |
cfc44919 | 99 | |
d246c457 | 100 | explicit CommonClockClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} |
cfc44919 PP |
101 | { |
102 | } | |
103 | ||
104 | template <typename OtherLibObjT> | |
100fa861 | 105 | CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept : |
0d218157 | 106 | _ThisBorrowedObject {clkClass} |
cfc44919 PP |
107 | { |
108 | } | |
109 | ||
110 | template <typename OtherLibObjT> | |
ac19444e | 111 | CommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept |
cfc44919 | 112 | { |
0d218157 | 113 | _ThisBorrowedObject::operator=(clkClass); |
cfc44919 PP |
114 | return *this; |
115 | } | |
116 | ||
328a274a PP |
117 | CommonClockClass<const bt_clock_class> asConst() const noexcept |
118 | { | |
119 | return CommonClockClass<const bt_clock_class> {*this}; | |
120 | } | |
121 | ||
dcb8ae9b | 122 | void frequency(const std::uint64_t frequency) const noexcept |
cfc44919 | 123 | { |
5c895f64 | 124 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 125 | |
341a67c4 | 126 | bt_clock_class_set_frequency(this->libObjPtr(), frequency); |
cfc44919 PP |
127 | } |
128 | ||
129 | std::uint64_t frequency() const noexcept | |
130 | { | |
341a67c4 | 131 | return bt_clock_class_get_frequency(this->libObjPtr()); |
cfc44919 PP |
132 | } |
133 | ||
dcb8ae9b | 134 | void offset(const ClockClassOffset& offset) const noexcept |
cfc44919 | 135 | { |
5c895f64 | 136 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 137 | |
341a67c4 | 138 | bt_clock_class_set_offset(this->libObjPtr(), offset.seconds(), offset.cycles()); |
cfc44919 PP |
139 | } |
140 | ||
141 | ClockClassOffset offset() const noexcept | |
142 | { | |
143 | std::int64_t seconds; | |
144 | std::uint64_t cycles; | |
145 | ||
341a67c4 | 146 | bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles); |
cfc44919 PP |
147 | return ClockClassOffset {seconds, cycles}; |
148 | } | |
149 | ||
dcb8ae9b | 150 | void precision(const std::uint64_t precision) const noexcept |
cfc44919 | 151 | { |
5c895f64 | 152 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 153 | |
341a67c4 | 154 | bt_clock_class_set_precision(this->libObjPtr(), precision); |
cfc44919 PP |
155 | } |
156 | ||
157 | std::uint64_t precision() const noexcept | |
158 | { | |
341a67c4 | 159 | return bt_clock_class_get_precision(this->libObjPtr()); |
cfc44919 PP |
160 | } |
161 | ||
dcb8ae9b | 162 | void originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept |
cfc44919 | 163 | { |
5c895f64 | 164 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 165 | |
341a67c4 | 166 | bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(), |
cfc44919 PP |
167 | static_cast<bt_bool>(originIsUnixEpoch)); |
168 | } | |
169 | ||
170 | bool originIsUnixEpoch() const noexcept | |
171 | { | |
341a67c4 | 172 | return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr())); |
cfc44919 PP |
173 | } |
174 | ||
15030982 | 175 | void name(const bt2c::CStringView name) const |
cfc44919 | 176 | { |
5c895f64 | 177 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 178 | |
341a67c4 | 179 | const auto status = bt_clock_class_set_name(this->libObjPtr(), name); |
cfc44919 PP |
180 | |
181 | if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) { | |
39278ebc | 182 | throw MemoryError {}; |
cfc44919 PP |
183 | } |
184 | } | |
185 | ||
e7f0f07b | 186 | bt2c::CStringView name() const noexcept |
cfc44919 | 187 | { |
5cc5088c | 188 | return bt_clock_class_get_name(this->libObjPtr()); |
cfc44919 PP |
189 | } |
190 | ||
15030982 | 191 | void description(const bt2c::CStringView description) const |
cfc44919 | 192 | { |
5c895f64 | 193 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 194 | |
341a67c4 | 195 | const auto status = bt_clock_class_set_description(this->libObjPtr(), description); |
cfc44919 PP |
196 | |
197 | if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) { | |
39278ebc | 198 | throw MemoryError {}; |
cfc44919 PP |
199 | } |
200 | } | |
201 | ||
e7f0f07b | 202 | bt2c::CStringView description() const noexcept |
cfc44919 | 203 | { |
5cc5088c | 204 | return bt_clock_class_get_description(this->libObjPtr()); |
cfc44919 PP |
205 | } |
206 | ||
dcb8ae9b | 207 | void uuid(const std::uint8_t * const uuid) const noexcept |
cfc44919 | 208 | { |
341a67c4 | 209 | bt_clock_class_set_uuid(this->libObjPtr(), uuid); |
cfc44919 PP |
210 | } |
211 | ||
c022776a | 212 | bt2s::optional<bt2c::UuidView> uuid() const noexcept |
cfc44919 | 213 | { |
341a67c4 | 214 | const auto uuid = bt_clock_class_get_uuid(this->libObjPtr()); |
cfc44919 PP |
215 | |
216 | if (uuid) { | |
094bf3f2 | 217 | return bt2c::UuidView {uuid}; |
cfc44919 PP |
218 | } |
219 | ||
c022776a | 220 | return bt2s::nullopt; |
cfc44919 PP |
221 | } |
222 | ||
223 | template <typename LibValT> | |
b7ffa6f0 | 224 | void userAttributes(const CommonMapValue<LibValT> userAttrs) const noexcept |
cfc44919 | 225 | { |
5c895f64 | 226 | static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`."); |
cfc44919 | 227 | |
341a67c4 | 228 | bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr()); |
cfc44919 PP |
229 | } |
230 | ||
dcb8ae9b | 231 | UserAttributes userAttributes() const noexcept |
cfc44919 PP |
232 | { |
233 | return UserAttributes { | |
341a67c4 | 234 | internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())}; |
cfc44919 PP |
235 | } |
236 | ||
237 | std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const | |
238 | { | |
239 | std::int64_t nsFromOrigin; | |
240 | const auto status = | |
341a67c4 | 241 | bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin); |
cfc44919 PP |
242 | |
243 | if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) { | |
39278ebc | 244 | throw OverflowError {}; |
cfc44919 PP |
245 | } |
246 | ||
247 | return nsFromOrigin; | |
248 | } | |
249 | ||
250 | Shared shared() const noexcept | |
251 | { | |
c9c0b6e2 | 252 | return Shared::createWithRef(*this); |
cfc44919 PP |
253 | } |
254 | }; | |
255 | ||
256 | using ClockClass = CommonClockClass<bt_clock_class>; | |
257 | using ConstClockClass = CommonClockClass<const bt_clock_class>; | |
258 | ||
4927bae7 PP |
259 | namespace internal { |
260 | ||
261 | struct ClockClassTypeDescr | |
262 | { | |
263 | using Const = ConstClockClass; | |
264 | using NonConst = ClockClass; | |
265 | }; | |
266 | ||
267 | template <> | |
268 | struct TypeDescr<ClockClass> : public ClockClassTypeDescr | |
269 | { | |
270 | }; | |
271 | ||
272 | template <> | |
273 | struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr | |
274 | { | |
275 | }; | |
276 | ||
277 | } /* namespace internal */ | |
b5f55e9f | 278 | } /* namespace bt2 */ |
cfc44919 | 279 | |
b5f55e9f | 280 | #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */ |