cpp-common: add `bt2s::optional`, alias of `nonstd::optional`
[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/bt2c/uuid-view.hpp"
17#include "cpp-common/bt2s/optional.hpp"
18
19#include "borrowed-object.hpp"
20#include "exc.hpp"
21#include "internal/utils.hpp"
22#include "shared-object.hpp"
23#include "value.hpp"
24
25namespace bt2 {
26namespace internal {
27
28struct ClockClassRefFuncs final
29{
30 static void get(const bt_clock_class * const libObjPtr) noexcept
31 {
32 bt_clock_class_get_ref(libObjPtr);
33 }
34
35 static void put(const bt_clock_class * const libObjPtr) noexcept
36 {
37 bt_clock_class_put_ref(libObjPtr);
38 }
39};
40
41template <typename LibObjT>
42struct CommonClockClassSpec;
43
44/* Functions specific to mutable clock classes */
45template <>
46struct CommonClockClassSpec<bt_clock_class> final
47{
48 static bt_value *userAttributes(bt_clock_class * const libObjPtr) noexcept
49 {
50 return bt_clock_class_borrow_user_attributes(libObjPtr);
51 }
52};
53
54/* Functions specific to constant clock classes */
55template <>
56struct CommonClockClassSpec<const bt_clock_class> final
57{
58 static const bt_value *userAttributes(const bt_clock_class * const libObjPtr) noexcept
59 {
60 return bt_clock_class_borrow_user_attributes_const(libObjPtr);
61 }
62};
63
64} /* namespace internal */
65
66class ClockClassOffset final
67{
68public:
69 explicit ClockClassOffset(const std::int64_t seconds, const std::uint64_t cycles) :
70 _mSeconds {seconds}, _mCycles {cycles}
71 {
72 }
73
74 std::int64_t seconds() const noexcept
75 {
76 return _mSeconds;
77 }
78
79 std::uint64_t cycles() const noexcept
80 {
81 return _mCycles;
82 }
83
84private:
85 std::int64_t _mSeconds;
86 std::uint64_t _mCycles;
87};
88
89template <typename LibObjT>
90class CommonClockClass final : public BorrowedObject<LibObjT>
91{
92private:
93 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
94 using typename BorrowedObject<LibObjT>::_LibObjPtr;
95
96public:
97 using Shared = SharedObject<CommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
98 using UserAttributes = internal::DepUserAttrs<LibObjT>;
99
100 explicit CommonClockClass(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
101 {
102 }
103
104 template <typename OtherLibObjT>
105 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
106 _ThisBorrowedObject {clkClass}
107 {
108 }
109
110 template <typename OtherLibObjT>
111 CommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
112 {
113 _ThisBorrowedObject::operator=(clkClass);
114 return *this;
115 }
116
117 CommonClockClass<const bt_clock_class> asConst() const noexcept
118 {
119 return CommonClockClass<const bt_clock_class> {*this};
120 }
121
122 void frequency(const std::uint64_t frequency) const noexcept
123 {
124 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
125
126 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
127 }
128
129 std::uint64_t frequency() const noexcept
130 {
131 return bt_clock_class_get_frequency(this->libObjPtr());
132 }
133
134 void offset(const ClockClassOffset& offset) const noexcept
135 {
136 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
137
138 bt_clock_class_set_offset(this->libObjPtr(), offset.seconds(), offset.cycles());
139 }
140
141 ClockClassOffset offset() const noexcept
142 {
143 std::int64_t seconds;
144 std::uint64_t cycles;
145
146 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
147 return ClockClassOffset {seconds, cycles};
148 }
149
150 void precision(const std::uint64_t precision) const noexcept
151 {
152 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
153
154 bt_clock_class_set_precision(this->libObjPtr(), precision);
155 }
156
157 std::uint64_t precision() const noexcept
158 {
159 return bt_clock_class_get_precision(this->libObjPtr());
160 }
161
162 void originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept
163 {
164 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
165
166 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
167 static_cast<bt_bool>(originIsUnixEpoch));
168 }
169
170 bool originIsUnixEpoch() const noexcept
171 {
172 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
173 }
174
175 void name(const char * const name) const
176 {
177 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
178
179 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
180
181 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
182 throw MemoryError {};
183 }
184 }
185
186 void name(const std::string& name) const
187 {
188 this->name(name.data());
189 }
190
191 const char *name() const noexcept
192 {
193 return bt_clock_class_get_name(this->libObjPtr());
194 }
195
196 void description(const char * const description) const
197 {
198 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
199
200 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
201
202 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
203 throw MemoryError {};
204 }
205 }
206
207 void description(const std::string& description) const
208 {
209 this->description(description.data());
210 }
211
212 const char *description() const noexcept
213 {
214 return bt_clock_class_get_description(this->libObjPtr());
215 }
216
217 void uuid(const std::uint8_t * const uuid) const noexcept
218 {
219 bt_clock_class_set_uuid(this->libObjPtr(), uuid);
220 }
221
222 bt2s::optional<bt2c::UuidView> uuid() const noexcept
223 {
224 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
225
226 if (uuid) {
227 return bt2c::UuidView {uuid};
228 }
229
230 return bt2s::nullopt;
231 }
232
233 template <typename LibValT>
234 void userAttributes(const CommonMapValue<LibValT> userAttrs) const noexcept
235 {
236 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
237
238 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
239 }
240
241 UserAttributes userAttributes() const noexcept
242 {
243 return UserAttributes {
244 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
245 }
246
247 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
248 {
249 std::int64_t nsFromOrigin;
250 const auto status =
251 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
252
253 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
254 throw OverflowError {};
255 }
256
257 return nsFromOrigin;
258 }
259
260 Shared shared() const noexcept
261 {
262 return Shared::createWithRef(*this);
263 }
264};
265
266using ClockClass = CommonClockClass<bt_clock_class>;
267using ConstClockClass = CommonClockClass<const bt_clock_class>;
268
269namespace internal {
270
271struct ClockClassTypeDescr
272{
273 using Const = ConstClockClass;
274 using NonConst = ClockClass;
275};
276
277template <>
278struct TypeDescr<ClockClass> : public ClockClassTypeDescr
279{
280};
281
282template <>
283struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
284{
285};
286
287} /* namespace internal */
288} /* namespace bt2 */
289
290#endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */
This page took 0.022752 seconds and 4 git commands to generate.