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