Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/interprocess for documentation. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP Chris@16: #define BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: # Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@101: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@101: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: namespace ipcdetail{ Chris@16: Chris@16: class file_wrapper Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: BOOST_MOVABLE_BUT_NOT_COPYABLE(file_wrapper) Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: public: Chris@16: Chris@16: //!Default constructor. Chris@16: //!Represents an empty file_wrapper. Chris@16: file_wrapper(); Chris@16: Chris@16: //!Creates a file object with name "name" and mode "mode", with the access mode "mode" Chris@16: //!If the file previously exists, throws an error. Chris@16: file_wrapper(create_only_t, const char *name, mode_t mode, const permissions &perm = permissions()) Chris@16: { this->priv_open_or_create(ipcdetail::DoCreate, name, mode, perm); } Chris@16: Chris@16: //!Tries to create a file with name "name" and mode "mode", with the Chris@16: //!access mode "mode". If the file previously exists, it tries to open it with mode "mode". Chris@16: //!Otherwise throws an error. Chris@16: file_wrapper(open_or_create_t, const char *name, mode_t mode, const permissions &perm = permissions()) Chris@16: { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, name, mode, perm); } Chris@16: Chris@16: //!Tries to open a file with name "name", with the access mode "mode". Chris@16: //!If the file does not previously exist, it throws an error. Chris@16: file_wrapper(open_only_t, const char *name, mode_t mode) Chris@16: { this->priv_open_or_create(ipcdetail::DoOpen, name, mode, permissions()); } Chris@16: Chris@16: //!Moves the ownership of "moved"'s file to *this. Chris@16: //!After the call, "moved" does not represent any file. Chris@16: //!Does not throw Chris@16: file_wrapper(BOOST_RV_REF(file_wrapper) moved) Chris@16: : m_handle(file_handle_t(ipcdetail::invalid_file())) Chris@16: { this->swap(moved); } Chris@16: Chris@16: //!Moves the ownership of "moved"'s file to *this. Chris@16: //!After the call, "moved" does not represent any file. Chris@16: //!Does not throw Chris@16: file_wrapper &operator=(BOOST_RV_REF(file_wrapper) moved) Chris@16: { Chris@16: file_wrapper tmp(boost::move(moved)); Chris@16: this->swap(tmp); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //!Swaps to file_wrappers. Chris@16: //!Does not throw Chris@16: void swap(file_wrapper &other); Chris@16: Chris@16: //!Erases a file from the system. Chris@16: //!Returns false on error. Never throws Chris@16: static bool remove(const char *name); Chris@16: Chris@16: //!Sets the size of the file Chris@16: void truncate(offset_t length); Chris@16: Chris@16: //!Closes the Chris@16: //!file Chris@16: ~file_wrapper(); Chris@16: Chris@16: //!Returns the name of the file Chris@16: //!used in the constructor Chris@16: const char *get_name() const; Chris@16: Chris@16: //!Returns the name of the file Chris@16: //!used in the constructor Chris@16: bool get_size(offset_t &size) const; Chris@16: Chris@16: //!Returns access mode Chris@16: //!used in the constructor Chris@16: mode_t get_mode() const; Chris@16: Chris@16: //!Get mapping handle Chris@16: //!to use with mapped_region Chris@16: mapping_handle_t get_mapping_handle() const; Chris@16: Chris@16: private: Chris@16: //!Closes a previously opened file mapping. Never throws. Chris@16: void priv_close(); Chris@16: //!Closes a previously opened file mapping. Never throws. Chris@16: bool priv_open_or_create(ipcdetail::create_enum_t type, const char *filename, mode_t mode, const permissions &perm); Chris@16: Chris@16: file_handle_t m_handle; Chris@16: mode_t m_mode; Chris@16: std::string m_filename; Chris@16: }; Chris@16: Chris@16: inline file_wrapper::file_wrapper() Chris@16: : m_handle(file_handle_t(ipcdetail::invalid_file())) Chris@16: {} Chris@16: Chris@16: inline file_wrapper::~file_wrapper() Chris@16: { this->priv_close(); } Chris@16: Chris@16: inline const char *file_wrapper::get_name() const Chris@16: { return m_filename.c_str(); } Chris@16: Chris@16: inline bool file_wrapper::get_size(offset_t &size) const Chris@16: { return get_file_size((file_handle_t)m_handle, size); } Chris@16: Chris@16: inline void file_wrapper::swap(file_wrapper &other) Chris@16: { Chris@101: (simple_swap)(m_handle, other.m_handle); Chris@101: (simple_swap)(m_mode, other.m_mode); Chris@16: m_filename.swap(other.m_filename); Chris@16: } Chris@16: Chris@16: inline mapping_handle_t file_wrapper::get_mapping_handle() const Chris@16: { return mapping_handle_from_file_handle(m_handle); } Chris@16: Chris@16: inline mode_t file_wrapper::get_mode() const Chris@16: { return m_mode; } Chris@16: Chris@16: inline bool file_wrapper::priv_open_or_create Chris@16: (ipcdetail::create_enum_t type, Chris@16: const char *filename, Chris@16: mode_t mode, Chris@16: const permissions &perm = permissions()) Chris@16: { Chris@16: m_filename = filename; Chris@16: Chris@16: if(mode != read_only && mode != read_write){ Chris@16: error_info err(mode_error); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: Chris@16: //Open file existing native API to obtain the handle Chris@16: switch(type){ Chris@16: case ipcdetail::DoOpen: Chris@16: m_handle = open_existing_file(filename, mode); Chris@16: break; Chris@16: case ipcdetail::DoCreate: Chris@16: m_handle = create_new_file(filename, mode, perm); Chris@16: break; Chris@16: case ipcdetail::DoOpenOrCreate: Chris@16: m_handle = create_or_open_file(filename, mode, perm); Chris@16: break; Chris@16: default: Chris@16: { Chris@16: error_info err = other_error; Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: } Chris@16: Chris@16: //Check for error Chris@16: if(m_handle == invalid_file()){ Chris@101: error_info err = system_error_code(); Chris@101: throw interprocess_exception(err); Chris@16: } Chris@16: Chris@16: m_mode = mode; Chris@16: return true; Chris@16: } Chris@16: Chris@16: inline bool file_wrapper::remove(const char *filename) Chris@16: { return delete_file(filename); } Chris@16: Chris@16: inline void file_wrapper::truncate(offset_t length) Chris@16: { Chris@16: if(!truncate_file(m_handle, length)){ Chris@16: error_info err(system_error_code()); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: } Chris@16: Chris@16: inline void file_wrapper::priv_close() Chris@16: { Chris@16: if(m_handle != invalid_file()){ Chris@16: close_file(m_handle); Chris@16: m_handle = invalid_file(); Chris@16: } Chris@16: } Chris@16: Chris@16: } //namespace ipcdetail{ Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP