fix doc example typo
[boost.git] / boost / interprocess / file_mapping.hpp
blob6dbefb44fe65283be70596f8ebdb5bb33b75adf1
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2008. 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 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_FILE_MAPPING_HPP
12 #define BOOST_INTERPROCESS_FILE_MAPPING_HPP
14 #include <boost/interprocess/detail/config_begin.hpp>
15 #include <boost/interprocess/detail/workaround.hpp>
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/interprocess/detail/move.hpp>
23 #include <string> //std::string
25 //!\file
26 //!Describes file_mapping and mapped region classes
28 namespace boost {
29 namespace interprocess {
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
35 /// @cond
36 //Non-copyable and non-assignable
37 file_mapping(file_mapping &);
38 file_mapping &operator=(file_mapping &);
39 /// @endcond
41 public:
42 BOOST_INTERPROCESS_ENABLE_MOVE_EMULATION(file_mapping)
44 //!Constructs an empty file mapping.
45 //!Does not throw
46 file_mapping();
48 //!Opens a file mapping of file "filename", starting in offset
49 //!"file_offset", and the mapping's size will be "size". The mapping
50 //!can be opened for read-only "read_only" or read-write "read_write"
51 //!modes. Throws interprocess_exception on error.
52 file_mapping(const char *filename, mode_t mode);
54 //!Moves the ownership of "moved"'s file mapping object to *this.
55 //!After the call, "moved" does not represent any file mapping object.
56 //!Does not throw
57 file_mapping(BOOST_INTERPROCESS_RV_REF(file_mapping) moved)
58 : m_handle(file_handle_t(detail::invalid_file()))
59 { this->swap(moved); }
61 //!Moves the ownership of "moved"'s file mapping to *this.
62 //!After the call, "moved" does not represent any file mapping.
63 //!Does not throw
64 file_mapping &operator=(BOOST_INTERPROCESS_RV_REF(file_mapping) moved)
66 file_mapping tmp(boost::interprocess::move(moved));
67 this->swap(tmp);
68 return *this;
71 //!Swaps to file_mappings.
72 //!Does not throw.
73 void swap(file_mapping &other);
75 //!Returns access mode
76 //!used in the constructor
77 mode_t get_mode() const;
79 //!Obtains the mapping handle
80 //!to be used with mapped_region
81 mapping_handle_t get_mapping_handle() const;
83 //!Destroys the file mapping. All mapped regions created from this are still
84 //!valid. Does not throw
85 ~file_mapping();
87 //!Returns the name of the file
88 //!used in the constructor.
89 const char *get_name() const;
91 //!Removes the file named "filename" even if it's been memory mapped.
92 //!Returns true on success.
93 //!The function might fail in some operating systems if the file is
94 //!being used other processes and no deletion permission was shared.
95 static bool remove(const char *filename);
97 /// @cond
98 private:
99 //!Closes a previously opened file mapping. Never throws.
100 void priv_close();
101 file_handle_t m_handle;
102 mode_t m_mode;
103 std::string m_filename;
104 /// @endcond
107 inline file_mapping::file_mapping()
108 : m_handle(file_handle_t(detail::invalid_file()))
111 inline file_mapping::~file_mapping()
112 { this->priv_close(); }
114 inline const char *file_mapping::get_name() const
115 { return m_filename.c_str(); }
117 inline void file_mapping::swap(file_mapping &other)
119 std::swap(m_handle, other.m_handle);
120 std::swap(m_mode, other.m_mode);
121 m_filename.swap(other.m_filename);
124 inline mapping_handle_t file_mapping::get_mapping_handle() const
125 { return detail::mapping_handle_from_file_handle(m_handle); }
127 inline mode_t file_mapping::get_mode() const
128 { return m_mode; }
130 inline file_mapping::file_mapping
131 (const char *filename, mode_t mode)
132 : m_filename(filename)
134 //Check accesses
135 if (mode != read_write && mode != read_only){
136 error_info err = other_error;
137 throw interprocess_exception(err);
140 //Open file
141 m_handle = detail::open_existing_file(filename, mode);
143 //Check for error
144 if(m_handle == detail::invalid_file()){
145 error_info err = system_error_code();
146 this->priv_close();
147 throw interprocess_exception(err);
149 m_mode = mode;
152 inline bool file_mapping::remove(const char *filename)
153 { return detail::delete_file(filename); }
155 ///@cond
157 inline void file_mapping::priv_close()
159 if(m_handle != detail::invalid_file()){
160 detail::close_file(m_handle);
161 m_handle = detail::invalid_file();
165 ///@endcond
167 //!A class that stores the name of a file
168 //!and tries to remove it in its destructor
169 //!Useful to remove temporary files in the presence
170 //!of exceptions
171 class remove_file_on_destroy
173 const char * m_name;
174 public:
175 remove_file_on_destroy(const char *name)
176 : m_name(name)
179 ~remove_file_on_destroy()
180 { detail::delete_file(m_name); }
183 } //namespace interprocess {
184 } //namespace boost {
186 #include <boost/interprocess/detail/config_end.hpp>
188 #endif //BOOST_INTERPROCESS_FILE_MAPPING_HPP