Sort includes in C++ files
[babeltrace.git] / src / cpp-common / bt2 / clock-class.hpp
CommitLineData
cfc44919
PP
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
cfc44919
PP
10#include <cstdint>
11#include <string>
c802cacb
SM
12#include <type_traits>
13
cfc44919
PP
14#include <babeltrace2/babeltrace.h>
15
cfc44919
PP
16#include "cpp-common/optional.hpp"
17#include "cpp-common/string_view.hpp"
18#include "cpp-common/uuid-view.hpp"
c802cacb 19
39278ebc 20#include "exc.hpp"
c802cacb
SM
21#include "internal/borrowed-obj.hpp"
22#include "internal/shared-obj.hpp"
23#include "internal/utils.hpp"
cfc44919
PP
24#include "value.hpp"
25
26namespace bt2 {
27
28namespace internal {
29
30struct 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
43template <typename LibObjT>
44struct CommonClockClassSpec;
45
b5f55e9f 46/* Functions specific to mutable clock classes */
cfc44919
PP
47template <>
48struct 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
b5f55e9f 56/* Functions specific to constant clock classes */
cfc44919
PP
57template <>
58struct 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
b5f55e9f 66} /* namespace internal */
cfc44919
PP
67
68class ClockClassOffset final
69{
70public:
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
89private:
90 std::int64_t _mSeconds;
91 std::uint64_t _mCycles;
92};
93
94template <typename LibObjT>
95class CommonClockClass final : public internal::BorrowedObj<LibObjT>
96{
97private:
98 using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
99 using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
100 using _ThisCommonClockClass = CommonClockClass<LibObjT>;
101
102public:
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>
100fa861 114 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
cfc44919
PP
115 _ThisBorrowedObj {clkClass}
116 {
117 }
118
119 template <typename OtherLibObjT>
100fa861 120 _ThisCommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
cfc44919
PP
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
341a67c4 130 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
cfc44919
PP
131 }
132
133 std::uint64_t frequency() const noexcept
134 {
341a67c4 135 return bt_clock_class_get_frequency(this->libObjPtr());
cfc44919
PP
136 }
137
138 void offset(const ClockClassOffset& offset) noexcept
139 {
140 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
141
341a67c4 142 bt_clock_class_set_offset(this->libObjPtr(), offset.seconds(), offset.cycles());
cfc44919
PP
143 }
144
145 ClockClassOffset offset() const noexcept
146 {
147 std::int64_t seconds;
148 std::uint64_t cycles;
149
341a67c4 150 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
cfc44919
PP
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
341a67c4 158 bt_clock_class_set_precision(this->libObjPtr(), precision);
cfc44919
PP
159 }
160
161 std::uint64_t precision() const noexcept
162 {
341a67c4 163 return bt_clock_class_get_precision(this->libObjPtr());
cfc44919
PP
164 }
165
166 void originIsUnixEpoch(const bool originIsUnixEpoch) noexcept
167 {
168 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
169
341a67c4 170 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
cfc44919
PP
171 static_cast<bt_bool>(originIsUnixEpoch));
172 }
173
174 bool originIsUnixEpoch() const noexcept
175 {
341a67c4 176 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
cfc44919
PP
177 }
178
179 void name(const char * const name)
180 {
181 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
182
341a67c4 183 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
cfc44919
PP
184
185 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
39278ebc 186 throw MemoryError {};
cfc44919
PP
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 {
341a67c4 197 const auto name = bt_clock_class_get_name(this->libObjPtr());
cfc44919
PP
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
341a67c4 210 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
cfc44919
PP
211
212 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
39278ebc 213 throw MemoryError {};
cfc44919
PP
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 {
341a67c4 224 const auto description = bt_clock_class_get_description(this->libObjPtr());
cfc44919
PP
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 {
341a67c4 235 bt_clock_class_set_uuid(this->libObjPtr(), uuid);
cfc44919
PP
236 }
237
238 nonstd::optional<bt2_common::UuidView> uuid() const noexcept
239 {
341a67c4 240 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
cfc44919
PP
241
242 if (uuid) {
243 return bt2_common::UuidView {uuid};
244 }
245
246 return nonstd::nullopt;
247 }
248
249 template <typename LibValT>
100fa861 250 void userAttributes(const CommonMapValue<LibValT> userAttrs)
cfc44919
PP
251 {
252 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
253
341a67c4 254 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
cfc44919
PP
255 }
256
257 ConstMapValue userAttributes() const noexcept
258 {
259 return ConstMapValue {internal::CommonClockClassSpec<const bt_clock_class>::userAttributes(
341a67c4 260 this->libObjPtr())};
cfc44919
PP
261 }
262
263 UserAttributes userAttributes() noexcept
264 {
265 return UserAttributes {
341a67c4 266 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
cfc44919
PP
267 }
268
269 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
270 {
271 std::int64_t nsFromOrigin;
272 const auto status =
341a67c4 273 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
cfc44919
PP
274
275 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
39278ebc 276 throw OverflowError {};
cfc44919
PP
277 }
278
279 return nsFromOrigin;
280 }
281
282 Shared shared() const noexcept
283 {
c9c0b6e2 284 return Shared::createWithRef(*this);
cfc44919
PP
285 }
286};
287
288using ClockClass = CommonClockClass<bt_clock_class>;
289using ConstClockClass = CommonClockClass<const bt_clock_class>;
290
4927bae7
PP
291namespace internal {
292
293struct ClockClassTypeDescr
294{
295 using Const = ConstClockClass;
296 using NonConst = ClockClass;
297};
298
299template <>
300struct TypeDescr<ClockClass> : public ClockClassTypeDescr
301{
302};
303
304template <>
305struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
306{
307};
308
309} /* namespace internal */
b5f55e9f 310} /* namespace bt2 */
cfc44919 311
b5f55e9f 312#endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */
This page took 0.044778 seconds and 4 git commands to generate.