]> git.tdb.fi Git - libs/core.git/blob - source/io/base.h
Make certain functions pure virtual so I won't forget to implement them
[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 <msp/core/mutex.h>
6 #include "handle.h"
7 #include "mode.h"
8 #include "poll.h"
9
10 namespace Msp {
11 namespace IO {
12
13 /**
14 Common interface for all I/O objects.
15 */
16 class Base
17 {
18 public:
19         /** RAII synchronization primitive.  Prevents concurrent access to the
20         target object during the lifetime of the Synchronize object. */
21         class Synchronize
22         {
23         private:
24                 Base &io;
25
26         public:
27                 Synchronize(Base &);
28                 ~Synchronize();
29         };
30
31         /** Emitted when there is no more data to be read. */
32         sigc::signal<void> signal_end_of_file;
33
34         /** Emitted when there is a nonlinearity in I/O (such as a file being
35         seeked) and any data buffered by upper layers needs to be flushed. */
36         sigc::signal<void> signal_flush_required;
37
38         /** Emitted when the object is deleted.  Mainly for use by
39         EventDispatcher. */
40         sigc::signal<void> signal_deleted;
41
42 protected:
43         Mode mode;
44         bool eof_flag;
45         Mutex *mutex;
46
47         Base();
48 private:
49         Base(const Base &);
50         Base &operator=(const Base &);
51 public:
52         virtual ~Base();
53
54         /** Sets blocking mode.  When blocking is enabled, most operations won't
55         return until they can make progress.  When blocking is disabled, these
56         operations may return immediately with a return code indicating that nothing
57         was done.
58
59         Blocking is enabled by default. */
60         virtual void set_block(bool) = 0;
61
62         /** Returns the current mode flags. */
63         Mode get_mode() const { return mode; }
64
65 protected:
66         void check_access(Mode) const;
67
68         virtual unsigned do_write(const char *, unsigned) = 0;
69         virtual unsigned do_read(char *, unsigned) = 0;
70
71 public:
72         /** Writes data from a buffer.  Subject to blocking.  Returns the number of
73         bytes written, which may be zero for a non-blockin operation. */
74         unsigned write(const char *b, unsigned c) { return do_write(b, c); }
75
76         /** Writes a string.  This is identical to calling
77         write(s.data(), s.size()). */
78         unsigned write(const std::string &s) { return do_write(s.data(), s.size()); }
79
80         /** Writes a single character.  This is identical to calling
81         write(&c, 1). */
82         virtual unsigned put(char c) { return do_write(&c, 1); }
83
84         /** Reads data into a buffer.  Subject to blocking.  Returns the number of
85         bytes read, which may be zero for a non-blocking operation. */
86         unsigned read(char *b, unsigned c) { return do_read(b, c); }
87
88         /** Reads characters up to the next linefeed or end-of-file.  The linefeed
89         is not included in the line.  Returns true if a line was successfully read,
90         false otherwise. */
91         virtual bool getline(std::string &);
92
93         /** Reads a single character.  Returns -1 if no character was available due
94         to end-of-file or non-blocking operation. */
95         virtual int get();
96
97 protected:
98         void set_eof();
99
100 public:
101         /** Returns the end-of-file flag.  Note that some types of objects won't
102         indicate end-of-file until you try to read at least one byte past the actual
103         end, while others indicate it when you've read the last byte. */
104         bool eof() const { return eof_flag; }
105
106         /** Returns the system-level handle for the object.  Used by Console to
107         perform redirections. */
108         virtual const Handle &get_handle(Mode) = 0;
109 };
110
111 } // namespace IO
112 } // namespace Msp
113
114 #endif