From: Philippe Proulx Date: Thu, 30 Nov 2023 03:00:06 +0000 (+0000) Subject: `ctf` plugin: add `ctf::IntRange` class template X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=8eba18cb258a42ba96a222cefd09799f1ccb19f8;p=babeltrace.git `ctf` plugin: add `ctf::IntRange` class template This new class template is conceptually the same as the libbabeltrace2 equivalent (`bt2::ConstIntegerRange`), but it's templated and avoids having to make libbabeltrace2 function calls to access data. That being said, the contains() and intersects() methods don't have their libbabeltrace2 equivalent. Signed-off-by: Philippe Proulx Change-Id: Ic2ab4b4b8dd97bce8dc69a1a7331fed8a46d9254 Reviewed-on: https://review.lttng.org/c/babeltrace/+/7922 Reviewed-on: https://review.lttng.org/c/babeltrace/+/12251 --- diff --git a/src/Makefile.am b/src/Makefile.am index 237ae890..1034a7ac 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -676,6 +676,7 @@ endif # ctf plugin plugins_ctf_babeltrace_plugin_ctf_la_SOURCES = \ + plugins/ctf/common/metadata/int-range.hpp \ plugins/ctf/common/src/bfcr/bfcr.cpp \ plugins/ctf/common/src/bfcr/bfcr.hpp \ plugins/ctf/common/src/clk-cls-cfg.hpp \ diff --git a/src/plugins/ctf/common/metadata/int-range.hpp b/src/plugins/ctf/common/metadata/int-range.hpp new file mode 100644 index 00000000..73d9e683 --- /dev/null +++ b/src/plugins/ctf/common/metadata/int-range.hpp @@ -0,0 +1,131 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright 2022-2024 Philippe Proulx + */ + +#ifndef BABELTRACE_PLUGINS_CTF_COMMON_METADATA_INT_RANGE_HPP +#define BABELTRACE_PLUGINS_CTF_COMMON_METADATA_INT_RANGE_HPP + +#include "common/assert.h" + +namespace ctf { + +/* + * An integer range is a simple pair of lower and upper values of type + * `ValT`, both included in the range. + */ +template +class IntRange final +{ +public: + /* Type of the lower and upper values */ + using Val = ValT; + +private: + /* + * Builds an integer range [`lower`, `upper`], validating the + * lower/upper precondition if `validate` is true. + */ + explicit IntRange(const ValT lower, const ValT upper, const bool validate) : + _mLower {lower}, _mUpper {upper} + { + if (validate) { + BT_ASSERT_DBG(lower <= upper); + } + } + +public: + /* + * Builds an integer range [`lower`, `upper`]. + * + * `upper` must be greater than or equal to `lower`. + */ + explicit IntRange(const ValT lower, const ValT upper) : IntRange {lower, upper, true} + { + } + + /* + * Builds the temporary integer range [`lower`, `upper`]. + * + * `upper` may be less than `lower`. + */ + static IntRange makeTemp(const ValT lower, const ValT upper) + { + return IntRange {lower, upper, false}; + } + + /* + * Lower bound of this integer range. + */ + ValT lower() const noexcept + { + return _mLower; + } + + /* + * Upper bound of this integer range. + */ + ValT upper() const noexcept + { + return _mUpper; + } + + /* + * Returns whether or not this integer range contains the value + * `val`. + */ + bool contains(const ValT val) const noexcept + { + return val >= _mLower && val <= _mUpper; + } + + /* + * Returns whether or not the integer range `range` intersects with + * this integer range, that is, `range` and this integer range share + * at least one integer value. + */ + bool intersects(const IntRange& other) const noexcept + { + return _mLower <= other.upper() && other.lower() <= _mUpper; + } + + bool operator==(const IntRange& other) const noexcept + { + return other.lower() == _mLower && other.upper() == _mUpper; + } + + bool operator!=(const IntRange& other) const noexcept + { + return !(*this == other); + } + + bool operator<(const IntRange& other) const noexcept + { + if (_mLower < other._mLower) { + return true; + } + + if (other._mLower < _mLower) { + return false; + } + + if (_mUpper < other._mUpper) { + return true; + } + + return false; + } + +private: + ValT _mLower; + ValT _mUpper; +}; + +/* Convenient aliases */ +using UIntRange = IntRange; +using SIntRange = IntRange; + +} /* namespace ctf */ + +#endif /* BABELTRACE_PLUGINS_CTF_COMMON_METADATA_INT_RANGE_HPP */