6c8c66380588cff939459947c95cec9895ba1259
[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/bt2c/c-string-view.hpp"
17 #include "cpp-common/bt2c/uuid.hpp"
18 #include "cpp-common/bt2s/optional.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 namespace internal {
28
29 struct ClockClassRefFuncs final
30 {
31 static void get(const bt_clock_class * const libObjPtr) noexcept
32 {
33 bt_clock_class_get_ref(libObjPtr);
34 }
35
36 static void put(const bt_clock_class * const libObjPtr) noexcept
37 {
38 bt_clock_class_put_ref(libObjPtr);
39 }
40 };
41
42 template <typename LibObjT>
43 struct CommonClockClassSpec;
44
45 /* Functions specific to mutable clock classes */
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
55 /* Functions specific to constant clock classes */
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
65 } /* namespace internal */
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
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>
91 class CommonClockClass final : public BorrowedObject<LibObjT>
92 {
93 private:
94 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
95
96 public:
97 using typename BorrowedObject<LibObjT>::LibObjPtr;
98 using Shared = SharedObject<CommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
99 using UserAttributes = internal::DepUserAttrs<LibObjT>;
100
101 explicit CommonClockClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
102 {
103 }
104
105 template <typename OtherLibObjT>
106 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
107 _ThisBorrowedObject {clkClass}
108 {
109 }
110
111 template <typename OtherLibObjT>
112 CommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
113 {
114 _ThisBorrowedObject::operator=(clkClass);
115 return *this;
116 }
117
118 CommonClockClass<const bt_clock_class> asConst() const noexcept
119 {
120 return CommonClockClass<const bt_clock_class> {*this};
121 }
122
123 void frequency(const std::uint64_t frequency) const noexcept
124 {
125 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
126
127 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
128 }
129
130 std::uint64_t frequency() const noexcept
131 {
132 return bt_clock_class_get_frequency(this->libObjPtr());
133 }
134
135 void offset(const ClockClassOffset& offset) 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(), offset.seconds(), offset.cycles());
140 }
141
142 ClockClassOffset offset() const noexcept
143 {
144 std::int64_t seconds;
145 std::uint64_t cycles;
146
147 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
148 return ClockClassOffset {seconds, cycles};
149 }
150
151 void precision(const std::uint64_t precision) const noexcept
152 {
153 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
154
155 bt_clock_class_set_precision(this->libObjPtr(), precision);
156 }
157
158 std::uint64_t precision() const noexcept
159 {
160 return bt_clock_class_get_precision(this->libObjPtr());
161 }
162
163 void originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept
164 {
165 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
166
167 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
168 static_cast<bt_bool>(originIsUnixEpoch));
169 }
170
171 bool originIsUnixEpoch() const noexcept
172 {
173 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
174 }
175
176 void name(const char * const name) const
177 {
178 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
179
180 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
181
182 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
183 throw MemoryError {};
184 }
185 }
186
187 void name(const std::string& name) const
188 {
189 this->name(name.data());
190 }
191
192 bt2c::CStringView name() const noexcept
193 {
194 return bt_clock_class_get_name(this->libObjPtr());
195 }
196
197 void description(const char * const description) const
198 {
199 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
200
201 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
202
203 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
204 throw MemoryError {};
205 }
206 }
207
208 void description(const std::string& description) const
209 {
210 this->description(description.data());
211 }
212
213 bt2c::CStringView description() const noexcept
214 {
215 return bt_clock_class_get_description(this->libObjPtr());
216 }
217
218 void uuid(const std::uint8_t * const uuid) const noexcept
219 {
220 bt_clock_class_set_uuid(this->libObjPtr(), uuid);
221 }
222
223 bt2s::optional<bt2c::UuidView> uuid() const noexcept
224 {
225 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
226
227 if (uuid) {
228 return bt2c::UuidView {uuid};
229 }
230
231 return bt2s::nullopt;
232 }
233
234 template <typename LibValT>
235 void userAttributes(const CommonMapValue<LibValT> userAttrs) const noexcept
236 {
237 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
238
239 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
240 }
241
242 UserAttributes userAttributes() const noexcept
243 {
244 return UserAttributes {
245 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
246 }
247
248 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
249 {
250 std::int64_t nsFromOrigin;
251 const auto status =
252 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
253
254 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
255 throw OverflowError {};
256 }
257
258 return nsFromOrigin;
259 }
260
261 Shared shared() const noexcept
262 {
263 return Shared::createWithRef(*this);
264 }
265 };
266
267 using ClockClass = CommonClockClass<bt_clock_class>;
268 using ConstClockClass = CommonClockClass<const bt_clock_class>;
269
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 */
289 } /* namespace bt2 */
290
291 #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */
This page took 0.035457 seconds and 3 git commands to generate.