mirror of
https://github.com/boostorg/core.git
synced 2025-05-09 23:03:54 +00:00
Added a check for NULL pointer in fclose_deleter.
The deleter can be called on a null pointer by shared_ptr. Also added tests with unique_ptr from Boost.Move and shared_ptr from Boost.SmartPtr.
This commit is contained in:
parent
3510f6244b
commit
00f4f11f14
@ -36,6 +36,7 @@ struct fclose_deleter
|
||||
*/
|
||||
void operator() (std::FILE* p) const BOOST_NOEXCEPT
|
||||
{
|
||||
if (BOOST_LIKELY(!!p))
|
||||
std::fclose(p);
|
||||
}
|
||||
};
|
||||
|
@ -186,8 +186,8 @@ 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 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 ;
|
||||
|
@ -16,17 +16,29 @@
|
||||
#include <cstdio>
|
||||
#include <cstddef>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/move/unique_ptr.hpp>
|
||||
#include <boost/smart_ptr/shared_ptr.hpp>
|
||||
#if !defined(BOOST_NO_CXX11_SMART_PTR)
|
||||
#include <memory>
|
||||
#endif
|
||||
|
||||
boost::movelib::unique_ptr< std::FILE, boost::fclose_deleter > make_boost_unique_file(const char* filename)
|
||||
{
|
||||
return boost::movelib::unique_ptr< std::FILE, boost::fclose_deleter >(std::fopen(filename, "w"));
|
||||
}
|
||||
|
||||
boost::shared_ptr< std::FILE > make_boost_shared_file(const char* filename)
|
||||
{
|
||||
return boost::shared_ptr< std::FILE >(std::fopen(filename, "w"), boost::fclose_deleter());
|
||||
}
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_SMART_PTR)
|
||||
std::unique_ptr< std::FILE, boost::fclose_deleter > make_unique_file(const char* filename)
|
||||
std::unique_ptr< std::FILE, boost::fclose_deleter > make_std_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)
|
||||
std::shared_ptr< std::FILE > make_std_shared_file(const char* filename)
|
||||
{
|
||||
return std::shared_ptr< std::FILE >(std::fopen(filename, "w"), boost::fclose_deleter());
|
||||
}
|
||||
@ -43,9 +55,18 @@ int main()
|
||||
file = NULL;
|
||||
}
|
||||
|
||||
make_boost_unique_file(filename);
|
||||
make_boost_shared_file(filename);
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_SMART_PTR)
|
||||
make_unique_file(filename);
|
||||
make_shared_file(filename);
|
||||
make_std_unique_file(filename);
|
||||
make_std_shared_file(filename);
|
||||
#endif
|
||||
|
||||
// Test if the deleter can be called on a NULL pointer
|
||||
boost::shared_ptr< std::FILE >(static_cast< std::FILE* >(NULL), boost::fclose_deleter());
|
||||
#if !defined(BOOST_NO_CXX11_SMART_PTR)
|
||||
std::shared_ptr< std::FILE >(static_cast< std::FILE* >(NULL), boost::fclose_deleter());
|
||||
#endif
|
||||
|
||||
std::remove(filename);
|
||||
|
Loading…
x
Reference in New Issue
Block a user