cpp-common: remove `cfg-*.hpp` and `log-cfg.hpp`
[babeltrace.git] / src / cpp-common / bt2 / integer-range-set.hpp
index 52fc540dcbb10c5b2f26bf8f4c29c4e2603dd638..1cfbfc3a133ac0ec0b9dc2da6601463a0732ecff 100644 (file)
@@ -9,15 +9,17 @@
 
 #include <cstdint>
 #include <type_traits>
+
 #include <babeltrace2/babeltrace.h>
 
-#include "internal/borrowed-obj.hpp"
-#include "internal/utils.hpp"
+#include "borrowed-object-iterator.hpp"
+#include "borrowed-object.hpp"
+#include "exc.hpp"
 #include "integer-range.hpp"
-#include "lib-error.hpp"
+#include "internal/utils.hpp"
+#include "shared-object.hpp"
 
 namespace bt2 {
-
 namespace internal {
 
 template <typename LibObjT>
@@ -26,39 +28,39 @@ struct IntegerRangeSetRefFuncs;
 template <>
 struct IntegerRangeSetRefFuncs<const bt_integer_range_set_unsigned> final
 {
-    static void get(const bt_integer_range_set_unsigned * const libObjPtr)
+    static void get(const bt_integer_range_set_unsigned * const libObjPtr) noexcept
     {
         bt_integer_range_set_unsigned_get_ref(libObjPtr);
     }
 
-    static void put(const bt_integer_range_set_unsigned * const libObjPtr)
+    static void put(const bt_integer_range_set_unsigned * const libObjPtr) noexcept
     {
-        bt_integer_range_set_unsigned_get_ref(libObjPtr);
+        bt_integer_range_set_unsigned_put_ref(libObjPtr);
     }
 };
 
 template <>
 struct IntegerRangeSetRefFuncs<const bt_integer_range_set_signed> final
 {
-    static void get(const bt_integer_range_set_signed * const libObjPtr)
+    static void get(const bt_integer_range_set_signed * const libObjPtr) noexcept
     {
         bt_integer_range_set_signed_get_ref(libObjPtr);
     }
 
-    static void put(const bt_integer_range_set_signed * const libObjPtr)
+    static void put(const bt_integer_range_set_signed * const libObjPtr) noexcept
     {
-        bt_integer_range_set_signed_get_ref(libObjPtr);
+        bt_integer_range_set_signed_put_ref(libObjPtr);
     }
 };
 
 template <typename LibObjT>
 struct CommonIntegerRangeSetSpec;
 
-// Functions specific to unsigned integer range sets
+/* Functions specific to unsigned integer range sets */
 template <>
 struct CommonIntegerRangeSetSpec<const bt_integer_range_set_unsigned> final
 {
-    static std::uint64_t size(const bt_integer_range_set_unsigned * const libRangePtr) noexcept
+    static std::uint64_t length(const bt_integer_range_set_unsigned * const libRangePtr) noexcept
     {
         return bt_integer_range_set_get_range_count(
             bt_integer_range_set_unsigned_as_range_set_const(libRangePtr));
@@ -91,11 +93,11 @@ struct CommonIntegerRangeSetSpec<const bt_integer_range_set_unsigned> final
     }
 };
 
-// Functions specific to signed integer range sets
+/* Functions specific to signed integer range sets */
 template <>
 struct CommonIntegerRangeSetSpec<const bt_integer_range_set_signed> final
 {
-    static std::uint64_t size(const bt_integer_range_set_signed * const libRangePtr) noexcept
+    static std::uint64_t length(const bt_integer_range_set_signed * const libRangePtr) noexcept
     {
         return bt_integer_range_set_get_range_count(
             bt_integer_range_set_signed_as_range_set_const(libRangePtr));
@@ -127,36 +129,30 @@ struct CommonIntegerRangeSetSpec<const bt_integer_range_set_signed> final
     }
 };
 
-} // namespace internal
+} /* namespace internal */
 
 template <typename LibObjT>
-class CommonIntegerRangeSet final : public internal::BorrowedObj<LibObjT>
+class CommonIntegerRangeSet final : public BorrowedObject<LibObjT>
 {
-    // Allow operator==() to call `other._libObjPtr()`
-    friend class CommonIntegerRangeSet<bt_integer_range_set_unsigned>;
-    friend class CommonIntegerRangeSet<const bt_integer_range_set_unsigned>;
-    friend class CommonIntegerRangeSet<bt_integer_range_set_signed>;
-    friend class CommonIntegerRangeSet<const bt_integer_range_set_signed>;
-
 private:
-    using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
-    using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
+    using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
+    using typename BorrowedObject<LibObjT>::_LibObjPtr;
     using _ConstLibObjT = typename std::add_const<LibObjT>::type;
-    using _RefFuncs = internal::IntegerRangeSetRefFuncs<_ConstLibObjT>;
     using _Spec = internal::CommonIntegerRangeSetSpec<_ConstLibObjT>;
-    using _ThisCommonIntegerRangeSet = CommonIntegerRangeSet<LibObjT>;
 
 public:
-    using Shared = internal::SharedObj<_ThisCommonIntegerRangeSet, LibObjT, _RefFuncs>;
+    using Shared = SharedObject<CommonIntegerRangeSet, LibObjT,
+                                internal::IntegerRangeSetRefFuncs<_ConstLibObjT>>;
 
     using Range = typename std::conditional<
         std::is_same<_ConstLibObjT, const bt_integer_range_set_unsigned>::value,
         ConstUnsignedIntegerRange, ConstSignedIntegerRange>::type;
 
     using Value = typename Range::Value;
+    using Iterator = BorrowedObjectIterator<CommonIntegerRangeSet>;
 
     explicit CommonIntegerRangeSet(const _LibObjPtr libObjPtr) noexcept :
-        _ThisBorrowedObj {libObjPtr}
+        _ThisBorrowedObject {libObjPtr}
     {
     }
 
@@ -165,59 +161,75 @@ public:
         const auto libObjPtr = _Spec::create();
 
         internal::validateCreatedObjPtr(libObjPtr);
-        return Shared {_ThisCommonIntegerRangeSet {libObjPtr}};
+        return CommonIntegerRangeSet::Shared::createWithoutRef(libObjPtr);
     }
 
     template <typename OtherLibObjT>
-    CommonIntegerRangeSet(const CommonIntegerRangeSet<OtherLibObjT>& rangeSet) noexcept :
-        _ThisBorrowedObj {rangeSet}
+    CommonIntegerRangeSet(const CommonIntegerRangeSet<OtherLibObjT> rangeSet) noexcept :
+        _ThisBorrowedObject {rangeSet}
     {
     }
 
     template <typename OtherLibObjT>
-    _ThisCommonIntegerRangeSet&
-    operator=(const CommonIntegerRangeSet<OtherLibObjT>& rangeSet) noexcept
+    CommonIntegerRangeSet operator=(const CommonIntegerRangeSet<OtherLibObjT> rangeSet) noexcept
     {
-        _ThisBorrowedObj::operator=(rangeSet);
+        _ThisBorrowedObject::operator=(rangeSet);
         return *this;
     }
 
+    _ConstLibObjT asConst() const noexcept
+    {
+        return _ConstLibObjT {*this};
+    }
+
     template <typename OtherLibObjT>
-    bool operator==(const CommonIntegerRangeSet<OtherLibObjT>& other) const noexcept
+    bool operator==(const CommonIntegerRangeSet<OtherLibObjT> other) const noexcept
     {
-        return _Spec::isEqual(this->_libObjPtr(), other._libObjPtr());
+        return _Spec::isEqual(this->libObjPtr(), other.libObjPtr());
     }
 
     template <typename OtherLibObjT>
-    bool operator!=(const CommonIntegerRangeSet<OtherLibObjT>& other) const noexcept
+    bool operator!=(const CommonIntegerRangeSet<OtherLibObjT> other) const noexcept
     {
         return !(*this == other);
     }
 
-    void addRange(const Value lower, const Value upper)
+    void addRange(const Value lower, const Value upper) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(
+            !std::is_const<LibObjT>::value,
+            "Not available with `bt2::ConstUnsignedIntegerRangeSet` or `bt2::ConstSignedIntegerRangeSet`.");
 
-        const auto status = _Spec::addRange(this->_libObjPtr(), lower, upper);
+        const auto status = _Spec::addRange(this->libObjPtr(), lower, upper);
 
         if (status == BT_INTEGER_RANGE_SET_ADD_RANGE_STATUS_MEMORY_ERROR) {
-            throw LibMemoryError {};
+            throw MemoryError {};
         }
     }
 
-    std::uint64_t size() const noexcept
+    std::uint64_t length() const noexcept
     {
-        return _Spec::size(this->_libObjPtr());
+        return _Spec::length(this->libObjPtr());
     }
 
     Range operator[](const std::uint64_t index) const noexcept
     {
-        return Range {_Spec::rangeByIndex(this->_libObjPtr(), index)};
+        return Range {_Spec::rangeByIndex(this->libObjPtr(), index)};
+    }
+
+    Iterator begin() const noexcept
+    {
+        return Iterator {*this, 0};
+    }
+
+    Iterator end() const noexcept
+    {
+        return Iterator {*this, this->length()};
     }
 
     Shared shared() const noexcept
     {
-        return Shared {*this};
+        return Shared::createWithRef(*this);
     }
 };
 
@@ -226,6 +238,41 @@ using ConstUnsignedIntegerRangeSet = CommonIntegerRangeSet<const bt_integer_rang
 using SignedIntegerRangeSet = CommonIntegerRangeSet<bt_integer_range_set_signed>;
 using ConstSignedIntegerRangeSet = CommonIntegerRangeSet<const bt_integer_range_set_signed>;
 
-} // namespace bt2
+namespace internal {
+
+struct UnsignedIntegerRangeSetTypeDescr
+{
+    using Const = ConstUnsignedIntegerRangeSet;
+    using NonConst = UnsignedIntegerRangeSet;
+};
+
+template <>
+struct TypeDescr<UnsignedIntegerRangeSet> : public UnsignedIntegerRangeSetTypeDescr
+{
+};
+
+template <>
+struct TypeDescr<ConstUnsignedIntegerRangeSet> : public UnsignedIntegerRangeSetTypeDescr
+{
+};
+
+struct SignedIntegerRangeSetTypeDescr
+{
+    using Const = ConstSignedIntegerRangeSet;
+    using NonConst = SignedIntegerRangeSet;
+};
+
+template <>
+struct TypeDescr<SignedIntegerRangeSet> : public SignedIntegerRangeSetTypeDescr
+{
+};
+
+template <>
+struct TypeDescr<ConstSignedIntegerRangeSet> : public SignedIntegerRangeSetTypeDescr
+{
+};
+
+} /* namespace internal */
+} /* namespace bt2 */
 
-#endif // BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_SET_HPP
+#endif /* BABELTRACE_CPP_COMMON_BT2_INTEGER_RANGE_SET_HPP */
This page took 0.025869 seconds and 4 git commands to generate.