cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / cpp-common / bt2 / clock-class.hpp
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 <cstdint>
11 #include <type_traits>
12
13 #include <babeltrace2/babeltrace.h>
14
15 #include "cpp-common/bt2c/c-string-view.hpp"
16 #include "cpp-common/bt2c/uuid.hpp"
17 #include "cpp-common/bt2s/optional.hpp"
18
19 #include "borrowed-object.hpp"
20 #include "exc.hpp"
21 #include "internal/utils.hpp"
22 #include "shared-object.hpp"
23 #include "value.hpp"
24
25 namespace bt2 {
26 namespace internal {
27
28 struct ClockClassRefFuncs final
29 {
30 static void get(const bt_clock_class * const libObjPtr) noexcept
31 {
32 bt_clock_class_get_ref(libObjPtr);
33 }
34
35 static void put(const bt_clock_class * const libObjPtr) noexcept
36 {
37 bt_clock_class_put_ref(libObjPtr);
38 }
39 };
40
41 template <typename LibObjT>
42 struct CommonClockClassSpec;
43
44 /* Functions specific to mutable clock classes */
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
54 /* Functions specific to constant clock classes */
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
64 } /* namespace internal */
65
66 class ClockOffset final
67 {
68 public:
69 explicit ClockOffset(const std::int64_t seconds, const std::uint64_t cycles) :
70 _mSeconds {seconds}, _mCycles {cycles}
71 {
72 }
73
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>
90 class CommonClockClass final : public BorrowedObject<LibObjT>
91 {
92 private:
93 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
94
95 public:
96 using typename BorrowedObject<LibObjT>::LibObjPtr;
97 using Shared = SharedObject<CommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
98 using UserAttributes = internal::DepUserAttrs<LibObjT>;
99
100 explicit CommonClockClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
101 {
102 }
103
104 template <typename OtherLibObjT>
105 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
106 _ThisBorrowedObject {clkClass}
107 {
108 }
109
110 template <typename OtherLibObjT>
111 CommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
112 {
113 _ThisBorrowedObject::operator=(clkClass);
114 return *this;
115 }
116
117 CommonClockClass<const bt_clock_class> asConst() const noexcept
118 {
119 return CommonClockClass<const bt_clock_class> {*this};
120 }
121
122 CommonClockClass frequency(const std::uint64_t frequency) const noexcept
123 {
124 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
125
126 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
127 return *this;
128 }
129
130 std::uint64_t frequency() const noexcept
131 {
132 return bt_clock_class_get_frequency(this->libObjPtr());
133 }
134
135 CommonClockClass offsetFromOrigin(const ClockOffset& offsetFromOrigin) const noexcept
136 {
137 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
138
139 bt_clock_class_set_offset(this->libObjPtr(), offsetFromOrigin.seconds(),
140 offsetFromOrigin.cycles());
141 return *this;
142 }
143
144 ClockOffset offsetFromOrigin() const noexcept
145 {
146 std::int64_t seconds;
147 std::uint64_t cycles;
148
149 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
150 return ClockOffset {seconds, cycles};
151 }
152
153 CommonClockClass precision(const std::uint64_t precision) const noexcept
154 {
155 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
156
157 bt_clock_class_set_precision(this->libObjPtr(), precision);
158 return *this;
159 }
160
161 std::uint64_t precision() const noexcept
162 {
163 return bt_clock_class_get_precision(this->libObjPtr());
164 }
165
166 CommonClockClass originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept
167 {
168 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
169
170 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
171 static_cast<bt_bool>(originIsUnixEpoch));
172 return *this;
173 }
174
175 bool originIsUnixEpoch() const noexcept
176 {
177 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
178 }
179
180 CommonClockClass name(const bt2c::CStringView name) const
181 {
182 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
183
184 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
185
186 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
187 throw MemoryError {};
188 }
189
190 return *this;
191 }
192
193 bt2c::CStringView name() const noexcept
194 {
195 return bt_clock_class_get_name(this->libObjPtr());
196 }
197
198 CommonClockClass description(const bt2c::CStringView description) const
199 {
200 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
201
202 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
203
204 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
205 throw MemoryError {};
206 }
207
208 return *this;
209 }
210
211 bt2c::CStringView description() const noexcept
212 {
213 return bt_clock_class_get_description(this->libObjPtr());
214 }
215
216 CommonClockClass uuid(const bt2c::UuidView uuid) const noexcept
217 {
218 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
219
220 bt_clock_class_set_uuid(this->libObjPtr(), uuid.data());
221 return *this;
222 }
223
224 bt2s::optional<bt2c::UuidView> uuid() const noexcept
225 {
226 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
227
228 if (uuid) {
229 return bt2c::UuidView {uuid};
230 }
231
232 return bt2s::nullopt;
233 }
234
235 template <typename LibValT>
236 CommonClockClass userAttributes(const CommonMapValue<LibValT> userAttrs) const noexcept
237 {
238 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
239
240 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
241 return *this;
242 }
243
244 UserAttributes userAttributes() const noexcept
245 {
246 return UserAttributes {
247 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
248 }
249
250 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
251 {
252 std::int64_t nsFromOrigin;
253 const auto status =
254 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
255
256 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
257 throw OverflowError {};
258 }
259
260 return nsFromOrigin;
261 }
262
263 Shared shared() const noexcept
264 {
265 return Shared::createWithRef(*this);
266 }
267 };
268
269 using ClockClass = CommonClockClass<bt_clock_class>;
270 using ConstClockClass = CommonClockClass<const bt_clock_class>;
271
272 namespace internal {
273
274 struct ClockClassTypeDescr
275 {
276 using Const = ConstClockClass;
277 using NonConst = ClockClass;
278 };
279
280 template <>
281 struct TypeDescr<ClockClass> : public ClockClassTypeDescr
282 {
283 };
284
285 template <>
286 struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
287 {
288 };
289
290 } /* namespace internal */
291 } /* namespace bt2 */
292
293 #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */
This page took 0.035446 seconds and 4 git commands to generate.