]> git.tdb.fi Git - libs/core.git/commitdiff
Use the new Handle class to hide platform details from public headers
authorMikko Rasa <tdb@tdb.fi>
Sat, 11 Jun 2011 11:55:46 +0000 (14:55 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 11 Jun 2011 11:55:46 +0000 (14:55 +0300)
15 files changed:
source/io/base.h
source/io/buffered.cpp
source/io/buffered.h
source/io/console.cpp
source/io/console.h
source/io/file.cpp
source/io/file.h
source/io/memory.cpp
source/io/memory.h
source/io/pipe.cpp
source/io/pipe.h
source/io/poll.cpp
source/io/serial.cpp
source/io/serial.h
source/io/types.h [deleted file]

index 774d03467b57f73b5af04217dcf8e3cae70046d1..df63b5b1ed3ed2d5cfbd4797bc319919fa110794 100644 (file)
@@ -4,11 +4,12 @@
 #include <sigc++/sigc++.h>
 #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);
index 5dbd49463fc2666a39019036c4882231b73b653d..b2d48e660a56f337334db1f2f2decc8dbee96a86 100644 (file)
@@ -1,6 +1,7 @@
 #include <cstring>
 #include <stdexcept>
 #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");
 }
index 36d2a1165a3e52b7413397bf01575984efa3ac49..1de54d202456546cecf9604c01d9a766b061648d 100644 (file)
@@ -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
index 32b37ef745553811fc4a341d55ad7b3789e256da..a0e0c28f07cbbd506683cb92166f3def8b57b93a 100644 (file)
@@ -6,6 +6,7 @@
 #endif
 #include <msp/core/systemerror.h>
 #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);
index 3c0bb71ff3826d23467172a8e86c28f565ae1088..54774323558a905d8dbe416114d932fd5e279d83 100644 (file)
@@ -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);
 };
index 74930baaf497286f32925ee395641147813daf58..25e3b4af115855a6b332e99ebcf947599590cbc8 100644 (file)
@@ -3,9 +3,10 @@
 #include <fcntl.h>
 #include <unistd.h>
 #endif
+#include <msp/strings/format.h>
 #include <msp/core/systemerror.h>
-#include <msp/strings/formatter.h>
 #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);
 }
 
index d59cd577fc0f33e1acbcbb62bcaa9fa0bb31c141..57dc6831449416a0a0e9af75626ac1702757c8ce 100644 (file)
@@ -5,6 +5,7 @@
 #include <string>
 #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;
index 79956cdafecae37a113b7421d10cb30fadd1d2c1..f89d9fc894c26d207e6e3b8300d9f9dc99c6f7ae 100644 (file)
@@ -1,5 +1,6 @@
 #include <algorithm>
 #include <cstring>
+#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");
 }
index 27fd6cbd56aeeb1fda7b9d1260ca667c114c7ce4..870edcf0adf3f6d506a71de57a6c0be52dacc51e 100644 (file)
@@ -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;
index 2975a0635074e981aea9dc909cdef5f872d3ca36..1b275e731667f4736929499aaad7e9a17745343b 100644 (file)
@@ -4,6 +4,7 @@
 #endif
 #include <msp/core/systemerror.h>
 #include <msp/strings/formatter.h>
+#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);
                }
        }
 
index 4e4e42b031796f3588dea1a88a3f4c607f84928f..a2a7d0f99f53ec6fafb69752c2f60886d5365a93 100644 (file)
@@ -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
index d78a2c01c023602bbcdb080abde3c0eab798887e..1ca0987ac10fd0ce7795f5649f667ffc6f178c42 100644 (file)
@@ -7,6 +7,8 @@
 #include <msp/strings/format.h>
 #include <msp/time/units.h>
 #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);
        }
index 5d5871ef74f1f85855e75fb228a6b4e7e8149b68..9df797109d840cc3554136035410977926bc802e 100644 (file)
@@ -7,6 +7,7 @@
 #endif
 #include <msp/strings/format.h>
 #include <msp/core/systemerror.h>
+#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");
index 069789c85097a69e8510d347ca43a02630009a2a..dba62fbf955e5db860f763c2a233cb3e130d75fe 100644 (file)
@@ -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 (file)
index f18b7cb..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-#ifndef MSP_IO_TYPES_H_
-#define MSP_IO_TYPES_H_
-
-#ifdef WIN32
-#include <windows.h>
-#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