cpp-common/bt2: use the "length" term everywhere instead of "size"
[babeltrace.git] / src / cpp-common / bt2 / clock-class.hpp
... / ...
CommitLineData
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
26namespace bt2 {
27namespace internal {
28
29struct 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
42template <typename LibObjT>
43struct CommonClockClassSpec;
44
45/* Functions specific to mutable clock classes */
46template <>
47struct 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 */
56template <>
57struct 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
67class ClockClassOffset final
68{
69public:
70 explicit ClockClassOffset(const std::int64_t seconds, const std::uint64_t cycles) :
71 _mSeconds {seconds}, _mCycles {cycles}
72 {
73 }
74
75 ClockClassOffset(const ClockClassOffset&) noexcept = default;
76 ClockClassOffset& operator=(const ClockClassOffset&) noexcept = default;
77
78 std::int64_t seconds() const noexcept
79 {
80 return _mSeconds;
81 }
82
83 std::uint64_t cycles() const noexcept
84 {
85 return _mCycles;
86 }
87
88private:
89 std::int64_t _mSeconds;
90 std::uint64_t _mCycles;
91};
92
93template <typename LibObjT>
94class CommonClockClass final : public BorrowedObject<LibObjT>
95{
96private:
97 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
98 using typename BorrowedObject<LibObjT>::_LibObjPtr;
99 using _ThisCommonClockClass = CommonClockClass<LibObjT>;
100
101public:
102 using Shared = SharedObject<_ThisCommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
103
104 using UserAttributes =
105 typename std::conditional<std::is_const<LibObjT>::value, ConstMapValue, MapValue>::type;
106
107 explicit CommonClockClass(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
108 {
109 }
110
111 template <typename OtherLibObjT>
112 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
113 _ThisBorrowedObject {clkClass}
114 {
115 }
116
117 template <typename OtherLibObjT>
118 _ThisCommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
119 {
120 _ThisBorrowedObject::operator=(clkClass);
121 return *this;
122 }
123
124 void frequency(const std::uint64_t frequency) const noexcept
125 {
126 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
127
128 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
129 }
130
131 std::uint64_t frequency() const noexcept
132 {
133 return bt_clock_class_get_frequency(this->libObjPtr());
134 }
135
136 void offset(const ClockClassOffset& offset) const noexcept
137 {
138 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
139
140 bt_clock_class_set_offset(this->libObjPtr(), offset.seconds(), offset.cycles());
141 }
142
143 ClockClassOffset offset() const noexcept
144 {
145 std::int64_t seconds;
146 std::uint64_t cycles;
147
148 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
149 return ClockClassOffset {seconds, cycles};
150 }
151
152 void precision(const std::uint64_t precision) const noexcept
153 {
154 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
155
156 bt_clock_class_set_precision(this->libObjPtr(), precision);
157 }
158
159 std::uint64_t precision() const noexcept
160 {
161 return bt_clock_class_get_precision(this->libObjPtr());
162 }
163
164 void originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept
165 {
166 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
167
168 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
169 static_cast<bt_bool>(originIsUnixEpoch));
170 }
171
172 bool originIsUnixEpoch() const noexcept
173 {
174 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
175 }
176
177 void name(const char * const name) const
178 {
179 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
180
181 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
182
183 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
184 throw MemoryError {};
185 }
186 }
187
188 void name(const std::string& name) const
189 {
190 this->name(name.data());
191 }
192
193 nonstd::optional<bpstd::string_view> name() const noexcept
194 {
195 const auto name = bt_clock_class_get_name(this->libObjPtr());
196
197 if (name) {
198 return name;
199 }
200
201 return nonstd::nullopt;
202 }
203
204 void description(const char * const description) const
205 {
206 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
207
208 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
209
210 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
211 throw MemoryError {};
212 }
213 }
214
215 void description(const std::string& description) const
216 {
217 this->description(description.data());
218 }
219
220 nonstd::optional<bpstd::string_view> description() const noexcept
221 {
222 const auto description = bt_clock_class_get_description(this->libObjPtr());
223
224 if (description) {
225 return description;
226 }
227
228 return nonstd::nullopt;
229 }
230
231 void uuid(const std::uint8_t * const uuid) const noexcept
232 {
233 bt_clock_class_set_uuid(this->libObjPtr(), uuid);
234 }
235
236 nonstd::optional<bt2_common::UuidView> uuid() const noexcept
237 {
238 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
239
240 if (uuid) {
241 return bt2_common::UuidView {uuid};
242 }
243
244 return nonstd::nullopt;
245 }
246
247 template <typename LibValT>
248 void userAttributes(const CommonMapValue<LibValT> userAttrs) const noexcept
249 {
250 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
251
252 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
253 }
254
255 UserAttributes userAttributes() const noexcept
256 {
257 return UserAttributes {
258 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
259 }
260
261 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
262 {
263 std::int64_t nsFromOrigin;
264 const auto status =
265 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
266
267 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
268 throw OverflowError {};
269 }
270
271 return nsFromOrigin;
272 }
273
274 Shared shared() const noexcept
275 {
276 return Shared::createWithRef(*this);
277 }
278};
279
280using ClockClass = CommonClockClass<bt_clock_class>;
281using ConstClockClass = CommonClockClass<const bt_clock_class>;
282
283namespace internal {
284
285struct ClockClassTypeDescr
286{
287 using Const = ConstClockClass;
288 using NonConst = ClockClass;
289};
290
291template <>
292struct TypeDescr<ClockClass> : public ClockClassTypeDescr
293{
294};
295
296template <>
297struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
298{
299};
300
301} /* namespace internal */
302} /* namespace bt2 */
303
304#endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */
This page took 0.044794 seconds and 4 git commands to generate.