From e0c2afae96a9cdfd2a5bab7f97a6342131da499c Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Wed, 16 Dec 2020 16:16:55 -0500 Subject: [PATCH] Add C++ interface for the libbabeltrace2 `bt_integer_range_*` API This patch adds C++ wrappers for Babeltrace 2 integer range objects. The two new available types are `bt2::ConstUnsignedIntegerRange` and `bt2::ConstSignedIntegerRange`. Get the lower and upper bounds of an integer range object with its lower() and upper() methods. Compare compatible integer ranges with with operator==() and operator!=(). You can't create such objects: they are what integer range set methods (not part of `src/cpp-common/bt2` yet) return. Signed-off-by: Philippe Proulx Change-Id: I712a34756aa170b862b358e3553814ffa59547e8 Reviewed-on: https://review.lttng.org/c/babeltrace/+/4597 --- src/cpp-common/bt2/integer-range.hpp | 122 +++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/cpp-common/bt2/integer-range.hpp diff --git a/src/cpp-common/bt2/integer-range.hpp b/src/cpp-common/bt2/integer-range.hpp new file mode 100644 index 00000000..7e94e9e0 --- /dev/null +++ b/src/cpp-common/bt2/integer-range.hpp @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2020 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_HPP +#define BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_HPP + +#include +#include +#include + +#include "internal/borrowed-obj.hpp" + +namespace bt2 { + +namespace internal { + +template +struct ConstIntegerRangeSpec; + +// Functions specific to unsigned integer ranges +template <> +struct ConstIntegerRangeSpec final +{ + static std::uint64_t lower(const bt_integer_range_unsigned * const libRangePtr) noexcept + { + return bt_integer_range_unsigned_get_lower(libRangePtr); + } + + static std::uint64_t upper(const bt_integer_range_unsigned * const libRangePtr) noexcept + { + return bt_integer_range_unsigned_get_upper(libRangePtr); + } + + static bool isEqual(const bt_integer_range_unsigned * const libRangePtrA, + const bt_integer_range_unsigned * const libRangePtrB) noexcept + { + return static_cast(bt_integer_range_unsigned_is_equal(libRangePtrA, libRangePtrB)); + } +}; + +// Functions specific to signed integer ranges +template <> +struct ConstIntegerRangeSpec final +{ + static std::int64_t lower(const bt_integer_range_signed * const libRangePtr) noexcept + { + return bt_integer_range_signed_get_lower(libRangePtr); + } + + static std::int64_t upper(const bt_integer_range_signed * const libRangePtr) noexcept + { + return bt_integer_range_signed_get_upper(libRangePtr); + } + + static bool isEqual(const bt_integer_range_signed * const libRangePtrA, + const bt_integer_range_signed * const libRangePtrB) noexcept + { + return static_cast(bt_integer_range_signed_is_equal(libRangePtrA, libRangePtrB)); + } +}; + +} // namespace internal + +template +class ConstIntegerRange final : public internal::BorrowedObj +{ +private: + using typename internal::BorrowedObj::_ThisBorrowedObj; + using typename internal::BorrowedObj::_LibObjPtr; + using _ThisConstIntegerRange = ConstIntegerRange; + +public: + using Value = + typename std::conditional::value, + std::uint64_t, std::int64_t>::type; + +public: + explicit ConstIntegerRange(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr} + { + } + + ConstIntegerRange(const _ThisConstIntegerRange& range) noexcept : _ThisBorrowedObj {range} + { + } + + _ThisConstIntegerRange& operator=(const _ThisConstIntegerRange& range) noexcept + { + _ThisBorrowedObj::operator=(range); + return *this; + } + + bool operator==(const _ThisConstIntegerRange& other) const noexcept + { + return internal::ConstIntegerRangeSpec::isEqual(this->_libObjPtr(), + other._libObjPtr()); + } + + bool operator!=(const _ThisConstIntegerRange& other) const noexcept + { + return !(*this == other); + } + + Value lower() const noexcept + { + return internal::ConstIntegerRangeSpec::lower(this->_libObjPtr()); + } + + Value upper() const noexcept + { + return internal::ConstIntegerRangeSpec::upper(this->_libObjPtr()); + } +}; + +using ConstUnsignedIntegerRange = ConstIntegerRange; +using ConstSignedIntegerRange = ConstIntegerRange; + +} // namespace bt2 + +#endif // BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_HPP -- 2.34.1