blob: 52a825e614cd6c5647af178896ae27e92fd2e555 [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001/*
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: ndnboost/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 NDNBOOST_IOSTREAMS_DETAIL_FUNCTIONAL_HPP_INCLUDED
18#define NDNBOOST_IOSTREAMS_DETAIL_FUNCTIONAL_HPP_INCLUDED
19
20#if defined(_MSC_VER) && (_MSC_VER >= 1020)
21# pragma once
22#endif
23
24#include <ndnboost/iostreams/close.hpp>
25#include <ndnboost/iostreams/detail/ios.hpp> // NDNBOOST_IOS
26
27namespace ndnboost { namespace iostreams { namespace detail {
28
29 // Function objects and object generators for invoking
30 // ndnboost::iostreams::close
31
32template<typename T>
33class device_close_operation {
34public:
35 typedef void result_type;
36 device_close_operation(T& t, NDNBOOST_IOS::openmode which)
37 : t_(t), which_(which)
38 { }
39 void operator()() const { ndnboost::iostreams::close(t_, which_); }
40private:
41 device_close_operation& operator=(const device_close_operation&);
42 T& t_;
43 NDNBOOST_IOS::openmode which_;
44};
45
46template<typename T, typename Sink>
47class filter_close_operation {
48public:
49 typedef void result_type;
50 filter_close_operation(T& t, Sink& snk, NDNBOOST_IOS::openmode which)
51 : t_(t), snk_(snk), which_(which)
52 { }
53 void operator()() const { ndnboost::iostreams::close(t_, snk_, which_); }
54private:
55 filter_close_operation& operator=(const filter_close_operation&);
56 T& t_;
57 Sink& snk_;
58 NDNBOOST_IOS::openmode which_;
59};
60
61template<typename T>
62device_close_operation<T>
63call_close(T& t, NDNBOOST_IOS::openmode which)
64{ return device_close_operation<T>(t, which); }
65
66template<typename T, typename Sink>
67filter_close_operation<T, Sink>
68call_close(T& t, Sink& snk, NDNBOOST_IOS::openmode which)
69{ return filter_close_operation<T, Sink>(t, snk, which); }
70
71 // Function objects and object generators for invoking
72 // ndnboost::iostreams::detail::close_all
73
74template<typename T>
75class device_close_all_operation {
76public:
77 typedef void result_type;
78 device_close_all_operation(T& t) : t_(t) { }
79 void operator()() const { detail::close_all(t_); }
80private:
81 device_close_all_operation& operator=(const device_close_all_operation&);
82 T& t_;
83};
84
85template<typename T, typename Sink>
86class filter_close_all_operation {
87public:
88 typedef void result_type;
89 filter_close_all_operation(T& t, Sink& snk) : t_(t), snk_(snk) { }
90 void operator()() const { detail::close_all(t_, snk_); }
91private:
92 filter_close_all_operation& operator=(const filter_close_all_operation&);
93 T& t_;
94 Sink& snk_;
95};
96
97template<typename T>
98device_close_all_operation<T> call_close_all(T& t)
99{ return device_close_all_operation<T>(t); }
100
101template<typename T, typename Sink>
102filter_close_all_operation<T, Sink>
103call_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
109template<typename T>
110class member_close_operation {
111public:
112 typedef void result_type;
113 member_close_operation(T& t, NDNBOOST_IOS::openmode which)
114 : t_(t), which_(which)
115 { }
116 void operator()() const { t_.close(which_); }
117private:
118 member_close_operation& operator=(const member_close_operation&);
119 T& t_;
120 NDNBOOST_IOS::openmode which_;
121};
122
123template<typename T>
124member_close_operation<T> call_member_close(T& t, NDNBOOST_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
130template<typename T>
131class reset_operation {
132public:
133 reset_operation(T& t) : t_(t) { }
134 void operator()() const { t_.reset(); }
135private:
136 reset_operation& operator=(const reset_operation&);
137 T& t_;
138};
139
140template<typename T>
141reset_operation<T> call_reset(T& t) { return reset_operation<T>(t); }
142
143 // Function object and object generator for clearing a flag
144
145template<typename T>
146class clear_flags_operation {
147public:
148 typedef void result_type;
149 clear_flags_operation(T& t) : t_(t) { }
150 void operator()() const { t_ = 0; }
151private:
152 clear_flags_operation& operator=(const clear_flags_operation&);
153 T& t_;
154};
155
156template<typename T>
157clear_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()
163template<typename Buffer, typename Device>
164class flush_buffer_operation {
165public:
166 typedef void result_type;
167 flush_buffer_operation(Buffer& buf, Device& dev, bool flush)
168 : buf_(buf), dev_(dev), flush_(flush)
169 { }
170 void operator()() const
171 {
172 if (flush_)
173 buf_.flush(dev_);
174 }
175private:
176 flush_buffer_operation& operator=(const flush_buffer_operation&);
177 Buffer& buf_;
178 Device& dev_;
179 bool flush_;
180};
181
182template<typename Buffer, typename Device>
183flush_buffer_operation<Buffer, Device>
184flush_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 NDNBOOST_IOSTREAMS_DETAIL_FUNCTIONAL_HPP_INCLUDED