ostream_guard.hpp 933 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. Copyright 2019-2020 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_IO_DETAIL_OSTREAM_GUARD_HPP
  8. #define BOOST_IO_DETAIL_OSTREAM_GUARD_HPP
  9. #include <boost/config.hpp>
  10. #include <iosfwd>
  11. namespace boost {
  12. namespace io {
  13. namespace detail {
  14. template<class Char, class Traits>
  15. class ostream_guard {
  16. public:
  17. explicit ostream_guard(std::basic_ostream<Char, Traits>& os) BOOST_NOEXCEPT
  18. : os_(&os) { }
  19. ~ostream_guard() BOOST_NOEXCEPT_IF(false) {
  20. if (os_) {
  21. os_->setstate(std::basic_ostream<Char, Traits>::badbit);
  22. }
  23. }
  24. void release() BOOST_NOEXCEPT {
  25. os_ = 0;
  26. }
  27. private:
  28. ostream_guard(const ostream_guard&);
  29. ostream_guard& operator=(const ostream_guard&);
  30. std::basic_ostream<Char, Traits>* os_;
  31. };
  32. } /* detail */
  33. } /* io */
  34. } /* boost */
  35. #endif