]> git.tdb.fi Git - libs/core.git/blob - source/base.h
Drop copyright and license notices from files
[libs/core.git] / source / 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         /**
22         Emitted when there is data available for reading.  If all data is not read,
23         the signal is emitted again immediately.
24         */
25         sigc::signal<void> signal_data_available;
26
27         /**
28         Emitted when there is no more data to be read.
29         */
30         sigc::signal<void> signal_end_of_file;
31
32         /**
33         Emitted when there is a nonlinearity in I/O (such as a file being seeked)
34         and any data buffered by upper layers needs to be flushed.
35         */
36         sigc::signal<void> signal_flush_required;
37
38         /**
39         Emitted when the I/O object has closed.
40         */
41         sigc::signal<void> signal_closed;
42
43         /**
44         Emitted when the mask of interesting events changes.  Mainly for use by
45         EventDispatcher.
46         */
47         sigc::signal<void, PollEvent> signal_events_changed;
48
49         /**
50         Emitted when the object is deleted.  Mainly for use by EventDispatcher.
51         */
52         sigc::signal<void> signal_deleted;
53
54         /**
55         Sets blocking mode.  When blocking is enabled, most operations won't return
56         until they can make progress.  When blocking is disabled, these operations
57         may return immediately with a return code indicating that nothing was done.
58
59         Blocking is enabled by default.
60         */
61         virtual void set_block(bool) { }
62
63         /**
64         Returns the current mode flags.
65         */
66         Mode get_mode() const { return mode; }
67
68         /**
69         Writes data from a buffer.  Subject to blocking.
70
71         @param   b  Buffer to write from
72         @param   c  Number of bytes to write
73
74         @return  Number of bytes written
75         */
76         unsigned write(const char *b, unsigned c) { return do_write(b, c); }
77
78         /**
79         Writes a string.  This is identical to calling write(s.data(), s.size()).
80         */
81         unsigned write(const std::string &s) { return do_write(s.data(), s.size()); }
82
83         /**
84         Writes a single character.  This is identical to calling write(&c, 1).
85         */
86         virtual unsigned put(char c) { return do_write(&c, 1); }
87
88         /**
89         Reads data into a buffer.  Subject to blocking.
90
91         @param   b  Buffer to read into
92         @param   c  Maximum number of bytes to read
93
94         @return  Number of bytes read
95         */
96         unsigned read(char *b, unsigned c) { return do_read(b, c); }
97
98         /**
99         Reads characters up to the next linefeed or end-of-file.  The linefeed is not
100         included in the line.
101
102         @param   line  String to put the characters into
103
104         @return  Whether anything was read
105         */
106         virtual bool getline(std::string &);
107
108         /**
109         Reads a single character.
110
111         @return  A character, or -1 if none were available
112         */
113         virtual int get();
114
115         /**
116         Returns the end-of-file flag.
117         */
118         bool eof() const { return eof_flag; }
119
120         /**
121         Returns a mask of the currently interesting events.  Used by EventDispatcher.
122         */
123         PollEvent get_events() const { return events; }
124
125         /**
126         Returns a handle for polling.  Should throw if the object does not have an
127         event handle.
128         */
129         virtual Handle get_event_handle() =0;
130
131         /**
132         Notifies the object of an event.  Used by EventDispatcher.
133         */
134         void event(PollEvent);
135
136         virtual ~Base();
137 protected:
138         Mode mode;
139         PollEvent events;
140         bool eof_flag;
141
142         Base();
143         void             set_events(PollEvent);
144         virtual void     on_event(PollEvent) { }
145         virtual unsigned do_write(const char *, unsigned) =0;
146         virtual unsigned do_read(char *, unsigned) =0;
147 private:
148         Base(const Base &);
149         Base &operator=(const Base &);
150 };
151
152 } // namespace IO
153 } // namespace Msp
154
155 #endif