Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:06

0001 // Copyright (c) 2016 Klemens D. Morgenstern
0002 //
0003 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_PROCESS_DETAIL_POSIX_FILE_DESCRIPTOR_HPP_
0007 #define BOOST_PROCESS_DETAIL_POSIX_FILE_DESCRIPTOR_HPP_
0008 
0009 #include <fcntl.h>
0010 #include <string>
0011 #include <boost/process/filesystem.hpp>
0012 #include <boost/core/exchange.hpp>
0013 
0014 namespace boost { namespace process { namespace detail { namespace posix {
0015 
0016 struct file_descriptor
0017 {
0018     enum mode_t
0019     {
0020         read  = 1,
0021         write = 2,
0022         read_write = 3
0023     };
0024 
0025 
0026     file_descriptor() = default;
0027     explicit file_descriptor(const boost::process::filesystem::path& p, mode_t mode = read_write)
0028         : file_descriptor(p.native(), mode)
0029     {
0030     }
0031 
0032     explicit file_descriptor(const std::string & path , mode_t mode = read_write)
0033         : file_descriptor(path.c_str(), mode) {}
0034 
0035 
0036     explicit file_descriptor(const char*    path, mode_t mode = read_write)
0037         : _handle(create_file(path, mode))
0038     {
0039 
0040     }
0041 
0042     file_descriptor(const file_descriptor & ) = delete;
0043     file_descriptor(file_descriptor &&other)
0044         : _handle(boost::exchange(other._handle, -1))
0045     {
0046     }
0047 
0048     file_descriptor& operator=(const file_descriptor & ) = delete;
0049     file_descriptor& operator=(file_descriptor &&other)
0050     {
0051         if (this != &other)
0052         {
0053             if (_handle != -1)
0054                 ::close(_handle);
0055             _handle = boost::exchange(other._handle, -1);
0056         }
0057         return *this;
0058     }
0059 
0060     ~file_descriptor()
0061     {
0062         if (_handle != -1)
0063             ::close(_handle);
0064     }
0065 
0066     int handle() const { return _handle;}
0067 
0068 private:
0069     static int create_file(const char* name, mode_t mode )
0070     {
0071         switch(mode)
0072         {
0073         case read:
0074             return ::open(name, O_RDONLY);
0075         case write:
0076             return ::open(name, O_WRONLY | O_CREAT, 0660);
0077         case read_write:
0078             return ::open(name, O_RDWR | O_CREAT, 0660);
0079         default:
0080             return -1;
0081         }
0082     }
0083 
0084     int _handle = -1;
0085 };
0086 
0087 }}}}
0088 
0089 #endif /* BOOST_PROCESS_DETAIL_WINDOWS_FILE_DESCRIPTOR_HPP_ */