Memory-mapped files stream
Provide read-write access to memory-mapped files on Windows and POSIX systems.
 All Classes Functions
fmstream.h
1 /*
2  * Copyright (c) 2013, Benichou Software
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the author nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * Purpose: Provide read-write access to memory-mapped files on Windows and POSIX systems.
28  *
29  * $Id: fmstream.h 4 2013-09-24 14:03:10Z benichou $
30  */
31 
32 #ifndef FILE_MAPPING_STREAM_H_
33 #define FILE_MAPPING_STREAM_H_
34 
35 #include <istream>
36 
40 #if ((__cplusplus > 199711L) || (_HAS_CPP0X > 0)) && ((_MSC_VER >= 1600) || (__INTEL_COMPILER >= 1200))
41 #define _HAS_CPP11_ 1
42 #endif
43 
48 {
49 public:
58  static std::streamoff offset_granularity();
59 };
60 
70 class filemappingbuf : public std::streambuf
71 {
72 public:
78 
82  virtual ~filemappingbuf();
83 
84 #ifdef _HAS_CPP11_
85 
88 
89 
96  filemappingbuf(filemappingbuf&& rhs_buf);
97 
106 
113  void swap(filemappingbuf& buf);
115 #endif // _HAS_CPP11_
116 
125  bool is_open() const;
126 
151  filemappingbuf* open(const char* path_name, std::ios_base::openmode mode, std::streamsize max_length = 0, std::streamoff offset = 0);
152 
162 
174  void* pubseekptr(void* ptr, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out);
175 
182  const void* data() const;
183 
190  void* data();
191 
198  std::streamsize size() const;
199 
200 protected:
201  virtual int sync();
202  virtual std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode which);
203  virtual std::streampos seekpos(std::streampos sp, std::ios_base::openmode which);
204  virtual void* seekptr(void* ptr, std::ios_base::openmode which);
205 
206 private:
211 
216 
217 private:
218  char* m_pAddress;
219  std::streamsize m_MapLength;
220 #ifdef _WIN32
221  void* m_pFile;
222  void* m_pFileMapping;
223 #else // If not Windows, this is a POSIX system !
224  int m_fd;
225 #endif
226 };
227 
236 class ifmstream : public std::istream
237 {
238 public:
244  ifmstream();
245 
261  explicit ifmstream(const char* path_name, std::streamsize max_length = 0, std::streamoff offset = 0);
262 
266  virtual ~ifmstream() {}
267 
268 #ifdef _HAS_CPP11_
269 
272 
273 
280  ifmstream(ifmstream&& rhs_stream);
281 
289  ifmstream& operator=(ifmstream&& rhs_stream);
290 
297  void swap(ifmstream& stream);
299 #endif // _HAS_CPP11_
300 
307  filemappingbuf* rdbuf() const;
308 
317  bool is_open() const;
318 
335  void open(const char* path_name, std::streamsize max_length = 0, std::streamoff offset = 0);
336 
345  void close();
346 
355  const void* ptellg();
356 
367  std::istream& pseekg(const void* ptr);
368 
375  const void* data() const;
376 
383  std::streamsize size() const;
384 
385 private:
389  ifmstream(const ifmstream&);
390 
394  ifmstream& operator=(const ifmstream&);
395 
396 private:
397  filemappingbuf m_rdbuf;
398 };
399 
409 class fmstream : public std::iostream
410 {
411 public:
417  fmstream();
418 
434  explicit fmstream(const char* path_name, std::streamsize max_length = 0, std::streamoff offset = 0);
435 
439  virtual ~fmstream() {}
440 
441 #ifdef _HAS_CPP11_
442 
445 
446 
453  fmstream(fmstream&& rhs_stream);
454 
462  fmstream& operator=(fmstream&& rhs_stream);
463 
470  void swap(fmstream& stream);
472 #endif // _HAS_CPP11_
473 
480  filemappingbuf* rdbuf() const;
481 
490  bool is_open() const;
491 
510  void open(const char* path_name, std::streamsize max_length = 0, std::streamoff offset = 0);
511 
520  void close();
521 
531  const void* ptellg();
532 
542  void* ptellp();
543 
555  std::istream& pseekg(const void* ptr);
556 
568  std::ostream& pseekp(void* ptr);
569 
576  const void* data() const;
577 
584  void* data();
585 
592  std::streamsize size() const;
593 
594 private:
598  fmstream(const fmstream&);
599 
603  fmstream& operator=(const fmstream&);
604 
605 private:
606  filemappingbuf m_rdbuf;
607 };
608 
609 #ifdef _HAS_CPP11_
610 
613 
614 
621 void swap(filemappingbuf& lhs, filemappingbuf& rhs);
622 
630 void swap(ifmstream& lhs, ifmstream& rhs);
631 
639 void swap(fmstream& lhs, fmstream& rhs);
641 #endif // _HAS_CPP11_
642 
643 #endif /* FILE_MAPPING_STREAM_H_ */