]> git.tdb.fi Git - libs/core.git/blob - source/io/base.h
1a1485efc67d752a6e893d6cdb6dd605f78b362f
[libs/core.git] / source / io / base.h
1 #ifndef MSP_IO_BASE_H_
2 #define MSP_IO_BASE_H_
3
4 #include <cstddef>
5 #include <sigc++/sigc++.h>
6 #include <msp/core/mutex.h>
7 #include <msp/core/noncopyable.h>
8 #include "handle.h"
9 #include "mode.h"
10 #include "poll.h"
11
12 namespace Msp {
13 namespace IO {
14
15 /**
16 Common interface for all I/O objects.
17 */
18 class Base: private NonCopyable
19 {
20 public:
21         /** RAII synchronization primitive.  Prevents concurrent access to the
22         target object during the lifetime of the Synchronize object. */
23         class Synchronize
24         {
25         private:
26                 Base &io;
27
28         public:
29                 Synchronize(Base &);
30                 ~Synchronize();
31         };
32
33         /** Emitted when there is no more data to be read. */
34         sigc::signal<void> signal_end_of_file;
35
36         /** Emitted when there is a nonlinearity in I/O (such as a file being
37         seeked) and any data buffered by upper layers needs to be flushed. */
38         sigc::signal<void> signal_flush_required;
39
40         /** Emitted when the object is deleted.  Mainly for use by
41         EventDispatcher. */
42         sigc::signal<void> signal_deleted;
43
44 protected:
45         Mode mode = M_READ;
46         bool eof_flag = false;
47         Mutex *mutex = 0;
48
49         Base();
50 public:
51         virtual ~Base();
52
53         /** Sets blocking mode.  When blocking is enabled, most operations won't
54         return until they can make progress.  When blocking is disabled, these
55         operations may return immediately with a return code indicating that nothing
56         was done.
57
58         Blocking is enabled by default. */
59         virtual void set_block(bool) = 0;
60
61         /** Sets inheritance mode.  When inheritance is enabled, the file descriptor
62         will persist through Process::execute.
63
64         Inheritance is disabled by default. */
65         virtual void set_inherit(bool) = 0;
66
67         /** Returns the current mode flags. */
68         Mode get_mode() const { return mode; }
69
70 protected:
71         void check_access(Mode) const;
72
73         virtual std::size_t do_write(const char *, std::size_t) = 0;
74         virtual std::size_t do_read(char *, std::size_t) = 0;
75
76 public:
77         /** Writes data from a buffer.  Subject to blocking.  Returns the number of
78         bytes written, which may be zero for a non-blockin operation. */
79         std::size_t write(const char *b, std::size_t c) { return do_write(b, c); }
80
81         /** Writes a string.  This is identical to calling
82         write(s.data(), s.size()). */
83         std::size_t write(const std::string &s) { return do_write(s.data(), s.size()); }
84
85         /** Writes a single character.  This is identical to calling
86         write(&c, 1). */
87         virtual std::size_t put(char c) { return do_write(&c, 1); }
88
89         /** Reads data into a buffer.  Subject to blocking.  Returns the number of
90         bytes read, which may be zero for a non-blocking operation. */
91         std::size_t read(char *b, std::size_t c) { return do_read(b, c); }
92
93         /** Reads characters up to the next linefeed or end-of-file.  The linefeed
94         is not included in the line.  Returns true if a line was successfully read,
95         false otherwise. */
96         virtual bool getline(std::string &);
97
98         /** Reads a single character.  Returns -1 if no character was available due
99         to end-of-file or non-blocking operation. */
100         virtual int get();
101
102 protected:
103         void set_eof();
104
105 public:
106         /** Returns the end-of-file flag.  Note that some types of objects won't
107         indicate end-of-file until you try to read at least one byte past the actual
108         end, while others indicate it when you've read the last byte. */
109         bool eof() const { return eof_flag; }
110
111         /** Returns the system-level handle for the object.  Used by Console to
112         perform redirections. */
113         virtual const Handle &get_handle(Mode) = 0;
114 };
115
116 } // namespace IO
117 } // namespace Msp
118
119 #endif