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