cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[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 10#include <cstdint>
c802cacb
SM
11#include <type_traits>
12
cfc44919
PP
13#include <babeltrace2/babeltrace.h>
14
e7f0f07b 15#include "cpp-common/bt2c/c-string-view.hpp"
12958bb9 16#include "cpp-common/bt2c/uuid.hpp"
c022776a 17#include "cpp-common/bt2s/optional.hpp"
c802cacb 18
0d218157 19#include "borrowed-object.hpp"
39278ebc 20#include "exc.hpp"
c802cacb 21#include "internal/utils.hpp"
7f5cdaf0 22#include "shared-object.hpp"
cfc44919
PP
23#include "value.hpp"
24
25namespace bt2 {
cfc44919
PP
26namespace internal {
27
28struct ClockClassRefFuncs final
29{
c677c492 30 static void get(const bt_clock_class * const libObjPtr) noexcept
cfc44919
PP
31 {
32 bt_clock_class_get_ref(libObjPtr);
33 }
34
c677c492 35 static void put(const bt_clock_class * const libObjPtr) noexcept
cfc44919
PP
36 {
37 bt_clock_class_put_ref(libObjPtr);
38 }
39};
40
41template <typename LibObjT>
42struct CommonClockClassSpec;
43
b5f55e9f 44/* Functions specific to mutable clock classes */
cfc44919
PP
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
b5f55e9f 54/* Functions specific to constant clock classes */
cfc44919
PP
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
b5f55e9f 64} /* namespace internal */
cfc44919 65
aa5aac94 66class ClockOffset final
cfc44919
PP
67{
68public:
aa5aac94 69 explicit ClockOffset(const std::int64_t seconds, const std::uint64_t cycles) :
cfc44919
PP
70 _mSeconds {seconds}, _mCycles {cycles}
71 {
72 }
73
cfc44919
PP
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>
0d218157 90class CommonClockClass final : public BorrowedObject<LibObjT>
cfc44919
PP
91{
92private:
0d218157 93 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
cfc44919
PP
94
95public:
d246c457 96 using typename BorrowedObject<LibObjT>::LibObjPtr;
ac19444e 97 using Shared = SharedObject<CommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
8047a175 98 using UserAttributes = internal::DepUserAttrs<LibObjT>;
cfc44919 99
d246c457 100 explicit CommonClockClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
cfc44919
PP
101 {
102 }
103
104 template <typename OtherLibObjT>
100fa861 105 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
0d218157 106 _ThisBorrowedObject {clkClass}
cfc44919
PP
107 {
108 }
109
110 template <typename OtherLibObjT>
ac19444e 111 CommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
cfc44919 112 {
0d218157 113 _ThisBorrowedObject::operator=(clkClass);
cfc44919
PP
114 return *this;
115 }
116
328a274a
PP
117 CommonClockClass<const bt_clock_class> asConst() const noexcept
118 {
119 return CommonClockClass<const bt_clock_class> {*this};
120 }
121
2a24eba8 122 CommonClockClass frequency(const std::uint64_t frequency) const noexcept
cfc44919 123 {
5c895f64 124 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 125
341a67c4 126 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
2a24eba8 127 return *this;
cfc44919
PP
128 }
129
130 std::uint64_t frequency() const noexcept
131 {
341a67c4 132 return bt_clock_class_get_frequency(this->libObjPtr());
cfc44919
PP
133 }
134
2a24eba8 135 CommonClockClass offsetFromOrigin(const ClockOffset& offsetFromOrigin) const noexcept
cfc44919 136 {
5c895f64 137 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 138
aa5aac94
PP
139 bt_clock_class_set_offset(this->libObjPtr(), offsetFromOrigin.seconds(),
140 offsetFromOrigin.cycles());
2a24eba8 141 return *this;
cfc44919
PP
142 }
143
aa5aac94 144 ClockOffset offsetFromOrigin() const noexcept
cfc44919
PP
145 {
146 std::int64_t seconds;
147 std::uint64_t cycles;
148
341a67c4 149 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
aa5aac94 150 return ClockOffset {seconds, cycles};
cfc44919
PP
151 }
152
2a24eba8 153 CommonClockClass precision(const std::uint64_t precision) const noexcept
cfc44919 154 {
5c895f64 155 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 156
341a67c4 157 bt_clock_class_set_precision(this->libObjPtr(), precision);
2a24eba8 158 return *this;
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
2a24eba8 166 CommonClockClass originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept
cfc44919 167 {
5c895f64 168 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 169
341a67c4 170 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
cfc44919 171 static_cast<bt_bool>(originIsUnixEpoch));
2a24eba8 172 return *this;
cfc44919
PP
173 }
174
175 bool originIsUnixEpoch() const noexcept
176 {
341a67c4 177 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
cfc44919
PP
178 }
179
2a24eba8 180 CommonClockClass name(const bt2c::CStringView name) const
cfc44919 181 {
5c895f64 182 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 183
341a67c4 184 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
cfc44919
PP
185
186 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
39278ebc 187 throw MemoryError {};
cfc44919 188 }
2a24eba8
SM
189
190 return *this;
cfc44919
PP
191 }
192
e7f0f07b 193 bt2c::CStringView name() const noexcept
cfc44919 194 {
5cc5088c 195 return bt_clock_class_get_name(this->libObjPtr());
cfc44919
PP
196 }
197
2a24eba8 198 CommonClockClass description(const bt2c::CStringView description) const
cfc44919 199 {
5c895f64 200 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 201
341a67c4 202 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
cfc44919
PP
203
204 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
39278ebc 205 throw MemoryError {};
cfc44919 206 }
2a24eba8
SM
207
208 return *this;
cfc44919
PP
209 }
210
e7f0f07b 211 bt2c::CStringView description() const noexcept
cfc44919 212 {
5cc5088c 213 return bt_clock_class_get_description(this->libObjPtr());
cfc44919
PP
214 }
215
72ff0d99 216 CommonClockClass uuid(const bt2c::UuidView uuid) const noexcept
cfc44919 217 {
77160ac2
SM
218 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
219
72ff0d99 220 bt_clock_class_set_uuid(this->libObjPtr(), uuid.data());
2a24eba8 221 return *this;
cfc44919
PP
222 }
223
c022776a 224 bt2s::optional<bt2c::UuidView> uuid() const noexcept
cfc44919 225 {
341a67c4 226 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
cfc44919
PP
227
228 if (uuid) {
094bf3f2 229 return bt2c::UuidView {uuid};
cfc44919
PP
230 }
231
c022776a 232 return bt2s::nullopt;
cfc44919
PP
233 }
234
235 template <typename LibValT>
2a24eba8 236 CommonClockClass userAttributes(const CommonMapValue<LibValT> userAttrs) const noexcept
cfc44919 237 {
5c895f64 238 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 239
341a67c4 240 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
2a24eba8 241 return *this;
cfc44919
PP
242 }
243
dcb8ae9b 244 UserAttributes userAttributes() const noexcept
cfc44919
PP
245 {
246 return UserAttributes {
341a67c4 247 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
cfc44919
PP
248 }
249
250 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
251 {
252 std::int64_t nsFromOrigin;
253 const auto status =
341a67c4 254 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
cfc44919
PP
255
256 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
39278ebc 257 throw OverflowError {};
cfc44919
PP
258 }
259
260 return nsFromOrigin;
261 }
262
263 Shared shared() const noexcept
264 {
c9c0b6e2 265 return Shared::createWithRef(*this);
cfc44919
PP
266 }
267};
268
269using ClockClass = CommonClockClass<bt_clock_class>;
270using ConstClockClass = CommonClockClass<const bt_clock_class>;
271
4927bae7
PP
272namespace internal {
273
274struct ClockClassTypeDescr
275{
276 using Const = ConstClockClass;
277 using NonConst = ClockClass;
278};
279
280template <>
281struct TypeDescr<ClockClass> : public ClockClassTypeDescr
282{
283};
284
285template <>
286struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
287{
288};
289
290} /* namespace internal */
b5f55e9f 291} /* namespace bt2 */
cfc44919 292
b5f55e9f 293#endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */
This page took 0.055904 seconds and 4 git commands to generate.