1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006. 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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP
12 #define BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP
14 #include <boost/interprocess/detail/config_begin.hpp>
15 #include <boost/interprocess/detail/workaround.hpp>
16 #include <boost/interprocess/detail/os_file_functions.hpp>
17 #include <boost/interprocess/creation_tags.hpp>
18 #include <boost/interprocess/detail/move.hpp>
19 #include <boost/interprocess/creation_tags.hpp>
22 namespace interprocess
{
28 file_wrapper(file_wrapper
&);
29 file_wrapper
& operator=(file_wrapper
&);
32 BOOST_INTERPROCESS_ENABLE_MOVE_EMULATION(file_wrapper
)
34 //!Default constructor.
35 //!Represents an empty file_wrapper.
38 //!Creates a file object with name "name" and mode "mode", with the access mode "mode"
39 //!If the file previously exists, throws an error.
40 file_wrapper(create_only_t
, const char *name
, mode_t mode
)
41 { this->priv_open_or_create(detail::DoCreate
, name
, mode
); }
43 //!Tries to create a file with name "name" and mode "mode", with the
44 //!access mode "mode". If the file previously exists, it tries to open it with mode "mode".
45 //!Otherwise throws an error.
46 file_wrapper(open_or_create_t
, const char *name
, mode_t mode
)
47 { this->priv_open_or_create(detail::DoOpenOrCreate
, name
, mode
); }
49 //!Tries to open a file with name "name", with the access mode "mode".
50 //!If the file does not previously exist, it throws an error.
51 file_wrapper(open_only_t
, const char *name
, mode_t mode
)
52 { this->priv_open_or_create(detail::DoOpen
, name
, mode
); }
54 //!Moves the ownership of "moved"'s file to *this.
55 //!After the call, "moved" does not represent any file.
57 file_wrapper(BOOST_INTERPROCESS_RV_REF(file_wrapper
) moved
)
58 { this->swap(moved
); }
60 //!Moves the ownership of "moved"'s file to *this.
61 //!After the call, "moved" does not represent any file.
63 file_wrapper
&operator=(BOOST_INTERPROCESS_RV_REF(file_wrapper
) moved
)
65 file_wrapper
tmp(boost::interprocess::move(moved
));
70 //!Swaps to file_wrappers.
72 void swap(file_wrapper
&other
);
74 //!Erases a file from the system.
75 //!Returns false on error. Never throws
76 static bool remove(const char *name
);
78 //!Sets the size of the file
79 void truncate(offset_t length
);
85 //!Returns the name of the file
86 //!used in the constructor
87 const char *get_name() const;
89 //!Returns the name of the file
90 //!used in the constructor
91 bool get_size(offset_t
&size
) const;
93 //!Returns access mode
94 //!used in the constructor
95 mode_t
get_mode() const;
98 //!to use with mapped_region
99 mapping_handle_t
get_mapping_handle() const;
102 //!Closes a previously opened file mapping. Never throws.
104 //!Closes a previously opened file mapping. Never throws.
105 bool priv_open_or_create(detail::create_enum_t type
, const char *filename
, mode_t mode
);
107 file_handle_t m_handle
;
109 std::string m_filename
;
112 inline file_wrapper::file_wrapper()
113 : m_handle(file_handle_t(detail::invalid_file()))
116 inline file_wrapper::~file_wrapper()
117 { this->priv_close(); }
119 inline const char *file_wrapper::get_name() const
120 { return m_filename
.c_str(); }
122 inline bool file_wrapper::get_size(offset_t
&size
) const
123 { return get_file_size((file_handle_t
)m_handle
, size
); }
125 inline void file_wrapper::swap(file_wrapper
&other
)
127 std::swap(m_handle
, other
.m_handle
);
128 std::swap(m_mode
, other
.m_mode
);
129 m_filename
.swap(other
.m_filename
);
132 inline mapping_handle_t
file_wrapper::get_mapping_handle() const
133 { return mapping_handle_from_file_handle(m_handle
); }
135 inline mode_t
file_wrapper::get_mode() const
138 inline bool file_wrapper::priv_open_or_create
139 (detail::create_enum_t type
,
140 const char *filename
,
143 m_filename
= filename
;
145 if(mode
!= read_only
&& mode
!= read_write
){
146 error_info
err(mode_error
);
147 throw interprocess_exception(err
);
150 //Open file existing native API to obtain the handle
153 m_handle
= open_existing_file(filename
, mode
);
155 case detail::DoCreate
:
156 m_handle
= create_new_file(filename
, mode
);
158 case detail::DoOpenOrCreate
:
159 m_handle
= create_or_open_file(filename
, mode
);
163 error_info err
= other_error
;
164 throw interprocess_exception(err
);
169 if(m_handle
== invalid_file()){
170 throw interprocess_exception(error_info(system_error_code()));
177 inline bool file_wrapper::remove(const char *filename
)
178 { return delete_file(filename
); }
180 inline void file_wrapper::truncate(offset_t length
)
182 if(!truncate_file(m_handle
, length
)){
183 error_info
err(system_error_code());
184 throw interprocess_exception(err
);
188 inline void file_wrapper::priv_close()
190 if(m_handle
!= invalid_file()){
191 close_file(m_handle
);
192 m_handle
= invalid_file();
196 } //namespace detail{
197 } //namespace interprocess {
198 } //namespace boost {
200 #include <boost/interprocess/detail/config_end.hpp>
202 #endif //BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP