cpp-common: add FileUP
authorSimon Marchi <simon.marchi@efficios.com>
Sat, 28 May 2022 18:18:17 +0000 (14:18 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 11 Sep 2023 15:24:02 +0000 (11:24 -0400)
Add a unique_ptr type to manage the lifetime of a `FILE *`, with a
custom deleter that calls fclose.

Change-Id: Idded4546d2a709cd0652975ef83c3ad6a0f511bc
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/8172
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10843
Tested-by: jenkins <jenkins@lttng.org>
CI-Build: Philippe Proulx <eeppeliteloop@gmail.com>

src/cpp-common/Makefile.am
src/cpp-common/libc-up.hpp [new file with mode: 0644]

index 62e5aff46d954db3ddcfa0a6d8e4c34e1e6a0021..13f31e73982c1e4d70b734c1296da425db9ca854 100644 (file)
@@ -21,4 +21,5 @@ EXTRA_DIST = bt2 \
        vector.hpp \
        std-int.hpp \
        read-fixed-len-int.hpp \
-       glib-up.hpp
+       glib-up.hpp \
+       libc-up.hpp
diff --git a/src/cpp-common/libc-up.hpp b/src/cpp-common/libc-up.hpp
new file mode 100644 (file)
index 0000000..c53a39a
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * 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 */
This page took 0.024517 seconds and 4 git commands to generate.