Add C++ interface for the libbabeltrace2 `bt_integer_range_*` API
[babeltrace.git] / src / cpp-common / bt2 / integer-range.hpp
CommitLineData
e0c2afae
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_INTEGER_RANGE_HPP
8#define BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_HPP
9
10#include <type_traits>
11#include <cstdint>
12#include <babeltrace2/babeltrace.h>
13
14#include "internal/borrowed-obj.hpp"
15
16namespace bt2 {
17
18namespace internal {
19
20template <typename ValueT>
21struct ConstIntegerRangeSpec;
22
23// Functions specific to unsigned integer ranges
24template <>
25struct ConstIntegerRangeSpec<const bt_integer_range_unsigned> final
26{
27 static std::uint64_t lower(const bt_integer_range_unsigned * const libRangePtr) noexcept
28 {
29 return bt_integer_range_unsigned_get_lower(libRangePtr);
30 }
31
32 static std::uint64_t upper(const bt_integer_range_unsigned * const libRangePtr) noexcept
33 {
34 return bt_integer_range_unsigned_get_upper(libRangePtr);
35 }
36
37 static bool isEqual(const bt_integer_range_unsigned * const libRangePtrA,
38 const bt_integer_range_unsigned * const libRangePtrB) noexcept
39 {
40 return static_cast<bool>(bt_integer_range_unsigned_is_equal(libRangePtrA, libRangePtrB));
41 }
42};
43
44// Functions specific to signed integer ranges
45template <>
46struct ConstIntegerRangeSpec<const bt_integer_range_signed> final
47{
48 static std::int64_t lower(const bt_integer_range_signed * const libRangePtr) noexcept
49 {
50 return bt_integer_range_signed_get_lower(libRangePtr);
51 }
52
53 static std::int64_t upper(const bt_integer_range_signed * const libRangePtr) noexcept
54 {
55 return bt_integer_range_signed_get_upper(libRangePtr);
56 }
57
58 static bool isEqual(const bt_integer_range_signed * const libRangePtrA,
59 const bt_integer_range_signed * const libRangePtrB) noexcept
60 {
61 return static_cast<bool>(bt_integer_range_signed_is_equal(libRangePtrA, libRangePtrB));
62 }
63};
64
65} // namespace internal
66
67template <typename LibObjT>
68class ConstIntegerRange final : public internal::BorrowedObj<LibObjT>
69{
70private:
71 using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
72 using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
73 using _ThisConstIntegerRange = ConstIntegerRange<LibObjT>;
74
75public:
76 using Value =
77 typename std::conditional<std::is_same<LibObjT, const bt_integer_range_unsigned>::value,
78 std::uint64_t, std::int64_t>::type;
79
80public:
81 explicit ConstIntegerRange(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
82 {
83 }
84
85 ConstIntegerRange(const _ThisConstIntegerRange& range) noexcept : _ThisBorrowedObj {range}
86 {
87 }
88
89 _ThisConstIntegerRange& operator=(const _ThisConstIntegerRange& range) noexcept
90 {
91 _ThisBorrowedObj::operator=(range);
92 return *this;
93 }
94
95 bool operator==(const _ThisConstIntegerRange& other) const noexcept
96 {
97 return internal::ConstIntegerRangeSpec<LibObjT>::isEqual(this->_libObjPtr(),
98 other._libObjPtr());
99 }
100
101 bool operator!=(const _ThisConstIntegerRange& other) const noexcept
102 {
103 return !(*this == other);
104 }
105
106 Value lower() const noexcept
107 {
108 return internal::ConstIntegerRangeSpec<LibObjT>::lower(this->_libObjPtr());
109 }
110
111 Value upper() const noexcept
112 {
113 return internal::ConstIntegerRangeSpec<LibObjT>::upper(this->_libObjPtr());
114 }
115};
116
117using ConstUnsignedIntegerRange = ConstIntegerRange<const bt_integer_range_unsigned>;
118using ConstSignedIntegerRange = ConstIntegerRange<const bt_integer_range_signed>;
119
120} // namespace bt2
121
122#endif // BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_HPP
This page took 0.032751 seconds and 4 git commands to generate.