3 This file is part of libmspio
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include <sigc++/sigc++.h>
19 Common interface for all I/O objects.
21 A derived class must call set_events(P_NONE) before it is destroyed to avoid
22 leaving stale pointers in an EventDispatcher.
28 Emitted when there is data available for reading. If all data is not read,
29 the signal is emitted again immediately.
31 sigc::signal<void> signal_data_available;
34 Emitted when there is no more data to be read.
36 sigc::signal<void> signal_end_of_file;
39 Emitted when there is a nonlinearity in I/O (such as a file being seeked)
40 and any data buffered by upper layers needs to be flushed.
42 sigc::signal<void> signal_flush_required;
45 Emitted when the I/O object has closed.
47 sigc::signal<void> signal_closed;
50 Emitted when the mask of interesting events changes. Mainly for use by
53 sigc::signal<void, PollEvent> signal_events_changed;
56 Emitted when the object is deleted. Mainly for use by EventDispatcher.
58 sigc::signal<void> signal_deleted;
61 Sets blocking mode. When blocking is enabled, most operations won't return
62 until they can make progress. When blocking is disabled, these operations
63 may return immediately with a return code indicating that nothing was done.
65 Blocking is enabled by default.
67 virtual void set_block(bool) { }
70 Returns the current mode flags.
72 Mode get_mode() const { return mode; }
75 Writes data from a buffer. Subject to blocking.
77 @param b Buffer to write from
78 @param c Number of bytes to write
80 @return Number of bytes written
82 unsigned write(const char *b, unsigned c) { return do_write(b, c); }
85 Writes a string. This is identical to calling write(s.data(), s.size()).
87 unsigned write(const std::string &s) { return do_write(s.data(), s.size()); }
90 Writes a single character. This is identical to calling write(&c, 1).
92 virtual unsigned put(char c) { return do_write(&c, 1); }
95 Reads data into a buffer. Subject to blocking.
97 @param b Buffer to read into
98 @param c Maximum number of bytes to read
100 @return Number of bytes read
102 unsigned read(char *b, unsigned c) { return do_read(b, c); }
105 Reads characters up to the next linefeed or end-of-file. The linefeed is not
106 included in the line.
108 @param line String to put the characters into
110 @return Whether anything was read
112 virtual bool getline(std::string &);
115 Reads a single character.
117 @return A character, or -1 if none were available
122 Returns the end-of-file flag.
124 bool eof() const { return eof_flag; }
127 Returns a mask of the currently interesting events. Used by EventDispatcher.
129 PollEvent get_events() const { return events; }
132 Returns a handle for polling. Should throw if the object does not have an
135 virtual Handle get_event_handle() =0;
138 Notifies the object of an event. Used by EventDispatcher.
140 void event(PollEvent);
149 void set_events(PollEvent);
150 virtual void on_event(PollEvent) { }
151 virtual unsigned do_write(const char *, unsigned) =0;
152 virtual unsigned do_read(char *, unsigned) =0;
155 Base &operator=(const Base &);