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