cpp-common/bt2c: move `uuid-view.*pp` contents to `uuid.hpp`
[babeltrace.git] / src / cpp-common / bt2c / uuid.hpp
index 86d1217f9e3d4ad408b7f4d94fb431b68a5ae686..30ff54a86b59a1908af1e2a68a8df5b9533209bf 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
+ * SPDX-FileCopyrightText: 2020-2023 Philippe Proulx <pproulx@efficios.com>
+ * SPDX-FileCopyrightText: 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
  *
  * SPDX-License-Identifier: MIT
  */
 
 #include <algorithm>
 #include <array>
+#include <cstdint>
 #include <string>
 
 #include "common/assert.h"
 #include "common/uuid.h"
 
-#include "uuid-view.hpp"
-
 namespace bt2c {
 
+class Uuid;
+
+/*
+ * A view on existing UUID data.
+ *
+ * A `UuidView` object doesn't contain its UUID data: see `Uuid` for a
+ * UUID data container.
+ */
+class UuidView final
+{
+public:
+    using Val = std::uint8_t;
+    using ConstIter = const Val *;
+
+public:
+    explicit UuidView(const Val * const uuid) noexcept : _mUuid {uuid}
+    {
+        BT_ASSERT_DBG(uuid);
+    }
+
+    explicit UuidView(const Uuid& uuid) noexcept;
+    UuidView(const UuidView&) noexcept = default;
+    UuidView& operator=(const UuidView&) noexcept = default;
+
+    UuidView& operator=(const Val * const uuid) noexcept
+    {
+        _mUuid = uuid;
+        return *this;
+    }
+
+    operator Uuid() const noexcept;
+
+    std::string str() const
+    {
+        std::string s;
+
+        s.resize(BT_UUID_STR_LEN);
+        bt_uuid_to_str(_mUuid, &s[0]);
+
+        return s;
+    }
+
+    bool operator==(const UuidView& other) const noexcept
+    {
+        return bt_uuid_compare(_mUuid, other._mUuid) == 0;
+    }
+
+    bool operator!=(const UuidView& other) const noexcept
+    {
+        return !(*this == other);
+    }
+
+    bool operator<(const UuidView& other) const noexcept
+    {
+        return bt_uuid_compare(_mUuid, other._mUuid) < 0;
+    }
+
+    static constexpr std::size_t size() noexcept
+    {
+        return BT_UUID_LEN;
+    }
+
+    const Val *data() const noexcept
+    {
+        return _mUuid;
+    }
+
+    Val operator[](const std::size_t index) const noexcept
+    {
+        return _mUuid[index];
+    }
+
+    ConstIter begin() const noexcept
+    {
+        return _mUuid;
+    }
+
+    ConstIter end() const noexcept
+    {
+        return _mUuid + this->size();
+    }
+
+    bool isNil() const noexcept
+    {
+        return std::all_of(this->begin(), this->end(), [](const std::uint8_t byte) {
+            return byte == 0;
+        });
+    }
+
+private:
+    const Val *_mUuid;
+};
+
 /*
  * A universally unique identifier.
  *
@@ -149,6 +242,15 @@ private:
     std::array<Val, UuidView::size()> _mUuid = {};
 };
 
+inline UuidView::UuidView(const Uuid& uuid) noexcept : _mUuid {uuid.data()}
+{
+}
+
+inline UuidView::operator Uuid() const noexcept
+{
+    return Uuid {*this};
+}
+
 } /* namespace bt2c */
 
 #endif /* BABELTRACE_CPP_COMMON_BT2C_UUID_HPP */
This page took 0.024983 seconds and 4 git commands to generate.