cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[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
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
17namespace bt2 {
e0c2afae
PP
18namespace internal {
19
20template <typename ValueT>
21struct ConstIntegerRangeSpec;
22
b5f55e9f 23/* Functions specific to unsigned integer ranges */
e0c2afae
PP
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
b5f55e9f 44/* Functions specific to signed integer ranges */
e0c2afae
PP
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
b5f55e9f 65} /* namespace internal */
e0c2afae
PP
66
67template <typename LibObjT>
0d218157 68class ConstIntegerRange final : public BorrowedObject<LibObjT>
e0c2afae
PP
69{
70private:
0d218157 71 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
e0c2afae
PP
72
73public:
d246c457
PP
74 using typename BorrowedObject<LibObjT>::LibObjPtr;
75
e0c2afae
PP
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:
d246c457 81 explicit ConstIntegerRange(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
e0c2afae
PP
82 {
83 }
84
ac19444e 85 bool operator==(const ConstIntegerRange& other) const noexcept
e0c2afae 86 {
341a67c4
FD
87 return internal::ConstIntegerRangeSpec<LibObjT>::isEqual(this->libObjPtr(),
88 other.libObjPtr());
e0c2afae
PP
89 }
90
ac19444e 91 bool operator!=(const ConstIntegerRange& other) const noexcept
e0c2afae
PP
92 {
93 return !(*this == other);
94 }
95
96 Value lower() const noexcept
97 {
341a67c4 98 return internal::ConstIntegerRangeSpec<LibObjT>::lower(this->libObjPtr());
e0c2afae
PP
99 }
100
101 Value upper() const noexcept
102 {
341a67c4 103 return internal::ConstIntegerRangeSpec<LibObjT>::upper(this->libObjPtr());
e0c2afae
PP
104 }
105};
106
107using ConstUnsignedIntegerRange = ConstIntegerRange<const bt_integer_range_unsigned>;
108using ConstSignedIntegerRange = ConstIntegerRange<const bt_integer_range_signed>;
109
b5f55e9f 110} /* namespace bt2 */
e0c2afae 111
b5f55e9f 112#endif /* BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_HPP */
This page took 0.044316 seconds and 4 git commands to generate.