Ticket #1612: mapped_file.hpp

File mapped_file.hpp, 9.9 KB (added by Jorge Lodos, 15 years ago)

Modified with the bool variant

Line 
1// (C) Copyright Jonathan Turkanis 2003.
2// (C) Copyright Craig Henderson 2002. 'boost/memmap.hpp' from sandbox
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
5
6// See http://www.boost.org/libs/iostreams for documentation.
7
8//
9// This header and its accompanying source file libs/iostreams/memmap.cpp are
10// an adaptation of Craig Henderson's memmory mapped file library. The
11// interface has been revised significantly, but the underlying OS-specific
12// code is essentially the same, with some code from Boost.Filesystem
13// mixed in. (See notations in source.)
14//
15// The following changes have been made:
16//
17// 1. OS-specific code put in a .cpp file.
18// 2. Name of main class changed to mapped_file.
19// 3. mapped_file given an interface similar to std::fstream (open(),
20// is_open(), close()) and std::string (data(), size(), begin(), end()).
21// 4. An additional class readonly_mapped_file has been provided as a
22// convenience.
23// 5. [Obsolete: Error states are reported using filesystem::error_code.]
24// 6. Read-only or read-write states are specified using ios_base::openmode.
25// 7. Access to the underlying file handles and to security parameters
26// has been removed.
27//
28
29#ifndef BOOST_IOSTREAMS_MAPPED_FILE_HPP_INCLUDED
30#define BOOST_IOSTREAMS_MAPPED_FILE_HPP_INCLUDED
31
32#if defined(_MSC_VER) && (_MSC_VER >= 1020)
33# pragma once
34#endif
35
36#include <boost/config.hpp> // make sure size_t is in std.
37#include <cstddef> // size_t.
38#include <string> // pathnames.
39#include <utility> // pair.
40#include <boost/config.hpp> // BOOST_MSVC.
41#include <boost/detail/workaround.hpp>
42#include <boost/iostreams/close.hpp>
43#include <boost/iostreams/concepts.hpp>
44#include <boost/iostreams/detail/config/auto_link.hpp>
45#include <boost/iostreams/detail/config/dyn_link.hpp>
46#include <boost/iostreams/detail/ios.hpp> // openmode.
47#include <boost/iostreams/operations_fwd.hpp>
48#include <boost/iostreams/positioning.hpp>
49#include <boost/shared_ptr.hpp>
50
51// Must come last.
52#include <boost/iostreams/detail/config/disable_warnings.hpp>
53#include <boost/config/abi_prefix.hpp>
54
55namespace boost { namespace iostreams {
56
57namespace detail {
58
59struct mapped_file_impl;
60
61} // End namespace detail.
62
63struct mapped_file_params {
64 explicit mapped_file_params()
65 : mode(), offset(0), length(static_cast<std::size_t>(-1)),
66 new_file_size(0), hint(0), priv(false)
67 { }
68 explicit mapped_file_params(const std::string& path)
69 : path(path), mode(), offset(0),
70 length(static_cast<std::size_t>(-1)),
71 new_file_size(0), hint(0)
72 { }
73 std::string path;
74 BOOST_IOS::openmode mode;
75 stream_offset offset;
76 std::size_t length;
77 stream_offset new_file_size;
78 const char* hint;
79 bool priv;
80};
81
82//------------------Definition of mapped_file_source--------------------------//
83
84class BOOST_IOSTREAMS_DECL mapped_file_source {
85private:
86 struct safe_bool_helper { int x; }; // From Bronek Kozicki.
87 typedef int safe_bool_helper::* safe_bool;
88 friend struct operations<mapped_file_source>;
89public:
90 typedef char char_type;
91 struct category
92 : public source_tag,
93 public direct_tag,
94 public closable_tag
95 { };
96 typedef std::size_t size_type;
97 typedef const char* iterator;
98 BOOST_STATIC_CONSTANT(size_type, max_length = static_cast<size_type>(-1));
99
100 mapped_file_source() { }
101 explicit mapped_file_source(mapped_file_params);
102 explicit mapped_file_source( const std::string& path,
103 size_type length = max_length,
104 boost::intmax_t offset = 0, bool priv = false );
105
106 //--------------Stream interface------------------------------------------//
107
108 void open(mapped_file_params params);
109 void open( const std::string& path,
110 size_type length = max_length,
111 boost::intmax_t offset = 0, bool priv = false );
112 bool is_open() const;
113 void close();
114
115 operator safe_bool() const;
116 bool operator!() const;
117 BOOST_IOS::openmode mode() const;
118
119 //--------------Container interface---------------------------------------//
120
121 size_type size() const;
122 const char* data() const;
123 iterator begin() const;
124 iterator end() const;
125
126 //--------------Query admissible offsets----------------------------------//
127
128 // Returns the allocation granularity for virtual memory. Values passed
129 // as offsets must be multiples of this value.
130 static int alignment();
131private:
132 friend class mapped_file;
133 typedef detail::mapped_file_impl impl_type;
134 void open_impl(mapped_file_params);
135
136 boost::shared_ptr<impl_type> pimpl_;
137};
138
139//------------------Definition of mapped_file---------------------------------//
140
141class BOOST_IOSTREAMS_DECL mapped_file {
142private:
143 typedef mapped_file_source delegate_type;
144 delegate_type delegate_;
145 friend struct operations<mapped_file>;
146public:
147 typedef char char_type;
148 struct category
149 : public seekable_device_tag,
150 public direct_tag,
151 public closable_tag
152 { };
153 typedef mapped_file_source::size_type size_type;
154 typedef char* iterator;
155 typedef const char* const_iterator;
156 BOOST_STATIC_CONSTANT(size_type, max_length = delegate_type::max_length);
157 mapped_file() { }
158 explicit mapped_file(mapped_file_params p);
159 explicit mapped_file( const std::string& path,
160 BOOST_IOS::openmode mode =
161 BOOST_IOS::in | BOOST_IOS::out,
162 size_type length = max_length,
163 stream_offset offset = 0, bool priv = false );
164
165 //--------------Conversion to readonly_mapped_file------------------------//
166
167 operator mapped_file_source&() { return delegate_; }
168 operator const mapped_file_source&() const { return delegate_; }
169
170 //--------------Stream interface------------------------------------------//
171
172 void open(mapped_file_params p);
173 void open( const std::string& path,
174 BOOST_IOS::openmode mode =
175 BOOST_IOS::in | BOOST_IOS::out,
176 size_type length = max_length,
177 stream_offset offset = 0,
178 bool priv = false);
179 bool is_open() const { return delegate_.is_open(); }
180 void close() { delegate_.close(); }
181 operator delegate_type::safe_bool() const { return delegate_; }
182 bool operator!() const { return !is_open(); }
183 BOOST_IOS::openmode mode() const { return delegate_.mode(); }
184
185 //--------------Container interface---------------------------------------//
186
187 size_type size() const { return delegate_.size(); }
188 char* data() const
189 {
190 return (mode() & BOOST_IOS::out) ?
191 const_cast<char*>(delegate_.data()) :
192 0;
193 }
194 const char* const_data() const { return delegate_.data(); }
195 iterator begin() const { return data(); }
196 const_iterator const_begin() const { return data(); }
197 iterator end() const { return data() + size(); }
198 const_iterator const_end() const { return data() + size(); }
199
200 //--------------Query admissible offsets----------------------------------//
201
202 // Returns the allocation granularity for virtual memory. Values passed
203 // as offsets must be multiples of this value.
204 static int alignment() { return mapped_file_source::alignment(); }
205};
206
207struct BOOST_IOSTREAMS_DECL mapped_file_sink : private mapped_file {
208 friend struct operations<mapped_file_sink>;
209 typedef char char_type;
210 struct category
211 : public sink_tag,
212 public direct_tag,
213 public closable_tag
214 { };
215 using mapped_file::close;
216 explicit mapped_file_sink(mapped_file_params p);
217 explicit mapped_file_sink( const std::string& path,
218 size_type length = max_length,
219 boost::intmax_t offset = 0,
220 bool priv = false);
221 void open(mapped_file_params p);
222 void open( const std::string& path,
223 size_type length = max_length,
224 boost::intmax_t offset = 0, bool priv = false );
225};
226
227//------------------Specialization of direct_impl-----------------------------//
228
229template<>
230struct operations<boost::iostreams::mapped_file_source>
231 : detail::close_impl<closable_tag>
232{
233 static std::pair<char*, char*>
234 input_sequence(boost::iostreams::mapped_file_source& src)
235 {
236 return std::make_pair( const_cast<char*>(src.begin()),
237 const_cast<char*>(src.end()) );
238 }
239};
240
241template<>
242struct operations<boost::iostreams::mapped_file_sink>
243 : detail::close_impl<closable_tag>
244{
245 static std::pair<char*, char*>
246 output_sequence(boost::iostreams::mapped_file_sink& sink)
247 {
248 return std::make_pair(sink.begin(), sink.end());
249 }
250};
251
252template<>
253struct operations<boost::iostreams::mapped_file>
254 : detail::close_impl<closable_tag>
255{
256 static std::pair<char*, char*>
257 input_sequence(boost::iostreams::mapped_file& file)
258 {
259 return std::make_pair(file.begin(), file.end());
260 }
261 static std::pair<char*, char*>
262 output_sequence(boost::iostreams::mapped_file& file)
263 {
264 return std::make_pair(file.begin(), file.end());
265 }
266};
267
268} } // End namespaces iostreams, boost.
269
270#include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
271#include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC.
272
273#endif // #ifndef BOOST_IOSTREAMS_MAPPED_FILE_HPP_INCLUDED