]> git.tdb.fi Git - libs/core.git/blob - source/io/base.h
61819cfe8144c782dae9e8749d971c3f336aa641
[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 object is deleted.  Mainly for use by
28         EventDispatcher. */
29         sigc::signal<void> signal_deleted;
30
31 protected:
32         Mode mode;
33         bool eof_flag;
34
35         Base();
36 private:
37         Base(const Base &);
38         Base &operator=(const Base &);
39 public:
40         virtual ~Base();
41
42         /** Sets blocking mode.  When blocking is enabled, most operations won't
43         return until they can make progress.  When blocking is disabled, these
44         operations may return immediately with a return code indicating that nothing
45         was done.
46
47         Blocking is enabled by default. */
48         virtual void set_block(bool) { }
49
50         /** Returns the current mode flags. */
51         Mode get_mode() const { return mode; }
52
53 protected:
54         void check_access(Mode) const;
55
56         virtual unsigned do_write(const char *, unsigned) = 0;
57         virtual unsigned do_read(char *, unsigned) = 0;
58
59 public:
60         /** Writes data from a buffer.  Subject to blocking.  Returns the number of
61         bytes written, which may be zero for a non-blockin operation. */
62         unsigned write(const char *b, unsigned c) { return do_write(b, c); }
63
64         /** Writes a string.  This is identical to calling
65         write(s.data(), s.size()). */
66         unsigned write(const std::string &s) { return do_write(s.data(), s.size()); }
67
68         /** Writes a single character.  This is identical to calling
69         write(&c, 1). */
70         virtual unsigned put(char c) { return do_write(&c, 1); }
71
72         /** Reads data into a buffer.  Subject to blocking.  Returns the number of
73         bytes read, which may be zero for a non-blocking operation. */
74         unsigned read(char *b, unsigned c) { return do_read(b, c); }
75
76         /** Reads characters up to the next linefeed or end-of-file.  The linefeed
77         is not included in the line.  Returns true if a line was successfully read,
78         false otherwise. */
79         virtual bool getline(std::string &);
80
81         /** Reads a single character.  Returns -1 if no character was available due
82         to end-of-file or non-blocking operation. */
83         virtual int get();
84
85 protected:
86         void set_eof();
87
88 public:
89         /** Returns the end-of-file flag. */
90         bool eof() const { return eof_flag; }
91 };
92
93 } // namespace IO
94 } // namespace Msp
95
96 #endif