From edeb93fa4182984422f70c453ced8ad3acabe1d0 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 2 Dec 2017 16:32:13 +0200 Subject: [PATCH] Add a helper function for adjusting mode flags --- source/io/file.cpp | 2 +- source/io/mode.h | 3 +++ source/io/pipe.cpp | 5 +---- source/io/serial.cpp | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/source/io/file.cpp b/source/io/file.cpp index 1b4a738..d44dad3 100644 --- a/source/io/file.cpp +++ b/source/io/file.cpp @@ -28,7 +28,7 @@ File::~File() void File::set_block(bool b) { - mode = b?(mode&~M_NONBLOCK):(mode|M_NONBLOCK); + adjust_mode(mode, M_NONBLOCK, !b); sys_set_blocking(handle, b); } diff --git a/source/io/mode.h b/source/io/mode.h index 706609f..2e7527d 100644 --- a/source/io/mode.h +++ b/source/io/mode.h @@ -25,6 +25,9 @@ inline Mode operator&(Mode m, Mode n) inline Mode operator~(Mode m) { return Mode(~static_cast(m)); } +inline void adjust_mode(Mode &m, Mode f, bool b) +{ m = b ? (m|f) : (m&~f); } + class invalid_access: public std::logic_error { diff --git a/source/io/pipe.cpp b/source/io/pipe.cpp index 0a72a80..444f8c7 100644 --- a/source/io/pipe.cpp +++ b/source/io/pipe.cpp @@ -42,10 +42,7 @@ void Pipe::set_mode(Mode m) void Pipe::set_block(bool b) { - mode = (mode&~M_NONBLOCK); - if(b) - mode = (mode|M_NONBLOCK); - + adjust_mode(mode, M_NONBLOCK, !b); sys_set_blocking(read_handle, b); sys_set_blocking(write_handle, b); } diff --git a/source/io/serial.cpp b/source/io/serial.cpp index cf2c695..2b3458a 100644 --- a/source/io/serial.cpp +++ b/source/io/serial.cpp @@ -42,7 +42,7 @@ void Serial::close() void Serial::set_block(bool b) { - mode = b?(mode&~M_NONBLOCK):(mode|M_NONBLOCK); + adjust_mode(mode, M_NONBLOCK, !b); sys_set_blocking(handle, b); } -- 2.45.2