comparison osx/include/kj/io.h @ 62:0994c39f1e94

Cap'n Proto v0.6 + build for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 22 May 2017 10:01:37 +0100
parents 3ab5a40c4e3b
children
comparison
equal deleted inserted replaced
61:d101c4099725 62:0994c39f1e94
302 KJ_DISALLOW_COPY(FdInputStream); 302 KJ_DISALLOW_COPY(FdInputStream);
303 ~FdInputStream() noexcept(false); 303 ~FdInputStream() noexcept(false);
304 304
305 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; 305 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
306 306
307 inline int getFd() const { return fd; }
308
307 private: 309 private:
308 int fd; 310 int fd;
309 AutoCloseFd autoclose; 311 AutoCloseFd autoclose;
310 }; 312 };
311 313
319 ~FdOutputStream() noexcept(false); 321 ~FdOutputStream() noexcept(false);
320 322
321 void write(const void* buffer, size_t size) override; 323 void write(const void* buffer, size_t size) override;
322 void write(ArrayPtr<const ArrayPtr<const byte>> pieces) override; 324 void write(ArrayPtr<const ArrayPtr<const byte>> pieces) override;
323 325
326 inline int getFd() const { return fd; }
327
324 private: 328 private:
325 int fd; 329 int fd;
326 AutoCloseFd autoclose; 330 AutoCloseFd autoclose;
327 }; 331 };
328 332
333 // =======================================================================================
334 // Win32 Handle I/O
335
336 #ifdef _WIN32
337
338 class AutoCloseHandle {
339 // A wrapper around a Win32 HANDLE which automatically closes the handle when destroyed.
340 // The wrapper supports move construction for transferring ownership of the handle. If
341 // CloseHandle() returns an error, the destructor throws an exception, UNLESS the destructor is
342 // being called during unwind from another exception, in which case the close error is ignored.
343 //
344 // If your code is not exception-safe, you should not use AutoCloseHandle. In this case you will
345 // have to call close() yourself and handle errors appropriately.
346
347 public:
348 inline AutoCloseHandle(): handle((void*)-1) {}
349 inline AutoCloseHandle(decltype(nullptr)): handle((void*)-1) {}
350 inline explicit AutoCloseHandle(void* handle): handle(handle) {}
351 inline AutoCloseHandle(AutoCloseHandle&& other) noexcept: handle(other.handle) {
352 other.handle = (void*)-1;
353 }
354 KJ_DISALLOW_COPY(AutoCloseHandle);
355 ~AutoCloseHandle() noexcept(false);
356
357 inline AutoCloseHandle& operator=(AutoCloseHandle&& other) {
358 AutoCloseHandle old(kj::mv(*this));
359 handle = other.handle;
360 other.handle = (void*)-1;
361 return *this;
362 }
363
364 inline AutoCloseHandle& operator=(decltype(nullptr)) {
365 AutoCloseHandle old(kj::mv(*this));
366 return *this;
367 }
368
369 inline operator void*() const { return handle; }
370 inline void* get() const { return handle; }
371
372 operator bool() const = delete;
373 // Deleting this operator prevents accidental use in boolean contexts, which
374 // the void* conversion operator above would otherwise allow.
375
376 inline bool operator==(decltype(nullptr)) { return handle != (void*)-1; }
377 inline bool operator!=(decltype(nullptr)) { return handle == (void*)-1; }
378
379 private:
380 void* handle; // -1 (aka INVALID_HANDLE_VALUE) if not valid.
381 };
382
383 class HandleInputStream: public InputStream {
384 // An InputStream wrapping a Win32 HANDLE.
385
386 public:
387 explicit HandleInputStream(void* handle): handle(handle) {}
388 explicit HandleInputStream(AutoCloseHandle handle): handle(handle), autoclose(mv(handle)) {}
389 KJ_DISALLOW_COPY(HandleInputStream);
390 ~HandleInputStream() noexcept(false);
391
392 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
393
394 private:
395 void* handle;
396 AutoCloseHandle autoclose;
397 };
398
399 class HandleOutputStream: public OutputStream {
400 // An OutputStream wrapping a Win32 HANDLE.
401
402 public:
403 explicit HandleOutputStream(void* handle): handle(handle) {}
404 explicit HandleOutputStream(AutoCloseHandle handle): handle(handle), autoclose(mv(handle)) {}
405 KJ_DISALLOW_COPY(HandleOutputStream);
406 ~HandleOutputStream() noexcept(false);
407
408 void write(const void* buffer, size_t size) override;
409
410 private:
411 void* handle;
412 AutoCloseHandle autoclose;
413 };
414
415 #endif // _WIN32
416
329 } // namespace kj 417 } // namespace kj
330 418
331 #endif // KJ_IO_H_ 419 #endif // KJ_IO_H_