]> git.tdb.fi Git - libs/core.git/blob - source/io/base.h
Unify end-of-file handling
[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         void check_access(Mode) const;
58
59         virtual unsigned do_write(const char *, unsigned) = 0;
60         virtual unsigned do_read(char *, unsigned) = 0;
61
62 public:
63         /** Writes data from a buffer.  Subject to blocking.  Returns the number of
64         bytes written, which may be zero for a non-blockin operation. */
65         unsigned write(const char *b, unsigned c) { return do_write(b, c); }
66
67         /** Writes a string.  This is identical to calling
68         write(s.data(), s.size()). */
69         unsigned write(const std::string &s) { return do_write(s.data(), s.size()); }
70
71         /** Writes a single character.  This is identical to calling
72         write(&c, 1). */
73         virtual unsigned put(char c) { return do_write(&c, 1); }
74
75         /** Reads data into a buffer.  Subject to blocking.  Returns the number of
76         bytes read, which may be zero for a non-blocking operation. */
77         unsigned read(char *b, unsigned c) { return do_read(b, c); }
78
79         /** Reads characters up to the next linefeed or end-of-file.  The linefeed
80         is not included in the line.  Returns true if a line was successfully read,
81         false otherwise. */
82         virtual bool getline(std::string &);
83
84         /** Reads a single character.  Returns -1 if no character was available due
85         to end-of-file or non-blocking operation. */
86         virtual int get();
87
88 protected:
89         void set_eof();
90
91 public:
92         /** Returns the end-of-file flag. */
93         bool eof() const { return eof_flag; }
94 };
95
96 } // namespace IO
97 } // namespace Msp
98
99 #endif