src/cpp-common: add bt2_common::makeUnique()
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 2 Mar 2022 18:47:25 +0000 (13:47 -0500)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 11 Sep 2023 15:24:02 +0000 (11:24 -0400)
This is our equivalent of std::make_unique() (introduced in C++14) for
C++11.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ic58341f7419ac447f83401358ca0884151246dc8
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7465
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10815
Tested-by: jenkins <jenkins@lttng.org>
src/cpp-common/Makefile.am
src/cpp-common/make-unique.hpp [new file with mode: 0644]

index 016bc0e93710d1e4569f3fe78c4c33d3ad0e67c6..09b19a230deacb7b17b7bd144be10d0aebaa4d0e 100644 (file)
@@ -12,4 +12,5 @@ EXTRA_DIST = bt2 \
        optional.hpp \
        string_view.hpp \
        uuid-view.hpp \
-       endian.hpp
+       endian.hpp \
+       make-unique.hpp
diff --git a/src/cpp-common/make-unique.hpp b/src/cpp-common/make-unique.hpp
new file mode 100644 (file)
index 0000000..66dd7f7
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * 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 <utility>
+#include <type_traits>
+
+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 */
This page took 0.025177 seconds and 4 git commands to generate.