blob: f8fbd5528526d19891afc11c9300ffaca0054a7d [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001// (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 NDNBOOST_IOSTREAMS_MAPPED_FILE_HPP_INCLUDED
8#define NDNBOOST_IOSTREAMS_MAPPED_FILE_HPP_INCLUDED
9
10#if defined(_MSC_VER) && (_MSC_VER >= 1020)
11# pragma once
12#endif
13
14#include <ndnboost/config.hpp> // make sure size_t is in std.
15#include <cstddef> // size_t.
16#include <string> // pathnames.
17#include <utility> // pair.
18#include <ndnboost/config.hpp> // NDNBOOST_MSVC.
19#include <ndnboost/detail/workaround.hpp>
20#include <ndnboost/iostreams/close.hpp>
21#include <ndnboost/iostreams/concepts.hpp>
22#include <ndnboost/iostreams/detail/config/auto_link.hpp>
23#include <ndnboost/iostreams/detail/config/dyn_link.hpp>
24#include <ndnboost/iostreams/detail/config/wide_streams.hpp>
25#include <ndnboost/iostreams/detail/ios.hpp> // openmode, failure
26#include <ndnboost/iostreams/detail/path.hpp>
27#include <ndnboost/iostreams/operations_fwd.hpp>
28#include <ndnboost/iostreams/positioning.hpp>
29#include <ndnboost/shared_ptr.hpp>
30#include <ndnboost/static_assert.hpp>
31#include <ndnboost/throw_exception.hpp>
32#include <ndnboost/type_traits/is_same.hpp>
33
34// Must come last.
35#include <ndnboost/config/abi_prefix.hpp>
36
37namespace ndnboost { namespace iostreams {
38
39//------------------Definition of mapped_file_base and mapped_file_params-----//
40
41// Forward declarations
42class mapped_file_source;
43class mapped_file_sink;
44class mapped_file;
45namespace detail { class mapped_file_impl; }
46
47class mapped_file_base {
48public:
49 enum mapmode {
50 readonly = 1,
51 readwrite = 2,
52 priv = 4
53 };
54};
55
56// Bitmask operations for mapped_file_base::mapmode
57mapped_file_base::mapmode
58operator|(mapped_file_base::mapmode a, mapped_file_base::mapmode b);
59
60mapped_file_base::mapmode
61operator&(mapped_file_base::mapmode a, mapped_file_base::mapmode b);
62
63mapped_file_base::mapmode
64operator^(mapped_file_base::mapmode a, mapped_file_base::mapmode b);
65
66mapped_file_base::mapmode
67operator~(mapped_file_base::mapmode a);
68
69mapped_file_base::mapmode
70operator|=(mapped_file_base::mapmode& a, mapped_file_base::mapmode b);
71
72mapped_file_base::mapmode
73operator&=(mapped_file_base::mapmode& a, mapped_file_base::mapmode b);
74
75mapped_file_base::mapmode
76operator^=(mapped_file_base::mapmode& a, mapped_file_base::mapmode b);
77
78//------------------Definition of mapped_file_params--------------------------//
79
80namespace detail {
81
82struct 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 { }
88private:
89 friend class mapped_file_impl;
90 void normalize();
91public:
92 mapped_file_base::mapmode flags;
93 NDNBOOST_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// ndnboost::filesystem::path, ndnboost::filesystem::wpath,
106// and ndnboost::iostreams::detail::path (used to store either a std::string or a
107// std::wstring).
108template<typename Path>
109struct 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 ndnboost::filesystem::wpath
116#ifndef NDNBOOST_IOSTREAMS_NO_WIDE_STREAMS
117 NDNBOOST_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
145typedef basic_mapped_file_params<std::string> mapped_file_params;
146
147//------------------Definition of mapped_file_source--------------------------//
148
149class NDNBOOST_IOSTREAMS_DECL mapped_file_source : public mapped_file_base {
150private:
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 ndnboost::iostreams::operations<mapped_file_source>;
158public:
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 NDNBOOST_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 ndnboost::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 ndnboost::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
214private:
215 void init();
216 void open_impl(const param_type& p);
217
218 ndnboost::shared_ptr<impl_type> pimpl_;
219};
220
221//------------------Definition of mapped_file---------------------------------//
222
223class NDNBOOST_IOSTREAMS_DECL mapped_file : public mapped_file_base {
224private:
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 ndnboost::iostreams::operations<mapped_file >;
229 friend class mapped_file_sink;
230public:
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 NDNBOOST_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 NDNBOOST_IOS::openmode mode =
261 NDNBOOST_IOS::in | NDNBOOST_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 NDNBOOST_IOS::openmode mode =
291 NDNBOOST_IOS::in | NDNBOOST_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);
320private:
321 delegate_type delegate_;
322};
323
324//------------------Definition of mapped_file_sink----------------------------//
325
326class NDNBOOST_IOSTREAMS_DECL mapped_file_sink : private mapped_file {
327public:
328 friend struct ndnboost::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 ndnboost::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 ndnboost::intmax_t offset = 0,
380 mapmode flags = readwrite );
381};
382
383//------------------Implementation of mapped_file_source----------------------//
384
385template<typename Path>
386mapped_file_source::mapped_file_source(const basic_mapped_file_params<Path>& p)
387{ init(); open(p); }
388
389template<typename Path>
390mapped_file_source::mapped_file_source(
391 const Path& path, size_type length, ndnboost::intmax_t offset)
392{ init(); open(path, length, offset); }
393
394template<typename Path>
395void 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 ndnboost::throw_exception(NDNBOOST_IOSTREAMS_FAILURE("invalid flags"));
401 } else {
402 if (params.mode & NDNBOOST_IOS::out)
403 ndnboost::throw_exception(NDNBOOST_IOSTREAMS_FAILURE("invalid mode"));
404 params.mode |= NDNBOOST_IOS::in;
405 }
406 open_impl(params);
407}
408
409template<typename Path>
410void mapped_file_source::open(
411 const Path& path, size_type length, ndnboost::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
421template<typename Path>
422mapped_file::mapped_file(const basic_mapped_file_params<Path>& p)
423{ open(p); }
424
425template<typename Path>
426mapped_file::mapped_file(
427 const Path& path, mapmode flags,
428 size_type length, stream_offset offset )
429{ open(path, flags, length, offset); }
430
431template<typename Path>
432mapped_file::mapped_file(
433 const Path& path, NDNBOOST_IOS::openmode mode,
434 size_type length, stream_offset offset )
435{ open(path, mode, length, offset); }
436
437template<typename Path>
438void mapped_file::open(const basic_mapped_file_params<Path>& p)
439{ delegate_.open_impl(p); }
440
441template<typename Path>
442void 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
453template<typename Path>
454void mapped_file::open(
455 const Path& path, NDNBOOST_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
465inline char* mapped_file::data() const
466{ return (flags() != readonly) ? const_cast<char*>(delegate_.data()) : 0; }
467
468//------------------Implementation of mapped_file_sink------------------------//
469
470template<typename Path>
471mapped_file_sink::mapped_file_sink(const basic_mapped_file_params<Path>& p)
472{ open(p); }
473
474template<typename Path>
475mapped_file_sink::mapped_file_sink(
476 const Path& path, size_type length,
477 ndnboost::intmax_t offset, mapmode flags )
478{ open(path, length, offset, flags); }
479
480template<typename Path>
481void 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 ndnboost::throw_exception(NDNBOOST_IOSTREAMS_FAILURE("invalid flags"));
487 } else {
488 if (params.mode & NDNBOOST_IOS::in)
489 ndnboost::throw_exception(NDNBOOST_IOSTREAMS_FAILURE("invalid mode"));
490 params.mode |= NDNBOOST_IOS::out;
491 }
492 mapped_file::open(params);
493}
494
495template<typename Path>
496void mapped_file_sink::open(
497 const Path& path, size_type length,
498 ndnboost::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
509template<>
510struct operations<mapped_file_source>
511 : ndnboost::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
521template<>
522struct operations<mapped_file>
523 : ndnboost::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
537template<>
538struct operations<mapped_file_sink>
539 : ndnboost::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
550inline mapped_file::mapmode
551operator|(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
557inline mapped_file::mapmode
558operator&(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
564inline mapped_file::mapmode
565operator^(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
571inline mapped_file::mapmode
572operator~(mapped_file::mapmode a)
573{
574 return static_cast<mapped_file::mapmode>(~static_cast<int>(a));
575}
576
577inline mapped_file::mapmode
578operator|=(mapped_file::mapmode& a, mapped_file::mapmode b)
579{
580 return a = a | b;
581}
582
583inline mapped_file::mapmode
584operator&=(mapped_file::mapmode& a, mapped_file::mapmode b)
585{
586 return a = a & b;
587}
588
589inline mapped_file::mapmode
590operator^=(mapped_file::mapmode& a, mapped_file::mapmode b)
591{
592 return a = a ^ b;
593}
594
595} } // End namespaces iostreams, boost.
596
597#include <ndnboost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
598
599#endif // #ifndef NDNBOOST_IOSTREAMS_MAPPED_FILE_HPP_INCLUDED