cpp-common: rename `bt2_common` namespace -> `bt2c`
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 11 Dec 2023 21:05:13 +0000 (16:05 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Thu, 14 Dec 2023 15:57:04 +0000 (10:57 -0500)
I always end up using the `bt2c` namespace alias in `*.cpp` files
anyway.

Also put all the `bt2c` stuff in `cpp-common/bt2c` to make it clear.

What remains in `cpp-common`, but outside `cpp-common/bt2c`, are the
libbabeltrace2 C++ bindings (`bt2` namespace) and external projects.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ife8527aa961025f28d06707eb9f374cafea572f5
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11390
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
32 files changed:
src/Makefile.am
src/cpp-common/align.hpp [deleted file]
src/cpp-common/bt2/clock-class.hpp
src/cpp-common/bt2/exc.hpp
src/cpp-common/bt2/trace-ir.hpp
src/cpp-common/bt2c/align.hpp [new file with mode: 0644]
src/cpp-common/bt2c/endian.hpp [new file with mode: 0644]
src/cpp-common/bt2c/exc.hpp [new file with mode: 0644]
src/cpp-common/bt2c/glib-up.hpp [new file with mode: 0644]
src/cpp-common/bt2c/lib-str.hpp [new file with mode: 0644]
src/cpp-common/bt2c/libc-up.hpp [new file with mode: 0644]
src/cpp-common/bt2c/make-unique.hpp [new file with mode: 0644]
src/cpp-common/bt2c/read-fixed-len-int.hpp [new file with mode: 0644]
src/cpp-common/bt2c/safe-ops.hpp [new file with mode: 0644]
src/cpp-common/bt2c/std-int.hpp [new file with mode: 0644]
src/cpp-common/bt2c/uuid-view.cpp [new file with mode: 0644]
src/cpp-common/bt2c/uuid-view.hpp [new file with mode: 0644]
src/cpp-common/bt2c/uuid.hpp [new file with mode: 0644]
src/cpp-common/bt2c/vector.hpp [new file with mode: 0644]
src/cpp-common/endian.hpp [deleted file]
src/cpp-common/exc.hpp [deleted file]
src/cpp-common/glib-up.hpp [deleted file]
src/cpp-common/lib-str.hpp [deleted file]
src/cpp-common/libc-up.hpp [deleted file]
src/cpp-common/make-unique.hpp [deleted file]
src/cpp-common/read-fixed-len-int.hpp [deleted file]
src/cpp-common/safe-ops.hpp [deleted file]
src/cpp-common/std-int.hpp [deleted file]
src/cpp-common/uuid-view.cpp [deleted file]
src/cpp-common/uuid-view.hpp [deleted file]
src/cpp-common/uuid.hpp [deleted file]
src/cpp-common/vector.hpp [deleted file]

index 02b5a173b6adce97f949c5a1b110e6939fb4109e..b5d24fe1c91113a4d0d6c1d31b2521cee0439bcb 100644 (file)
@@ -20,10 +20,10 @@ noinst_HEADERS = \
        cpp-common/bt2/component-port.hpp \
        cpp-common/bt2/exc.hpp \
        cpp-common/bt2/field-class.hpp \
-       cpp-common/bt2/field.hpp \
        cpp-common/bt2/field-path.hpp \
-       cpp-common/bt2/integer-range.hpp \
+       cpp-common/bt2/field.hpp \
        cpp-common/bt2/integer-range-set.hpp \
+       cpp-common/bt2/integer-range.hpp \
        cpp-common/bt2/internal/utils.hpp \
        cpp-common/bt2/logging.hpp \
        cpp-common/bt2/message-array.hpp \
@@ -36,26 +36,27 @@ noinst_HEADERS = \
        cpp-common/bt2/self-component-port.hpp \
        cpp-common/bt2/self-message-iterator-configuration.hpp \
        cpp-common/bt2/self-message-iterator.hpp \
+       cpp-common/bt2/shared-object.hpp \
        cpp-common/bt2/trace-ir.hpp \
        cpp-common/bt2/type-traits.hpp \
        cpp-common/bt2/value.hpp \
        cpp-common/bt2/wrap.hpp \
-       cpp-common/align.hpp \
-       cpp-common/endian.hpp \
-       cpp-common/exc.hpp \
-       cpp-common/glib-up.hpp \
-       cpp-common/libc-up.hpp \
-       cpp-common/lib-str.hpp \
-       cpp-common/make-unique.hpp \
+       cpp-common/bt2c/align.hpp \
+       cpp-common/bt2c/endian.hpp \
+       cpp-common/bt2c/exc.hpp \
+       cpp-common/bt2c/glib-up.hpp \
+       cpp-common/bt2c/lib-str.hpp \
+       cpp-common/bt2c/libc-up.hpp \
+       cpp-common/bt2c/make-unique.hpp \
+       cpp-common/bt2c/read-fixed-len-int.hpp \
+       cpp-common/bt2c/safe-ops.hpp \
+       cpp-common/bt2c/std-int.hpp \
+       cpp-common/bt2c/uuid-view.hpp \
+       cpp-common/bt2c/uuid.hpp \
+       cpp-common/bt2c/vector.hpp \
        cpp-common/nlohmann/json.hpp \
        cpp-common/optional.hpp \
-       cpp-common/read-fixed-len-int.hpp \
-       cpp-common/safe-ops.hpp \
-       cpp-common/std-int.hpp \
-       cpp-common/string_view.hpp \
-       cpp-common/uuid.hpp \
-       cpp-common/uuid-view.hpp \
-       cpp-common/vector.hpp
+       cpp-common/string_view.hpp
 
 ## This target generates an include file that contains the git version
 ## string of the current branch, it must be continuously updated when
diff --git a/src/cpp-common/align.hpp b/src/cpp-common/align.hpp
deleted file mode 100644 (file)
index 3a036c8..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
- *
- * SPDX-License-Identifier: MIT
- */
-
-#ifndef BABELTRACE_CPP_COMMON_ALIGN_HPP
-#define BABELTRACE_CPP_COMMON_ALIGN_HPP
-
-#include <type_traits>
-
-#include "common/align.h"
-
-namespace bt2_common {
-
-template <typename ValT, typename AlignT>
-ValT align(const ValT val, const AlignT align) noexcept
-{
-    static_assert(std::is_unsigned<ValT>::value, "`ValT` is unsigned.");
-    return BT_ALIGN(val, static_cast<ValT>(align));
-}
-
-} /* namespace bt2_common */
-
-#endif /* BABELTRACE_CPP_COMMON_ALIGN_HPP */
index 72064f844f49bf6ce36f4e81f2a57682ab078b49..64e093f40004a7c6f33f0cd4c6f1b443854714a7 100644 (file)
@@ -13,8 +13,8 @@
 
 #include <babeltrace2/babeltrace.h>
 
+#include "cpp-common/bt2c/uuid-view.hpp"
 #include "cpp-common/optional.hpp"
-#include "cpp-common/uuid-view.hpp"
 
 #include "borrowed-object.hpp"
 #include "exc.hpp"
@@ -219,12 +219,12 @@ public:
         bt_clock_class_set_uuid(this->libObjPtr(), uuid);
     }
 
-    nonstd::optional<bt2_common::UuidView> uuid() const noexcept
+    nonstd::optional<bt2c::UuidView> uuid() const noexcept
     {
         const auto uuid = bt_clock_class_get_uuid(this->libObjPtr());
 
         if (uuid) {
-            return bt2_common::UuidView {uuid};
+            return bt2c::UuidView {uuid};
         }
 
         return nonstd::nullopt;
index 71997c3eb85487e01239079333ed35e38b9c8b67..723368c6378155ecfbced551cfa209b2f412260c 100644 (file)
@@ -7,14 +7,14 @@
 #ifndef BABELTRACE_CPP_COMMON_BT2_EXC_HPP
 #define BABELTRACE_CPP_COMMON_BT2_EXC_HPP
 
-#include "cpp-common/exc.hpp"
+#include "cpp-common/bt2c/exc.hpp"
 
 namespace bt2 {
 
-using Error = bt2_common::Error;
-using OverflowError = bt2_common::OverflowError;
-using MemoryError = bt2_common::MemoryError;
-using TryAgain = bt2_common::TryAgain;
+using Error = bt2c::Error;
+using OverflowError = bt2c::OverflowError;
+using MemoryError = bt2c::MemoryError;
+using TryAgain = bt2c::TryAgain;
 
 } /* namespace bt2 */
 
index b6f349be9a6f7545292078460e7f6c2bdd3c94ff..9df6abbf732ec9cc99d0794d4b1e8ad523845731 100644 (file)
@@ -685,17 +685,17 @@ public:
         return bt_trace_get_name(this->libObjPtr());
     }
 
-    void uuid(const bt2_common::UuidView& uuid) const noexcept
+    void uuid(const bt2c::UuidView& uuid) const noexcept
     {
         bt_trace_set_uuid(this->libObjPtr(), uuid.begin());
     }
 
-    nonstd::optional<bt2_common::UuidView> uuid() const noexcept
+    nonstd::optional<bt2c::UuidView> uuid() const noexcept
     {
         const auto uuid = bt_trace_get_uuid(this->libObjPtr());
 
         if (uuid) {
-            return bt2_common::UuidView {uuid};
+            return bt2c::UuidView {uuid};
         }
 
         return nonstd::nullopt;
diff --git a/src/cpp-common/bt2c/align.hpp b/src/cpp-common/bt2c/align.hpp
new file mode 100644 (file)
index 0000000..ef443aa
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_ALIGN_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_ALIGN_HPP
+
+#include <type_traits>
+
+#include "common/align.h"
+
+namespace bt2c {
+
+template <typename ValT, typename AlignT>
+ValT align(const ValT val, const AlignT align) noexcept
+{
+    static_assert(std::is_unsigned<ValT>::value, "`ValT` is unsigned.");
+    return BT_ALIGN(val, static_cast<ValT>(align));
+}
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_ALIGN_HPP */
diff --git a/src/cpp-common/bt2c/endian.hpp b/src/cpp-common/bt2c/endian.hpp
new file mode 100644 (file)
index 0000000..4d315b2
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_ENDIAN_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_ENDIAN_HPP
+
+#include <cstdint>
+
+#include "compat/endian.h" /* IWYU pragma: keep  */
+
+namespace bt2c {
+
+static inline std::uint8_t littleEndianToNative(const std::uint8_t val) noexcept
+{
+    return val;
+}
+
+static inline std::uint8_t bigEndianToNative(const std::uint8_t val) noexcept
+{
+    return val;
+}
+
+static inline std::int8_t littleEndianToNative(const std::int8_t val) noexcept
+{
+    return val;
+}
+
+static inline std::int8_t bigEndianToNative(const std::int8_t val) noexcept
+{
+    return val;
+}
+
+static inline std::uint16_t littleEndianToNative(const std::uint16_t val) noexcept
+{
+    return static_cast<std::uint16_t>(le16toh(val));
+}
+
+static inline std::uint16_t bigEndianToNative(const std::uint16_t val) noexcept
+{
+    return static_cast<std::uint16_t>(be16toh(val));
+}
+
+static inline std::int16_t littleEndianToNative(const std::int16_t val) noexcept
+{
+    return static_cast<std::int16_t>(littleEndianToNative(static_cast<std::uint16_t>(val)));
+}
+
+static inline std::int16_t bigEndianToNative(const std::int16_t val) noexcept
+{
+    return static_cast<std::int16_t>(bigEndianToNative(static_cast<std::uint16_t>(val)));
+}
+
+static inline std::uint32_t littleEndianToNative(const std::uint32_t val) noexcept
+{
+    return static_cast<std::uint32_t>(le32toh(val));
+}
+
+static inline std::uint32_t bigEndianToNative(const std::uint32_t val) noexcept
+{
+    return static_cast<std::uint32_t>(be32toh(val));
+}
+
+static inline std::int32_t littleEndianToNative(const std::int32_t val) noexcept
+{
+    return static_cast<std::int32_t>(littleEndianToNative(static_cast<std::uint32_t>(val)));
+}
+
+static inline std::int32_t bigEndianToNative(const std::int32_t val) noexcept
+{
+    return static_cast<std::int32_t>(bigEndianToNative(static_cast<std::uint32_t>(val)));
+}
+
+static inline std::uint64_t littleEndianToNative(const std::uint64_t val) noexcept
+{
+    return static_cast<std::uint64_t>(le64toh(val));
+}
+
+static inline std::uint64_t bigEndianToNative(const std::uint64_t val) noexcept
+{
+    return static_cast<std::uint64_t>(be64toh(val));
+}
+
+static inline std::int64_t littleEndianToNative(const std::int64_t val) noexcept
+{
+    return static_cast<std::int64_t>(littleEndianToNative(static_cast<std::uint64_t>(val)));
+}
+
+static inline std::int64_t bigEndianToNative(const std::int64_t val) noexcept
+{
+    return static_cast<std::int64_t>(bigEndianToNative(static_cast<std::uint64_t>(val)));
+}
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_ENDIAN_HPP */
diff --git a/src/cpp-common/bt2c/exc.hpp b/src/cpp-common/bt2c/exc.hpp
new file mode 100644 (file)
index 0000000..3138fdb
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_EXC_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_EXC_HPP
+
+#include <exception>
+#include <new>
+#include <stdexcept>
+#include <string>
+
+namespace bt2c {
+
+/*
+ * End of iteration.
+ */
+class End : public std::exception
+{
+public:
+    explicit End() noexcept : std::exception {}
+    {
+    }
+};
+
+/*
+ * General error.
+ */
+class Error : public std::runtime_error
+{
+public:
+    explicit Error(std::string msg = "Error") : std::runtime_error {std::move(msg)}
+    {
+    }
+};
+
+/*
+ * Overflow error.
+ */
+class OverflowError : public Error
+{
+public:
+    explicit OverflowError() noexcept : Error {"Overflow error"}
+    {
+    }
+};
+
+/*
+ * Memory error.
+ */
+class MemoryError : public std::bad_alloc
+{
+public:
+    explicit MemoryError() noexcept : std::bad_alloc {}
+    {
+    }
+};
+
+/*
+ * Not available right now: try again later.
+ */
+class TryAgain : public std::exception
+{
+public:
+    explicit TryAgain() noexcept : std::exception {}
+    {
+    }
+};
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_EXC_HPP */
diff --git a/src/cpp-common/bt2c/glib-up.hpp b/src/cpp-common/bt2c/glib-up.hpp
new file mode 100644 (file)
index 0000000..bfe2ead
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2022 EfficiOS, inc.
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_GLIB_UP_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_GLIB_UP_HPP
+
+#include <memory>
+
+#include <glib.h>
+
+namespace bt2c {
+namespace internal {
+
+struct GCharDeleter final
+{
+    void operator()(gchar * const p) noexcept
+    {
+        g_free(p);
+    }
+};
+
+} /* namespace internal */
+
+using GCharUP = std::unique_ptr<gchar, internal::GCharDeleter>;
+
+namespace internal {
+
+struct GStringDeleter final
+{
+    void operator()(GString * const str)
+    {
+        g_string_free(str, TRUE);
+    }
+};
+
+} /* namespace internal */
+
+using GStringUP = std::unique_ptr<GString, internal::GStringDeleter>;
+
+namespace internal {
+
+struct GDirDeleter final
+{
+    void operator()(GDir * const dir)
+    {
+        g_dir_close(dir);
+    }
+};
+
+} /* namespace internal */
+
+using GDirUP = std::unique_ptr<GDir, internal::GDirDeleter>;
+
+namespace internal {
+
+struct GMappedFileDeleter final
+{
+    void operator()(GMappedFile * const f)
+    {
+        g_mapped_file_unref(f);
+    }
+};
+
+} /* namespace internal */
+
+using GMappedFileUP = std::unique_ptr<GMappedFile, internal::GMappedFileDeleter>;
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_GLIB_UP_HPP */
diff --git a/src/cpp-common/bt2c/lib-str.hpp b/src/cpp-common/bt2c/lib-str.hpp
new file mode 100644 (file)
index 0000000..815f105
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2022 EfficiOS, Inc.
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_LIB_STR_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_LIB_STR_HPP
+
+#include "cpp-common/bt2/message.hpp"
+
+namespace bt2c {
+
+static inline const char *messageTypeStr(const bt2::MessageType type)
+{
+    return bt_common_message_type_string(static_cast<bt_message_type>(type));
+}
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_LIB_STR_HPP */
diff --git a/src/cpp-common/bt2c/libc-up.hpp b/src/cpp-common/bt2c/libc-up.hpp
new file mode 100644 (file)
index 0000000..fa7468f
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2022 EfficiOS, inc.
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_LIBC_UP_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_LIBC_UP_HPP
+
+#include <cstdio>
+#include <memory>
+
+namespace bt2c {
+namespace internal {
+
+struct FileCloserDeleter
+{
+    void operator()(std::FILE * const f) noexcept
+    {
+        std::fclose(f);
+    }
+};
+
+} /* namespace internal */
+
+using FileUP = std::unique_ptr<std::FILE, internal::FileCloserDeleter>;
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_LIBC_UP_HPP */
diff --git a/src/cpp-common/bt2c/make-unique.hpp b/src/cpp-common/bt2c/make-unique.hpp
new file mode 100644 (file)
index 0000000..95f5534
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_MAKE_UNIQUE_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_MAKE_UNIQUE_HPP
+
+#include <memory>
+#include <type_traits>
+#include <utility>
+
+namespace bt2c {
+
+/*
+ * Our implementation of std::make_unique<>() for C++11.
+ */
+template <typename T, typename... ArgTs>
+std::unique_ptr<T> makeUnique(ArgTs&&...args)
+{
+    static_assert(!std::is_array<T>::value, "`T` is not an array (unsupported).");
+
+    return std::unique_ptr<T>(new T {std::forward<ArgTs>(args)...});
+}
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_MAKE_UNIQUE_HPP */
diff --git a/src/cpp-common/bt2c/read-fixed-len-int.hpp b/src/cpp-common/bt2c/read-fixed-len-int.hpp
new file mode 100644 (file)
index 0000000..93d8503
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_READ_FIXED_LEN_INT_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_READ_FIXED_LEN_INT_HPP
+
+#include <cstdint>
+#include <cstring>
+#include <type_traits>
+
+#include "endian.hpp"
+
+namespace bt2c {
+
+/*
+ * Reads a fixed-length integer of unknown byte order into a value of integral
+ * type `IntT` from the buffer `buf` and returns it.
+ */
+template <typename IntT>
+IntT readFixedLenInt(const std::uint8_t * const buf)
+{
+    static_assert(std::is_integral<IntT>::value, "`IntT` is an integral type.");
+
+    IntT val;
+
+    std::memcpy(&val, buf, sizeof(val));
+    return val;
+}
+
+/*
+ * Reads a fixed-length little-endian integer into a value of integral
+ * type `IntT` from the buffer `buf` and returns it.
+ */
+template <typename IntT>
+IntT readFixedLenIntLe(const std::uint8_t * const buf)
+{
+    return bt2c::littleEndianToNative(readFixedLenInt<IntT>(buf));
+}
+
+/*
+ * Reads a fixed-length big-endian integer into a value of integral
+ * type `IntT` from the buffer `buf` and returns it.
+ */
+template <typename IntT>
+IntT readFixedLenIntBe(const std::uint8_t * const buf)
+{
+    return bt2c::bigEndianToNative(readFixedLenInt<IntT>(buf));
+}
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_READ_FIXED_LEN_INT_HPP */
diff --git a/src/cpp-common/bt2c/safe-ops.hpp b/src/cpp-common/bt2c/safe-ops.hpp
new file mode 100644 (file)
index 0000000..e1d6601
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_SAFE_OPS_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_SAFE_OPS_HPP
+
+#include <limits>
+#include <type_traits>
+
+#include "common/assert.h"
+
+namespace bt2c {
+
+template <typename T>
+constexpr bool safeToMul(const T a, const T b)
+{
+    static_assert(std::is_unsigned<T>::value, "`T` is an unsigned type.");
+
+    return a == 0 || b == 0 || a < std::numeric_limits<T>::max() / b;
+}
+
+template <typename T>
+T safeMul(const T a, const T b) noexcept
+{
+    static_assert(std::is_unsigned<T>::value, "`T` is an unsigned type.");
+
+    BT_ASSERT_DBG(safeToMul(a, b));
+    return a * b;
+}
+
+template <typename T>
+constexpr bool safeToAdd(const T a, const T b)
+{
+    static_assert(std::is_unsigned<T>::value, "`T` is an unsigned type.");
+
+    return a <= std::numeric_limits<T>::max() - b;
+}
+
+template <typename T>
+T safeAdd(const T a, const T b) noexcept
+{
+    static_assert(std::is_unsigned<T>::value, "`T` is an unsigned type.");
+
+    BT_ASSERT_DBG(safeToAdd(a, b));
+    return a + b;
+}
+
+template <typename T>
+constexpr bool safeToSub(const T a, const T b)
+{
+    static_assert(std::is_unsigned<T>::value, "`T` is an unsigned type.");
+
+    return a >= b;
+}
+
+template <typename T>
+T safeSub(const T a, const T b) noexcept
+{
+    static_assert(std::is_unsigned<T>::value, "`T` is an unsigned type.");
+
+    BT_ASSERT_DBG(safeToSub(a, b));
+    return a - b;
+}
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_SAFE_OPS_HPP */
diff --git a/src/cpp-common/bt2c/std-int.hpp b/src/cpp-common/bt2c/std-int.hpp
new file mode 100644 (file)
index 0000000..2fe3535
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_STD_INT_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_STD_INT_HPP
+
+#include <cstdint>
+
+namespace bt2c {
+namespace internal {
+
+template <std::size_t LenBitsV, bool IsSignedV>
+struct StdIntTBase;
+
+template <>
+struct StdIntTBase<8, true>
+{
+    using Type = std::int8_t;
+};
+
+template <>
+struct StdIntTBase<8, false>
+{
+    using Type = std::uint8_t;
+};
+
+template <>
+struct StdIntTBase<16, true>
+{
+    using Type = std::int16_t;
+};
+
+template <>
+struct StdIntTBase<16, false>
+{
+    using Type = std::uint16_t;
+};
+
+template <>
+struct StdIntTBase<32, true>
+{
+    using Type = std::int32_t;
+};
+
+template <>
+struct StdIntTBase<32, false>
+{
+    using Type = std::uint32_t;
+};
+
+template <>
+struct StdIntTBase<64, true>
+{
+    using Type = std::int64_t;
+};
+
+template <>
+struct StdIntTBase<64, false>
+{
+    using Type = std::uint64_t;
+};
+
+} /* namespace internal */
+
+/*
+ * Standard fixed-length integer type `Type` of length `LenBitsV` bits
+ * and signedness `IsSignedV`.
+ *
+ * `LenBitsV` must be one of 8, 16, 32, or 64.
+ *
+ * For example, `StdIntT<32, true>` is `std::int32_t`.
+ */
+template <std::size_t LenBitsV, bool IsSignedV>
+using StdIntT = typename internal::StdIntTBase<LenBitsV, IsSignedV>::Type;
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_STD_INT_HPP */
diff --git a/src/cpp-common/bt2c/uuid-view.cpp b/src/cpp-common/bt2c/uuid-view.cpp
new file mode 100644 (file)
index 0000000..2d784a5
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2016-2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include "uuid-view.hpp"
+#include "uuid.hpp"
+
+namespace bt2c {
+
+UuidView::UuidView(const Uuid& uuid) noexcept : _mUuid {uuid.data()}
+{
+}
+
+UuidView::operator Uuid() const noexcept
+{
+    return Uuid {*this};
+}
+
+} /* namespace bt2c */
diff --git a/src/cpp-common/bt2c/uuid-view.hpp b/src/cpp-common/bt2c/uuid-view.hpp
new file mode 100644 (file)
index 0000000..a87388a
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * SPDX-FileCopyrightText: 2020 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_UUID_VIEW_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_UUID_VIEW_HPP
+
+#include <algorithm>
+#include <cstdint>
+#include <string>
+
+#include "common/assert.h"
+#include "common/uuid.h"
+
+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;
+};
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_UUID_VIEW_HPP */
diff --git a/src/cpp-common/bt2c/uuid.hpp b/src/cpp-common/bt2c/uuid.hpp
new file mode 100644 (file)
index 0000000..86d1217
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+ * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_UUID_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_UUID_HPP
+
+#include <algorithm>
+#include <array>
+#include <string>
+
+#include "common/assert.h"
+#include "common/uuid.h"
+
+#include "uuid-view.hpp"
+
+namespace bt2c {
+
+/*
+ * A universally unique identifier.
+ *
+ * A `Uuid` object contains its UUID data: see `UuidView` to have a
+ * UUID view on existing UUID data.
+ */
+class Uuid final
+{
+public:
+    using Val = UuidView::Val;
+    using ConstIter = UuidView::ConstIter;
+
+public:
+    /*
+     * Builds a nil UUID.
+     */
+    explicit Uuid() noexcept = default;
+
+    explicit Uuid(const Val * const uuid) noexcept
+    {
+        this->_setFromPtr(uuid);
+    }
+
+    explicit Uuid(const char * const str) noexcept
+    {
+        const auto ret = bt_uuid_from_str(str, _mUuid.data());
+        BT_ASSERT(ret == 0);
+    }
+
+    explicit Uuid(const std::string& str) noexcept : Uuid {str.c_str()}
+    {
+    }
+
+    explicit Uuid(const UuidView& view) noexcept : Uuid {view.data()}
+    {
+    }
+
+    Uuid(const Uuid&) noexcept = default;
+    Uuid& operator=(const Uuid&) noexcept = default;
+
+    Uuid& operator=(const Val * const uuid) noexcept
+    {
+        this->_setFromPtr(uuid);
+        return *this;
+    }
+
+    static Uuid generate() noexcept
+    {
+        bt_uuid_t uuidGen;
+
+        bt_uuid_generate(uuidGen);
+        return Uuid {uuidGen};
+    }
+
+    std::string str() const
+    {
+        return this->_view().str();
+    }
+
+    bool operator==(const Uuid& other) const noexcept
+    {
+        return this->_view() == other._view();
+    }
+
+    bool operator!=(const Uuid& other) const noexcept
+    {
+        return this->_view() != other._view();
+    }
+
+    bool operator<(const Uuid& other) const noexcept
+    {
+        return this->_view() < other._view();
+    }
+
+    /*
+     * The returned UUID view must not outlive the UUID object.
+     */
+    operator UuidView() const noexcept
+    {
+        return this->_view();
+    }
+
+    static constexpr std::size_t size() noexcept
+    {
+        return UuidView::size();
+    }
+
+    const Val *data() const noexcept
+    {
+        return _mUuid.data();
+    }
+
+    Val operator[](const std::size_t index) const noexcept
+    {
+        return this->_view()[index];
+    }
+
+    ConstIter begin() const noexcept
+    {
+        return this->_view().begin();
+    }
+
+    ConstIter end() const noexcept
+    {
+        return this->_view().end();
+    }
+
+    bool isNil() const noexcept
+    {
+        return this->_view().isNil();
+    }
+
+private:
+    /*
+     * std::copy_n() won't throw when simply copying bytes below,
+     * therefore this method won't throw.
+     */
+    void _setFromPtr(const Val * const uuid) noexcept
+    {
+        BT_ASSERT(uuid);
+        std::copy_n(uuid, BT_UUID_LEN, std::begin(_mUuid));
+    }
+
+    UuidView _view() const noexcept
+    {
+        return UuidView {_mUuid.data()};
+    }
+
+    std::array<Val, UuidView::size()> _mUuid = {};
+};
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_UUID_HPP */
diff --git a/src/cpp-common/bt2c/vector.hpp b/src/cpp-common/bt2c/vector.hpp
new file mode 100644 (file)
index 0000000..516a826
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * SPDX-FileCopyrightText: 2022 Simon Marchi <simon.marchi@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef SRC_CPP_COMMON_VECTOR_HPP
+#define SRC_CPP_COMMON_VECTOR_HPP
+
+#include <vector>
+
+#include "common/assert.h"
+
+namespace bt2c {
+
+/*
+ * Moves the last entry of `vec` to the index `idx`, then removes the last entry.
+ *
+ * Meant to be a direct replacement for g_ptr_array_remove_index_fast(), but for
+ * `std::vector`.
+ */
+template <typename T, typename AllocatorT>
+void vectorFastRemove(std::vector<T, AllocatorT>& vec,
+                      const typename std::vector<T, AllocatorT>::size_type idx)
+{
+    BT_ASSERT_DBG(idx < vec.size());
+
+    if (idx < vec.size() - 1) {
+        vec[idx] = std::move(vec.back());
+    }
+
+    vec.pop_back();
+}
+
+} /* namespace bt2c */
+
+#endif /* SRC_CPP_COMMON_VECTOR_HPP */
diff --git a/src/cpp-common/endian.hpp b/src/cpp-common/endian.hpp
deleted file mode 100644 (file)
index a31e31a..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
- *
- * SPDX-License-Identifier: MIT
- */
-
-#ifndef BABELTRACE_CPP_COMMON_ENDIAN_HPP
-#define BABELTRACE_CPP_COMMON_ENDIAN_HPP
-
-#include <cstdint>
-
-#include "compat/endian.h" /* IWYU pragma: keep  */
-
-namespace bt2_common {
-
-static inline std::uint8_t littleEndianToNative(const std::uint8_t val) noexcept
-{
-    return val;
-}
-
-static inline std::uint8_t bigEndianToNative(const std::uint8_t val) noexcept
-{
-    return val;
-}
-
-static inline std::int8_t littleEndianToNative(const std::int8_t val) noexcept
-{
-    return val;
-}
-
-static inline std::int8_t bigEndianToNative(const std::int8_t val) noexcept
-{
-    return val;
-}
-
-static inline std::uint16_t littleEndianToNative(const std::uint16_t val) noexcept
-{
-    return static_cast<std::uint16_t>(le16toh(val));
-}
-
-static inline std::uint16_t bigEndianToNative(const std::uint16_t val) noexcept
-{
-    return static_cast<std::uint16_t>(be16toh(val));
-}
-
-static inline std::int16_t littleEndianToNative(const std::int16_t val) noexcept
-{
-    return static_cast<std::int16_t>(littleEndianToNative(static_cast<std::uint16_t>(val)));
-}
-
-static inline std::int16_t bigEndianToNative(const std::int16_t val) noexcept
-{
-    return static_cast<std::int16_t>(bigEndianToNative(static_cast<std::uint16_t>(val)));
-}
-
-static inline std::uint32_t littleEndianToNative(const std::uint32_t val) noexcept
-{
-    return static_cast<std::uint32_t>(le32toh(val));
-}
-
-static inline std::uint32_t bigEndianToNative(const std::uint32_t val) noexcept
-{
-    return static_cast<std::uint32_t>(be32toh(val));
-}
-
-static inline std::int32_t littleEndianToNative(const std::int32_t val) noexcept
-{
-    return static_cast<std::int32_t>(littleEndianToNative(static_cast<std::uint32_t>(val)));
-}
-
-static inline std::int32_t bigEndianToNative(const std::int32_t val) noexcept
-{
-    return static_cast<std::int32_t>(bigEndianToNative(static_cast<std::uint32_t>(val)));
-}
-
-static inline std::uint64_t littleEndianToNative(const std::uint64_t val) noexcept
-{
-    return static_cast<std::uint64_t>(le64toh(val));
-}
-
-static inline std::uint64_t bigEndianToNative(const std::uint64_t val) noexcept
-{
-    return static_cast<std::uint64_t>(be64toh(val));
-}
-
-static inline std::int64_t littleEndianToNative(const std::int64_t val) noexcept
-{
-    return static_cast<std::int64_t>(littleEndianToNative(static_cast<std::uint64_t>(val)));
-}
-
-static inline std::int64_t bigEndianToNative(const std::int64_t val) noexcept
-{
-    return static_cast<std::int64_t>(bigEndianToNative(static_cast<std::uint64_t>(val)));
-}
-
-} /* namespace bt2_common */
-
-#endif /* BABELTRACE_CPP_COMMON_ENDIAN_HPP */
diff --git a/src/cpp-common/exc.hpp b/src/cpp-common/exc.hpp
deleted file mode 100644 (file)
index 09da6be..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
- *
- * SPDX-License-Identifier: MIT
- */
-
-#ifndef BABELTRACE_CPP_COMMON_EXC_HPP
-#define BABELTRACE_CPP_COMMON_EXC_HPP
-
-#include <exception>
-#include <new>
-#include <stdexcept>
-#include <string>
-
-namespace bt2_common {
-
-/*
- * End of iteration.
- */
-class End : public std::exception
-{
-public:
-    explicit End() noexcept : std::exception {}
-    {
-    }
-};
-
-/*
- * General error.
- */
-class Error : public std::runtime_error
-{
-public:
-    explicit Error(std::string msg = "Error") : std::runtime_error {std::move(msg)}
-    {
-    }
-};
-
-/*
- * Overflow error.
- */
-class OverflowError : public Error
-{
-public:
-    explicit OverflowError() noexcept : Error {"Overflow error"}
-    {
-    }
-};
-
-/*
- * Memory error.
- */
-class MemoryError : public std::bad_alloc
-{
-public:
-    explicit MemoryError() noexcept : std::bad_alloc {}
-    {
-    }
-};
-
-/*
- * Not available right now: try again later.
- */
-class TryAgain : public std::exception
-{
-public:
-    explicit TryAgain() noexcept : std::exception {}
-    {
-    }
-};
-
-} /* namespace bt2_common */
-
-#endif /* BABELTRACE_CPP_COMMON_EXC_HPP */
diff --git a/src/cpp-common/glib-up.hpp b/src/cpp-common/glib-up.hpp
deleted file mode 100644 (file)
index 33e04a9..0000000
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (c) 2022 EfficiOS, inc.
- *
- * SPDX-License-Identifier: MIT
- */
-
-#ifndef BABELTRACE_CPP_COMMON_GLIB_UP_HPP
-#define BABELTRACE_CPP_COMMON_GLIB_UP_HPP
-
-#include <memory>
-
-#include <glib.h>
-
-namespace bt2_common {
-namespace internal {
-
-struct GCharDeleter final
-{
-    void operator()(gchar * const p) noexcept
-    {
-        g_free(p);
-    }
-};
-
-} /* namespace internal */
-
-using GCharUP = std::unique_ptr<gchar, internal::GCharDeleter>;
-
-namespace internal {
-
-struct GStringDeleter final
-{
-    void operator()(GString * const str)
-    {
-        g_string_free(str, TRUE);
-    }
-};
-
-} /* namespace internal */
-
-using GStringUP = std::unique_ptr<GString, internal::GStringDeleter>;
-
-namespace internal {
-
-struct GDirDeleter final
-{
-    void operator()(GDir * const dir)
-    {
-        g_dir_close(dir);
-    }
-};
-
-} /* namespace internal */
-
-using GDirUP = std::unique_ptr<GDir, internal::GDirDeleter>;
-
-namespace internal {
-
-struct GMappedFileDeleter final
-{
-    void operator()(GMappedFile * const f)
-    {
-        g_mapped_file_unref(f);
-    }
-};
-
-} /* namespace internal */
-
-using GMappedFileUP = std::unique_ptr<GMappedFile, internal::GMappedFileDeleter>;
-
-} /* namespace bt2_common */
-
-#endif /* BABELTRACE_CPP_COMMON_GLIB_UP_HPP */
diff --git a/src/cpp-common/lib-str.hpp b/src/cpp-common/lib-str.hpp
deleted file mode 100644 (file)
index fbd5401..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Copyright (c) 2022 EfficiOS, Inc.
- *
- * SPDX-License-Identifier: MIT
- */
-
-#ifndef BABELTRACE_CPP_COMMON_LIB_STR_HPP
-#define BABELTRACE_CPP_COMMON_LIB_STR_HPP
-
-#include "bt2/message.hpp"
-
-namespace bt2_common {
-
-static inline const char *messageTypeStr(const bt2::MessageType type)
-{
-    return bt_common_message_type_string(static_cast<bt_message_type>(type));
-}
-
-} /* namespace bt2_common */
-
-#endif /* BABELTRACE_CPP_COMMON_LIB_STR_HPP */
diff --git a/src/cpp-common/libc-up.hpp b/src/cpp-common/libc-up.hpp
deleted file mode 100644 (file)
index c53a39a..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (c) 2022 EfficiOS, inc.
- *
- * SPDX-License-Identifier: MIT
- */
-
-#ifndef BABELTRACE_CPP_COMMON_LIBC_UP_HPP
-#define BABELTRACE_CPP_COMMON_LIBC_UP_HPP
-
-#include <cstdio>
-#include <memory>
-
-namespace bt2_common {
-namespace internal {
-
-struct FileCloserDeleter
-{
-    void operator()(std::FILE * const f) noexcept
-    {
-        std::fclose(f);
-    }
-};
-
-} /* namespace internal */
-
-using FileUP = std::unique_ptr<std::FILE, internal::FileCloserDeleter>;
-
-} /* namespace bt2_common */
-
-#endif /* BABELTRACE_CPP_COMMON_LIBC_UP_HPP */
diff --git a/src/cpp-common/make-unique.hpp b/src/cpp-common/make-unique.hpp
deleted file mode 100644 (file)
index 12e85f4..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
- *
- * SPDX-License-Identifier: MIT
- */
-
-#ifndef BABELTRACE_CPP_COMMON_MAKE_UNIQUE_HPP
-#define BABELTRACE_CPP_COMMON_MAKE_UNIQUE_HPP
-
-#include <memory>
-#include <type_traits>
-#include <utility>
-
-namespace bt2_common {
-
-/*
- * Our implementation of std::make_unique<>() for C++11.
- */
-template <typename T, typename... ArgTs>
-std::unique_ptr<T> makeUnique(ArgTs&&...args)
-{
-    static_assert(!std::is_array<T>::value, "`T` is not an array (unsupported).");
-
-    return std::unique_ptr<T>(new T {std::forward<ArgTs>(args)...});
-}
-
-} /* namespace bt2_common */
-
-#endif /* BABELTRACE_CPP_COMMON_MAKE_UNIQUE_HPP */
diff --git a/src/cpp-common/read-fixed-len-int.hpp b/src/cpp-common/read-fixed-len-int.hpp
deleted file mode 100644 (file)
index 07481f2..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
- *
- * SPDX-License-Identifier: MIT
- */
-
-#ifndef BABELTRACE_CPP_COMMON_READ_FIXED_LEN_INT_HPP
-#define BABELTRACE_CPP_COMMON_READ_FIXED_LEN_INT_HPP
-
-#include <cstdint>
-#include <cstring>
-#include <type_traits>
-
-#include "cpp-common/endian.hpp"
-
-namespace bt2_common {
-
-/*
- * Reads a fixed-length integer of unknown byte order into a value of integral
- * type `IntT` from the buffer `buf` and returns it.
- */
-template <typename IntT>
-IntT readFixedLenInt(const std::uint8_t * const buf)
-{
-    static_assert(std::is_integral<IntT>::value, "`IntT` is an integral type.");
-
-    IntT val;
-
-    std::memcpy(&val, buf, sizeof(val));
-    return val;
-}
-
-/*
- * Reads a fixed-length little-endian integer into a value of integral
- * type `IntT` from the buffer `buf` and returns it.
- */
-template <typename IntT>
-IntT readFixedLenIntLe(const std::uint8_t * const buf)
-{
-    return bt2_common::littleEndianToNative(readFixedLenInt<IntT>(buf));
-}
-
-/*
- * Reads a fixed-length big-endian integer into a value of integral
- * type `IntT` from the buffer `buf` and returns it.
- */
-template <typename IntT>
-IntT readFixedLenIntBe(const std::uint8_t * const buf)
-{
-    return bt2_common::bigEndianToNative(readFixedLenInt<IntT>(buf));
-}
-
-} /* namespace bt2_common */
-
-#endif /* BABELTRACE_CPP_COMMON_READ_FIXED_LEN_INT_HPP */
diff --git a/src/cpp-common/safe-ops.hpp b/src/cpp-common/safe-ops.hpp
deleted file mode 100644 (file)
index 90dae88..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
- *
- * SPDX-License-Identifier: MIT
- */
-
-#ifndef BABELTRACE_CPP_COMMON_SAFE_OPS_HPP
-#define BABELTRACE_CPP_COMMON_SAFE_OPS_HPP
-
-#include <limits>
-#include <type_traits>
-
-#include "common/assert.h"
-
-namespace bt2_common {
-
-template <typename T>
-constexpr bool safeToMul(const T a, const T b)
-{
-    static_assert(std::is_unsigned<T>::value, "`T` is an unsigned type.");
-
-    return a == 0 || b == 0 || a < std::numeric_limits<T>::max() / b;
-}
-
-template <typename T>
-T safeMul(const T a, const T b) noexcept
-{
-    static_assert(std::is_unsigned<T>::value, "`T` is an unsigned type.");
-
-    BT_ASSERT_DBG(safeToMul(a, b));
-    return a * b;
-}
-
-template <typename T>
-constexpr bool safeToAdd(const T a, const T b)
-{
-    static_assert(std::is_unsigned<T>::value, "`T` is an unsigned type.");
-
-    return a <= std::numeric_limits<T>::max() - b;
-}
-
-template <typename T>
-T safeAdd(const T a, const T b) noexcept
-{
-    static_assert(std::is_unsigned<T>::value, "`T` is an unsigned type.");
-
-    BT_ASSERT_DBG(safeToAdd(a, b));
-    return a + b;
-}
-
-template <typename T>
-constexpr bool safeToSub(const T a, const T b)
-{
-    static_assert(std::is_unsigned<T>::value, "`T` is an unsigned type.");
-
-    return a >= b;
-}
-
-template <typename T>
-T safeSub(const T a, const T b) noexcept
-{
-    static_assert(std::is_unsigned<T>::value, "`T` is an unsigned type.");
-
-    BT_ASSERT_DBG(safeToSub(a, b));
-    return a - b;
-}
-
-} /* namespace bt2_common */
-
-#endif /* BABELTRACE_CPP_COMMON_SAFE_OPS_HPP */
diff --git a/src/cpp-common/std-int.hpp b/src/cpp-common/std-int.hpp
deleted file mode 100644 (file)
index 2bdb4ec..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
- *
- * SPDX-License-Identifier: MIT
- */
-
-#ifndef BABELTRACE_CPP_COMMON_STD_INT_HPP
-#define BABELTRACE_CPP_COMMON_STD_INT_HPP
-
-#include <cstdint>
-
-namespace bt2_common {
-namespace internal {
-
-template <std::size_t LenBitsV, bool IsSignedV>
-struct StdIntTBase;
-
-template <>
-struct StdIntTBase<8, true>
-{
-    using Type = std::int8_t;
-};
-
-template <>
-struct StdIntTBase<8, false>
-{
-    using Type = std::uint8_t;
-};
-
-template <>
-struct StdIntTBase<16, true>
-{
-    using Type = std::int16_t;
-};
-
-template <>
-struct StdIntTBase<16, false>
-{
-    using Type = std::uint16_t;
-};
-
-template <>
-struct StdIntTBase<32, true>
-{
-    using Type = std::int32_t;
-};
-
-template <>
-struct StdIntTBase<32, false>
-{
-    using Type = std::uint32_t;
-};
-
-template <>
-struct StdIntTBase<64, true>
-{
-    using Type = std::int64_t;
-};
-
-template <>
-struct StdIntTBase<64, false>
-{
-    using Type = std::uint64_t;
-};
-
-} /* namespace internal */
-
-/*
- * Standard fixed-length integer type `Type` of length `LenBitsV` bits
- * and signedness `IsSignedV`.
- *
- * `LenBitsV` must be one of 8, 16, 32, or 64.
- *
- * For example, `StdIntT<32, true>` is `std::int32_t`.
- */
-template <std::size_t LenBitsV, bool IsSignedV>
-using StdIntT = typename internal::StdIntTBase<LenBitsV, IsSignedV>::Type;
-
-} /* namespace bt2_common */
-
-#endif /* BABELTRACE_CPP_COMMON_STD_INT_HPP */
diff --git a/src/cpp-common/uuid-view.cpp b/src/cpp-common/uuid-view.cpp
deleted file mode 100644 (file)
index 5739230..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Copyright (c) 2016-2022 Philippe Proulx <pproulx@efficios.com>
- *
- * SPDX-License-Identifier: MIT
- */
-
-#include "uuid-view.hpp"
-#include "uuid.hpp"
-
-namespace bt2_common {
-
-UuidView::UuidView(const Uuid& uuid) noexcept : _mUuid {uuid.data()}
-{
-}
-
-UuidView::operator Uuid() const noexcept
-{
-    return Uuid {*this};
-}
-
-} /* namespace bt2_common */
diff --git a/src/cpp-common/uuid-view.hpp b/src/cpp-common/uuid-view.hpp
deleted file mode 100644 (file)
index d719d87..0000000
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 2020 Philippe Proulx <pproulx@efficios.com>
- *
- * SPDX-License-Identifier: MIT
- */
-
-#ifndef BABELTRACE_CPP_COMMON_UUID_VIEW_HPP
-#define BABELTRACE_CPP_COMMON_UUID_VIEW_HPP
-
-#include <algorithm>
-#include <cstdint>
-#include <string>
-
-#include "common/assert.h"
-#include "common/uuid.h"
-
-namespace bt2_common {
-
-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;
-};
-
-} /* namespace bt2_common */
-
-#endif /* BABELTRACE_CPP_COMMON_UUID_VIEW_HPP */
diff --git a/src/cpp-common/uuid.hpp b/src/cpp-common/uuid.hpp
deleted file mode 100644 (file)
index 13c3cb9..0000000
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
- *
- * SPDX-License-Identifier: MIT
- */
-
-#ifndef BABELTRACE_CPP_COMMON_UUID_HPP
-#define BABELTRACE_CPP_COMMON_UUID_HPP
-
-#include <algorithm>
-#include <array>
-#include <string>
-
-#include "common/assert.h"
-#include "common/uuid.h"
-
-#include "uuid-view.hpp"
-
-namespace bt2_common {
-
-/*
- * A universally unique identifier.
- *
- * A `Uuid` object contains its UUID data: see `UuidView` to have a
- * UUID view on existing UUID data.
- */
-class Uuid final
-{
-public:
-    using Val = UuidView::Val;
-    using ConstIter = UuidView::ConstIter;
-
-public:
-    /*
-     * Builds a nil UUID.
-     */
-    explicit Uuid() noexcept = default;
-
-    explicit Uuid(const Val * const uuid) noexcept
-    {
-        this->_setFromPtr(uuid);
-    }
-
-    explicit Uuid(const char * const str) noexcept
-    {
-        const auto ret = bt_uuid_from_str(str, _mUuid.data());
-        BT_ASSERT(ret == 0);
-    }
-
-    explicit Uuid(const std::string& str) noexcept : Uuid {str.c_str()}
-    {
-    }
-
-    explicit Uuid(const UuidView& view) noexcept : Uuid {view.data()}
-    {
-    }
-
-    Uuid(const Uuid&) noexcept = default;
-    Uuid& operator=(const Uuid&) noexcept = default;
-
-    Uuid& operator=(const Val * const uuid) noexcept
-    {
-        this->_setFromPtr(uuid);
-        return *this;
-    }
-
-    static Uuid generate() noexcept
-    {
-        bt_uuid_t uuidGen;
-
-        bt_uuid_generate(uuidGen);
-        return Uuid {uuidGen};
-    }
-
-    std::string str() const
-    {
-        return this->_view().str();
-    }
-
-    bool operator==(const Uuid& other) const noexcept
-    {
-        return this->_view() == other._view();
-    }
-
-    bool operator!=(const Uuid& other) const noexcept
-    {
-        return this->_view() != other._view();
-    }
-
-    bool operator<(const Uuid& other) const noexcept
-    {
-        return this->_view() < other._view();
-    }
-
-    /*
-     * The returned UUID view must not outlive the UUID object.
-     */
-    operator UuidView() const noexcept
-    {
-        return this->_view();
-    }
-
-    static constexpr std::size_t size() noexcept
-    {
-        return UuidView::size();
-    }
-
-    const Val *data() const noexcept
-    {
-        return _mUuid.data();
-    }
-
-    Val operator[](const std::size_t index) const noexcept
-    {
-        return this->_view()[index];
-    }
-
-    ConstIter begin() const noexcept
-    {
-        return this->_view().begin();
-    }
-
-    ConstIter end() const noexcept
-    {
-        return this->_view().end();
-    }
-
-    bool isNil() const noexcept
-    {
-        return this->_view().isNil();
-    }
-
-private:
-    /*
-     * std::copy_n() won't throw when simply copying bytes below,
-     * therefore this method won't throw.
-     */
-    void _setFromPtr(const Val * const uuid) noexcept
-    {
-        BT_ASSERT(uuid);
-        std::copy_n(uuid, BT_UUID_LEN, std::begin(_mUuid));
-    }
-
-    UuidView _view() const noexcept
-    {
-        return UuidView {_mUuid.data()};
-    }
-
-    std::array<Val, UuidView::size()> _mUuid = {};
-};
-
-} /* namespace bt2_common */
-
-#endif /* BABELTRACE_CPP_COMMON_UUID_HPP */
diff --git a/src/cpp-common/vector.hpp b/src/cpp-common/vector.hpp
deleted file mode 100644 (file)
index 2d6b6cb..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 2022 Simon Marchi <simon.marchi@efficios.com>
- *
- * SPDX-License-Identifier: MIT
- */
-
-#ifndef SRC_CPP_COMMON_VECTOR_HPP
-#define SRC_CPP_COMMON_VECTOR_HPP
-
-#include <vector>
-
-#include "common/assert.h"
-
-namespace bt2_common {
-
-/*
- * Moves the last entry of `vec` to the index `idx`, then removes the last entry.
- *
- * Meant to be a direct replacement for g_ptr_array_remove_index_fast(), but for
- * `std::vector`.
- */
-template <typename T, typename AllocatorT>
-void vectorFastRemove(std::vector<T, AllocatorT>& vec,
-                      const typename std::vector<T, AllocatorT>::size_type idx)
-{
-    BT_ASSERT_DBG(idx < vec.size());
-
-    if (idx < vec.size() - 1) {
-        vec[idx] = std::move(vec.back());
-    }
-
-    vec.pop_back();
-}
-
-} /* namespace bt2_common */
-
-#endif /* SRC_CPP_COMMON_VECTOR_HPP */
This page took 0.049935 seconds and 4 git commands to generate.