cpp-common/bt2/value.hpp: move static assertion (wrong place)
[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
0d218157 20#include "borrowed-object.hpp"
39278ebc 21#include "exc.hpp"
c802cacb 22#include "internal/utils.hpp"
7f5cdaf0 23#include "shared-object.hpp"
cfc44919
PP
24#include "value.hpp"
25
26namespace bt2 {
cfc44919
PP
27namespace internal {
28
29struct ClockClassRefFuncs final
30{
c677c492 31 static void get(const bt_clock_class * const libObjPtr) noexcept
cfc44919
PP
32 {
33 bt_clock_class_get_ref(libObjPtr);
34 }
35
c677c492 36 static void put(const bt_clock_class * const libObjPtr) noexcept
cfc44919
PP
37 {
38 bt_clock_class_put_ref(libObjPtr);
39 }
40};
41
42template <typename LibObjT>
43struct CommonClockClassSpec;
44
b5f55e9f 45/* Functions specific to mutable clock classes */
cfc44919
PP
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
b5f55e9f 55/* Functions specific to constant clock classes */
cfc44919
PP
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
b5f55e9f 65} /* namespace internal */
cfc44919
PP
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>
0d218157 94class CommonClockClass final : public BorrowedObject<LibObjT>
cfc44919
PP
95{
96private:
0d218157
PP
97 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
98 using typename BorrowedObject<LibObjT>::_LibObjPtr;
cfc44919
PP
99 using _ThisCommonClockClass = CommonClockClass<LibObjT>;
100
101public:
7f5cdaf0 102 using Shared = SharedObject<_ThisCommonClockClass, LibObjT, internal::ClockClassRefFuncs>;
cfc44919 103
8047a175 104 using UserAttributes = internal::DepUserAttrs<LibObjT>;
cfc44919 105
0d218157 106 explicit CommonClockClass(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
cfc44919
PP
107 {
108 }
109
110 template <typename OtherLibObjT>
100fa861 111 CommonClockClass(const CommonClockClass<OtherLibObjT> clkClass) noexcept :
0d218157 112 _ThisBorrowedObject {clkClass}
cfc44919
PP
113 {
114 }
115
116 template <typename OtherLibObjT>
100fa861 117 _ThisCommonClockClass& operator=(const CommonClockClass<OtherLibObjT> clkClass) noexcept
cfc44919 118 {
0d218157 119 _ThisBorrowedObject::operator=(clkClass);
cfc44919
PP
120 return *this;
121 }
122
328a274a
PP
123 CommonClockClass<const bt_clock_class> asConst() const noexcept
124 {
125 return CommonClockClass<const bt_clock_class> {*this};
126 }
127
dcb8ae9b 128 void frequency(const std::uint64_t frequency) const noexcept
cfc44919 129 {
5c895f64 130 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 131
341a67c4 132 bt_clock_class_set_frequency(this->libObjPtr(), frequency);
cfc44919
PP
133 }
134
135 std::uint64_t frequency() const noexcept
136 {
341a67c4 137 return bt_clock_class_get_frequency(this->libObjPtr());
cfc44919
PP
138 }
139
dcb8ae9b 140 void offset(const ClockClassOffset& offset) const noexcept
cfc44919 141 {
5c895f64 142 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 143
341a67c4 144 bt_clock_class_set_offset(this->libObjPtr(), offset.seconds(), offset.cycles());
cfc44919
PP
145 }
146
147 ClockClassOffset offset() const noexcept
148 {
149 std::int64_t seconds;
150 std::uint64_t cycles;
151
341a67c4 152 bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles);
cfc44919
PP
153 return ClockClassOffset {seconds, cycles};
154 }
155
dcb8ae9b 156 void precision(const std::uint64_t precision) const noexcept
cfc44919 157 {
5c895f64 158 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 159
341a67c4 160 bt_clock_class_set_precision(this->libObjPtr(), precision);
cfc44919
PP
161 }
162
163 std::uint64_t precision() const noexcept
164 {
341a67c4 165 return bt_clock_class_get_precision(this->libObjPtr());
cfc44919
PP
166 }
167
dcb8ae9b 168 void originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept
cfc44919 169 {
5c895f64 170 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 171
341a67c4 172 bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(),
cfc44919
PP
173 static_cast<bt_bool>(originIsUnixEpoch));
174 }
175
176 bool originIsUnixEpoch() const noexcept
177 {
341a67c4 178 return static_cast<bool>(bt_clock_class_origin_is_unix_epoch(this->libObjPtr()));
cfc44919
PP
179 }
180
dcb8ae9b 181 void name(const char * const name) const
cfc44919 182 {
5c895f64 183 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 184
341a67c4 185 const auto status = bt_clock_class_set_name(this->libObjPtr(), name);
cfc44919
PP
186
187 if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) {
39278ebc 188 throw MemoryError {};
cfc44919
PP
189 }
190 }
191
dcb8ae9b 192 void name(const std::string& name) const
cfc44919
PP
193 {
194 this->name(name.data());
195 }
196
197 nonstd::optional<bpstd::string_view> name() const noexcept
198 {
341a67c4 199 const auto name = bt_clock_class_get_name(this->libObjPtr());
cfc44919
PP
200
201 if (name) {
202 return name;
203 }
204
205 return nonstd::nullopt;
206 }
207
dcb8ae9b 208 void description(const char * const description) const
cfc44919 209 {
5c895f64 210 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 211
341a67c4 212 const auto status = bt_clock_class_set_description(this->libObjPtr(), description);
cfc44919
PP
213
214 if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) {
39278ebc 215 throw MemoryError {};
cfc44919
PP
216 }
217 }
218
dcb8ae9b 219 void description(const std::string& description) const
cfc44919
PP
220 {
221 this->description(description.data());
222 }
223
224 nonstd::optional<bpstd::string_view> description() const noexcept
225 {
341a67c4 226 const auto description = bt_clock_class_get_description(this->libObjPtr());
cfc44919
PP
227
228 if (description) {
229 return description;
230 }
231
232 return nonstd::nullopt;
233 }
234
dcb8ae9b 235 void uuid(const std::uint8_t * const uuid) const noexcept
cfc44919 236 {
341a67c4 237 bt_clock_class_set_uuid(this->libObjPtr(), uuid);
cfc44919
PP
238 }
239
240 nonstd::optional<bt2_common::UuidView> uuid() const noexcept
241 {
341a67c4 242 const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
cfc44919
PP
243
244 if (uuid) {
245 return bt2_common::UuidView {uuid};
246 }
247
248 return nonstd::nullopt;
249 }
250
251 template <typename LibValT>
b7ffa6f0 252 void userAttributes(const CommonMapValue<LibValT> userAttrs) const noexcept
cfc44919 253 {
5c895f64 254 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstClockClass`.");
cfc44919 255
341a67c4 256 bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr());
cfc44919
PP
257 }
258
dcb8ae9b 259 UserAttributes userAttributes() const noexcept
cfc44919
PP
260 {
261 return UserAttributes {
341a67c4 262 internal::CommonClockClassSpec<LibObjT>::userAttributes(this->libObjPtr())};
cfc44919
PP
263 }
264
265 std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const
266 {
267 std::int64_t nsFromOrigin;
268 const auto status =
341a67c4 269 bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin);
cfc44919
PP
270
271 if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) {
39278ebc 272 throw OverflowError {};
cfc44919
PP
273 }
274
275 return nsFromOrigin;
276 }
277
278 Shared shared() const noexcept
279 {
c9c0b6e2 280 return Shared::createWithRef(*this);
cfc44919
PP
281 }
282};
283
284using ClockClass = CommonClockClass<bt_clock_class>;
285using ConstClockClass = CommonClockClass<const bt_clock_class>;
286
4927bae7
PP
287namespace internal {
288
289struct ClockClassTypeDescr
290{
291 using Const = ConstClockClass;
292 using NonConst = ClockClass;
293};
294
295template <>
296struct TypeDescr<ClockClass> : public ClockClassTypeDescr
297{
298};
299
300template <>
301struct TypeDescr<ConstClockClass> : public ClockClassTypeDescr
302{
303};
304
305} /* namespace internal */
b5f55e9f 306} /* namespace bt2 */
cfc44919 307
b5f55e9f 308#endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */
This page took 0.046855 seconds and 4 git commands to generate.