From fa5e9d5714433f38d2b3d8e6f1422a223d781125 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Wed, 2 Mar 2022 13:47:25 -0500 Subject: [PATCH] src/cpp-common: add bt2_common::makeUnique() This is our equivalent of std::make_unique() (introduced in C++14) for C++11. Signed-off-by: Philippe Proulx Change-Id: Ic58341f7419ac447f83401358ca0884151246dc8 Reviewed-on: https://review.lttng.org/c/babeltrace/+/7465 Reviewed-by: Francis Deslauriers Reviewed-on: https://review.lttng.org/c/babeltrace/+/10815 Tested-by: jenkins --- src/cpp-common/Makefile.am | 3 ++- src/cpp-common/make-unique.hpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/cpp-common/make-unique.hpp diff --git a/src/cpp-common/Makefile.am b/src/cpp-common/Makefile.am index 016bc0e9..09b19a23 100644 --- a/src/cpp-common/Makefile.am +++ b/src/cpp-common/Makefile.am @@ -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 index 00000000..66dd7f7a --- /dev/null +++ b/src/cpp-common/make-unique.hpp @@ -0,0 +1,29 @@ +/* + * 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 */ -- 2.34.1