comparison DEPENDENCIES/generic/include/boost/iostreams/device/mapped_file.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
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.)
6
7 #ifndef BOOST_IOSTREAMS_MAPPED_FILE_HPP_INCLUDED
8 #define BOOST_IOSTREAMS_MAPPED_FILE_HPP_INCLUDED
9
10 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
11 # pragma once
12 #endif
13
14 #include <boost/config.hpp> // make sure size_t is in std.
15 #include <cstddef> // size_t.
16 #include <string> // pathnames.
17 #include <utility> // pair.
18 #include <boost/config.hpp> // BOOST_MSVC.
19 #include <boost/detail/workaround.hpp>
20 #include <boost/iostreams/close.hpp>
21 #include <boost/iostreams/concepts.hpp>
22 #include <boost/iostreams/detail/config/auto_link.hpp>
23 #include <boost/iostreams/detail/config/dyn_link.hpp>
24 #include <boost/iostreams/detail/config/wide_streams.hpp>
25 #include <boost/iostreams/detail/ios.hpp> // openmode, failure
26 #include <boost/iostreams/detail/path.hpp>
27 #include <boost/iostreams/operations_fwd.hpp>
28 #include <boost/iostreams/positioning.hpp>
29 #include <boost/shared_ptr.hpp>
30 #include <boost/static_assert.hpp>
31 #include <boost/throw_exception.hpp>
32 #include <boost/type_traits/is_same.hpp>
33
34 // Must come last.
35 #include <boost/config/abi_prefix.hpp>
36
37 namespace boost { namespace iostreams {
38
39 //------------------Definition of mapped_file_base and mapped_file_params-----//
40
41 // Forward declarations
42 class mapped_file_source;
43 class mapped_file_sink;
44 class mapped_file;
45 namespace detail { class mapped_file_impl; }
46
47 class mapped_file_base {
48 public:
49 enum mapmode {
50 readonly = 1,
51 readwrite = 2,
52 priv = 4
53 };
54 };
55
56 // Bitmask operations for mapped_file_base::mapmode
57 mapped_file_base::mapmode
58 operator|(mapped_file_base::mapmode a, mapped_file_base::mapmode b);
59
60 mapped_file_base::mapmode
61 operator&(mapped_file_base::mapmode a, mapped_file_base::mapmode b);
62
63 mapped_file_base::mapmode
64 operator^(mapped_file_base::mapmode a, mapped_file_base::mapmode b);
65
66 mapped_file_base::mapmode
67 operator~(mapped_file_base::mapmode a);
68
69 mapped_file_base::mapmode
70 operator|=(mapped_file_base::mapmode& a, mapped_file_base::mapmode b);
71
72 mapped_file_base::mapmode
73 operator&=(mapped_file_base::mapmode& a, mapped_file_base::mapmode b);
74
75 mapped_file_base::mapmode
76 operator^=(mapped_file_base::mapmode& a, mapped_file_base::mapmode b);
77
78 //------------------Definition of mapped_file_params--------------------------//
79
80 namespace detail {
81
82 struct mapped_file_params_base {
83 mapped_file_params_base()
84 : flags(static_cast<mapped_file_base::mapmode>(0)),
85 mode(), offset(0), length(static_cast<std::size_t>(-1)),
86 new_file_size(0), hint(0)
87 { }
88 private:
89 friend class mapped_file_impl;
90 void normalize();
91 public:
92 mapped_file_base::mapmode flags;
93 BOOST_IOS::openmode mode; // Deprecated
94 stream_offset offset;
95 std::size_t length;
96 stream_offset new_file_size;
97 const char* hint;
98 };
99
100 } // End namespace detail.
101
102 // This template allows Boost.Filesystem paths to be specified when creating or
103 // reopening a memory mapped file, without creating a dependence on
104 // Boost.Filesystem. Possible values of Path include std::string,
105 // boost::filesystem::path, boost::filesystem::wpath,
106 // and boost::iostreams::detail::path (used to store either a std::string or a
107 // std::wstring).
108 template<typename Path>
109 struct basic_mapped_file_params
110 : detail::mapped_file_params_base
111 {
112 typedef detail::mapped_file_params_base base_type;
113
114 // For wide paths, instantiate basic_mapped_file_params
115 // with boost::filesystem::wpath
116 #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
117 BOOST_STATIC_ASSERT((!is_same<Path, std::wstring>::value));
118 #endif
119
120 // Default constructor
121 basic_mapped_file_params() { }
122
123 // Construction from a Path
124 explicit basic_mapped_file_params(const Path& p) : path(p) { }
125
126 // Construction from a path of a different type
127 template<typename PathT>
128 explicit basic_mapped_file_params(const PathT& p) : path(p) { }
129
130 // Copy constructor
131 basic_mapped_file_params(const basic_mapped_file_params& other)
132 : base_type(other), path(other.path)
133 { }
134
135 // Templated copy constructor
136 template<typename PathT>
137 basic_mapped_file_params(const basic_mapped_file_params<PathT>& other)
138 : base_type(other), path(other.path)
139 { }
140
141 typedef Path path_type;
142 Path path;
143 };
144
145 typedef basic_mapped_file_params<std::string> mapped_file_params;
146
147 //------------------Definition of mapped_file_source--------------------------//
148
149 class BOOST_IOSTREAMS_DECL mapped_file_source : public mapped_file_base {
150 private:
151 struct safe_bool_helper { int x; };
152 typedef int safe_bool_helper::* safe_bool;
153 typedef detail::mapped_file_impl impl_type;
154 typedef basic_mapped_file_params<detail::path> param_type;
155 friend class mapped_file;
156 friend class detail::mapped_file_impl;
157 friend struct boost::iostreams::operations<mapped_file_source>;
158 public:
159 typedef char char_type;
160 struct category
161 : public source_tag,
162 public direct_tag,
163 public closable_tag
164 { };
165 typedef std::size_t size_type;
166 typedef const char* iterator;
167 BOOST_STATIC_CONSTANT(size_type, max_length = static_cast<size_type>(-1));
168
169 // Default constructor
170 mapped_file_source();
171
172 // Constructor taking a parameters object
173 template<typename Path>
174 explicit mapped_file_source(const basic_mapped_file_params<Path>& p);
175
176 // Constructor taking a list of parameters
177 template<typename Path>
178 explicit mapped_file_source( const Path& path,
179 size_type length = max_length,
180 boost::intmax_t offset = 0 );
181
182 // Copy Constructor
183 mapped_file_source(const mapped_file_source& other);
184
185 //--------------Stream interface------------------------------------------//
186
187 template<typename Path>
188 void open(const basic_mapped_file_params<Path>& p);
189
190 template<typename Path>
191 void open( const Path& path,
192 size_type length = max_length,
193 boost::intmax_t offset = 0 );
194
195 bool is_open() const;
196 void close();
197 operator safe_bool() const;
198 bool operator!() const;
199 mapmode flags() const;
200
201 //--------------Container interface---------------------------------------//
202
203 size_type size() const;
204 const char* data() const;
205 iterator begin() const;
206 iterator end() const;
207
208 //--------------Query admissible offsets----------------------------------//
209
210 // Returns the allocation granularity for virtual memory. Values passed
211 // as offsets must be multiples of this value.
212 static int alignment();
213
214 private:
215 void init();
216 void open_impl(const param_type& p);
217
218 boost::shared_ptr<impl_type> pimpl_;
219 };
220
221 //------------------Definition of mapped_file---------------------------------//
222
223 class BOOST_IOSTREAMS_DECL mapped_file : public mapped_file_base {
224 private:
225 typedef mapped_file_source delegate_type;
226 typedef delegate_type::safe_bool safe_bool;
227 typedef basic_mapped_file_params<detail::path> param_type;
228 friend struct boost::iostreams::operations<mapped_file >;
229 friend class mapped_file_sink;
230 public:
231 typedef char char_type;
232 struct category
233 : public seekable_device_tag,
234 public direct_tag,
235 public closable_tag
236 { };
237 typedef mapped_file_source::size_type size_type;
238 typedef char* iterator;
239 typedef const char* const_iterator;
240 BOOST_STATIC_CONSTANT(size_type, max_length = delegate_type::max_length);
241
242 // Default constructor
243 mapped_file() { }
244
245 // Construstor taking a parameters object
246 template<typename Path>
247 explicit mapped_file(const basic_mapped_file_params<Path>& p);
248
249 // Constructor taking a list of parameters
250 template<typename Path>
251 mapped_file( const Path& path,
252 mapmode flags,
253 size_type length = max_length,
254 stream_offset offset = 0 );
255
256 // Constructor taking a list of parameters, including a
257 // std::ios_base::openmode (deprecated)
258 template<typename Path>
259 explicit mapped_file( const Path& path,
260 BOOST_IOS::openmode mode =
261 BOOST_IOS::in | BOOST_IOS::out,
262 size_type length = max_length,
263 stream_offset offset = 0 );
264
265 // Copy Constructor
266 mapped_file(const mapped_file& other);
267
268 //--------------Conversion to mapped_file_source (deprecated)-------------//
269
270 operator mapped_file_source&() { return delegate_; }
271 operator const mapped_file_source&() const { return delegate_; }
272
273 //--------------Stream interface------------------------------------------//
274
275 // open overload taking a parameters object
276 template<typename Path>
277 void open(const basic_mapped_file_params<Path>& p);
278
279 // open overload taking a list of parameters
280 template<typename Path>
281 void open( const Path& path,
282 mapmode mode,
283 size_type length = max_length,
284 stream_offset offset = 0 );
285
286 // open overload taking a list of parameters, including a
287 // std::ios_base::openmode (deprecated)
288 template<typename Path>
289 void open( const Path& path,
290 BOOST_IOS::openmode mode =
291 BOOST_IOS::in | BOOST_IOS::out,
292 size_type length = max_length,
293 stream_offset offset = 0 );
294
295 bool is_open() const { return delegate_.is_open(); }
296 void close() { delegate_.close(); }
297 operator safe_bool() const { return delegate_; }
298 bool operator!() const { return !delegate_; }
299 mapmode flags() const { return delegate_.flags(); }
300
301 //--------------Container interface---------------------------------------//
302
303 size_type size() const { return delegate_.size(); }
304 char* data() const;
305 const char* const_data() const { return delegate_.data(); }
306 iterator begin() const { return data(); }
307 const_iterator const_begin() const { return const_data(); }
308 iterator end() const { return data() + size(); }
309 const_iterator const_end() const { return const_data() + size(); }
310
311 //--------------Query admissible offsets----------------------------------//
312
313 // Returns the allocation granularity for virtual memory. Values passed
314 // as offsets must be multiples of this value.
315 static int alignment() { return mapped_file_source::alignment(); }
316
317 //--------------File access----------------------------------------------//
318
319 void resize(stream_offset new_size);
320 private:
321 delegate_type delegate_;
322 };
323
324 //------------------Definition of mapped_file_sink----------------------------//
325
326 class BOOST_IOSTREAMS_DECL mapped_file_sink : private mapped_file {
327 public:
328 friend struct boost::iostreams::operations<mapped_file_sink>;
329 using mapped_file::mapmode;
330 using mapped_file::readonly;
331 using mapped_file::readwrite;
332 using mapped_file::priv;
333 using mapped_file::char_type;
334 struct category
335 : public sink_tag,
336 public direct_tag,
337 public closable_tag
338 { };
339 using mapped_file::size_type;
340 using mapped_file::iterator;
341 using mapped_file::max_length;
342 using mapped_file::is_open;
343 using mapped_file::close;
344 using mapped_file::operator safe_bool;
345 using mapped_file::operator !;
346 using mapped_file::flags;
347 using mapped_file::size;
348 using mapped_file::data;
349 using mapped_file::begin;
350 using mapped_file::end;
351 using mapped_file::alignment;
352 using mapped_file::resize;
353
354 // Default constructor
355 mapped_file_sink() { }
356
357 // Constructor taking a parameters object
358 template<typename Path>
359 explicit mapped_file_sink(const basic_mapped_file_params<Path>& p);
360
361 // Constructor taking a list of parameters
362 template<typename Path>
363 explicit mapped_file_sink( const Path& path,
364 size_type length = max_length,
365 boost::intmax_t offset = 0,
366 mapmode flags = readwrite );
367
368 // Copy Constructor
369 mapped_file_sink(const mapped_file_sink& other);
370
371 // open overload taking a parameters object
372 template<typename Path>
373 void open(const basic_mapped_file_params<Path>& p);
374
375 // open overload taking a list of parameters
376 template<typename Path>
377 void open( const Path& path,
378 size_type length = max_length,
379 boost::intmax_t offset = 0,
380 mapmode flags = readwrite );
381 };
382
383 //------------------Implementation of mapped_file_source----------------------//
384
385 template<typename Path>
386 mapped_file_source::mapped_file_source(const basic_mapped_file_params<Path>& p)
387 { init(); open(p); }
388
389 template<typename Path>
390 mapped_file_source::mapped_file_source(
391 const Path& path, size_type length, boost::intmax_t offset)
392 { init(); open(path, length, offset); }
393
394 template<typename Path>
395 void mapped_file_source::open(const basic_mapped_file_params<Path>& p)
396 {
397 param_type params(p);
398 if (params.flags) {
399 if (params.flags != mapped_file::readonly)
400 boost::throw_exception(BOOST_IOSTREAMS_FAILURE("invalid flags"));
401 } else {
402 if (params.mode & BOOST_IOS::out)
403 boost::throw_exception(BOOST_IOSTREAMS_FAILURE("invalid mode"));
404 params.mode |= BOOST_IOS::in;
405 }
406 open_impl(params);
407 }
408
409 template<typename Path>
410 void mapped_file_source::open(
411 const Path& path, size_type length, boost::intmax_t offset)
412 {
413 param_type p(path);
414 p.length = length;
415 p.offset = offset;
416 open(p);
417 }
418
419 //------------------Implementation of mapped_file-----------------------------//
420
421 template<typename Path>
422 mapped_file::mapped_file(const basic_mapped_file_params<Path>& p)
423 { open(p); }
424
425 template<typename Path>
426 mapped_file::mapped_file(
427 const Path& path, mapmode flags,
428 size_type length, stream_offset offset )
429 { open(path, flags, length, offset); }
430
431 template<typename Path>
432 mapped_file::mapped_file(
433 const Path& path, BOOST_IOS::openmode mode,
434 size_type length, stream_offset offset )
435 { open(path, mode, length, offset); }
436
437 template<typename Path>
438 void mapped_file::open(const basic_mapped_file_params<Path>& p)
439 { delegate_.open_impl(p); }
440
441 template<typename Path>
442 void mapped_file::open(
443 const Path& path, mapmode flags,
444 size_type length, stream_offset offset )
445 {
446 param_type p(path);
447 p.flags = flags;
448 p.length = length;
449 p.offset = offset;
450 open(p);
451 }
452
453 template<typename Path>
454 void mapped_file::open(
455 const Path& path, BOOST_IOS::openmode mode,
456 size_type length, stream_offset offset )
457 {
458 param_type p(path);
459 p.mode = mode;
460 p.length = length;
461 p.offset = offset;
462 open(p);
463 }
464
465 inline char* mapped_file::data() const
466 { return (flags() != readonly) ? const_cast<char*>(delegate_.data()) : 0; }
467
468 //------------------Implementation of mapped_file_sink------------------------//
469
470 template<typename Path>
471 mapped_file_sink::mapped_file_sink(const basic_mapped_file_params<Path>& p)
472 { open(p); }
473
474 template<typename Path>
475 mapped_file_sink::mapped_file_sink(
476 const Path& path, size_type length,
477 boost::intmax_t offset, mapmode flags )
478 { open(path, length, offset, flags); }
479
480 template<typename Path>
481 void mapped_file_sink::open(const basic_mapped_file_params<Path>& p)
482 {
483 param_type params(p);
484 if (params.flags) {
485 if (params.flags & mapped_file::readonly)
486 boost::throw_exception(BOOST_IOSTREAMS_FAILURE("invalid flags"));
487 } else {
488 if (params.mode & BOOST_IOS::in)
489 boost::throw_exception(BOOST_IOSTREAMS_FAILURE("invalid mode"));
490 params.mode |= BOOST_IOS::out;
491 }
492 mapped_file::open(params);
493 }
494
495 template<typename Path>
496 void mapped_file_sink::open(
497 const Path& path, size_type length,
498 boost::intmax_t offset, mapmode flags )
499 {
500 param_type p(path);
501 p.flags = flags;
502 p.length = length;
503 p.offset = offset;
504 open(p);
505 }
506
507 //------------------Specialization of direct_impl-----------------------------//
508
509 template<>
510 struct operations<mapped_file_source>
511 : boost::iostreams::detail::close_impl<closable_tag>
512 {
513 static std::pair<char*, char*>
514 input_sequence(mapped_file_source& src)
515 {
516 return std::make_pair( const_cast<char*>(src.begin()),
517 const_cast<char*>(src.end()) );
518 }
519 };
520
521 template<>
522 struct operations<mapped_file>
523 : boost::iostreams::detail::close_impl<closable_tag>
524 {
525 static std::pair<char*, char*>
526 input_sequence(mapped_file& file)
527 {
528 return std::make_pair(file.begin(), file.end());
529 }
530 static std::pair<char*, char*>
531 output_sequence(mapped_file& file)
532 {
533 return std::make_pair(file.begin(), file.end());
534 }
535 };
536
537 template<>
538 struct operations<mapped_file_sink>
539 : boost::iostreams::detail::close_impl<closable_tag>
540 {
541 static std::pair<char*, char*>
542 output_sequence(mapped_file_sink& sink)
543 {
544 return std::make_pair(sink.begin(), sink.end());
545 }
546 };
547
548 //------------------Definition of mapmode operators---------------------------//
549
550 inline mapped_file::mapmode
551 operator|(mapped_file::mapmode a, mapped_file::mapmode b)
552 {
553 return static_cast<mapped_file::mapmode>
554 (static_cast<int>(a) | static_cast<int>(b));
555 }
556
557 inline mapped_file::mapmode
558 operator&(mapped_file::mapmode a, mapped_file::mapmode b)
559 {
560 return static_cast<mapped_file::mapmode>
561 (static_cast<int>(a) & static_cast<int>(b));
562 }
563
564 inline mapped_file::mapmode
565 operator^(mapped_file::mapmode a, mapped_file::mapmode b)
566 {
567 return static_cast<mapped_file::mapmode>
568 (static_cast<int>(a) ^ static_cast<int>(b));
569 }
570
571 inline mapped_file::mapmode
572 operator~(mapped_file::mapmode a)
573 {
574 return static_cast<mapped_file::mapmode>(~static_cast<int>(a));
575 }
576
577 inline mapped_file::mapmode
578 operator|=(mapped_file::mapmode& a, mapped_file::mapmode b)
579 {
580 return a = a | b;
581 }
582
583 inline mapped_file::mapmode
584 operator&=(mapped_file::mapmode& a, mapped_file::mapmode b)
585 {
586 return a = a & b;
587 }
588
589 inline mapped_file::mapmode
590 operator^=(mapped_file::mapmode& a, mapped_file::mapmode b)
591 {
592 return a = a ^ b;
593 }
594
595 } } // End namespaces iostreams, boost.
596
597 #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
598
599 #endif // #ifndef BOOST_IOSTREAMS_MAPPED_FILE_HPP_INCLUDED