Hide win32-specific stuff in Pipe behind pimpl
[libs/core.git] / source / io / pipe.cpp
index afa0c259bdc3655195ccca692439774f8f967d8b..2975a0635074e981aea9dc909cdef5f872d3ca36 100644 (file)
@@ -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