functional.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.(See accompanying
  3. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  4. *
  5. * See http://www.boost.org/libs/iostreams for documentation.
  6. * File: boost/iostreams/detail/functional.hpp
  7. * Date: Sun Dec 09 05:38:03 MST 2007
  8. * Copyright: 2007-2008 CodeRage, LLC
  9. * Author: Jonathan Turkanis
  10. * Contact: turkanis at coderage dot com
  11. * Defines several function objects and object generators for use with
  12. * execute_all()
  13. */
  14. #ifndef BOOST_IOSTREAMS_DETAIL_FUNCTIONAL_HPP_INCLUDED
  15. #define BOOST_IOSTREAMS_DETAIL_FUNCTIONAL_HPP_INCLUDED
  16. #if defined(_MSC_VER)
  17. # pragma once
  18. #endif
  19. #include <boost/iostreams/close.hpp>
  20. #include <boost/iostreams/detail/ios.hpp> // BOOST_IOS
  21. namespace boost { namespace iostreams { namespace detail {
  22. // Function objects and object generators for invoking
  23. // boost::iostreams::close
  24. template<typename T>
  25. class device_close_operation {
  26. public:
  27. typedef void result_type;
  28. device_close_operation(T& t, BOOST_IOS::openmode which)
  29. : t_(t), which_(which)
  30. { }
  31. void operator()() const { boost::iostreams::close(t_, which_); }
  32. private:
  33. BOOST_DELETED_FUNCTION(device_close_operation& operator=(const device_close_operation&));
  34. T& t_;
  35. BOOST_IOS::openmode which_;
  36. };
  37. template<typename T, typename Sink>
  38. class filter_close_operation {
  39. public:
  40. typedef void result_type;
  41. filter_close_operation(T& t, Sink& snk, BOOST_IOS::openmode which)
  42. : t_(t), snk_(snk), which_(which)
  43. { }
  44. void operator()() const { boost::iostreams::close(t_, snk_, which_); }
  45. private:
  46. BOOST_DELETED_FUNCTION(filter_close_operation& operator=(const filter_close_operation&));
  47. T& t_;
  48. Sink& snk_;
  49. BOOST_IOS::openmode which_;
  50. };
  51. template<typename T>
  52. device_close_operation<T>
  53. call_close(T& t, BOOST_IOS::openmode which)
  54. { return device_close_operation<T>(t, which); }
  55. template<typename T, typename Sink>
  56. filter_close_operation<T, Sink>
  57. call_close(T& t, Sink& snk, BOOST_IOS::openmode which)
  58. { return filter_close_operation<T, Sink>(t, snk, which); }
  59. // Function objects and object generators for invoking
  60. // boost::iostreams::detail::close_all
  61. template<typename T>
  62. class device_close_all_operation {
  63. public:
  64. typedef void result_type;
  65. device_close_all_operation(T& t) : t_(t) { }
  66. void operator()() const { detail::close_all(t_); }
  67. private:
  68. BOOST_DELETED_FUNCTION(device_close_all_operation& operator=(const device_close_all_operation&));
  69. T& t_;
  70. };
  71. template<typename T, typename Sink>
  72. class filter_close_all_operation {
  73. public:
  74. typedef void result_type;
  75. filter_close_all_operation(T& t, Sink& snk) : t_(t), snk_(snk) { }
  76. void operator()() const { detail::close_all(t_, snk_); }
  77. private:
  78. BOOST_DELETED_FUNCTION(filter_close_all_operation& operator=(const filter_close_all_operation&));
  79. T& t_;
  80. Sink& snk_;
  81. };
  82. template<typename T>
  83. device_close_all_operation<T> call_close_all(T& t)
  84. { return device_close_all_operation<T>(t); }
  85. template<typename T, typename Sink>
  86. filter_close_all_operation<T, Sink>
  87. call_close_all(T& t, Sink& snk)
  88. { return filter_close_all_operation<T, Sink>(t, snk); }
  89. // Function object and object generator for invoking a
  90. // member function void close(std::ios_base::openmode)
  91. template<typename T>
  92. class member_close_operation {
  93. public:
  94. typedef void result_type;
  95. member_close_operation(T& t, BOOST_IOS::openmode which)
  96. : t_(t), which_(which)
  97. { }
  98. void operator()() const { t_.close(which_); }
  99. private:
  100. BOOST_DELETED_FUNCTION(member_close_operation& operator=(const member_close_operation&));
  101. T& t_;
  102. BOOST_IOS::openmode which_;
  103. };
  104. template<typename T>
  105. member_close_operation<T> call_member_close(T& t, BOOST_IOS::openmode which)
  106. { return member_close_operation<T>(t, which); }
  107. // Function object and object generator for invoking a
  108. // member function void reset()
  109. template<typename T>
  110. class reset_operation {
  111. public:
  112. reset_operation(T& t) : t_(t) { }
  113. void operator()() const { t_.reset(); }
  114. private:
  115. BOOST_DELETED_FUNCTION(reset_operation& operator=(const reset_operation&));
  116. T& t_;
  117. };
  118. template<typename T>
  119. reset_operation<T> call_reset(T& t) { return reset_operation<T>(t); }
  120. // Function object and object generator for clearing a flag
  121. template<typename T>
  122. class clear_flags_operation {
  123. public:
  124. typedef void result_type;
  125. clear_flags_operation(T& t) : t_(t) { }
  126. void operator()() const { t_ = 0; }
  127. private:
  128. BOOST_DELETED_FUNCTION(clear_flags_operation& operator=(const clear_flags_operation&));
  129. T& t_;
  130. };
  131. template<typename T>
  132. clear_flags_operation<T> clear_flags(T& t)
  133. { return clear_flags_operation<T>(t); }
  134. // Function object and generator for flushing a buffer
  135. // Function object for use with execute_all()
  136. template<typename Buffer, typename Device>
  137. class flush_buffer_operation {
  138. public:
  139. typedef void result_type;
  140. flush_buffer_operation(Buffer& buf, Device& dev, bool flush)
  141. : buf_(buf), dev_(dev), flush_(flush)
  142. { }
  143. void operator()() const
  144. {
  145. if (flush_)
  146. buf_.flush(dev_);
  147. }
  148. private:
  149. BOOST_DELETED_FUNCTION(flush_buffer_operation& operator=(const flush_buffer_operation&));
  150. Buffer& buf_;
  151. Device& dev_;
  152. bool flush_;
  153. };
  154. template<typename Buffer, typename Device>
  155. flush_buffer_operation<Buffer, Device>
  156. flush_buffer(Buffer& buf, Device& dev, bool flush)
  157. { return flush_buffer_operation<Buffer, Device>(buf, dev, flush); }
  158. } } } // End namespaces detail, iostreams, boost.
  159. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_FUNCTIONAL_HPP_INCLUDED