Sort includes in C++ files
[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 <string>
12 #include <type_traits>
13
14 #include <babeltrace2/babeltrace.h>
15
16 #include "cpp-common/optional.hpp"
17 #include "cpp-common/string_view.hpp"
18 #include "cpp-common/uuid-view.hpp"
19
20 #include "exc.hpp"
21 #include "internal/borrowed-obj.hpp"
22 #include "internal/shared-obj.hpp"
23 #include "internal/utils.hpp"
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
46 /* Functions specific to mutable clock classes */
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
56 /* Functions specific to constant clock classes */
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
66 } /* namespace internal */
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>
95 class CommonClockClass final : public internal::BorrowedObj<LibObjT>
96 {
97 private:
98 using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
99 using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
100 using _ThisCommonClockClass = CommonClockClass<LibObjT>;
101
102 public:
103 using Shared =
104 internal::SharedObj<_ThisCommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
105
106 using UserAttributes =
107 typename std::conditional<std::is_const<LibObjT>::value, ConstMapValue, MapValue>::type;
108
109 explicit CommonClockClass(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
110 {
111 }
112
113 template <typename OtherLibObjT>
114 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
115 _ThisBorrowedObj {clkClass}
116 {
117 }
118
119 template <typename OtherLibObjT>
120 _ThisCommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
121 {
122 _ThisBorrowedObj::operator=(clkClass);
123 return *this;
124 }
125
126 void frequency(const std::uint64_t frequency) noexcept
127 {
128 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
129
130 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
131 }
132
133 std::uint64_t frequency() const noexcept
134 {
135 return bt_clock_class_get_frequency(this->libObjPtr());
136 }
137
138 void offset(const ClockClassOffset& offset) noexcept
139 {
140 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
141
142 bt_clock_class_set_offset(this->libObjPtr(), offset.seconds(), offset.cycles());
143 }
144
145 ClockClassOffset offset() const noexcept
146 {
147 std::int64_t seconds;
148 std::uint64_t cycles;
149
150 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
151 return ClockClassOffset {seconds, cycles};
152 }
153
154 void precision(const std::uint64_t precision) noexcept
155 {
156 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
157
158 bt_clock_class_set_precision(this->libObjPtr(), precision);
159 }
160
161 std::uint64_t precision() const noexcept
162 {
163 return bt_clock_class_get_precision(this->libObjPtr());
164 }
165
166 void originIsUnixEpoch(const bool originIsUnixEpoch) noexcept
167 {
168 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
169
170 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
171 static_cast<bt_bool>(originIsUnixEpoch));
172 }
173
174 bool originIsUnixEpoch() const noexcept
175 {
176 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
177 }
178
179 void name(const char * const name)
180 {
181 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
182
183 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
184
185 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
186 throw MemoryError {};
187 }
188 }
189
190 void name(const std::string& name)
191 {
192 this->name(name.data());
193 }
194
195 nonstd::optional<bpstd::string_view> name() const noexcept
196 {
197 const auto name = bt_clock_class_get_name(this->libObjPtr());
198
199 if (name) {
200 return name;
201 }
202
203 return nonstd::nullopt;
204 }
205
206 void description(const char * const description)
207 {
208 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
209
210 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
211
212 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
213 throw MemoryError {};
214 }
215 }
216
217 void description(const std::string& description)
218 {
219 this->description(description.data());
220 }
221
222 nonstd::optional<bpstd::string_view> description() const noexcept
223 {
224 const auto description = bt_clock_class_get_description(this->libObjPtr());
225
226 if (description) {
227 return description;
228 }
229
230 return nonstd::nullopt;
231 }
232
233 void uuid(const std::uint8_t * const uuid) noexcept
234 {
235 bt_clock_class_set_uuid(this->libObjPtr(), uuid);
236 }
237
238 nonstd::optional<bt2_common::UuidView> uuid() const noexcept
239 {
240 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
241
242 if (uuid) {
243 return bt2_common::UuidView {uuid};
244 }
245
246 return nonstd::nullopt;
247 }
248
249 template <typename LibValT>
250 void userAttributes(const CommonMapValue<LibValT> userAttrs)
251 {
252 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
253
254 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
255 }
256
257 ConstMapValue userAttributes() const noexcept
258 {
259 return ConstMapValue {internal::CommonClockClassSpec<const bt_clock_class>::userAttributes(
260 this->libObjPtr())};
261 }
262
263 UserAttributes userAttributes() noexcept
264 {
265 return UserAttributes {
266 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
267 }
268
269 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
270 {
271 std::int64_t nsFromOrigin;
272 const auto status =
273 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
274
275 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
276 throw OverflowError {};
277 }
278
279 return nsFromOrigin;
280 }
281
282 Shared shared() const noexcept
283 {
284 return Shared::createWithRef(*this);
285 }
286 };
287
288 using ClockClass = CommonClockClass<bt_clock_class>;
289 using ConstClockClass = CommonClockClass<const bt_clock_class>;
290
291 namespace internal {
292
293 struct ClockClassTypeDescr
294 {
295 using Const = ConstClockClass;
296 using NonConst = ClockClass;
297 };
298
299 template <>
300 struct TypeDescr<ClockClass> : public ClockClassTypeDescr
301 {
302 };
303
304 template <>
305 struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
306 {
307 };
308
309 } /* namespace internal */
310 } /* namespace bt2 */
311
312 #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */
This page took 0.036566 seconds and 4 git commands to generate.