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 
7  * File:        boost/iostreams/detail/functional.hpp
8  * Date:        Sun Dec 09 05:38:03 MST 2007
9  * Copyright:   2007-2008 CodeRage, LLC
10  * Author:      Jonathan Turkanis
11  * Contact:     turkanis at coderage dot com
12 
13  * Defines several function objects and object generators for use with
14  * execute_all()
15  */
16 
17 #ifndef BOOST_IOSTREAMS_DETAIL_FUNCTIONAL_HPP_INCLUDED
18 #define BOOST_IOSTREAMS_DETAIL_FUNCTIONAL_HPP_INCLUDED
19 
20 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
21 # pragma once
22 #endif
23 
24 #include <boost/iostreams/close.hpp>
25 #include <boost/iostreams/detail/ios.hpp> // BOOST_IOS
26 
27 namespace boost { namespace iostreams { namespace detail {
28 
29     // Function objects and object generators for invoking
30     // boost::iostreams::close
31 
32 template<typename T>
33 class device_close_operation {
34 public:
35     typedef void result_type;
device_close_operation(T & t,BOOST_IOS::openmode which)36     device_close_operation(T& t, BOOST_IOS::openmode which)
37         : t_(t), which_(which)
38         { }
operator ()() const39     void operator()() const { boost::iostreams::close(t_, which_); }
40 private:
41     device_close_operation& operator=(const device_close_operation&);
42     T&                   t_;
43     BOOST_IOS::openmode  which_;
44 };
45 
46 template<typename T, typename Sink>
47 class filter_close_operation {
48 public:
49     typedef void result_type;
filter_close_operation(T & t,Sink & snk,BOOST_IOS::openmode which)50     filter_close_operation(T& t, Sink& snk, BOOST_IOS::openmode which)
51         : t_(t), snk_(snk), which_(which)
52         { }
operator ()() const53     void operator()() const { boost::iostreams::close(t_, snk_, which_); }
54 private:
55     filter_close_operation& operator=(const filter_close_operation&);
56     T&                   t_;
57     Sink&                snk_;
58     BOOST_IOS::openmode  which_;
59 };
60 
61 template<typename T>
62 device_close_operation<T>
call_close(T & t,BOOST_IOS::openmode which)63 call_close(T& t, BOOST_IOS::openmode which)
64 { return device_close_operation<T>(t, which); }
65 
66 template<typename T, typename Sink>
67 filter_close_operation<T, Sink>
call_close(T & t,Sink & snk,BOOST_IOS::openmode which)68 call_close(T& t, Sink& snk, BOOST_IOS::openmode which)
69 { return filter_close_operation<T, Sink>(t, snk, which); }
70 
71     // Function objects and object generators for invoking
72     // boost::iostreams::detail::close_all
73 
74 template<typename T>
75 class device_close_all_operation {
76 public:
77     typedef void result_type;
device_close_all_operation(T & t)78     device_close_all_operation(T& t) : t_(t) { }
operator ()() const79     void operator()() const { detail::close_all(t_); }
80 private:
81     device_close_all_operation& operator=(const device_close_all_operation&);
82     T& t_;
83 };
84 
85 template<typename T, typename Sink>
86 class filter_close_all_operation {
87 public:
88     typedef void result_type;
filter_close_all_operation(T & t,Sink & snk)89     filter_close_all_operation(T& t, Sink& snk) : t_(t), snk_(snk) { }
operator ()() const90     void operator()() const { detail::close_all(t_, snk_); }
91 private:
92     filter_close_all_operation& operator=(const filter_close_all_operation&);
93     T&     t_;
94     Sink&  snk_;
95 };
96 
97 template<typename T>
call_close_all(T & t)98 device_close_all_operation<T> call_close_all(T& t)
99 { return device_close_all_operation<T>(t); }
100 
101 template<typename T, typename Sink>
102 filter_close_all_operation<T, Sink>
call_close_all(T & t,Sink & snk)103 call_close_all(T& t, Sink& snk)
104 { return filter_close_all_operation<T, Sink>(t, snk); }
105 
106     // Function object and object generator for invoking a
107     // member function void close(std::ios_base::openmode)
108 
109 template<typename T>
110 class member_close_operation {
111 public:
112     typedef void result_type;
member_close_operation(T & t,BOOST_IOS::openmode which)113     member_close_operation(T& t, BOOST_IOS::openmode which)
114         : t_(t), which_(which)
115         { }
operator ()() const116     void operator()() const { t_.close(which_); }
117 private:
118     member_close_operation& operator=(const member_close_operation&);
119     T&                   t_;
120     BOOST_IOS::openmode  which_;
121 };
122 
123 template<typename T>
call_member_close(T & t,BOOST_IOS::openmode which)124 member_close_operation<T> call_member_close(T& t, BOOST_IOS::openmode which)
125 { return member_close_operation<T>(t, which); }
126 
127     // Function object and object generator for invoking a
128     // member function void reset()
129 
130 template<typename T>
131 class reset_operation {
132 public:
reset_operation(T & t)133     reset_operation(T& t) : t_(t) { }
operator ()() const134     void operator()() const { t_.reset(); }
135 private:
136     reset_operation& operator=(const reset_operation&);
137     T& t_;
138 };
139 
140 template<typename T>
call_reset(T & t)141 reset_operation<T> call_reset(T& t) { return reset_operation<T>(t); }
142 
143     // Function object and object generator for clearing a flag
144 
145 template<typename T>
146 class clear_flags_operation {
147 public:
148     typedef void result_type;
clear_flags_operation(T & t)149     clear_flags_operation(T& t) : t_(t) { }
operator ()() const150     void operator()() const { t_ = 0; }
151 private:
152     clear_flags_operation& operator=(const clear_flags_operation&);
153     T& t_;
154 };
155 
156 template<typename T>
clear_flags(T & t)157 clear_flags_operation<T> clear_flags(T& t)
158 { return clear_flags_operation<T>(t); }
159 
160     // Function object and generator for flushing a buffer
161 
162 // Function object for use with execute_all()
163 template<typename Buffer, typename Device>
164 class flush_buffer_operation {
165 public:
166     typedef void result_type;
flush_buffer_operation(Buffer & buf,Device & dev,bool flush)167     flush_buffer_operation(Buffer& buf, Device& dev, bool flush)
168         : buf_(buf), dev_(dev), flush_(flush)
169         { }
operator ()() const170     void operator()() const
171     {
172         if (flush_)
173             buf_.flush(dev_);
174     }
175 private:
176     flush_buffer_operation& operator=(const flush_buffer_operation&);
177     Buffer&  buf_;
178     Device&  dev_;
179     bool     flush_;
180 };
181 
182 template<typename Buffer, typename Device>
183 flush_buffer_operation<Buffer, Device>
flush_buffer(Buffer & buf,Device & dev,bool flush)184 flush_buffer(Buffer& buf, Device& dev, bool flush)
185 { return flush_buffer_operation<Buffer, Device>(buf, dev, flush); }
186 
187 } } } // End namespaces detail, iostreams, boost.
188 
189 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_FUNCTIONAL_HPP_INCLUDED
190