4 #include <sigc++/sigc++.h>
5 #include <msp/core/mutex.h>
6 #include <msp/core/noncopyable.h>
15 Common interface for all I/O objects.
17 class Base: private NonCopyable
20 /** RAII synchronization primitive. Prevents concurrent access to the
21 target object during the lifetime of the Synchronize object. */
32 /** Emitted when there is no more data to be read. */
33 sigc::signal<void> signal_end_of_file;
35 /** Emitted when there is a nonlinearity in I/O (such as a file being
36 seeked) and any data buffered by upper layers needs to be flushed. */
37 sigc::signal<void> signal_flush_required;
39 /** Emitted when the object is deleted. Mainly for use by
41 sigc::signal<void> signal_deleted;
52 /** Sets blocking mode. When blocking is enabled, most operations won't
53 return until they can make progress. When blocking is disabled, these
54 operations may return immediately with a return code indicating that nothing
57 Blocking is enabled by default. */
58 virtual void set_block(bool) = 0;
60 /** Sets inheritance mode. When inheritance is enabled, the file descriptor
61 will persist through Process::execute.
63 Inheritance is disabled by default. */
64 virtual void set_inherit(bool) = 0;
66 /** Returns the current mode flags. */
67 Mode get_mode() const { return mode; }
70 void check_access(Mode) const;
72 virtual unsigned do_write(const char *, unsigned) = 0;
73 virtual unsigned do_read(char *, unsigned) = 0;
76 /** Writes data from a buffer. Subject to blocking. Returns the number of
77 bytes written, which may be zero for a non-blockin operation. */
78 unsigned write(const char *b, unsigned c) { return do_write(b, c); }
80 /** Writes a string. This is identical to calling
81 write(s.data(), s.size()). */
82 unsigned write(const std::string &s) { return do_write(s.data(), s.size()); }
84 /** Writes a single character. This is identical to calling
86 virtual unsigned put(char c) { return do_write(&c, 1); }
88 /** Reads data into a buffer. Subject to blocking. Returns the number of
89 bytes read, which may be zero for a non-blocking operation. */
90 unsigned read(char *b, unsigned c) { return do_read(b, c); }
92 /** Reads characters up to the next linefeed or end-of-file. The linefeed
93 is not included in the line. Returns true if a line was successfully read,
95 virtual bool getline(std::string &);
97 /** Reads a single character. Returns -1 if no character was available due
98 to end-of-file or non-blocking operation. */
105 /** Returns the end-of-file flag. Note that some types of objects won't
106 indicate end-of-file until you try to read at least one byte past the actual
107 end, while others indicate it when you've read the last byte. */
108 bool eof() const { return eof_flag; }
110 /** Returns the system-level handle for the object. Used by Console to
111 perform redirections. */
112 virtual const Handle &get_handle(Mode) = 0;