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