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