src/cpp-common: add bt2_common::makeUnique()
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 2 Mar 2022 18:47:25 +0000 (13:47 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Tue, 23 Aug 2022 16:06:14 +0000 (12:06 -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>
src/cpp-common/Makefile.am
src/cpp-common/make-unique.hpp [new file with mode: 0644]

index ca24e0d1bc4d91f3b5ec75073423387413ecc4af..f067b13e8b39c33afbb95525af956282a8c94b66 100644 (file)
@@ -17,6 +17,7 @@ libcppcommon_la_SOURCES = \
        text-loc-str.hpp text-loc-str.cpp \
        uuid-view.hpp \
        str-scanner.hpp str-scanner.cpp \
-       parse-json.hpp
+       parse-json.hpp \
+       make-unique.hpp
 
 EXTRA_DIST = bt2
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.024705 seconds and 5 git commands to generate.