comparison DEPENDENCIES/generic/include/boost/interprocess/file_mapping.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_INTERPROCESS_FILE_MAPPING_HPP
12 #define BOOST_INTERPROCESS_FILE_MAPPING_HPP
13
14 #include <boost/interprocess/detail/config_begin.hpp>
15 #include <boost/interprocess/detail/workaround.hpp>
16
17 #include <boost/interprocess/interprocess_fwd.hpp>
18 #include <boost/interprocess/exceptions.hpp>
19 #include <boost/interprocess/detail/utilities.hpp>
20 #include <boost/interprocess/creation_tags.hpp>
21 #include <boost/interprocess/detail/os_file_functions.hpp>
22 #include <boost/move/move.hpp>
23 #include <string> //std::string
24
25 //!\file
26 //!Describes file_mapping and mapped region classes
27
28 namespace boost {
29 namespace interprocess {
30
31 //!A class that wraps a file-mapping that can be used to
32 //!create mapped regions from the mapped files
33 class file_mapping
34 {
35 /// @cond
36 BOOST_MOVABLE_BUT_NOT_COPYABLE(file_mapping)
37 /// @endcond
38
39 public:
40 //!Constructs an empty file mapping.
41 //!Does not throw
42 file_mapping();
43
44 //!Opens a file mapping of file "filename", starting in offset
45 //!"file_offset", and the mapping's size will be "size". The mapping
46 //!can be opened for read-only "read_only" or read-write "read_write"
47 //!modes. Throws interprocess_exception on error.
48 file_mapping(const char *filename, mode_t mode);
49
50 //!Moves the ownership of "moved"'s file mapping object to *this.
51 //!After the call, "moved" does not represent any file mapping object.
52 //!Does not throw
53 file_mapping(BOOST_RV_REF(file_mapping) moved)
54 : m_handle(file_handle_t(ipcdetail::invalid_file()))
55 , m_mode(read_only)
56 { this->swap(moved); }
57
58 //!Moves the ownership of "moved"'s file mapping to *this.
59 //!After the call, "moved" does not represent any file mapping.
60 //!Does not throw
61 file_mapping &operator=(BOOST_RV_REF(file_mapping) moved)
62 {
63 file_mapping tmp(boost::move(moved));
64 this->swap(tmp);
65 return *this;
66 }
67
68 //!Swaps to file_mappings.
69 //!Does not throw.
70 void swap(file_mapping &other);
71
72 //!Returns access mode
73 //!used in the constructor
74 mode_t get_mode() const;
75
76 //!Obtains the mapping handle
77 //!to be used with mapped_region
78 mapping_handle_t get_mapping_handle() const;
79
80 //!Destroys the file mapping. All mapped regions created from this are still
81 //!valid. Does not throw
82 ~file_mapping();
83
84 //!Returns the name of the file
85 //!used in the constructor.
86 const char *get_name() const;
87
88 //!Removes the file named "filename" even if it's been memory mapped.
89 //!Returns true on success.
90 //!The function might fail in some operating systems if the file is
91 //!being used other processes and no deletion permission was shared.
92 static bool remove(const char *filename);
93
94 /// @cond
95 private:
96 //!Closes a previously opened file mapping. Never throws.
97 void priv_close();
98 file_handle_t m_handle;
99 mode_t m_mode;
100 std::string m_filename;
101 /// @endcond
102 };
103
104 inline file_mapping::file_mapping()
105 : m_handle(file_handle_t(ipcdetail::invalid_file()))
106 , m_mode(read_only)
107 {}
108
109 inline file_mapping::~file_mapping()
110 { this->priv_close(); }
111
112 inline const char *file_mapping::get_name() const
113 { return m_filename.c_str(); }
114
115 inline void file_mapping::swap(file_mapping &other)
116 {
117 std::swap(m_handle, other.m_handle);
118 std::swap(m_mode, other.m_mode);
119 m_filename.swap(other.m_filename);
120 }
121
122 inline mapping_handle_t file_mapping::get_mapping_handle() const
123 { return ipcdetail::mapping_handle_from_file_handle(m_handle); }
124
125 inline mode_t file_mapping::get_mode() const
126 { return m_mode; }
127
128 inline file_mapping::file_mapping
129 (const char *filename, mode_t mode)
130 : m_filename(filename)
131 {
132 //Check accesses
133 if (mode != read_write && mode != read_only){
134 error_info err = other_error;
135 throw interprocess_exception(err);
136 }
137
138 //Open file
139 m_handle = ipcdetail::open_existing_file(filename, mode);
140
141 //Check for error
142 if(m_handle == ipcdetail::invalid_file()){
143 error_info err = system_error_code();
144 this->priv_close();
145 throw interprocess_exception(err);
146 }
147 m_mode = mode;
148 }
149
150 inline bool file_mapping::remove(const char *filename)
151 { return ipcdetail::delete_file(filename); }
152
153 ///@cond
154
155 inline void file_mapping::priv_close()
156 {
157 if(m_handle != ipcdetail::invalid_file()){
158 ipcdetail::close_file(m_handle);
159 m_handle = ipcdetail::invalid_file();
160 }
161 }
162
163 ///@endcond
164
165 //!A class that stores the name of a file
166 //!and tries to remove it in its destructor
167 //!Useful to remove temporary files in the presence
168 //!of exceptions
169 class remove_file_on_destroy
170 {
171 const char * m_name;
172 public:
173 remove_file_on_destroy(const char *name)
174 : m_name(name)
175 {}
176
177 ~remove_file_on_destroy()
178 { ipcdetail::delete_file(m_name); }
179 };
180
181 } //namespace interprocess {
182 } //namespace boost {
183
184 #include <boost/interprocess/detail/config_end.hpp>
185
186 #endif //BOOST_INTERPROCESS_FILE_MAPPING_HPP