From: Simon Marchi Date: Mon, 11 Dec 2023 21:05:13 +0000 (-0500) Subject: cpp-common: rename `bt2_common` namespace -> `bt2c` X-Git-Url: https://git.efficios.com/?a=commitdiff_plain;h=094bf3f24c790995348235921f3c26d94b7f4f19;p=babeltrace.git cpp-common: rename `bt2_common` namespace -> `bt2c` 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 Change-Id: Ife8527aa961025f28d06707eb9f374cafea572f5 Reviewed-on: https://review.lttng.org/c/babeltrace/+/11390 Reviewed-by: Simon Marchi CI-Build: Simon Marchi Tested-by: jenkins --- diff --git a/src/Makefile.am b/src/Makefile.am index 02b5a173..b5d24fe1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 3a036c88..00000000 --- a/src/cpp-common/align.hpp +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2022 Philippe Proulx - * - * SPDX-License-Identifier: MIT - */ - -#ifndef BABELTRACE_CPP_COMMON_ALIGN_HPP -#define BABELTRACE_CPP_COMMON_ALIGN_HPP - -#include - -#include "common/align.h" - -namespace bt2_common { - -template -ValT align(const ValT val, const AlignT align) noexcept -{ - static_assert(std::is_unsigned::value, "`ValT` is unsigned."); - return BT_ALIGN(val, static_cast(align)); -} - -} /* namespace bt2_common */ - -#endif /* BABELTRACE_CPP_COMMON_ALIGN_HPP */ diff --git a/src/cpp-common/bt2/clock-class.hpp b/src/cpp-common/bt2/clock-class.hpp index 72064f84..64e093f4 100644 --- a/src/cpp-common/bt2/clock-class.hpp +++ b/src/cpp-common/bt2/clock-class.hpp @@ -13,8 +13,8 @@ #include +#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 uuid() const noexcept + nonstd::optional 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; diff --git a/src/cpp-common/bt2/exc.hpp b/src/cpp-common/bt2/exc.hpp index 71997c3e..723368c6 100644 --- a/src/cpp-common/bt2/exc.hpp +++ b/src/cpp-common/bt2/exc.hpp @@ -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 */ diff --git a/src/cpp-common/bt2/trace-ir.hpp b/src/cpp-common/bt2/trace-ir.hpp index b6f349be..9df6abbf 100644 --- a/src/cpp-common/bt2/trace-ir.hpp +++ b/src/cpp-common/bt2/trace-ir.hpp @@ -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 uuid() const noexcept + nonstd::optional 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 index 00000000..ef443aa3 --- /dev/null +++ b/src/cpp-common/bt2c/align.hpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2C_ALIGN_HPP +#define BABELTRACE_CPP_COMMON_BT2C_ALIGN_HPP + +#include + +#include "common/align.h" + +namespace bt2c { + +template +ValT align(const ValT val, const AlignT align) noexcept +{ + static_assert(std::is_unsigned::value, "`ValT` is unsigned."); + return BT_ALIGN(val, static_cast(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 index 00000000..4d315b26 --- /dev/null +++ b/src/cpp-common/bt2c/endian.hpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2022 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2C_ENDIAN_HPP +#define BABELTRACE_CPP_COMMON_BT2C_ENDIAN_HPP + +#include + +#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(le16toh(val)); +} + +static inline std::uint16_t bigEndianToNative(const std::uint16_t val) noexcept +{ + return static_cast(be16toh(val)); +} + +static inline std::int16_t littleEndianToNative(const std::int16_t val) noexcept +{ + return static_cast(littleEndianToNative(static_cast(val))); +} + +static inline std::int16_t bigEndianToNative(const std::int16_t val) noexcept +{ + return static_cast(bigEndianToNative(static_cast(val))); +} + +static inline std::uint32_t littleEndianToNative(const std::uint32_t val) noexcept +{ + return static_cast(le32toh(val)); +} + +static inline std::uint32_t bigEndianToNative(const std::uint32_t val) noexcept +{ + return static_cast(be32toh(val)); +} + +static inline std::int32_t littleEndianToNative(const std::int32_t val) noexcept +{ + return static_cast(littleEndianToNative(static_cast(val))); +} + +static inline std::int32_t bigEndianToNative(const std::int32_t val) noexcept +{ + return static_cast(bigEndianToNative(static_cast(val))); +} + +static inline std::uint64_t littleEndianToNative(const std::uint64_t val) noexcept +{ + return static_cast(le64toh(val)); +} + +static inline std::uint64_t bigEndianToNative(const std::uint64_t val) noexcept +{ + return static_cast(be64toh(val)); +} + +static inline std::int64_t littleEndianToNative(const std::int64_t val) noexcept +{ + return static_cast(littleEndianToNative(static_cast(val))); +} + +static inline std::int64_t bigEndianToNative(const std::int64_t val) noexcept +{ + return static_cast(bigEndianToNative(static_cast(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 index 00000000..3138fdbc --- /dev/null +++ b/src/cpp-common/bt2c/exc.hpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2022 Francis Deslauriers + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2C_EXC_HPP +#define BABELTRACE_CPP_COMMON_BT2C_EXC_HPP + +#include +#include +#include +#include + +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 index 00000000..bfe2eadc --- /dev/null +++ b/src/cpp-common/bt2c/glib-up.hpp @@ -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 + +#include + +namespace bt2c { +namespace internal { + +struct GCharDeleter final +{ + void operator()(gchar * const p) noexcept + { + g_free(p); + } +}; + +} /* namespace internal */ + +using GCharUP = std::unique_ptr; + +namespace internal { + +struct GStringDeleter final +{ + void operator()(GString * const str) + { + g_string_free(str, TRUE); + } +}; + +} /* namespace internal */ + +using GStringUP = std::unique_ptr; + +namespace internal { + +struct GDirDeleter final +{ + void operator()(GDir * const dir) + { + g_dir_close(dir); + } +}; + +} /* namespace internal */ + +using GDirUP = std::unique_ptr; + +namespace internal { + +struct GMappedFileDeleter final +{ + void operator()(GMappedFile * const f) + { + g_mapped_file_unref(f); + } +}; + +} /* namespace internal */ + +using GMappedFileUP = std::unique_ptr; + +} /* 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 index 00000000..815f105f --- /dev/null +++ b/src/cpp-common/bt2c/lib-str.hpp @@ -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(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 index 00000000..fa7468f2 --- /dev/null +++ b/src/cpp-common/bt2c/libc-up.hpp @@ -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 +#include + +namespace bt2c { +namespace internal { + +struct FileCloserDeleter +{ + void operator()(std::FILE * const f) noexcept + { + std::fclose(f); + } +}; + +} /* namespace internal */ + +using FileUP = std::unique_ptr; + +} /* 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 index 00000000..95f55340 --- /dev/null +++ b/src/cpp-common/bt2c/make-unique.hpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2C_MAKE_UNIQUE_HPP +#define BABELTRACE_CPP_COMMON_BT2C_MAKE_UNIQUE_HPP + +#include +#include +#include + +namespace bt2c { + +/* + * Our implementation of std::make_unique<>() for C++11. + */ +template +std::unique_ptr makeUnique(ArgTs&&...args) +{ + static_assert(!std::is_array::value, "`T` is not an array (unsupported)."); + + return std::unique_ptr(new T {std::forward(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 index 00000000..93d85039 --- /dev/null +++ b/src/cpp-common/bt2c/read-fixed-len-int.hpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022 Philippe Proulx + * + * 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 +#include +#include + +#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 +IntT readFixedLenInt(const std::uint8_t * const buf) +{ + static_assert(std::is_integral::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 +IntT readFixedLenIntLe(const std::uint8_t * const buf) +{ + return bt2c::littleEndianToNative(readFixedLenInt(buf)); +} + +/* + * Reads a fixed-length big-endian integer into a value of integral + * type `IntT` from the buffer `buf` and returns it. + */ +template +IntT readFixedLenIntBe(const std::uint8_t * const buf) +{ + return bt2c::bigEndianToNative(readFixedLenInt(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 index 00000000..e1d66012 --- /dev/null +++ b/src/cpp-common/bt2c/safe-ops.hpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2022 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2C_SAFE_OPS_HPP +#define BABELTRACE_CPP_COMMON_BT2C_SAFE_OPS_HPP + +#include +#include + +#include "common/assert.h" + +namespace bt2c { + +template +constexpr bool safeToMul(const T a, const T b) +{ + static_assert(std::is_unsigned::value, "`T` is an unsigned type."); + + return a == 0 || b == 0 || a < std::numeric_limits::max() / b; +} + +template +T safeMul(const T a, const T b) noexcept +{ + static_assert(std::is_unsigned::value, "`T` is an unsigned type."); + + BT_ASSERT_DBG(safeToMul(a, b)); + return a * b; +} + +template +constexpr bool safeToAdd(const T a, const T b) +{ + static_assert(std::is_unsigned::value, "`T` is an unsigned type."); + + return a <= std::numeric_limits::max() - b; +} + +template +T safeAdd(const T a, const T b) noexcept +{ + static_assert(std::is_unsigned::value, "`T` is an unsigned type."); + + BT_ASSERT_DBG(safeToAdd(a, b)); + return a + b; +} + +template +constexpr bool safeToSub(const T a, const T b) +{ + static_assert(std::is_unsigned::value, "`T` is an unsigned type."); + + return a >= b; +} + +template +T safeSub(const T a, const T b) noexcept +{ + static_assert(std::is_unsigned::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 index 00000000..2fe35353 --- /dev/null +++ b/src/cpp-common/bt2c/std-int.hpp @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2022 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2C_STD_INT_HPP +#define BABELTRACE_CPP_COMMON_BT2C_STD_INT_HPP + +#include + +namespace bt2c { +namespace internal { + +template +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 +using StdIntT = typename internal::StdIntTBase::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 index 00000000..2d784a56 --- /dev/null +++ b/src/cpp-common/bt2c/uuid-view.cpp @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2016-2022 Philippe Proulx + * + * 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 index 00000000..a87388ab --- /dev/null +++ b/src/cpp-common/bt2c/uuid-view.hpp @@ -0,0 +1,114 @@ +/* + * SPDX-FileCopyrightText: 2020 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2C_UUID_VIEW_HPP +#define BABELTRACE_CPP_COMMON_BT2C_UUID_VIEW_HPP + +#include +#include +#include + +#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 index 00000000..86d1217f --- /dev/null +++ b/src/cpp-common/bt2c/uuid.hpp @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2022 Francis Deslauriers + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2C_UUID_HPP +#define BABELTRACE_CPP_COMMON_BT2C_UUID_HPP + +#include +#include +#include + +#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 _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 index 00000000..516a8263 --- /dev/null +++ b/src/cpp-common/bt2c/vector.hpp @@ -0,0 +1,37 @@ +/* + * SPDX-FileCopyrightText: 2022 Simon Marchi + * + * SPDX-License-Identifier: MIT + */ + +#ifndef SRC_CPP_COMMON_VECTOR_HPP +#define SRC_CPP_COMMON_VECTOR_HPP + +#include + +#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 +void vectorFastRemove(std::vector& vec, + const typename std::vector::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 index a31e31ab..00000000 --- a/src/cpp-common/endian.hpp +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (c) 2022 Philippe Proulx - * - * SPDX-License-Identifier: MIT - */ - -#ifndef BABELTRACE_CPP_COMMON_ENDIAN_HPP -#define BABELTRACE_CPP_COMMON_ENDIAN_HPP - -#include - -#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(le16toh(val)); -} - -static inline std::uint16_t bigEndianToNative(const std::uint16_t val) noexcept -{ - return static_cast(be16toh(val)); -} - -static inline std::int16_t littleEndianToNative(const std::int16_t val) noexcept -{ - return static_cast(littleEndianToNative(static_cast(val))); -} - -static inline std::int16_t bigEndianToNative(const std::int16_t val) noexcept -{ - return static_cast(bigEndianToNative(static_cast(val))); -} - -static inline std::uint32_t littleEndianToNative(const std::uint32_t val) noexcept -{ - return static_cast(le32toh(val)); -} - -static inline std::uint32_t bigEndianToNative(const std::uint32_t val) noexcept -{ - return static_cast(be32toh(val)); -} - -static inline std::int32_t littleEndianToNative(const std::int32_t val) noexcept -{ - return static_cast(littleEndianToNative(static_cast(val))); -} - -static inline std::int32_t bigEndianToNative(const std::int32_t val) noexcept -{ - return static_cast(bigEndianToNative(static_cast(val))); -} - -static inline std::uint64_t littleEndianToNative(const std::uint64_t val) noexcept -{ - return static_cast(le64toh(val)); -} - -static inline std::uint64_t bigEndianToNative(const std::uint64_t val) noexcept -{ - return static_cast(be64toh(val)); -} - -static inline std::int64_t littleEndianToNative(const std::int64_t val) noexcept -{ - return static_cast(littleEndianToNative(static_cast(val))); -} - -static inline std::int64_t bigEndianToNative(const std::int64_t val) noexcept -{ - return static_cast(bigEndianToNative(static_cast(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 index 09da6bec..00000000 --- a/src/cpp-common/exc.hpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2022 Francis Deslauriers - * - * SPDX-License-Identifier: MIT - */ - -#ifndef BABELTRACE_CPP_COMMON_EXC_HPP -#define BABELTRACE_CPP_COMMON_EXC_HPP - -#include -#include -#include -#include - -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 index 33e04a9d..00000000 --- a/src/cpp-common/glib-up.hpp +++ /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 - -#include - -namespace bt2_common { -namespace internal { - -struct GCharDeleter final -{ - void operator()(gchar * const p) noexcept - { - g_free(p); - } -}; - -} /* namespace internal */ - -using GCharUP = std::unique_ptr; - -namespace internal { - -struct GStringDeleter final -{ - void operator()(GString * const str) - { - g_string_free(str, TRUE); - } -}; - -} /* namespace internal */ - -using GStringUP = std::unique_ptr; - -namespace internal { - -struct GDirDeleter final -{ - void operator()(GDir * const dir) - { - g_dir_close(dir); - } -}; - -} /* namespace internal */ - -using GDirUP = std::unique_ptr; - -namespace internal { - -struct GMappedFileDeleter final -{ - void operator()(GMappedFile * const f) - { - g_mapped_file_unref(f); - } -}; - -} /* namespace internal */ - -using GMappedFileUP = std::unique_ptr; - -} /* 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 index fbd5401f..00000000 --- a/src/cpp-common/lib-str.hpp +++ /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(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 index c53a39a5..00000000 --- a/src/cpp-common/libc-up.hpp +++ /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 -#include - -namespace bt2_common { -namespace internal { - -struct FileCloserDeleter -{ - void operator()(std::FILE * const f) noexcept - { - std::fclose(f); - } -}; - -} /* namespace internal */ - -using FileUP = std::unique_ptr; - -} /* 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 index 12e85f44..00000000 --- a/src/cpp-common/make-unique.hpp +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2022 Philippe Proulx - * - * SPDX-License-Identifier: MIT - */ - -#ifndef BABELTRACE_CPP_COMMON_MAKE_UNIQUE_HPP -#define BABELTRACE_CPP_COMMON_MAKE_UNIQUE_HPP - -#include -#include -#include - -namespace bt2_common { - -/* - * Our implementation of std::make_unique<>() for C++11. - */ -template -std::unique_ptr makeUnique(ArgTs&&...args) -{ - static_assert(!std::is_array::value, "`T` is not an array (unsupported)."); - - return std::unique_ptr(new T {std::forward(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 index 07481f21..00000000 --- a/src/cpp-common/read-fixed-len-int.hpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2022 Philippe Proulx - * - * SPDX-License-Identifier: MIT - */ - -#ifndef BABELTRACE_CPP_COMMON_READ_FIXED_LEN_INT_HPP -#define BABELTRACE_CPP_COMMON_READ_FIXED_LEN_INT_HPP - -#include -#include -#include - -#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 -IntT readFixedLenInt(const std::uint8_t * const buf) -{ - static_assert(std::is_integral::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 -IntT readFixedLenIntLe(const std::uint8_t * const buf) -{ - return bt2_common::littleEndianToNative(readFixedLenInt(buf)); -} - -/* - * Reads a fixed-length big-endian integer into a value of integral - * type `IntT` from the buffer `buf` and returns it. - */ -template -IntT readFixedLenIntBe(const std::uint8_t * const buf) -{ - return bt2_common::bigEndianToNative(readFixedLenInt(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 index 90dae882..00000000 --- a/src/cpp-common/safe-ops.hpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2022 Philippe Proulx - * - * SPDX-License-Identifier: MIT - */ - -#ifndef BABELTRACE_CPP_COMMON_SAFE_OPS_HPP -#define BABELTRACE_CPP_COMMON_SAFE_OPS_HPP - -#include -#include - -#include "common/assert.h" - -namespace bt2_common { - -template -constexpr bool safeToMul(const T a, const T b) -{ - static_assert(std::is_unsigned::value, "`T` is an unsigned type."); - - return a == 0 || b == 0 || a < std::numeric_limits::max() / b; -} - -template -T safeMul(const T a, const T b) noexcept -{ - static_assert(std::is_unsigned::value, "`T` is an unsigned type."); - - BT_ASSERT_DBG(safeToMul(a, b)); - return a * b; -} - -template -constexpr bool safeToAdd(const T a, const T b) -{ - static_assert(std::is_unsigned::value, "`T` is an unsigned type."); - - return a <= std::numeric_limits::max() - b; -} - -template -T safeAdd(const T a, const T b) noexcept -{ - static_assert(std::is_unsigned::value, "`T` is an unsigned type."); - - BT_ASSERT_DBG(safeToAdd(a, b)); - return a + b; -} - -template -constexpr bool safeToSub(const T a, const T b) -{ - static_assert(std::is_unsigned::value, "`T` is an unsigned type."); - - return a >= b; -} - -template -T safeSub(const T a, const T b) noexcept -{ - static_assert(std::is_unsigned::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 index 2bdb4ec7..00000000 --- a/src/cpp-common/std-int.hpp +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2022 Philippe Proulx - * - * SPDX-License-Identifier: MIT - */ - -#ifndef BABELTRACE_CPP_COMMON_STD_INT_HPP -#define BABELTRACE_CPP_COMMON_STD_INT_HPP - -#include - -namespace bt2_common { -namespace internal { - -template -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 -using StdIntT = typename internal::StdIntTBase::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 index 57392304..00000000 --- a/src/cpp-common/uuid-view.cpp +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (c) 2016-2022 Philippe Proulx - * - * 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 index d719d877..00000000 --- a/src/cpp-common/uuid-view.hpp +++ /dev/null @@ -1,114 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2020 Philippe Proulx - * - * SPDX-License-Identifier: MIT - */ - -#ifndef BABELTRACE_CPP_COMMON_UUID_VIEW_HPP -#define BABELTRACE_CPP_COMMON_UUID_VIEW_HPP - -#include -#include -#include - -#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 index 13c3cb91..00000000 --- a/src/cpp-common/uuid.hpp +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright (c) 2022 Francis Deslauriers - * - * SPDX-License-Identifier: MIT - */ - -#ifndef BABELTRACE_CPP_COMMON_UUID_HPP -#define BABELTRACE_CPP_COMMON_UUID_HPP - -#include -#include -#include - -#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 _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 index 2d6b6cbe..00000000 --- a/src/cpp-common/vector.hpp +++ /dev/null @@ -1,37 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022 Simon Marchi - * - * SPDX-License-Identifier: MIT - */ - -#ifndef SRC_CPP_COMMON_VECTOR_HPP -#define SRC_CPP_COMMON_VECTOR_HPP - -#include - -#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 -void vectorFastRemove(std::vector& vec, - const typename std::vector::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 */