X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Fbase.h;fp=source%2Fio%2Fbase.h;h=774d03467b57f73b5af04217dcf8e3cae70046d1;hp=0000000000000000000000000000000000000000;hb=6e0fd758970bcb5bad5e3f2454b694cc4d7b4b66;hpb=b97d4e9f86e90254ab9edef7ee62a910f6333c78 diff --git a/source/io/base.h b/source/io/base.h new file mode 100644 index 0000000..774d034 --- /dev/null +++ b/source/io/base.h @@ -0,0 +1,121 @@ +#ifndef MSP_IO_BASE_H_ +#define MSP_IO_BASE_H_ + +#include +#include "mode.h" +#include "poll.h" +#include "types.h" + +namespace Msp { +namespace IO { + +/** +Common interface for all I/O objects. + +A derived class must call set_events(P_NONE) before it is destroyed to avoid +leaving stale pointers in an EventDispatcher. +*/ +class Base +{ +public: + /** Emitted when there is data available for reading. If all data is not + read, the signal is emitted again immediately. */ + sigc::signal signal_data_available; + + /** Emitted when there is no more data to be read. */ + sigc::signal signal_end_of_file; + + /** Emitted when there is a nonlinearity in I/O (such as a file being + seeked) and any data buffered by upper layers needs to be flushed. */ + sigc::signal signal_flush_required; + + /** Emitted when the I/O object has closed. */ + sigc::signal signal_closed; + + /** Emitted when the mask of interesting events changes. Mainly for use by + EventDispatcher. */ + sigc::signal signal_events_changed; + + /** Emitted when the object is deleted. Mainly for use by + EventDispatcher. */ + sigc::signal signal_deleted; + +protected: + Mode mode; + PollEvent events; + bool eof_flag; + + Base(); +private: + Base(const Base &); + Base &operator=(const Base &); +public: + virtual ~Base(); + + /** Sets blocking mode. When blocking is enabled, most operations won't + return until they can make progress. When blocking is disabled, these + operations may return immediately with a return code indicating that nothing + was done. + + Blocking is enabled by default. */ + virtual void set_block(bool) { } + + /** Returns the current mode flags. */ + Mode get_mode() const { return mode; } + +protected: + virtual unsigned do_write(const char *, unsigned) =0; + virtual unsigned do_read(char *, unsigned) =0; + +public: + /** Writes data from a buffer. Subject to blocking. Returns the number of + bytes written, which may be zero for a non-blockin operation. */ + unsigned write(const char *b, unsigned c) { return do_write(b, c); } + + /** Writes a string. This is identical to calling + write(s.data(), s.size()). */ + unsigned write(const std::string &s) { return do_write(s.data(), s.size()); } + + /** Writes a single character. This is identical to calling + write(&c, 1). */ + virtual unsigned put(char c) { return do_write(&c, 1); } + + /** Reads data into a buffer. Subject to blocking. Returns the number of + bytes read, which may be zero for a non-blocking operation. */ + unsigned read(char *b, unsigned c) { return do_read(b, c); } + + /** Reads characters up to the next linefeed or end-of-file. The linefeed + is not included in the line. Returns true if a line was successfully read, + false otherwise. */ + virtual bool getline(std::string &); + + /** Reads a single character. Returns -1 if no character was available due + to end-of-file or non-blocking operation. */ + virtual int get(); + + /** Returns the end-of-file flag. */ + bool eof() const { return eof_flag; } + +protected: + void set_events(PollEvent); + +public: + /** Returns a mask of the currently interesting events. Used by + EventDispatcher. */ + PollEvent get_events() const { return events; } + + /** Returns a handle for polling. Should throw if the object does not have + an event handle. */ + virtual Handle get_event_handle() =0; + + /** Notifies the object of an event. Used by EventDispatcher. */ + void event(PollEvent); + +protected: + virtual void on_event(PollEvent) { } +}; + +} // namespace IO +} // namespace Msp + +#endif