From 31e72f50fbb34d86877e5110401c49ce3fefd4bb Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 11 Jun 2011 14:55:46 +0300 Subject: [PATCH] Use the new Handle class to hide platform details from public headers --- source/io/base.h | 9 +++---- source/io/buffered.cpp | 3 ++- source/io/buffered.h | 2 +- source/io/console.cpp | 50 ++++++++++++++++++--------------------- source/io/console.h | 3 ++- source/io/file.cpp | 43 +++++++++++++++++----------------- source/io/file.h | 3 ++- source/io/memory.cpp | 3 ++- source/io/memory.h | 2 +- source/io/pipe.cpp | 53 +++++++++++++++++++++++------------------- source/io/pipe.h | 3 ++- source/io/poll.cpp | 10 ++++---- source/io/serial.cpp | 45 +++++++++++++++++------------------ source/io/serial.h | 3 ++- source/io/types.h | 22 ------------------ 15 files changed, 122 insertions(+), 132 deletions(-) delete mode 100644 source/io/types.h diff --git a/source/io/base.h b/source/io/base.h index 774d034..df63b5b 100644 --- a/source/io/base.h +++ b/source/io/base.h @@ -4,11 +4,12 @@ #include #include "mode.h" #include "poll.h" -#include "types.h" namespace Msp { namespace IO { +struct Handle; + /** Common interface for all I/O objects. @@ -64,8 +65,8 @@ public: Mode get_mode() const { return mode; } protected: - virtual unsigned do_write(const char *, unsigned) =0; - virtual unsigned do_read(char *, unsigned) =0; + 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 @@ -106,7 +107,7 @@ public: /** Returns a handle for polling. Should throw if the object does not have an event handle. */ - virtual Handle get_event_handle() =0; + virtual const Handle &get_event_handle() = 0; /** Notifies the object of an event. Used by EventDispatcher. */ void event(PollEvent); diff --git a/source/io/buffered.cpp b/source/io/buffered.cpp index 5dbd494..b2d48e6 100644 --- a/source/io/buffered.cpp +++ b/source/io/buffered.cpp @@ -1,6 +1,7 @@ #include #include #include "buffered.h" +#include "handle.h" using namespace std; @@ -179,7 +180,7 @@ unsigned Buffered::get_current_size() const return end-begin; } -Handle Buffered::get_event_handle() +const Handle &Buffered::get_event_handle() { throw logic_error("Buffered doesn't support events"); } diff --git a/source/io/buffered.h b/source/io/buffered.h index 36d2a11..1de54d2 100644 --- a/source/io/buffered.h +++ b/source/io/buffered.h @@ -37,7 +37,7 @@ public: Mode get_current_op() const { return cur_op; } unsigned get_current_size() const; - virtual Handle get_event_handle(); + virtual const Handle &get_event_handle(); }; } // namespace IO diff --git a/source/io/console.cpp b/source/io/console.cpp index 32b37ef..a0e0c28 100644 --- a/source/io/console.cpp +++ b/source/io/console.cpp @@ -6,6 +6,7 @@ #endif #include #include "console.h" +#include "handle_private.h" using namespace std; @@ -30,15 +31,15 @@ Console::Console(unsigned n) #ifdef WIN32 switch(n) { - case 0: handle = GetStdHandle(STD_INPUT_HANDLE); break; - case 1: handle = GetStdHandle(STD_OUTPUT_HANDLE); break; - case 2: handle = GetStdHandle(STD_ERROR_HANDLE); break; + case 0: *handle = GetStdHandle(STD_INPUT_HANDLE); break; + case 1: *handle = GetStdHandle(STD_OUTPUT_HANDLE); break; + case 2: *handle = GetStdHandle(STD_ERROR_HANDLE); break; } #else - handle = n; + *handle = n; - if(handle==0) - tcgetattr(handle, &orig_attr); + if(n==0) + tcgetattr(*handle, &orig_attr); #endif if(n==0) @@ -49,7 +50,7 @@ Console::~Console() { #ifndef WIN32 if(handle==0) - tcsetattr(handle, TCSADRAIN, &orig_attr); + tcsetattr(*handle, TCSADRAIN, &orig_attr); #endif } @@ -59,9 +60,9 @@ void Console::set_block(bool b) // XXX Dunno how to do this in win32 (void)b; #else - int flags = fcntl(0, F_GETFL); + int flags = fcntl(*handle, F_GETFL); flags = (flags&~O_NONBLOCK) | (b?0:O_NONBLOCK); - fcntl(0, F_SETFL, flags); + fcntl(*handle, F_SETFL, flags); #endif } @@ -72,13 +73,13 @@ void Console::set_local_echo(bool e) #ifdef WIN32 DWORD m; - GetConsoleMode(handle, &m); - SetConsoleMode(handle, (m&~ENABLE_ECHO_INPUT) | (e?ENABLE_ECHO_INPUT:0)); + GetConsoleMode(*handle, &m); + SetConsoleMode(*handle, (m&~ENABLE_ECHO_INPUT) | (e?ENABLE_ECHO_INPUT:0)); #else termios t; - tcgetattr(0, &t); + tcgetattr(*handle, &t); t.c_lflag = (t.c_lflag&~ECHO) | (e?ECHO:0); - tcsetattr(0, TCSADRAIN, &t); + tcsetattr(*handle, TCSADRAIN, &t); #endif } @@ -89,14 +90,14 @@ void Console::set_line_buffer(bool l) #ifdef WIN32 DWORD m; - GetConsoleMode(handle, &m); - SetConsoleMode(handle, (m&~ENABLE_LINE_INPUT) | (l?ENABLE_LINE_INPUT:0)); + GetConsoleMode(*handle, &m); + SetConsoleMode(*handle, (m&~ENABLE_LINE_INPUT) | (l?ENABLE_LINE_INPUT:0)); #else // XXX ICANON does more than just set line buffering, may need a bit more thought termios t; - tcgetattr(0, &t); + tcgetattr(*handle, &t); t.c_lflag = (t.c_lflag&~ICANON) | (l?ICANON:0); - tcsetattr(0, TCSADRAIN, &t); + tcsetattr(*handle, TCSADRAIN, &t); #endif } @@ -111,7 +112,7 @@ void Console::get_size(unsigned &rows, unsigned &cols) cols = 80; #else struct winsize wsz; - ioctl(handle, TIOCGWINSZ, &wsz); + ioctl(*handle, TIOCGWINSZ, &wsz); rows = wsz.ws_row; cols = wsz.ws_col; #endif @@ -124,10 +125,10 @@ unsigned Console::do_write(const char *buf, unsigned len) #ifdef WIN32 DWORD ret; - if(!WriteFile(handle, buf, len, &ret, 0)) + if(!WriteFile(*handle, buf, len, &ret, 0)) throw system_error("WriteFile"); #else - int ret = ::write(handle, buf, len); + int ret = ::write(*handle, buf, len); if(ret==-1) throw system_error("write"); #endif @@ -142,10 +143,10 @@ unsigned Console::do_read(char *buf, unsigned len) #ifdef WIN32 DWORD ret; - if(!ReadFile(handle, buf, len, &ret, 0)) + if(!ReadFile(*handle, buf, len, &ret, 0)) throw system_error("ReadFile"); #else - int ret = ::read(handle, buf, len); + int ret = ::read(*handle, buf, len); if(ret==-1) { if(errno==EAGAIN) @@ -160,11 +161,6 @@ 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/io/console.h b/source/io/console.h index 3c0bb71..5477432 100644 --- a/source/io/console.h +++ b/source/io/console.h @@ -2,6 +2,7 @@ #define MSP_IO_CONSOLE_H_ #include "base.h" +#include "handle.h" namespace Msp { namespace IO { @@ -41,7 +42,7 @@ protected: virtual unsigned do_read(char *, unsigned); public: - virtual Handle get_event_handle(); + virtual const Handle &get_event_handle() { return handle; } static Console &instance(unsigned); }; diff --git a/source/io/file.cpp b/source/io/file.cpp index 74930ba..25e3b4a 100644 --- a/source/io/file.cpp +++ b/source/io/file.cpp @@ -3,9 +3,10 @@ #include #include #endif +#include #include -#include #include "file.h" +#include "handle_private.h" using namespace std; @@ -40,8 +41,8 @@ File::File(const string &fn, Mode m, CreateMode cm) } } - handle = CreateFile(fn.c_str(), flags, 0, 0, create_flags, FILE_ATTRIBUTE_NORMAL, 0); - if(handle==INVALID_HANDLE_VALUE) + *handle = CreateFile(fn.c_str(), flags, 0, 0, create_flags, FILE_ATTRIBUTE_NORMAL, 0); + if(!handle) { int err = GetLastError(); if(err==ERROR_FILE_NOT_FOUND) @@ -71,8 +72,8 @@ File::File(const string &fn, Mode m, CreateMode cm) if(mode&M_NONBLOCK) flags |= O_NONBLOCK; - handle = ::open(fn.c_str(), flags, 0666); - if(handle==-1) + *handle = ::open(fn.c_str(), flags, 0666); + if(!handle) { int err = errno; if(err==ENOENT) @@ -92,7 +93,7 @@ File::~File() void File::close() { - if(handle==MSP_IO_INVALID_HANDLE) + if(!handle) return; set_events(P_NONE); @@ -100,12 +101,12 @@ void File::close() signal_flush_required.emit(); #ifdef WIN32 - CloseHandle(handle); + CloseHandle(*handle); #else - ::close(handle); + ::close(*handle); #endif - handle = MSP_IO_INVALID_HANDLE; + handle = Handle(); signal_closed.emit(); } @@ -117,8 +118,8 @@ void File::set_block(bool b) if(b) mode = (mode|M_NONBLOCK); #ifndef WIN32 - int flags = fcntl(handle, F_GETFD); - fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); + int flags = fcntl(*handle, F_GETFD); + fcntl(*handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); #endif } @@ -133,10 +134,10 @@ unsigned File::do_write(const char *buf, unsigned size) if(mode&M_APPEND) seek(0, S_END); DWORD ret; - if(WriteFile(handle, buf, size, &ret, 0)==0) + if(WriteFile(*handle, buf, size, &ret, 0)==0) throw system_error("WriteFile"); #else - int ret = ::write(handle, buf, size); + int ret = ::write(*handle, buf, size); if(ret==-1) { if(errno==EAGAIN) @@ -158,10 +159,10 @@ unsigned File::do_read(char *buf, unsigned size) #ifdef WIN32 DWORD ret; - if(ReadFile(handle, buf, size, &ret, 0)==0) + if(ReadFile(*handle, buf, size, &ret, 0)==0) throw system_error("ReadFile"); #else - int ret = ::read(handle, buf, size); + int ret = ::read(*handle, buf, size); if(ret==-1) { if(errno==EAGAIN) @@ -185,7 +186,7 @@ void File::sync() #ifndef WIN32 signal_flush_required.emit(); - fsync(handle); + fsync(*handle); #endif } @@ -197,11 +198,11 @@ unsigned File::seek(int off, SeekType st) int type = sys_seek_type(st); #ifdef WIN32 - DWORD ret = SetFilePointer(handle, off, 0, type); + DWORD ret = SetFilePointer(*handle, off, 0, type); if(ret==INVALID_SET_FILE_POINTER) throw system_error("SetFilePointer"); #else - off_t ret = lseek(handle, off, type); + off_t ret = lseek(*handle, off, type); if(ret==(off_t)-1) throw system_error("lseek"); #endif @@ -216,11 +217,11 @@ unsigned File::tell() const check_access(M_NONE); #ifdef WIN32 - DWORD ret = SetFilePointer(handle, 0, 0, FILE_CURRENT); + DWORD ret = SetFilePointer(*handle, 0, 0, FILE_CURRENT); if(ret==INVALID_SET_FILE_POINTER) throw system_error("SetFilePointer"); #else - off_t ret = lseek(handle, 0, SEEK_CUR); + off_t ret = lseek(*handle, 0, SEEK_CUR); if(ret==(off_t)-1) throw system_error("lseek"); #endif @@ -230,7 +231,7 @@ unsigned File::tell() const void File::check_access(Mode m) const { - if(handle==MSP_IO_INVALID_HANDLE || (m && !(mode&m))) + if(!handle || (m && !(mode&m))) throw invalid_access(m); } diff --git a/source/io/file.h b/source/io/file.h index d59cd57..57dc683 100644 --- a/source/io/file.h +++ b/source/io/file.h @@ -5,6 +5,7 @@ #include #include "buffered.h" #include "filtered.h" +#include "handle.h" #include "seekable.h" namespace Msp { @@ -58,7 +59,7 @@ public: virtual unsigned seek(int, SeekType); virtual unsigned tell() const; - virtual Handle get_event_handle() { return handle; } + virtual const Handle &get_event_handle() { return handle; } private: void check_access(Mode) const; diff --git a/source/io/memory.cpp b/source/io/memory.cpp index 79956cd..f89d9fc 100644 --- a/source/io/memory.cpp +++ b/source/io/memory.cpp @@ -1,5 +1,6 @@ #include #include +#include "handle.h" #include "memory.h" using namespace std; @@ -106,7 +107,7 @@ unsigned Memory::seek(int off, SeekType type) return pos-begin; } -Handle Memory::get_event_handle() +const Handle &Memory::get_event_handle() { throw logic_error("Memory doesn't support events"); } diff --git a/source/io/memory.h b/source/io/memory.h index 27fd6cb..870edcf 100644 --- a/source/io/memory.h +++ b/source/io/memory.h @@ -31,7 +31,7 @@ public: virtual unsigned seek(int, SeekType); virtual unsigned tell() const { return pos-begin; } - virtual Handle get_event_handle(); + virtual const Handle &get_event_handle(); private: void check_mode(Mode) const; diff --git a/source/io/pipe.cpp b/source/io/pipe.cpp index 2975a06..1b275e7 100644 --- a/source/io/pipe.cpp +++ b/source/io/pipe.cpp @@ -4,6 +4,7 @@ #endif #include #include +#include "handle_private.h" #include "pipe.h" using namespace std; @@ -29,28 +30,32 @@ Pipe::Pipe(): { #ifdef WIN32 string name = format("\\\\.\\pipe\\%u.%p", GetCurrentProcessId(), this); - handle[0] = CreateNamedPipe(name.c_str(), PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 1024, 1024, 0, 0); - if(handle[0]==INVALID_HANDLE_VALUE) + *handle[0] = CreateNamedPipe(name.c_str(), PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 1024, 1024, 0, 0); + if(!handle[0]) throw system_error("CreateNamedPipe"); - handle[1] = CreateFile(name.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); - if(handle[1]==INVALID_HANDLE_VALUE) + *handle[1] = CreateFile(name.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); + if(!handle[1]) { unsigned err = GetLastError(); - CloseHandle(handle[0]); + CloseHandle(*handle[0]); throw system_error(format("CreateFile(%s)", name), err); } priv = new Private; priv->overlapped = 0; - priv->event = CreateEvent(0, true, false, 0); + *priv->event = CreateEvent(0, true, false, 0); priv->buf_size = 1024; priv->buffer = new char[priv->buf_size]; priv->buf_avail = 0; priv->buf_next = priv->buffer; #else - if(pipe(handle)==-1) + int pipe_fd[2]; + if(pipe(pipe_fd)==-1) throw system_error("pipe"); + + *handle[0] = pipe_fd[0]; + *handle[1] = pipe_fd[1]; #endif set_events(P_INPUT); @@ -60,7 +65,7 @@ Pipe::~Pipe() { close(); #ifdef WIN32 - CloseHandle(priv->event); + CloseHandle(*priv->event); delete priv->buffer; #endif delete priv; @@ -72,11 +77,11 @@ void Pipe::close() signal_flush_required.emit(); #ifdef WIN32 - CloseHandle(handle[0]); - CloseHandle(handle[1]); + CloseHandle(*handle[0]); + CloseHandle(*handle[1]); #else - ::close(handle[0]); - ::close(handle[1]); + ::close(*handle[0]); + ::close(*handle[1]); signal_closed.emit(); #endif } @@ -88,10 +93,10 @@ void Pipe::set_block(bool 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)); + 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 } @@ -102,10 +107,10 @@ unsigned Pipe::do_write(const char *buf, unsigned size) #ifdef WIN32 DWORD ret; - if(!WriteFile(handle[1], buf, size, &ret, 0)) + if(!WriteFile(*handle[1], buf, size, &ret, 0)) throw system_error("WriteFile"); #else - int ret = ::write(handle[1], buf, size); + int ret = ::write(*handle[1], buf, size); if(ret==-1) { if(errno==EAGAIN) @@ -130,7 +135,7 @@ unsigned Pipe::do_read(char *buf, unsigned size) if(priv->overlapped) { DWORD ret; - if(!GetOverlappedResult(handle[0], priv->overlapped, &ret, !priv->buf_avail)) + if(!GetOverlappedResult(*handle[0], priv->overlapped, &ret, !priv->buf_avail)) throw system_error("GetOverlappedResult"); else { @@ -148,7 +153,7 @@ unsigned Pipe::do_read(char *buf, unsigned size) // Initiate another overlapped read in case someone is polling us get_event_handle(); #else - int ret = ::read(handle[0], buf, size); + int ret = ::read(*handle[0], buf, size); if(ret==-1) { if(errno==EAGAIN) @@ -167,18 +172,18 @@ unsigned Pipe::do_read(char *buf, unsigned size) return ret; } -Handle Pipe::get_event_handle() +const Handle &Pipe::get_event_handle() { #ifdef WIN32 if(!priv->overlapped && !priv->buf_avail) { priv->overlapped = new OVERLAPPED; memset(priv->overlapped, 0, sizeof(OVERLAPPED)); - priv->overlapped->hEvent = priv->event; + priv->overlapped->hEvent = *priv->event; DWORD ret; priv->buf_next = priv->buffer; - if(!ReadFile(handle[0], priv->buffer, priv->buf_size, &ret, priv->overlapped)) + if(!ReadFile(*handle[0], priv->buffer, priv->buf_size, &ret, priv->overlapped)) { unsigned err = GetLastError(); if(err!=ERROR_IO_PENDING) @@ -189,7 +194,7 @@ Handle Pipe::get_event_handle() priv->buf_avail = ret; delete priv->overlapped; priv->overlapped = 0; - SetEvent(priv->event); + SetEvent(*priv->event); } } diff --git a/source/io/pipe.h b/source/io/pipe.h index 4e4e42b..a2a7d0f 100644 --- a/source/io/pipe.h +++ b/source/io/pipe.h @@ -2,6 +2,7 @@ #define MSP_IO_PIPE_H_ #include "base.h" +#include "handle.h" namespace Msp { namespace IO { @@ -27,7 +28,7 @@ protected: virtual unsigned do_read(char *, unsigned); public: - virtual Handle get_event_handle(); + virtual const Handle &get_event_handle(); }; } // namespace IO diff --git a/source/io/poll.cpp b/source/io/poll.cpp index d78a2c0..1ca0987 100644 --- a/source/io/poll.cpp +++ b/source/io/poll.cpp @@ -7,6 +7,8 @@ #include #include #include "base.h" +#include "handle.h" +#include "handle_private.h" #include "poll.h" using namespace std; @@ -61,7 +63,7 @@ inline PollEvent do_poll(Base &obj, PollEvent pe, int timeout) if(timeout<0) timeout = INFINITE; - DWORD ret = WaitForSingleObject(obj.get_event_handle(), timeout); + DWORD ret = WaitForSingleObject(*obj.get_event_handle(), timeout); if(ret==WAIT_OBJECT_0) return pe; else if(ret==WAIT_FAILED) @@ -69,7 +71,7 @@ inline PollEvent do_poll(Base &obj, PollEvent pe, int timeout) return P_NONE; #else - pollfd pfd = { obj.get_event_handle(), sys_poll_event(pe), 0 }; + pollfd pfd = { *obj.get_event_handle(), sys_poll_event(pe), 0 }; int ret = ::poll(&pfd, 1, timeout); if(ret==-1) @@ -152,14 +154,14 @@ void Poller::rebuild_array() priv->handles.clear(); for(EventMap::iterator i=objects.begin(); i!=objects.end(); ++i) - priv->handles.push_back(i->first->get_event_handle()); + priv->handles.push_back(*i->first->get_event_handle()); #else priv->pfd.clear(); for(EventMap::iterator i=objects.begin(); i!=objects.end(); ++i) { pollfd p; - p.fd = i->first->get_event_handle(); + p.fd = *i->first->get_event_handle(); p.events = sys_poll_event(i->second); priv->pfd.push_back(p); } diff --git a/source/io/serial.cpp b/source/io/serial.cpp index 5d5871e..9df7971 100644 --- a/source/io/serial.cpp +++ b/source/io/serial.cpp @@ -7,6 +7,7 @@ #endif #include #include +#include "handle_private.h" #include "serial.h" using namespace std; @@ -22,22 +23,22 @@ typedef DCB DeviceState; typedef termios DeviceState; #endif -void get_state(Handle handle, DeviceState &state) +void get_state(const Handle &handle, DeviceState &state) { #ifdef WIN32 - GetCommState(handle, &state); + GetCommState(*handle, &state); #else - tcgetattr(handle, &state); + tcgetattr(*handle, &state); #endif } -void set_state(Handle handle, DeviceState &state) +void set_state(const Handle &handle, DeviceState &state) { #ifdef WIN32 - if(SetCommState(handle, &state)==0) + if(SetCommState(*handle, &state)==0) throw system_error("SetCommState"); #else - if(tcsetattr(handle, TCSADRAIN, &state)==-1) + if(tcsetattr(*handle, TCSADRAIN, &state)==-1) throw system_error("tcsetattr"); #endif } @@ -156,8 +157,8 @@ Serial::Serial(const string &descr) #ifdef WIN32 port = "\\\\.\\"+port; - handle = CreateFile(port.c_str(), GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); - if(handle==INVALID_HANDLE_VALUE) + *handle = CreateFile(port.c_str(), GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); + if(!handle) throw system_error(format("CreateFile(%s)", port)); mode = M_READ|M_WRITE; @@ -167,21 +168,21 @@ Serial::Serial(const string &descr) timeouts.ReadTotalTimeoutConstant = MAXDWORD-1; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = 0; - SetCommTimeouts(handle, &timeouts); + SetCommTimeouts(*handle, &timeouts); #else if(port.compare(0, 5, "/dev/")) port = "/dev/"+port; - handle = open(port.c_str(), O_RDWR); - if(handle==-1) + *handle = open(port.c_str(), O_RDWR); + if(!handle) throw system_error(format("open(%s)", port)); mode = M_READ|M_WRITE; termios t; - tcgetattr(handle, &t); + tcgetattr(*handle, &t); t.c_lflag &= ~(ECHO|ICANON); t.c_oflag &= ~OPOST; - tcsetattr(handle, TCSADRAIN, &t); + tcsetattr(*handle, TCSADRAIN, &t); #endif if(comma!=string::npos) @@ -208,9 +209,9 @@ Serial::~Serial() void Serial::close() { #ifdef WIN32 - CloseHandle(handle); + CloseHandle(*handle); #else - ::close(handle); + ::close(*handle); #endif } @@ -222,8 +223,8 @@ void Serial::set_block(bool b) mode = mode&~M_NONBLOCK; #ifndef WIN32 - int flags = fcntl(handle, F_GETFD); - fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); + int flags = fcntl(*handle, F_GETFD); + fcntl(*handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); #endif } @@ -288,10 +289,10 @@ unsigned Serial::do_write(const char *buf, unsigned size) #ifdef WIN32 DWORD ret; - if(WriteFile(handle, buf, size, &ret, 0)==0) + if(WriteFile(*handle, buf, size, &ret, 0)==0) throw system_error("WriteFile"); #else - int ret = ::write(handle, buf, size); + int ret = ::write(*handle, buf, size); if(ret==-1) { if(errno==EAGAIN) @@ -311,10 +312,10 @@ unsigned Serial::do_read(char *buf, unsigned size) #ifdef WIN32 DWORD ret; - if(ReadFile(handle, buf, size, &ret, 0)==0) + if(ReadFile(*handle, buf, size, &ret, 0)==0) throw system_error("ReadFile"); #else - int ret = ::read(handle, buf, size); + int ret = ::read(*handle, buf, size); if(ret==-1) { if(errno==EAGAIN) @@ -327,7 +328,7 @@ unsigned Serial::do_read(char *buf, unsigned size) return ret; } -Handle Serial::get_event_handle() +const Handle &Serial::get_event_handle() { #ifdef WIN32 throw logic_error("Serial port events not supported on win32 yet"); diff --git a/source/io/serial.h b/source/io/serial.h index 069789c..dba62fb 100644 --- a/source/io/serial.h +++ b/source/io/serial.h @@ -2,6 +2,7 @@ #define MSP_IO_SERIAL_H_ #include "base.h" +#include "handle.h" namespace Msp { namespace IO { @@ -40,7 +41,7 @@ private: virtual unsigned do_read(char *, unsigned); public: - virtual Handle get_event_handle(); + virtual const Handle &get_event_handle(); }; } // namespace IO diff --git a/source/io/types.h b/source/io/types.h deleted file mode 100644 index f18b7cb..0000000 --- a/source/io/types.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef MSP_IO_TYPES_H_ -#define MSP_IO_TYPES_H_ - -#ifdef WIN32 -#include -#endif - -namespace Msp { -namespace IO { - -#ifdef WIN32 -typedef HANDLE Handle; -#define MSP_IO_INVALID_HANDLE INVALID_HANDLE_VALUE -#else -typedef int Handle; -#define MSP_IO_INVALID_HANDLE -1 -#endif - -} // namespace IO -} // namespace Msp - -#endif -- 2.43.0