From: Mikko Rasa Date: Sat, 11 Jun 2011 11:53:03 +0000 (+0300) Subject: Hide win32-specific stuff in Pipe behind pimpl X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=378ec811c8df431f5b4e7094eb1949476d3a79fe;p=libs%2Fcore.git Hide win32-specific stuff in Pipe behind pimpl --- diff --git a/source/io/pipe.cpp b/source/io/pipe.cpp index afa0c25..2975a06 100644 --- a/source/io/pipe.cpp +++ b/source/io/pipe.cpp @@ -11,7 +11,21 @@ using namespace std; namespace Msp { namespace IO { -Pipe::Pipe() +struct Pipe::Private +{ +#ifdef WIN32 + OVERLAPPED *overlapped; + Handle event; + unsigned buf_size; + char *buffer; + unsigned buf_avail; + char *buf_next; +#endif +}; + + +Pipe::Pipe(): + priv(0) { #ifdef WIN32 string name = format("\\\\.\\pipe\\%u.%p", GetCurrentProcessId(), this); @@ -27,12 +41,13 @@ Pipe::Pipe() throw system_error(format("CreateFile(%s)", name), err); } - overlapped = 0; - event = CreateEvent(0, true, false, 0); - buf_size = 1024; - buffer = new char[buf_size]; - buf_avail = 0; - buf_next = buffer; + priv = new Private; + priv->overlapped = 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) throw system_error("pipe"); @@ -44,6 +59,11 @@ Pipe::Pipe() Pipe::~Pipe() { close(); +#ifdef WIN32 + CloseHandle(priv->event); + delete priv->buffer; +#endif + delete priv; } void Pipe::close() @@ -107,23 +127,23 @@ unsigned Pipe::do_read(char *buf, unsigned size) // Initiate overlapped read if needed get_event_handle(); - if(overlapped) + if(priv->overlapped) { DWORD ret; - if(!GetOverlappedResult(handle[0], overlapped, &ret, !buf_avail)) + if(!GetOverlappedResult(handle[0], priv->overlapped, &ret, !priv->buf_avail)) throw system_error("GetOverlappedResult"); else { - buf_avail += ret; - delete overlapped; - overlapped = 0; + priv->buf_avail += ret; + delete priv->overlapped; + priv->overlapped = 0; } } - unsigned ret = min(buf_avail, size); - memcpy(buf, buf_next, ret); - buf_next += ret; - buf_avail -= ret; + unsigned ret = min(priv->buf_avail, size); + memcpy(buf, priv->buf_next, ret); + priv->buf_next += ret; + priv->buf_avail -= ret; // Initiate another overlapped read in case someone is polling us get_event_handle(); @@ -150,15 +170,15 @@ unsigned Pipe::do_read(char *buf, unsigned size) Handle Pipe::get_event_handle() { #ifdef WIN32 - if(!overlapped && !buf_avail) + if(!priv->overlapped && !priv->buf_avail) { - overlapped = new OVERLAPPED; - memset(overlapped, 0, sizeof(OVERLAPPED)); - overlapped->hEvent = event; + priv->overlapped = new OVERLAPPED; + memset(priv->overlapped, 0, sizeof(OVERLAPPED)); + priv->overlapped->hEvent = priv->event; DWORD ret; - buf_next = buffer; - if(!ReadFile(handle[0], buffer, buf_size, &ret, overlapped)) + priv->buf_next = priv->buffer; + if(!ReadFile(handle[0], priv->buffer, priv->buf_size, &ret, priv->overlapped)) { unsigned err = GetLastError(); if(err!=ERROR_IO_PENDING) @@ -166,14 +186,14 @@ Handle Pipe::get_event_handle() } else { - buf_avail = ret; - delete overlapped; - overlapped = 0; - SetEvent(event); + priv->buf_avail = ret; + delete priv->overlapped; + priv->overlapped = 0; + SetEvent(priv->event); } } - return event; + return priv->event; #else return handle[0]; #endif diff --git a/source/io/pipe.h b/source/io/pipe.h index 71edf7e..4e4e42b 100644 --- a/source/io/pipe.h +++ b/source/io/pipe.h @@ -9,15 +9,10 @@ namespace IO { class Pipe: public Base { private: + struct Private; + Handle handle[2]; -#ifdef WIN32 - OVERLAPPED *overlapped; - Handle event; - unsigned buf_size; - char *buffer; - unsigned buf_avail; - char *buf_next; -#endif + Private *priv; public: Pipe();