From 23ed167e8963e77526ba0515d188c653cd45ca34 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Sat, 28 May 2022 14:18:17 -0400 Subject: [PATCH] cpp-common: add FileUP 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 Reviewed-on: https://review.lttng.org/c/babeltrace/+/8172 Reviewed-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/10843 Tested-by: jenkins CI-Build: Philippe Proulx --- src/cpp-common/Makefile.am | 3 ++- src/cpp-common/libc-up.hpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/cpp-common/libc-up.hpp diff --git a/src/cpp-common/Makefile.am b/src/cpp-common/Makefile.am index 62e5aff4..13f31e73 100644 --- a/src/cpp-common/Makefile.am +++ b/src/cpp-common/Makefile.am @@ -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 index 00000000..c53a39a5 --- /dev/null +++ b/src/cpp-common/libc-up.hpp @@ -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 +#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 */ -- 2.34.1