]> git.tdb.fi Git - libs/core.git/blob - source/base.h
Initial revision
[libs/core.git] / source / base.h
1 /* $Id$
2
3 This file is part of libmspio
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7 #ifndef MSP_IO_BASE_H_
8 #define MSP_IO_BASE_H_
9
10 #include <sigc++/sigc++.h>
11 #include "mode.h"
12 #include "poll.h"
13 #include "types.h"
14
15 namespace Msp {
16 namespace IO {
17
18 /**
19 Common interface for all I/O objects.
20
21 A derived class must call set_events(P_NONE) before it is destroyed to avoid
22 leaving stale pointers in an EventDispatcher.
23 */
24 class Base
25 {
26 public:
27         /**
28         Emitted when there is data available for reading.  If all data is not read,
29         the signal is emitted again immediately.
30         */
31         sigc::signal<void> signal_data_available;
32
33         /**
34         Emitted when there is no more data to be read.
35         */
36         sigc::signal<void> signal_end_of_file;
37
38         /**
39         Emitted when the I/O object is about to close.  Mainly intended for
40         buffering objects that need to flush their buffers at closing.
41         */
42         sigc::signal<void> signal_closing;
43
44         /**
45         Emitted when the I/O object has closed.
46         */
47         sigc::signal<void> signal_closed;
48
49         /**
50         Emitted when the mask of interesting events changes.  Mainly for use by
51         EventDispatcher.
52         */
53         sigc::signal<void, PollEvent> signal_events_changed;
54
55         /**
56         Emitted when the object is deleted.  Mainly for use by EventDispatcher.
57         */
58         sigc::signal<void> signal_deleted;
59
60         /**
61         Sets blocking mode.  When blocking is enabled, most operations won't return
62         until they can make progress.  When blocking is disabled, these operations
63         may return immediately with a return code indicating that nothing was done.
64
65         Blocking is enabled by default.
66         */
67         virtual void set_block(bool) { }
68
69         /**
70         Returns the current mode flags.
71         */
72         Mode get_mode() const { return mode; }
73
74         /**
75         Writes data from a buffer.  Subject to blocking.
76
77         @param   b  Buffer to write from
78         @param   c  Number of bytes to write
79
80         @return  Number of bytes written
81         */
82         unsigned write(const char *b, unsigned c) { return do_write(b, c); }
83
84         /**
85         Writes a string.  This is identical to calling write(s.data(), s.size()).
86         */
87         unsigned write(const std::string &s) { return do_write(s.data(), s.size()); }
88
89         /**
90         Writes a single character.  This is identical to calling write(&c, 1).
91         */
92         virtual unsigned put(char c) { return do_write(&c, 1); }
93
94         /**
95         Reads data into a buffer.  Subject to blocking.
96
97         @param   b  Buffer to read into
98         @param   c  Maximum number of bytes to read
99
100         @return  Number of bytes read
101         */
102         unsigned read(char *b, unsigned c) { return do_read(b, c); }
103
104         /**
105         Reads characters up to the next linefeed or end-of-file.  The linefeed is not
106         included in the line.
107
108         @param   line  String to put the characters into
109
110         @return  Whether anything was read
111         */
112         virtual bool getline(std::string &);
113
114         /**
115         Reads a single character.
116
117         @return  A character, or -1 if none were available
118         */
119         virtual int get();
120
121         /**
122         Returns the end-of-file flag.
123         */
124         bool eof() const { return eof_flag; }
125
126         /**
127         Returns a mask of the currently interesting events.  Used by EventDispatcher.
128         */
129         PollEvent get_events() const { return events; }
130
131         /**
132         Returns a handle for polling.  Should throw if the object does not have an
133         event handle.
134         */
135         virtual Handle get_event_handle() =0;
136
137         /**
138         Notifies the object of an event.  Used by EventDispatcher.
139         */
140         void event(PollEvent);
141
142         virtual ~Base();
143 protected:
144         Mode mode;
145         PollEvent events;
146         bool eof_flag;
147
148         Base();
149         void             set_events(PollEvent);
150         virtual void     on_event(PollEvent) { }
151         virtual unsigned do_write(const char *, unsigned) =0;
152         virtual unsigned do_read(char *, unsigned) =0;
153 private:
154         Base(const Base &);
155         Base &operator=(const Base &);
156 };
157
158 } // namespace IO
159 } // namespace Msp
160
161 #endif