Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-17 08:51:57

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
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   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0040    public:
0041 
0042    //!Default constructor.
0043    //!Represents an empty file_wrapper.
0044    file_wrapper();
0045 
0046    //!Creates a file object with name "name" and mode "mode", with the access mode "mode"
0047    //!If the file previously exists, throws an error.
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    //!Tries to create a file with name "name" and mode "mode", with the
0052    //!access mode "mode". If the file previously exists, it tries to open it with mode "mode".
0053    //!Otherwise throws an error.
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    //!Tries to open a file with name "name", with the access mode "mode".
0058    //!If the file does not previously exist, it throws an error.
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    //!Creates a file object with name "name" and mode "mode", with the access mode "mode"
0065    //!If the file previously exists, throws an error.
0066    //! 
0067    //!Note: This function is only available on operating systems with
0068    //!      native wchar_t APIs (e.g. Windows).
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    //!Tries to create a file with name "name" and mode "mode", with the
0073    //!access mode "mode". If the file previously exists, it tries to open it with mode "mode".
0074    //!Otherwise throws an error.
0075    //! 
0076    //!Note: This function is only available on operating systems with
0077    //!      native wchar_t APIs (e.g. Windows).
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    //!Tries to open a file with name "name", with the access mode "mode".
0082    //!If the file does not previously exist, it throws an error.
0083    //! 
0084    //!Note: This function is only available on operating systems with
0085    //!      native wchar_t APIs (e.g. Windows).
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   //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0090    //!Moves the ownership of "moved"'s file to *this.
0091    //!After the call, "moved" does not represent any file.
0092    //!Does not throw
0093    file_wrapper(BOOST_RV_REF(file_wrapper) moved)
0094       :  m_handle(file_handle_t(ipcdetail::invalid_file()))
0095    {  this->swap(moved);   }
0096 
0097    //!Moves the ownership of "moved"'s file to *this.
0098    //!After the call, "moved" does not represent any file.
0099    //!Does not throw
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    //!Swaps to file_wrappers.
0108    //!Does not throw
0109    void swap(file_wrapper &other);
0110 
0111    //!Erases a file from the system.
0112    //!Returns false on error. Never throws
0113    static bool remove(const char *name);
0114 
0115    //!Sets the size of the file
0116    void truncate(offset_t length);
0117 
0118    //!Closes the
0119    //!file
0120    ~file_wrapper();
0121 
0122    //!Returns the name of the file
0123    //!used in the constructor
0124    const char *get_name() const;
0125 
0126    //!Returns the name of the file
0127    //!used in the constructor
0128    bool get_size(offset_t &size) const;
0129 
0130    //!Returns access mode
0131    //!used in the constructor
0132    mode_t get_mode() const;
0133 
0134    //!Get mapping handle
0135    //!to use with mapped_region
0136    mapping_handle_t get_mapping_handle() const;
0137 
0138    private:
0139    //!Closes a previously opened file mapping. Never throws.
0140    void priv_close();
0141    //!Closes a previously opened file mapping. Never throws.
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    //Open file existing native API to obtain the handle
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    //Check for error
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 }  //namespace ipcdetail{
0229 }  //namespace interprocess {
0230 }  //namespace boost {
0231 
0232 #include <boost/interprocess/detail/config_end.hpp>
0233 
0234 #endif   //BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP