fix doc example typo
[boost.git] / boost / iostreams / device / mapped_file.hpp
blobaae9dabfcb6352fc1d908f47544d973fdd60c410
1 // (C) Copyright Jorge Lodos 2008.
2 // (C) Copyright Jonathan Turkanis 2003.
3 // (C) Copyright Craig Henderson 2002. 'boost/memmap.hpp' from sandbox
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
7 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
8 # pragma once
9 #endif
11 #include <boost/config.hpp> // make sure size_t is in std.
12 #include <cstddef> // size_t.
13 #include <string> // pathnames.
14 #include <utility> // pair.
15 #include <boost/config.hpp> // BOOST_MSVC.
16 #include <boost/detail/workaround.hpp>
17 #include <boost/iostreams/close.hpp>
18 #include <boost/iostreams/concepts.hpp>
19 #include <boost/iostreams/detail/config/auto_link.hpp>
20 #include <boost/iostreams/detail/config/dyn_link.hpp>
21 #include <boost/iostreams/detail/config/wide_streams.hpp>
22 #include <boost/iostreams/detail/ios.hpp> // openmode, failure
23 #include <boost/iostreams/detail/path.hpp>
24 #include <boost/iostreams/operations_fwd.hpp>
25 #include <boost/iostreams/positioning.hpp>
26 #include <boost/shared_ptr.hpp>
27 #include <boost/static_assert.hpp>
28 #include <boost/type_traits/is_same.hpp>
30 // Must come last.
31 #include <boost/config/abi_prefix.hpp>
33 namespace boost { namespace iostreams {
35 //------------------Definition of mapped_file_base and mapped_file_params-----//
37 // Forward declarations
38 class mapped_file_source;
39 class mapped_file_sink;
40 class mapped_file;
41 namespace detail { class mapped_file_impl; }
43 class mapped_file_base {
44 public:
45 enum mapmode {
46 readonly = 1,
47 readwrite = 2,
48 priv = 4
52 // Bitmask operations for mapped_file_base::mapmode
53 mapped_file_base::mapmode
54 operator|(mapped_file_base::mapmode a, mapped_file_base::mapmode b);
56 mapped_file_base::mapmode
57 operator&(mapped_file_base::mapmode a, mapped_file_base::mapmode b);
59 mapped_file_base::mapmode
60 operator^(mapped_file_base::mapmode a, mapped_file_base::mapmode b);
62 mapped_file_base::mapmode
63 operator~(mapped_file_base::mapmode a);
65 mapped_file_base::mapmode
66 operator|=(mapped_file_base::mapmode& a, mapped_file_base::mapmode b);
68 mapped_file_base::mapmode
69 operator&=(mapped_file_base::mapmode& a, mapped_file_base::mapmode b);
71 mapped_file_base::mapmode
72 operator^=(mapped_file_base::mapmode& a, mapped_file_base::mapmode b);
74 //------------------Definition of mapped_file_params--------------------------//
76 namespace detail {
78 struct mapped_file_params_base {
79 mapped_file_params_base()
80 : flags(static_cast<mapped_file_base::mapmode>(0)),
81 mode(), offset(0), length(static_cast<std::size_t>(-1)),
82 new_file_size(0), hint(0)
83 { }
84 private:
85 friend class mapped_file_impl;
86 void normalize();
87 public:
88 mapped_file_base::mapmode flags;
89 BOOST_IOS::openmode mode; // Deprecated
90 stream_offset offset;
91 std::size_t length;
92 stream_offset new_file_size;
93 const char* hint;
96 } // End namespace detail.
98 // This template allows Boost.Filesystem paths to be specified when creating or
99 // reopening a memory mapped file, without creating a dependence on
100 // Boost.Filesystem. Possible values of Path include std::string,
101 // boost::filesystem::path, boost::filesystem::wpath,
102 // and boost::iostreams::detail::path (used to store either a std::string or a
103 // std::wstring).
104 template<typename Path>
105 struct basic_mapped_file_params
106 : detail::mapped_file_params_base
108 typedef detail::mapped_file_params_base base_type;
110 // For wide paths, instantiate basic_mapped_file_params
111 // with boost::filesystem::wpath
112 #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
113 BOOST_STATIC_ASSERT((!is_same<Path, std::wstring>::value));
114 #endif
116 // Default constructor
117 basic_mapped_file_params() { }
119 // Construction from a Path
120 explicit basic_mapped_file_params(const Path& p) : path(p) { }
122 // Construction from a path of a different type
123 template<typename PathT>
124 explicit basic_mapped_file_params(const PathT& p) : path(p) { }
126 // Copy constructor
127 basic_mapped_file_params(const basic_mapped_file_params& other)
128 : base_type(other), path(other.path)
131 // Templated copy constructor
132 template<typename PathT>
133 basic_mapped_file_params(const basic_mapped_file_params<PathT>& other)
134 : base_type(other), path(other.path)
137 typedef Path path_type;
138 Path path;
141 typedef basic_mapped_file_params<std::string> mapped_file_params;
143 //------------------Definition of mapped_file_source--------------------------//
145 class BOOST_IOSTREAMS_DECL mapped_file_source : public mapped_file_base {
146 private:
147 struct safe_bool_helper { int x; };
148 typedef int safe_bool_helper::* safe_bool;
149 typedef detail::mapped_file_impl impl_type;
150 typedef basic_mapped_file_params<detail::path> param_type;
151 friend class mapped_file;
152 friend class detail::mapped_file_impl;
153 friend struct boost::iostreams::operations<mapped_file_source>;
154 public:
155 typedef char char_type;
156 struct category
157 : public source_tag,
158 public direct_tag,
159 public closable_tag
160 { };
161 typedef std::size_t size_type;
162 typedef const char* iterator;
163 BOOST_STATIC_CONSTANT(size_type, max_length = static_cast<size_type>(-1));
165 // Default constructor
166 mapped_file_source();
168 // Constructor taking a parameters object
169 template<typename Path>
170 explicit mapped_file_source(const basic_mapped_file_params<Path>& p);
172 // Constructor taking a list of parameters
173 template<typename Path>
174 explicit mapped_file_source( const Path& path,
175 size_type length = max_length,
176 boost::intmax_t offset = 0 );
178 // Copy Constructor
179 mapped_file_source(const mapped_file_source& other);
181 //--------------Stream interface------------------------------------------//
183 template<typename Path>
184 void open(const basic_mapped_file_params<Path>& p);
186 template<typename Path>
187 void open( const Path& path,
188 size_type length = max_length,
189 boost::intmax_t offset = 0 );
191 bool is_open() const;
192 void close();
193 operator safe_bool() const;
194 bool operator!() const;
195 mapmode flags() const;
197 //--------------Container interface---------------------------------------//
199 size_type size() const;
200 const char* data() const;
201 iterator begin() const;
202 iterator end() const;
204 //--------------Query admissible offsets----------------------------------//
206 // Returns the allocation granularity for virtual memory. Values passed
207 // as offsets must be multiples of this value.
208 static int alignment();
210 private:
211 void init();
212 void open_impl(const param_type& p);
214 boost::shared_ptr<impl_type> pimpl_;
217 //------------------Definition of mapped_file---------------------------------//
219 class BOOST_IOSTREAMS_DECL mapped_file : public mapped_file_base {
220 private:
221 typedef mapped_file_source delegate_type;
222 typedef delegate_type::safe_bool safe_bool;
223 typedef basic_mapped_file_params<detail::path> param_type;
224 friend struct boost::iostreams::operations<mapped_file >;
225 friend class mapped_file_sink;
226 public:
227 typedef char char_type;
228 struct category
229 : public seekable_device_tag,
230 public direct_tag,
231 public closable_tag
232 { };
233 typedef mapped_file_source::size_type size_type;
234 typedef char* iterator;
235 typedef const char* const_iterator;
236 BOOST_STATIC_CONSTANT(size_type, max_length = delegate_type::max_length);
238 // Default constructor
239 mapped_file() { }
241 // Construstor taking a parameters object
242 template<typename Path>
243 explicit mapped_file(const basic_mapped_file_params<Path>& p);
245 // Constructor taking a list of parameters
246 template<typename Path>
247 mapped_file( const Path& path,
248 mapmode flags,
249 size_type length = max_length,
250 stream_offset offset = 0 );
252 // Constructor taking a list of parameters, including a
253 // std::ios_base::openmode (deprecated)
254 template<typename Path>
255 explicit mapped_file( const Path& path,
256 BOOST_IOS::openmode mode =
257 BOOST_IOS::in | BOOST_IOS::out,
258 size_type length = max_length,
259 stream_offset offset = 0 );
261 // Copy Constructor
262 mapped_file(const mapped_file& other);
264 //--------------Conversion to mapped_file_source (deprecated)-------------//
266 operator mapped_file_source&() { return delegate_; }
267 operator const mapped_file_source&() const { return delegate_; }
269 //--------------Stream interface------------------------------------------//
271 // open overload taking a parameters object
272 template<typename Path>
273 void open(const basic_mapped_file_params<Path>& p);
275 // open overload taking a list of parameters
276 template<typename Path>
277 void open( const Path& path,
278 mapmode mode,
279 size_type length = max_length,
280 stream_offset offset = 0 );
282 // open overload taking a list of parameters, including a
283 // std::ios_base::openmode (deprecated)
284 template<typename Path>
285 void open( const Path& path,
286 BOOST_IOS::openmode mode =
287 BOOST_IOS::in | BOOST_IOS::out,
288 size_type length = max_length,
289 stream_offset offset = 0 );
291 bool is_open() const { return delegate_.is_open(); }
292 void close() { delegate_.close(); }
293 operator safe_bool() const { return delegate_; }
294 bool operator!() const { return !delegate_; }
295 mapmode flags() const { return delegate_.flags(); }
297 //--------------Container interface---------------------------------------//
299 size_type size() const { return delegate_.size(); }
300 char* data() const;
301 const char* const_data() const { return delegate_.data(); }
302 iterator begin() const { return data(); }
303 const_iterator const_begin() const { return const_data(); }
304 iterator end() const { return data() + size(); }
305 const_iterator const_end() const { return const_data() + size(); }
307 //--------------Query admissible offsets----------------------------------//
309 // Returns the allocation granularity for virtual memory. Values passed
310 // as offsets must be multiples of this value.
311 static int alignment() { return mapped_file_source::alignment(); }
313 //--------------File access----------------------------------------------//
315 void resize(stream_offset new_size);
316 private:
317 delegate_type delegate_;
320 //------------------Definition of mapped_file_sink----------------------------//
322 class BOOST_IOSTREAMS_DECL mapped_file_sink : private mapped_file {
323 public:
324 friend struct boost::iostreams::operations<mapped_file_sink>;
325 using mapped_file::mapmode;
326 using mapped_file::readonly;
327 using mapped_file::readwrite;
328 using mapped_file::priv;
329 using mapped_file::char_type;
330 struct category
331 : public sink_tag,
332 public direct_tag,
333 public closable_tag
334 { };
335 using mapped_file::size_type;
336 using mapped_file::iterator;
337 using mapped_file::max_length;
338 using mapped_file::is_open;
339 using mapped_file::close;
340 using mapped_file::operator safe_bool;
341 using mapped_file::operator !;
342 using mapped_file::flags;
343 using mapped_file::size;
344 using mapped_file::data;
345 using mapped_file::begin;
346 using mapped_file::end;
347 using mapped_file::alignment;
348 using mapped_file::resize;
350 // Default constructor
351 mapped_file_sink() { }
353 // Constructor taking a parameters object
354 template<typename Path>
355 explicit mapped_file_sink(const basic_mapped_file_params<Path>& p);
357 // Constructor taking a list of parameters
358 template<typename Path>
359 explicit mapped_file_sink( const Path& path,
360 size_type length = max_length,
361 boost::intmax_t offset = 0,
362 mapmode flags = readwrite );
364 // Copy Constructor
365 mapped_file_sink(const mapped_file_sink& other);
367 // open overload taking a parameters object
368 template<typename Path>
369 void open(const basic_mapped_file_params<Path>& p);
371 // open overload taking a list of parameters
372 template<typename Path>
373 void open( const Path& path,
374 size_type length = max_length,
375 boost::intmax_t offset = 0,
376 mapmode flags = readwrite );
379 //------------------Implementation of mapped_file_source----------------------//
381 template<typename Path>
382 mapped_file_source::mapped_file_source(const basic_mapped_file_params<Path>& p)
383 { init(); open(p); }
385 template<typename Path>
386 mapped_file_source::mapped_file_source(
387 const Path& path, size_type length, boost::intmax_t offset)
388 { init(); open(path, length, offset); }
390 template<typename Path>
391 void mapped_file_source::open(const basic_mapped_file_params<Path>& p)
393 param_type params(p);
394 if (params.flags) {
395 if (params.flags != mapped_file::readonly)
396 throw new BOOST_IOSTREAMS_FAILURE("invalid flags");
397 } else {
398 if (params.mode & BOOST_IOS::out)
399 throw new BOOST_IOSTREAMS_FAILURE("invalid mode");
400 params.mode |= BOOST_IOS::in;
402 open_impl(params);
405 template<typename Path>
406 void mapped_file_source::open(
407 const Path& path, size_type length, boost::intmax_t offset)
409 param_type p(path);
410 p.length = length;
411 p.offset = offset;
412 open(p);
415 //------------------Implementation of mapped_file-----------------------------//
417 template<typename Path>
418 mapped_file::mapped_file(const basic_mapped_file_params<Path>& p)
419 { open(p); }
421 template<typename Path>
422 mapped_file::mapped_file(
423 const Path& path, mapmode flags,
424 size_type length, stream_offset offset )
425 { open(path, flags, length, offset); }
427 template<typename Path>
428 mapped_file::mapped_file(
429 const Path& path, BOOST_IOS::openmode mode,
430 size_type length, stream_offset offset )
431 { open(path, mode, length, offset); }
433 template<typename Path>
434 void mapped_file::open(const basic_mapped_file_params<Path>& p)
435 { delegate_.open_impl(p); }
437 template<typename Path>
438 void mapped_file::open(
439 const Path& path, mapmode flags,
440 size_type length, stream_offset offset )
442 param_type p(path);
443 p.flags = flags;
444 p.length = length;
445 p.offset = offset;
446 open(p);
449 template<typename Path>
450 void mapped_file::open(
451 const Path& path, BOOST_IOS::openmode mode,
452 size_type length, stream_offset offset )
454 param_type p(path);
455 p.mode = mode;
456 p.length = length;
457 p.offset = offset;
458 open(p);
461 inline char* mapped_file::data() const
462 { return (flags() != readonly) ? const_cast<char*>(delegate_.data()) : 0; }
464 //------------------Implementation of mapped_file_sink------------------------//
466 template<typename Path>
467 mapped_file_sink::mapped_file_sink(const basic_mapped_file_params<Path>& p)
468 { open(p); }
470 template<typename Path>
471 mapped_file_sink::mapped_file_sink(
472 const Path& path, size_type length,
473 boost::intmax_t offset, mapmode flags )
474 { open(path, length, offset, flags); }
476 template<typename Path>
477 void mapped_file_sink::open(const basic_mapped_file_params<Path>& p)
479 param_type params(p);
480 if (params.flags) {
481 if (params.flags & mapped_file::readonly)
482 throw new BOOST_IOSTREAMS_FAILURE("invalid flags");
483 } else {
484 if (params.mode & BOOST_IOS::in)
485 throw new BOOST_IOSTREAMS_FAILURE("invalid mode");
486 params.mode |= BOOST_IOS::out;
488 mapped_file::open(params);
491 template<typename Path>
492 void mapped_file_sink::open(
493 const Path& path, size_type length,
494 boost::intmax_t offset, mapmode flags )
496 param_type p(path);
497 p.flags = flags;
498 p.length = length;
499 p.offset = offset;
500 open(p);
503 //------------------Specialization of direct_impl-----------------------------//
505 template<>
506 struct operations<mapped_file_source>
507 : boost::iostreams::detail::close_impl<closable_tag>
509 static std::pair<char*, char*>
510 input_sequence(mapped_file_source& src)
512 return std::make_pair( const_cast<char*>(src.begin()),
513 const_cast<char*>(src.end()) );
517 template<>
518 struct operations<mapped_file>
519 : boost::iostreams::detail::close_impl<closable_tag>
521 static std::pair<char*, char*>
522 input_sequence(mapped_file& file)
524 return std::make_pair(file.begin(), file.end());
526 static std::pair<char*, char*>
527 output_sequence(mapped_file& file)
529 return std::make_pair(file.begin(), file.end());
533 template<>
534 struct operations<mapped_file_sink>
535 : boost::iostreams::detail::close_impl<closable_tag>
537 static std::pair<char*, char*>
538 output_sequence(mapped_file_sink& sink)
540 return std::make_pair(sink.begin(), sink.end());
544 //------------------Definition of mapmode operators---------------------------//
546 inline mapped_file::mapmode
547 operator|(mapped_file::mapmode a, mapped_file::mapmode b)
549 return static_cast<mapped_file::mapmode>
550 (static_cast<int>(a) | static_cast<int>(b));
553 inline mapped_file::mapmode
554 operator&(mapped_file::mapmode a, mapped_file::mapmode b)
556 return static_cast<mapped_file::mapmode>
557 (static_cast<int>(a) & static_cast<int>(b));
560 inline mapped_file::mapmode
561 operator^(mapped_file::mapmode a, mapped_file::mapmode b)
563 return static_cast<mapped_file::mapmode>
564 (static_cast<int>(a) ^ static_cast<int>(b));
567 inline mapped_file::mapmode
568 operator~(mapped_file::mapmode a)
570 return static_cast<mapped_file::mapmode>(~static_cast<int>(a));
573 inline mapped_file::mapmode
574 operator|=(mapped_file::mapmode& a, mapped_file::mapmode b)
576 return a = a | b;
579 inline mapped_file::mapmode
580 operator&=(mapped_file::mapmode& a, mapped_file::mapmode b)
582 return a = a & b;
585 inline mapped_file::mapmode
586 operator^=(mapped_file::mapmode& a, mapped_file::mapmode b)
588 return a = a ^ b;
591 } } // End namespaces iostreams, boost.
593 #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas