From: Mikko Rasa Date: Fri, 10 Jun 2011 18:00:11 +0000 (+0300) Subject: Move class members and comments around X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=b97d4e9f86e90254ab9edef7ee62a910f6333c78;p=libs%2Fcore.git Move class members and comments around --- diff --git a/source/base.cpp b/source/base.cpp index a740433..48de3ee 100644 --- a/source/base.cpp +++ b/source/base.cpp @@ -6,6 +6,17 @@ using namespace std; namespace Msp { namespace IO { +Base::Base(): + mode(M_READ), + events(P_NONE), + eof_flag(false) +{ } + +Base::~Base() +{ + signal_deleted.emit(); +} + bool Base::getline(string &line) { line.clear(); @@ -32,6 +43,12 @@ int Base::get() return static_cast(c); } +void Base::set_events(PollEvent e) +{ + events = e; + signal_events_changed.emit(events); +} + void Base::event(PollEvent ev) { if(ev&P_INPUT) @@ -40,22 +57,5 @@ void Base::event(PollEvent ev) on_event(ev); } -Base::~Base() -{ - signal_deleted.emit(); -} - -Base::Base(): - mode(M_READ), - events(P_NONE), - eof_flag(false) -{ } - -void Base::set_events(PollEvent e) -{ - events = e; - signal_events_changed.emit(events); -} - } // namespace IO } // namespace Msp diff --git a/source/base.h b/source/base.h index 584c14e..774d034 100644 --- a/source/base.h +++ b/source/base.h @@ -18,135 +18,101 @@ 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. - */ + /** 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. - */ + /** 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. - */ + /** 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. - */ + /** 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. - */ + /** 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. - */ + /** Emitted when the object is deleted. Mainly for use by + EventDispatcher. */ sigc::signal signal_deleted; - /** - 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. +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. - */ + Blocking is enabled by default. */ virtual void set_block(bool) { } - /** - Returns the current mode flags. - */ + /** Returns the current mode flags. */ Mode get_mode() const { return mode; } - /** - Writes data from a buffer. Subject to blocking. - - @param b Buffer to write from - @param c Number of bytes to write +protected: + virtual unsigned do_write(const char *, unsigned) =0; + virtual unsigned do_read(char *, unsigned) =0; - @return Number of bytes written - */ +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()). - */ + /** 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). - */ + /** 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. - - @param b Buffer to read into - @param c Maximum number of bytes to read - - @return Number of bytes read - */ + /** 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. - - @param line String to put the characters into - - @return Whether anything was read - */ + /** 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. - - @return A character, or -1 if none were available - */ + /** 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. - */ + /** Returns the end-of-file flag. */ bool eof() const { return eof_flag; } - /** - Returns a mask of the currently interesting events. Used by EventDispatcher. - */ +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. - */ + /** 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. - */ + /** Notifies the object of an event. Used by EventDispatcher. */ void event(PollEvent); - virtual ~Base(); protected: - Mode mode; - PollEvent events; - bool eof_flag; - - Base(); - void set_events(PollEvent); - virtual void on_event(PollEvent) { } - virtual unsigned do_write(const char *, unsigned) =0; - virtual unsigned do_read(char *, unsigned) =0; -private: - Base(const Base &); - Base &operator=(const Base &); + virtual void on_event(PollEvent) { } }; } // namespace IO diff --git a/source/buffered.cpp b/source/buffered.cpp index e36aef4..f53d2fc 100644 --- a/source/buffered.cpp +++ b/source/buffered.cpp @@ -19,17 +19,16 @@ Buffered::Buffered(Base &b, unsigned s): below.signal_flush_required.connect(sigc::mem_fun(this, &Buffered::flush)); } -unsigned Buffered::put(char c) +Buffered::~Buffered() { - set_op(M_WRITE); - - if(end(*begin++); - - char c; - if(do_read(&c, 1)==0) - return -1; - return static_cast(c); -} - -Handle Buffered::get_event_handle() -{ - throw Exception("Buffered doesn't support events"); -} - -unsigned Buffered::get_current_size() const -{ - return end-begin; -} - -Buffered::~Buffered() -{ - try - { - flush(); - } - catch(...) - { } - - delete[] buf; -} - -void Buffered::set_op(Mode op) -{ - if(op!=cur_op) - flush(); - cur_op = op; -} - unsigned Buffered::do_write(const char *data, unsigned size) { set_op(M_WRITE); @@ -184,5 +126,63 @@ unsigned Buffered::do_read(char *data, unsigned size) } } +unsigned Buffered::put(char c) +{ + set_op(M_WRITE); + + if(end(*begin++); + + char c; + if(do_read(&c, 1)==0) + return -1; + return static_cast(c); +} + +void Buffered::set_op(Mode op) +{ + if(op!=cur_op) + flush(); + cur_op = op; +} + +unsigned Buffered::get_current_size() const +{ + return end-begin; +} + +Handle Buffered::get_event_handle() +{ + throw Exception("Buffered doesn't support events"); +} + } // namespace IO } // namespace Msp diff --git a/source/buffered.h b/source/buffered.h index 9142a47..36d2a11 100644 --- a/source/buffered.h +++ b/source/buffered.h @@ -9,29 +9,35 @@ namespace IO { class Buffered: public Base { private: - Base &below; + Base &below; unsigned buf_size; - char *buf; - char *begin; - char *end; - Mode cur_op; + char *buf; + char *begin; + char *end; + Mode cur_op; public: Buffered(Base &, unsigned =8192); ~Buffered(); - unsigned put(char); void flush(); - bool getline(std::string &); - int get(); - Handle get_event_handle(); - Mode get_current_op() const { return cur_op; } - unsigned get_current_size() const; -private: - void set_op(Mode); + protected: unsigned do_write(const char *, unsigned); unsigned do_read(char *, unsigned); +public: + unsigned put(char); + + bool getline(std::string &); + int get(); + +private: + void set_op(Mode); +public: + Mode get_current_op() const { return cur_op; } + unsigned get_current_size() const; + + virtual Handle get_event_handle(); }; } // namespace IO diff --git a/source/console.cpp b/source/console.cpp index fed47be..9db7737 100644 --- a/source/console.cpp +++ b/source/console.cpp @@ -115,11 +115,6 @@ void Console::get_size(unsigned &rows, unsigned &cols) #endif } -Handle Console::get_event_handle() -{ - return 0; -} - unsigned Console::do_write(const char *buf, unsigned len) { if(!(mode&M_WRITE)) @@ -163,6 +158,11 @@ unsigned Console::do_read(char *buf, unsigned len) return ret; } +Handle Console::get_event_handle() +{ + return 0; +} + Console &Console::instance(unsigned n) { static Console in(0); diff --git a/source/console.h b/source/console.h index 2d425c8..3c0bb71 100644 --- a/source/console.h +++ b/source/console.h @@ -32,16 +32,17 @@ public: */ void set_line_buffer(bool); - /** - Retrieves the size of the Console. Can only be used on an output Console. - */ + /** Retrieves the size of the Console. Can only be used on an output + Console. */ void get_size(unsigned &rows, unsigned &cols); - virtual Handle get_event_handle(); protected: virtual unsigned do_write(const char *, unsigned); virtual unsigned do_read(char *, unsigned); + public: + virtual Handle get_event_handle(); + static Console &instance(unsigned); }; diff --git a/source/eventdispatcher.h b/source/eventdispatcher.h index dca6592..7e89d47 100644 --- a/source/eventdispatcher.h +++ b/source/eventdispatcher.h @@ -14,22 +14,6 @@ on some of them. */ class EventDispatcher: public sigc::trackable { -public: - EventDispatcher(); - void add(Base &); - void remove(Base &); - - /** - Checks for and dispatches events. If there are no events available, blocks - until there are. - */ - void tick(); - - /** - Checks for and dispatches events. If there are no events available, waits - at most the specified time before returning. - */ - void tick(const Time::TimeDelta &); private: struct Slot { @@ -39,11 +23,27 @@ private: Slot(Base *o): obj(o) { } }; + typedef std::map SlotMap; Poller poller; SlotMap objects; +public: + EventDispatcher(); + + void add(Base &); + void remove(Base &); + + /** Checks for and dispatches events. If there are no events available, + blocks until there are. */ + void tick(); + + /** Checks for and dispatches events. If there are no events available, + waits at most the specified time before returning. */ + void tick(const Time::TimeDelta &); + +private: void object_events_changed(PollEvent, Base *); void object_deleted(Base *); void dispatch(); diff --git a/source/file.cpp b/source/file.cpp index 8182e4a..2990d5c 100644 --- a/source/file.cpp +++ b/source/file.cpp @@ -12,15 +12,6 @@ using namespace std; namespace Msp { namespace IO { -/** -Creates a new file object and opens it. If the -create flag is true and write access is requested and the file does exist, it -is created. Otherwise a missing file is an error. - -@param fn Name of the file to open -@param m Open mode -@param cm Flags controlling creation of a new file -*/ File::File(const string &fn, Mode m, CreateMode cm) { if(!(m&M_RDWR)) @@ -94,10 +85,11 @@ File::File(const string &fn, Mode m, CreateMode cm) set_events(P_INPUT); } -/** -Closes the file. Any attempt to access the file after this will cause an -exception to be thrown. -*/ +File::~File() +{ + close(); +} + void File::close() { if(handle==MSP_IO_INVALID_HANDLE) @@ -117,10 +109,6 @@ void File::close() signal_closed.emit(); } -/** -Sets the blocking state of the file. If blocking is disabled, all operations -will return immediately, even if they can't be fully completed. -*/ void File::set_block(bool b) { check_access(M_NONE); @@ -134,88 +122,6 @@ void File::set_block(bool b) #endif } -void File::sync() -{ -#ifndef WIN32 - signal_flush_required.emit(); - - fsync(handle); -#endif -} - -/** -Seeks the file to the given byte offset. - -@param off Offset in bytes -@param st Seek type - -@return The resulting offset -*/ -int File::seek(int off, SeekType st) -{ - check_access(M_NONE); - - signal_flush_required.emit(); - - int type = sys_seek_type(st); -#ifdef WIN32 - DWORD ret = SetFilePointer(handle, off, 0, type); - if(ret==INVALID_SET_FILE_POINTER) - throw SystemError("Seek failed", GetLastError()); -#else - int ret = lseek(handle, off, type); - if(ret==-1) - throw SystemError("Seek failed", errno); -#endif - - eof_flag = false; - - return ret; -} - -/** -Returns the current read/write offset of the file. -*/ -int File::tell() const -{ - check_access(M_NONE); - -#ifdef WIN32 - DWORD ret = SetFilePointer(handle, 0, 0, FILE_CURRENT); - if(ret==INVALID_SET_FILE_POINTER) - throw SystemError("Tell failed", GetLastError()); -#else - int ret = lseek(handle, 0, SEEK_CUR); - if(ret==-1) - throw SystemError("Tell failed", errno); -#endif - - return ret; -} - -File::~File() -{ - close(); -} - -void File::check_access(Mode m) const -{ - if(handle==MSP_IO_INVALID_HANDLE) - throw InvalidState("File is not open"); - if(m==M_READ && !(mode&M_READ)) - throw InvalidState("File is not readable"); - if(m==M_WRITE && !(mode&M_WRITE)) - throw InvalidState("File is not writable"); -} - -/** -Writes data from a buffer to the file. - -@param buf Buffer to write from. -@param size Length of data to write. - -@return The number of bytes written -*/ unsigned File::do_write(const char *buf, unsigned size) { check_access(M_WRITE); @@ -243,14 +149,6 @@ unsigned File::do_write(const char *buf, unsigned size) return ret; } -/** -Reads data from the file. - -@param buf Buffer to read data into. -@param size Maximum size of data to read. - -@return The number of bytes read, possibly zero -*/ unsigned File::do_read(char *buf, unsigned size) { check_access(M_READ); @@ -282,5 +180,63 @@ unsigned File::do_read(char *buf, unsigned size) return ret; } +void File::sync() +{ +#ifndef WIN32 + signal_flush_required.emit(); + + fsync(handle); +#endif +} + +int File::seek(int off, SeekType st) +{ + check_access(M_NONE); + + signal_flush_required.emit(); + + int type = sys_seek_type(st); +#ifdef WIN32 + DWORD ret = SetFilePointer(handle, off, 0, type); + if(ret==INVALID_SET_FILE_POINTER) + throw SystemError("Seek failed", GetLastError()); +#else + int ret = lseek(handle, off, type); + if(ret==-1) + throw SystemError("Seek failed", errno); +#endif + + eof_flag = false; + + return ret; +} + +int File::tell() const +{ + check_access(M_NONE); + +#ifdef WIN32 + DWORD ret = SetFilePointer(handle, 0, 0, FILE_CURRENT); + if(ret==INVALID_SET_FILE_POINTER) + throw SystemError("Tell failed", GetLastError()); +#else + int ret = lseek(handle, 0, SEEK_CUR); + if(ret==-1) + throw SystemError("Tell failed", errno); +#endif + + return ret; +} + +void File::check_access(Mode m) const +{ + if(handle==MSP_IO_INVALID_HANDLE) + throw InvalidState("File is not open"); + if(m==M_READ && !(mode&M_READ)) + throw InvalidState("File is not readable"); + if(m==M_WRITE && !(mode&M_WRITE)) + throw InvalidState("File is not writable"); +} + } // namespace IO } // namespace Msp diff --git a/source/file.h b/source/file.h index 6353a46..05aaf95 100644 --- a/source/file.h +++ b/source/file.h @@ -25,27 +25,39 @@ public: C_TRUNCATE = 2 }; - File(const std::string &, Mode = M_READ, CreateMode =CreateMode(C_CREATE+C_TRUNCATE)); +private: + Handle handle; + +public: + /** Creates a new file object and opens it. If the create flag is set and + write access is requested and the file does exist, it is created. Otherwise + a missing file is an error. */ + File(const std::string &, Mode = M_READ, CreateMode = CreateMode(C_CREATE+C_TRUNCATE)); + virtual ~File(); + /** Closes the file. Any attempt to access the file after this will cause + an exception to be thrown. */ void close(); void set_block(bool); +protected: + virtual unsigned do_write(const char *, unsigned); + virtual unsigned do_read(char *, unsigned); + +public: virtual void sync(); - virtual int seek(int, SeekType); - virtual int tell() const; + /** Changes the read/write offset of the file. Returns the new offset. */ + virtual int seek(int, SeekType); + + /** Returns the current read/write offset of the file. */ + virtual int tell() const; virtual Handle get_event_handle() { return handle; } - virtual ~File(); private: - Handle handle; - - void check_access(Mode) const; -protected: - virtual unsigned do_write(const char *, unsigned); - virtual unsigned do_read(char *, unsigned); + void check_access(Mode) const; }; inline File::CreateMode operator|(File::CreateMode m, File::CreateMode n) diff --git a/source/filtered.h b/source/filtered.h index 98cda47..0d323ec 100644 --- a/source/filtered.h +++ b/source/filtered.h @@ -29,11 +29,6 @@ public: template Filtered(A0 a0, A1 a1): B(a0, a1), filter(*this), active(false) { } - virtual unsigned put(char c) { return filter.put(c); } - virtual bool getline(std::string &l) { return filter.getline(l); } - virtual int get() { return filter.get(); } - - F &get_filter() { return filter; } protected: virtual unsigned do_write(const char *b, unsigned s) { @@ -56,6 +51,13 @@ protected: else return B::do_read(b, s); } + +public: + virtual unsigned put(char c) { return filter.put(c); } + virtual bool getline(std::string &l) { return filter.getline(l); } + virtual int get() { return filter.get(); } + + F &get_filter() { return filter; } }; } // namespace IO diff --git a/source/memory.cpp b/source/memory.cpp index 13a6606..6ea130b 100644 --- a/source/memory.cpp +++ b/source/memory.cpp @@ -37,6 +37,30 @@ void Memory::init(char *b, char *e, Mode m) mode = m; } +unsigned Memory::do_write(const char *buf, unsigned size) +{ + check_mode(M_WRITE); + + size = min(size, end-pos); + memcpy(pos, buf, size); + pos += size; + return size; +} + +unsigned Memory::do_read(char *buf, unsigned size) +{ + if(pos==end) + { + eof_flag = true; + return 0; + } + + size = min(size, end-pos); + memcpy(buf, pos, size); + pos += size; + return size; +} + unsigned Memory::put(char c) { check_mode(M_WRITE); @@ -88,30 +112,6 @@ Handle Memory::get_event_handle() throw Exception("Memory doesn't support events"); } -unsigned Memory::do_write(const char *buf, unsigned size) -{ - check_mode(M_WRITE); - - size = min(size, end-pos); - memcpy(pos, buf, size); - pos += size; - return size; -} - -unsigned Memory::do_read(char *buf, unsigned size) -{ - if(pos==end) - { - eof_flag = true; - return 0; - } - - size = min(size, end-pos); - memcpy(buf, pos, size); - pos += size; - return size; -} - void Memory::check_mode(Mode m) const { if(m==M_WRITE) diff --git a/source/memory.h b/source/memory.h index 80f884e..ba6c4d8 100644 --- a/source/memory.h +++ b/source/memory.h @@ -22,16 +22,19 @@ public: private: void init(char *, char *, Mode); + virtual unsigned do_write(const char *, unsigned); + virtual unsigned do_read(char *, unsigned); public: virtual unsigned put(char); virtual bool getline(std::string &); virtual int get(); + unsigned seek(int, SeekType); unsigned tell() const { return pos-begin; } + virtual Handle get_event_handle(); + private: - virtual unsigned do_write(const char *, unsigned); - virtual unsigned do_read(char *, unsigned); void check_mode(Mode) const; }; diff --git a/source/pipe.cpp b/source/pipe.cpp index 7f0ff70..b3735aa 100644 --- a/source/pipe.cpp +++ b/source/pipe.cpp @@ -40,18 +40,9 @@ Pipe::Pipe() set_events(P_INPUT); } -void Pipe::set_block(bool b) +Pipe::~Pipe() { - mode = (mode&~M_NONBLOCK); - if(b) - mode = (mode|M_NONBLOCK); - -#ifndef WIN32 - int flags = fcntl(handle[0], F_GETFD); - fcntl(handle[0], F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); - flags = fcntl(handle[1], F_GETFD); - fcntl(handle[1], F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); -#endif + close(); } void Pipe::close() @@ -69,43 +60,20 @@ void Pipe::close() #endif } -Handle Pipe::get_event_handle() +void Pipe::set_block(bool b) { -#ifdef WIN32 - if(!overlapped && !buf_avail) - { - overlapped = new OVERLAPPED; - memset(overlapped, 0, sizeof(OVERLAPPED)); - overlapped->hEvent = event; - - DWORD ret; - buf_next = buffer; - if(!ReadFile(handle[0], buffer, buf_size, &ret, overlapped)) - { - unsigned err = GetLastError(); - if(err!=ERROR_IO_PENDING) - throw SystemError("Failed to start an overlapped read", err); - } - else - { - buf_avail = ret; - delete overlapped; - overlapped = 0; - SetEvent(event); - } - } + mode = (mode&~M_NONBLOCK); + if(b) + mode = (mode|M_NONBLOCK); - return event; -#else - return handle[0]; +#ifndef WIN32 + int flags = fcntl(handle[0], F_GETFD); + fcntl(handle[0], F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); + flags = fcntl(handle[1], F_GETFD); + fcntl(handle[1], F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); #endif } -Pipe::~Pipe() -{ - close(); -} - unsigned Pipe::do_write(const char *buf, unsigned size) { if(size==0) @@ -178,5 +146,37 @@ unsigned Pipe::do_read(char *buf, unsigned size) return ret; } +Handle Pipe::get_event_handle() +{ +#ifdef WIN32 + if(!overlapped && !buf_avail) + { + overlapped = new OVERLAPPED; + memset(overlapped, 0, sizeof(OVERLAPPED)); + overlapped->hEvent = event; + + DWORD ret; + buf_next = buffer; + if(!ReadFile(handle[0], buffer, buf_size, &ret, overlapped)) + { + unsigned err = GetLastError(); + if(err!=ERROR_IO_PENDING) + throw SystemError("Failed to start an overlapped read", err); + } + else + { + buf_avail = ret; + delete overlapped; + overlapped = 0; + SetEvent(event); + } + } + + return event; +#else + return handle[0]; +#endif +} + } // namespace IO } // namespace Msp diff --git a/source/pipe.h b/source/pipe.h index b67cd79..71edf7e 100644 --- a/source/pipe.h +++ b/source/pipe.h @@ -8,26 +8,31 @@ namespace IO { class Pipe: public Base { -public: - Pipe(); - void set_block(bool); - void close(); - Handle get_event_handle(); - ~Pipe(); private: Handle handle[2]; #ifdef WIN32 OVERLAPPED *overlapped; - Handle event; - unsigned buf_size; - char *buffer; - unsigned buf_avail; - char *buf_next; + Handle event; + unsigned buf_size; + char *buffer; + unsigned buf_avail; + char *buf_next; #endif +public: + Pipe(); + ~Pipe(); + + void close(); + + void set_block(bool); + protected: - unsigned do_write(const char *, unsigned); - unsigned do_read(char *, unsigned); + virtual unsigned do_write(const char *, unsigned); + virtual unsigned do_read(char *, unsigned); + +public: + virtual Handle get_event_handle(); }; } // namespace IO diff --git a/source/poll.h b/source/poll.h index 0f97cb1..6367d8f 100644 --- a/source/poll.h +++ b/source/poll.h @@ -33,6 +33,7 @@ inline PollEvent operator&(PollEvent e, PollEvent f) inline PollEvent operator~(PollEvent e) { return PollEvent(~static_cast(e)); } + class Poller { public: @@ -43,13 +44,8 @@ public: Slot(Base *o, PollEvent e): object(o), events(e) { } }; - typedef std::list SlotSeq; - Poller(); - void set_object(Base &, PollEvent); - int poll(); - int poll(const Time::TimeDelta &); - const SlotSeq &get_result() const { return poll_result; } + typedef std::list SlotSeq; private: typedef std::map SlotMap; @@ -67,6 +63,14 @@ private: void rebuild_pfd(); int do_poll(int); + +public: + Poller(); + + void set_object(Base &, PollEvent); + int poll(); + int poll(const Time::TimeDelta &); + const SlotSeq &get_result() const { return poll_result; } }; PollEvent poll(Base &, PollEvent); diff --git a/source/serial.cpp b/source/serial.cpp index 0a5eca0..ec6a198 100644 --- a/source/serial.cpp +++ b/source/serial.cpp @@ -205,6 +205,15 @@ Serial::~Serial() close(); } +void Serial::close() +{ +#ifdef WIN32 + CloseHandle(handle); +#else + ::close(handle); +#endif +} + void Serial::set_block(bool b) { if(b) @@ -272,24 +281,6 @@ void Serial::set_parameters(const string ¶ms) set_state(handle, state); } -Handle Serial::get_event_handle() -{ -#ifdef WIN32 - throw Exception("Serial port events not supported on win32 yet"); -#else - return handle; -#endif -} - -void Serial::close() -{ -#ifdef WIN32 - CloseHandle(handle); -#else - ::close(handle); -#endif -} - unsigned Serial::do_write(const char *buf, unsigned size) { if(size==0) @@ -336,5 +327,14 @@ unsigned Serial::do_read(char *buf, unsigned size) return ret; } +Handle Serial::get_event_handle() +{ +#ifdef WIN32 + throw Exception("Serial port events not supported on win32 yet"); +#else + return handle; +#endif +} + } // namespace IO } // namespace Msp diff --git a/source/serial.h b/source/serial.h index d5f1de7..069789c 100644 --- a/source/serial.h +++ b/source/serial.h @@ -23,6 +23,10 @@ public: Serial(const std::string &); virtual ~Serial(); +private: + void close(); + +public: virtual void set_block(bool); void set_baud_rate(unsigned); @@ -31,12 +35,12 @@ public: void set_stop_bits(unsigned); void set_parameters(const std::string &); - virtual Handle get_event_handle(); - private: - void close(); virtual unsigned do_write(const char *, unsigned); virtual unsigned do_read(char *, unsigned); + +public: + virtual Handle get_event_handle(); }; } // namespace IO diff --git a/source/utils.h b/source/utils.h index 066b15b..8d4f9dd 100644 --- a/source/utils.h +++ b/source/utils.h @@ -6,13 +6,12 @@ namespace IO { class Base; -/** -Reads data from an object. Does not return until the requested amount of data -is read, regardless of the blocking mode of the object. +/** Reads data from an object. Does not return until the requested amount of +data is read, regardless of the blocking mode of the object. Note: If the data is not immediately available and the object is in non-blocking -mode, this function effectively becomes a busyloop until it can get more data. -*/ +mode, this function effectively becomes a busyloop until it can get more +data. */ unsigned read_all(Base &, char *, unsigned); } // namespace IO