File indexing completed on 2026-09-17 08:51:57
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP
0012 #define BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP
0013
0014 #ifndef BOOST_CONFIG_HPP
0015 # include <boost/config.hpp>
0016 #endif
0017 0018 ">#
0019 #if defined(BOOST_HAS_PRAGMA_ONCE)
0020 # pragma once
0021 #endif
0022
0023 #include <boost/interprocess/detail/config_begin.hpp>
0024 #include <boost/interprocess/detail/workaround.hpp>
0025 #include <boost/interprocess/detail/os_file_functions.hpp>
0026 #include <boost/interprocess/creation_tags.hpp>
0027 #include <boost/move/utility_core.hpp>
0028 #include <boost/interprocess/creation_tags.hpp>
0029 #include <boost/interprocess/detail/simple_swap.hpp>
0030
0031 namespace boost {
0032 namespace interprocess {
0033 namespace ipcdetail{
0034
0035 class file_wrapper
0036 {
0037 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0038 BOOST_MOVABLE_BUT_NOT_COPYABLE(file_wrapper)
0039 #endif
0040 public:
0041
0042
0043
0044 file_wrapper();
0045
0046
0047
0048 file_wrapper(create_only_t, const char *name, mode_t mode, const permissions &perm = permissions())
0049 { this->priv_open_or_create(ipcdetail::DoCreate, name, mode, perm); }
0050
0051
0052
0053
0054 file_wrapper(open_or_create_t, const char *name, mode_t mode, const permissions &perm = permissions())
0055 { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, name, mode, perm); }
0056
0057
0058
0059 file_wrapper(open_only_t, const char *name, mode_t mode)
0060 { this->priv_open_or_create(ipcdetail::DoOpen, name, mode, permissions()); }
0061
0062 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0063
0064
0065
0066
0067
0068
0069 file_wrapper(create_only_t, const wchar_t *name, mode_t mode, const permissions &perm = permissions())
0070 { this->priv_open_or_create(ipcdetail::DoCreate, name, mode, perm); }
0071
0072
0073
0074
0075
0076
0077
0078 file_wrapper(open_or_create_t, const wchar_t *name, mode_t mode, const permissions &perm = permissions())
0079 { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, name, mode, perm); }
0080
0081
0082
0083
0084
0085
0086 file_wrapper(open_only_t, const wchar_t *name, mode_t mode)
0087 { this->priv_open_or_create(ipcdetail::DoOpen, name, mode, permissions()); }
0088
0089 #endif
0090
0091
0092
0093 file_wrapper(BOOST_RV_REF(file_wrapper) moved)
0094 : m_handle(file_handle_t(ipcdetail::invalid_file()))
0095 { this->swap(moved); }
0096
0097
0098
0099
0100 file_wrapper &operator=(BOOST_RV_REF(file_wrapper) moved)
0101 {
0102 file_wrapper tmp(boost::move(moved));
0103 this->swap(tmp);
0104 return *this;
0105 }
0106
0107
0108
0109 void swap(file_wrapper &other);
0110
0111
0112
0113 static bool remove(const char *name);
0114
0115
0116 void truncate(offset_t length);
0117
0118
0119
0120 ~file_wrapper();
0121
0122
0123
0124 const char *get_name() const;
0125
0126
0127
0128 bool get_size(offset_t &size) const;
0129
0130
0131
0132 mode_t get_mode() const;
0133
0134
0135
0136 mapping_handle_t get_mapping_handle() const;
0137
0138 private:
0139
0140 void priv_close();
0141
0142 template <class CharT>
0143 bool priv_open_or_create(ipcdetail::create_enum_t type, const CharT *filename, mode_t mode, const permissions &perm);
0144
0145 file_handle_t m_handle;
0146 mode_t m_mode;
0147 };
0148
0149 inline file_wrapper::file_wrapper()
0150 : m_handle(file_handle_t(ipcdetail::invalid_file()))
0151 , m_mode(read_only)
0152 {}
0153
0154 inline file_wrapper::~file_wrapper()
0155 { this->priv_close(); }
0156
0157 inline bool file_wrapper::get_size(offset_t &size) const
0158 { return get_file_size((file_handle_t)m_handle, size); }
0159
0160 inline void file_wrapper::swap(file_wrapper &other)
0161 {
0162 (simple_swap)(m_handle, other.m_handle);
0163 (simple_swap)(m_mode, other.m_mode);
0164 }
0165
0166 inline mapping_handle_t file_wrapper::get_mapping_handle() const
0167 { return mapping_handle_from_file_handle(m_handle); }
0168
0169 inline mode_t file_wrapper::get_mode() const
0170 { return m_mode; }
0171
0172 template <class CharT>
0173 inline bool file_wrapper::priv_open_or_create
0174 ( ipcdetail::create_enum_t type, const CharT *filename, mode_t mode, const permissions &perm)
0175 {
0176 if(mode != read_only && mode != read_write){
0177 error_info err(mode_error);
0178 throw interprocess_exception(err);
0179 }
0180
0181
0182 switch(type){
0183 case ipcdetail::DoOpen:
0184 m_handle = open_existing_file(filename, mode);
0185 break;
0186 case ipcdetail::DoCreate:
0187 m_handle = create_new_file(filename, mode, perm);
0188 break;
0189 case ipcdetail::DoOpenOrCreate:
0190 m_handle = create_or_open_file(filename, mode, perm);
0191 break;
0192 default:
0193 {
0194 error_info err = other_error;
0195 throw interprocess_exception(err);
0196 }
0197 }
0198
0199
0200 if(m_handle == invalid_file()){
0201 error_info err = system_error_code();
0202 throw interprocess_exception(err);
0203 }
0204
0205 m_mode = mode;
0206 return true;
0207 }
0208
0209 inline bool file_wrapper::remove(const char *filename)
0210 { return delete_file(filename); }
0211
0212 inline void file_wrapper::truncate(offset_t length)
0213 {
0214 if(!truncate_file(m_handle, (std::size_t)length)){
0215 error_info err(system_error_code());
0216 throw interprocess_exception(err);
0217 }
0218 }
0219
0220 inline void file_wrapper::priv_close()
0221 {
0222 if(m_handle != invalid_file()){
0223 close_file(m_handle);
0224 m_handle = invalid_file();
0225 }
0226 }
0227
0228 }
0229 }
0230 }
0231
0232 #include <boost/interprocess/detail/config_end.hpp>
0233
0234 #endif