Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:25

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