fclose_deleter.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright Andrey Semashev 2022.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file fclose_deleter.hpp
  9. * \author Andrey Semashev
  10. * \date 21.09.2022
  11. *
  12. * This header contains an \c fclose_deleter implementation. This is a deleter
  13. * function object that invokes <tt>std::fclose</tt> on the passed pointer to
  14. * a <tt>std::FILE</tt> structure.
  15. */
  16. #ifndef BOOST_CORE_FCLOSE_DELETER_HPP
  17. #define BOOST_CORE_FCLOSE_DELETER_HPP
  18. #include <cstdio>
  19. #include <boost/config.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. namespace boost {
  24. // Block unintended ADL
  25. namespace fclose_deleter_ns {
  26. //! A function object that closes a file
  27. struct fclose_deleter
  28. {
  29. //! Function object result type
  30. typedef void result_type;
  31. /*!
  32. * Closes the file handle
  33. */
  34. void operator() (std::FILE* p) const BOOST_NOEXCEPT
  35. {
  36. if (BOOST_LIKELY(!!p))
  37. std::fclose(p);
  38. }
  39. };
  40. } // namespace fclose_deleter_ns
  41. using fclose_deleter_ns::fclose_deleter;
  42. } // namespace boost
  43. #endif // BOOST_CORE_FCLOSE_DELETER_HPP