`ctf` plugin: add `ctf::IntRange` class template
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 30 Nov 2023 03:00:06 +0000 (03:00 +0000)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 4 Sep 2024 19:05:14 +0000 (15:05 -0400)
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 <eeppeliteloop@gmail.com>
Change-Id: Ic2ab4b4b8dd97bce8dc69a1a7331fed8a46d9254
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7922
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12251

src/Makefile.am
src/plugins/ctf/common/metadata/int-range.hpp [new file with mode: 0644]

index 237ae8900ec2073ee57ce31a7c650560df9de568..1034a7acc0571680e81be4f6419ffe3797029078 100644 (file)
@@ -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 (file)
index 0000000..73d9e68
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+ * SPDX-License-Identifier: MIT
+ *
+ * Copyright 2022-2024 Philippe Proulx <pproulx@efficios.com>
+ */
+
+#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 <typename ValT>
+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<unsigned long long>;
+using SIntRange = IntRange<long long>;
+
+} /* namespace ctf */
+
+#endif /* BABELTRACE_PLUGINS_CTF_COMMON_METADATA_INT_RANGE_HPP */
This page took 0.0266 seconds and 4 git commands to generate.