cpp-common/bt2: rename `bt2::SharedObj` -> `bt2::SharedObject`
[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 "borrowed-object.hpp"
21 #include "exc.hpp"
22 #include "internal/utils.hpp"
23 #include "shared-object.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 BorrowedObject<LibObjT>
96 {
97 private:
98 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
99 using typename BorrowedObject<LibObjT>::_LibObjPtr;
100 using _ThisCommonClockClass = CommonClockClass<LibObjT>;
101
102 public:
103 using Shared = SharedObject<_ThisCommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
104
105 using UserAttributes =
106 typename std::conditional<std::is_const<LibObjT>::value, ConstMapValue, MapValue>::type;
107
108 explicit CommonClockClass(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
109 {
110 }
111
112 template <typename OtherLibObjT>
113 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
114 _ThisBorrowedObject {clkClass}
115 {
116 }
117
118 template <typename OtherLibObjT>
119 _ThisCommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
120 {
121 _ThisBorrowedObject::operator=(clkClass);
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
129 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
130 }
131
132 std::uint64_t frequency() const noexcept
133 {
134 return bt_clock_class_get_frequency(this->libObjPtr());
135 }
136
137 void offset(const ClockClassOffset& offset) noexcept
138 {
139 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
140
141 bt_clock_class_set_offset(this->libObjPtr(), offset.seconds(), offset.cycles());
142 }
143
144 ClockClassOffset offset() 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 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
157 bt_clock_class_set_precision(this->libObjPtr(), precision);
158 }
159
160 std::uint64_t precision() const noexcept
161 {
162 return bt_clock_class_get_precision(this->libObjPtr());
163 }
164
165 void originIsUnixEpoch(const bool originIsUnixEpoch) noexcept
166 {
167 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
168
169 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
170 static_cast<bt_bool>(originIsUnixEpoch));
171 }
172
173 bool originIsUnixEpoch() const noexcept
174 {
175 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
176 }
177
178 void name(const char * const name)
179 {
180 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
181
182 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
183
184 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
185 throw MemoryError {};
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 {
196 const auto name = bt_clock_class_get_name(this->libObjPtr());
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
209 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
210
211 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
212 throw MemoryError {};
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 {
223 const auto description = bt_clock_class_get_description(this->libObjPtr());
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 {
234 bt_clock_class_set_uuid(this->libObjPtr(), uuid);
235 }
236
237 nonstd::optional<bt2_common::UuidView> uuid() const noexcept
238 {
239 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
240
241 if (uuid) {
242 return bt2_common::UuidView {uuid};
243 }
244
245 return nonstd::nullopt;
246 }
247
248 template <typename LibValT>
249 void userAttributes(const CommonMapValue<LibValT> userAttrs)
250 {
251 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
252
253 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
254 }
255
256 ConstMapValue userAttributes() const noexcept
257 {
258 return ConstMapValue {internal::CommonClockClassSpec<const bt_clock_class>::userAttributes(
259 this->libObjPtr())};
260 }
261
262 UserAttributes userAttributes() noexcept
263 {
264 return UserAttributes {
265 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
266 }
267
268 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
269 {
270 std::int64_t nsFromOrigin;
271 const auto status =
272 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
273
274 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
275 throw OverflowError {};
276 }
277
278 return nsFromOrigin;
279 }
280
281 Shared shared() const noexcept
282 {
283 return Shared::createWithRef(*this);
284 }
285 };
286
287 using ClockClass = CommonClockClass<bt_clock_class>;
288 using ConstClockClass = CommonClockClass<const bt_clock_class>;
289
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 */
309 } /* namespace bt2 */
310
311 #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */
This page took 0.054406 seconds and 5 git commands to generate.