Added fclose_deleter.

fclose_deleter can be used as a deleter function object for std::FILE
pointers returned by std::fopen.
This commit is contained in:
Andrey Semashev
2022-09-21 02:19:11 +03:00
parent 89852794ca
commit 3510f6244b
6 changed files with 136 additions and 0 deletions

View File

@@ -186,6 +186,9 @@ compile-fail scoped_enum_compile_fail_conv_to_int.cpp
run underlying_type.cpp ;
compile fclose_deleter_test.cpp
: <target-os>windows:<define>_CRT_SECURE_NO_WARNINGS <target-os>windows:<define>_CRT_SECURE_NO_DEPRECATE ;
run pointer_traits_pointer_test.cpp ;
run pointer_traits_element_type_test.cpp ;
run pointer_traits_difference_type_test.cpp ;

View File

@@ -0,0 +1,52 @@
/*
* Copyright Andrey Semashev 2022.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
/*!
* \file fclose_deleter_test.cpp
* \author Andrey Semashev
* \date 21.09.2022
*
* This file contains tests for \c boost::fclose_deleter.
*/
#include <boost/core/fclose_deleter.hpp>
#include <cstdio>
#include <cstddef>
#include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_SMART_PTR)
#include <memory>
#endif
#if !defined(BOOST_NO_CXX11_SMART_PTR)
std::unique_ptr< std::FILE, boost::fclose_deleter > make_unique_file(const char* filename)
{
return std::unique_ptr< std::FILE, boost::fclose_deleter >(std::fopen(filename, "w"));
}
std::shared_ptr< std::FILE > make_shared_file(const char* filename)
{
return std::shared_ptr< std::FILE >(std::fopen(filename, "w"), boost::fclose_deleter());
}
#endif
int main()
{
const char* const filename = "fcd_test.txt";
std::FILE* file = std::fopen(filename, "w");
if (file)
{
boost::fclose_deleter()(file);
file = NULL;
}
#if !defined(BOOST_NO_CXX11_SMART_PTR)
make_unique_file(filename);
make_shared_file(filename);
#endif
std::remove(filename);
}

View File

@@ -20,6 +20,7 @@
#include <boost/core/lightweight_test.hpp>
#include <boost/noncopyable.hpp>
#include <boost/core/null_deleter.hpp>
#include <boost/core/fclose_deleter.hpp>
#include <boost/core/pointer_traits.hpp>
#include <boost/ref.hpp>
#include <boost/core/scoped_enum.hpp>