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();
return static_cast<unsigned char>(c);
}
+void Base::set_events(PollEvent e)
+{
+ events = e;
+ signal_events_changed.emit(events);
+}
+
void Base::event(PollEvent ev)
{
if(ev&P_INPUT)
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
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<void> 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<void> 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<void> signal_flush_required;
- /**
- Emitted when the I/O object has closed.
- */
+ /** Emitted when the I/O object has closed. */
sigc::signal<void> 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<void, PollEvent> 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<void> 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
below.signal_flush_required.connect(sigc::mem_fun(this, &Buffered::flush));
}
-unsigned Buffered::put(char c)
+Buffered::~Buffered()
{
- set_op(M_WRITE);
-
- if(end<buf+buf_size)
+ try
{
- *end++ = c;
- return 1;
+ flush();
}
- else
- return do_write(&c, 1);
+ catch(...)
+ { }
+
+ delete[] buf;
}
void Buffered::flush()
begin=end = buf;
}
-bool Buffered::getline(std::string &line)
-{
- set_op(M_READ);
-
- for(char *i=begin; i!=end; ++i)
- if(*i=='\n')
- {
- line.assign(begin, i-begin);
- begin = i+1;
- return true;
- }
-
- return Base::getline(line);
-}
-
-int Buffered::get()
-{
- set_op(M_READ);
-
- if(begin<end)
- return static_cast<unsigned char>(*begin++);
-
- char c;
- if(do_read(&c, 1)==0)
- return -1;
- return static_cast<unsigned char>(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);
}
}
+unsigned Buffered::put(char c)
+{
+ set_op(M_WRITE);
+
+ if(end<buf+buf_size)
+ {
+ *end++ = c;
+ return 1;
+ }
+ else
+ return do_write(&c, 1);
+}
+
+bool Buffered::getline(std::string &line)
+{
+ set_op(M_READ);
+
+ for(char *i=begin; i!=end; ++i)
+ if(*i=='\n')
+ {
+ line.assign(begin, i-begin);
+ begin = i+1;
+ return true;
+ }
+
+ return Base::getline(line);
+}
+
+int Buffered::get()
+{
+ set_op(M_READ);
+
+ if(begin<end)
+ return static_cast<unsigned char>(*begin++);
+
+ char c;
+ if(do_read(&c, 1)==0)
+ return -1;
+ return static_cast<unsigned char>(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
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
#endif
}
-Handle Console::get_event_handle()
-{
- return 0;
-}
-
unsigned Console::do_write(const char *buf, unsigned len)
{
if(!(mode&M_WRITE))
return ret;
}
+Handle Console::get_event_handle()
+{
+ return 0;
+}
+
Console &Console::instance(unsigned n)
{
static Console in(0);
*/
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);
};
*/
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
{
Slot(Base *o): obj(o) { }
};
+
typedef std::map<Base *, Slot> 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();
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))
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)
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);
#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);
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);
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
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)
template<typename A0, typename A1>
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)
{
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
mode = m;
}
+unsigned Memory::do_write(const char *buf, unsigned size)
+{
+ check_mode(M_WRITE);
+
+ size = min<unsigned>(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<unsigned>(size, end-pos);
+ memcpy(buf, pos, size);
+ pos += size;
+ return size;
+}
+
unsigned Memory::put(char c)
{
check_mode(M_WRITE);
throw Exception("Memory doesn't support events");
}
-unsigned Memory::do_write(const char *buf, unsigned size)
-{
- check_mode(M_WRITE);
-
- size = min<unsigned>(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<unsigned>(size, end-pos);
- memcpy(buf, pos, size);
- pos += size;
- return size;
-}
-
void Memory::check_mode(Mode m) const
{
if(m==M_WRITE)
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;
};
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()
#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)
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
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
inline PollEvent operator~(PollEvent e)
{ return PollEvent(~static_cast<int>(e)); }
+
class Poller
{
public:
Slot(Base *o, PollEvent e): object(o), events(e) { }
};
- typedef std::list<Slot> 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<Slot> SlotSeq;
private:
typedef std::map<Base *, Slot> SlotMap;
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);
close();
}
+void Serial::close()
+{
+#ifdef WIN32
+ CloseHandle(handle);
+#else
+ ::close(handle);
+#endif
+}
+
void Serial::set_block(bool b)
{
if(b)
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)
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
Serial(const std::string &);
virtual ~Serial();
+private:
+ void close();
+
+public:
virtual void set_block(bool);
void set_baud_rate(unsigned);
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
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