]> git.tdb.fi Git - libs/core.git/blob - source/io/base.h
Some fixes for eof handling in Memory
[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 "handle.h"
6 #include "mode.h"
7 #include "poll.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 no more data to be read. */
22         sigc::signal<void> signal_end_of_file;
23
24         /** Emitted when there is a nonlinearity in I/O (such as a file being
25         seeked) and any data buffered by upper layers needs to be flushed. */
26         sigc::signal<void> signal_flush_required;
27
28         /** Emitted when the object is deleted.  Mainly for use by
29         EventDispatcher. */
30         sigc::signal<void> signal_deleted;
31
32 protected:
33         Mode mode;
34         bool eof_flag;
35
36         Base();
37 private:
38         Base(const Base &);
39         Base &operator=(const Base &);
40 public:
41         virtual ~Base();
42
43         /** Sets blocking mode.  When blocking is enabled, most operations won't
44         return until they can make progress.  When blocking is disabled, these
45         operations may return immediately with a return code indicating that nothing
46         was done.
47
48         Blocking is enabled by default. */
49         virtual void set_block(bool) { }
50
51         /** Returns the current mode flags. */
52         Mode get_mode() const { return mode; }
53
54 protected:
55         void check_access(Mode) const;
56
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 protected:
87         void set_eof();
88
89 public:
90         /** Returns the end-of-file flag.  Note that some types of objects won't
91         indicate end-of-file until you try to read at least one byte past the actual
92         end, while others indicate it when you've read the last byte. */
93         bool eof() const { return eof_flag; }
94
95         /** Returns the system-level handle for the object.  Used by Console to
96         perform redirections. */
97         virtual const Handle &get_handle(Mode);
98 };
99
100 } // namespace IO
101 } // namespace Msp
102
103 #endif