]> git.tdb.fi Git - libs/core.git/blob - source/io/base.h
Use the new Handle class to hide platform details from public headers
[libs/core.git] / source / io / base.h
1 #ifndef MSP_IO_BASE_H_
2 #define MSP_IO_BASE_H_
3
4 #include <sigc++/sigc++.h>
5 #include "mode.h"
6 #include "poll.h"
7
8 namespace Msp {
9 namespace IO {
10
11 struct Handle;
12
13 /**
14 Common interface for all I/O objects.
15
16 A derived class must call set_events(P_NONE) before it is destroyed to avoid
17 leaving stale pointers in an EventDispatcher.
18 */
19 class Base
20 {
21 public:
22         /** Emitted when there is data available for reading.  If all data is not
23         read, the signal is emitted again immediately. */
24         sigc::signal<void> signal_data_available;
25
26         /** Emitted when there is no more data to be read. */
27         sigc::signal<void> signal_end_of_file;
28
29         /** Emitted when there is a nonlinearity in I/O (such as a file being
30         seeked) and any data buffered by upper layers needs to be flushed. */
31         sigc::signal<void> signal_flush_required;
32
33         /** Emitted when the I/O object has closed. */
34         sigc::signal<void> signal_closed;
35
36         /** Emitted when the mask of interesting events changes.  Mainly for use by
37         EventDispatcher. */
38         sigc::signal<void, PollEvent> signal_events_changed;
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;
46         PollEvent events;
47         bool eof_flag;
48
49         Base();
50 private:
51         Base(const Base &);
52         Base &operator=(const Base &);
53 public:
54         virtual ~Base();
55
56         /** Sets blocking mode.  When blocking is enabled, most operations won't
57         return until they can make progress.  When blocking is disabled, these
58         operations may return immediately with a return code indicating that nothing
59         was done.
60
61         Blocking is enabled by default. */
62         virtual void set_block(bool) { }
63
64         /** Returns the current mode flags. */
65         Mode get_mode() const { return mode; }
66
67 protected:
68         virtual unsigned do_write(const char *, unsigned) = 0;
69         virtual unsigned do_read(char *, unsigned) = 0;
70
71 public:
72         /** Writes data from a buffer.  Subject to blocking.  Returns the number of
73         bytes written, which may be zero for a non-blockin operation. */
74         unsigned write(const char *b, unsigned c) { return do_write(b, c); }
75
76         /** Writes a string.  This is identical to calling
77         write(s.data(), s.size()). */
78         unsigned write(const std::string &s) { return do_write(s.data(), s.size()); }
79
80         /** Writes a single character.  This is identical to calling
81         write(&c, 1). */
82         virtual unsigned put(char c) { return do_write(&c, 1); }
83
84         /** Reads data into a buffer.  Subject to blocking.  Returns the number of
85         bytes read, which may be zero for a non-blocking operation. */
86         unsigned read(char *b, unsigned c) { return do_read(b, c); }
87
88         /** Reads characters up to the next linefeed or end-of-file.  The linefeed
89         is not included in the line.  Returns true if a line was successfully read,
90         false otherwise. */
91         virtual bool getline(std::string &);
92
93         /** Reads a single character.  Returns -1 if no character was available due
94         to end-of-file or non-blocking operation. */
95         virtual int get();
96
97         /** Returns the end-of-file flag. */
98         bool eof() const { return eof_flag; }
99
100 protected:
101         void set_events(PollEvent);
102
103 public:
104         /** Returns a mask of the currently interesting events.  Used by
105         EventDispatcher. */
106         PollEvent get_events() const { return events; }
107
108         /** Returns a handle for polling.  Should throw if the object does not have
109         an event handle. */
110         virtual const Handle &get_event_handle() = 0;
111
112         /** Notifies the object of an event.  Used by EventDispatcher. */
113         void event(PollEvent);
114
115 protected:
116         virtual void on_event(PollEvent) { }
117 };
118
119 } // namespace IO
120 } // namespace Msp
121
122 #endif