From 74261e41a1b94490607457e38b3b1c54cf6374a4 Mon Sep 17 00:00:00 2001 From: Maxim Georgiev Date: Wed, 18 Jan 2017 14:30:57 -0800 Subject: [PATCH] Adding 'compound assignment union operator' for folly::WriteFlags enum class Summary: There is "operator|()" defined for folly::WriteFlags enum class, but no "operator=|()". Because of that adding a new flag to an existing set of flags looks awkward. Adding the missing operator. Reviewed By: yfeldblum Differential Revision: D4421892 fbshipit-source-id: a053a8cc5b1d07579c233d19029a9801b8188413 --- folly/io/async/AsyncSocket.cpp | 2 +- folly/io/async/AsyncTransport.h | 16 ++++ folly/io/async/test/WriteFlagsTest.cpp | 112 +++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 folly/io/async/test/WriteFlagsTest.cpp diff --git a/folly/io/async/AsyncSocket.cpp b/folly/io/async/AsyncSocket.cpp index 6395ef58..db623129 100644 --- a/folly/io/async/AsyncSocket.cpp +++ b/folly/io/async/AsyncSocket.cpp @@ -93,7 +93,7 @@ class AsyncSocket::BytesWriteRequest : public AsyncSocket::WriteRequest { WriteResult performWrite() override { WriteFlags writeFlags = flags_; if (getNext() != nullptr) { - writeFlags = writeFlags | WriteFlags::CORK; + writeFlags |= WriteFlags::CORK; } auto writeResult = socket_->performWrite( getOps(), getOpCount(), writeFlags, &opsWritten_, &partialBytes_); diff --git a/folly/io/async/AsyncTransport.h b/folly/io/async/AsyncTransport.h index f686d95f..8af868f7 100644 --- a/folly/io/async/AsyncTransport.h +++ b/folly/io/async/AsyncTransport.h @@ -71,6 +71,14 @@ inline WriteFlags operator|(WriteFlags a, WriteFlags b) { static_cast(a) | static_cast(b)); } +/* + * compound assignment union operator + */ +inline WriteFlags& operator|=(WriteFlags& a, WriteFlags b) { + a = a | b; + return a; +} + /* * intersection operator */ @@ -79,6 +87,14 @@ inline WriteFlags operator&(WriteFlags a, WriteFlags b) { static_cast(a) & static_cast(b)); } +/* + * compound assignment intersection operator + */ +inline WriteFlags& operator&=(WriteFlags& a, WriteFlags b) { + a = a & b; + return a; +} + /* * exclusion parameter */ diff --git a/folly/io/async/test/WriteFlagsTest.cpp b/folly/io/async/test/WriteFlagsTest.cpp new file mode 100644 index 00000000..0ae9fd95 --- /dev/null +++ b/folly/io/async/test/WriteFlagsTest.cpp @@ -0,0 +1,112 @@ +/* + * Copyright 2017 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include + +//#include +#include + +using namespace testing; + +namespace folly { + +TEST(WriteFlagsTest, isSet) { + WriteFlags flags = WriteFlags::NONE; + ASSERT_FALSE(isSet(flags, WriteFlags::CORK)); + ASSERT_FALSE(isSet(flags, WriteFlags::EOR)); + + flags = WriteFlags::CORK; + ASSERT_TRUE(isSet(flags, WriteFlags::CORK)); + ASSERT_FALSE(isSet(flags, WriteFlags::EOR)); + + flags = static_cast( + static_cast(WriteFlags::CORK) | + static_cast(WriteFlags::EOR)); + ASSERT_TRUE(isSet(flags, WriteFlags::CORK)); + ASSERT_TRUE(isSet(flags, WriteFlags::EOR)); +} + +TEST(WriteFlagsTest, unionOperator) { + WriteFlags flags = WriteFlags::CORK | WriteFlags::NONE; + ASSERT_EQ(flags, WriteFlags::CORK); + + flags = static_cast( + static_cast(WriteFlags::CORK) | + static_cast(WriteFlags::EOR)); + ASSERT_EQ(flags, WriteFlags::CORK | WriteFlags::EOR); +} + +TEST(WriteFlagsTest, intersectionOperator) { + ASSERT_EQ(WriteFlags::NONE & WriteFlags::CORK, WriteFlags::NONE); + + WriteFlags flags = + WriteFlags::CORK | WriteFlags::EOR | WriteFlags::WRITE_SHUTDOWN; + ASSERT_EQ(flags & WriteFlags::CORK, WriteFlags::CORK); + ASSERT_EQ(flags & WriteFlags::EOR, WriteFlags::EOR); + ASSERT_EQ(flags & WriteFlags::WRITE_SHUTDOWN, WriteFlags::WRITE_SHUTDOWN); +} + +TEST(WriteFlagsTest, exclusionOperator) { + ASSERT_FALSE(isSet(~WriteFlags::CORK, WriteFlags::CORK)); + ASSERT_TRUE(isSet(~WriteFlags::CORK, WriteFlags::EOR)); + ASSERT_TRUE(isSet(~WriteFlags::CORK, WriteFlags::WRITE_SHUTDOWN)); + + ASSERT_FALSE(isSet(~WriteFlags::EOR, WriteFlags::EOR)); + ASSERT_TRUE(isSet(~WriteFlags::EOR, WriteFlags::CORK)); + ASSERT_TRUE(isSet(~WriteFlags::EOR, WriteFlags::WRITE_SHUTDOWN)); +} + +TEST(WriteFlagsTest, unsetOperator) { + WriteFlags flags = + WriteFlags::CORK | WriteFlags::EOR | WriteFlags::WRITE_SHUTDOWN; + ASSERT_TRUE(isSet(flags, WriteFlags::CORK)); + ASSERT_TRUE(isSet(flags, WriteFlags::EOR)); + ASSERT_TRUE(isSet(flags, WriteFlags::WRITE_SHUTDOWN)); + + flags = WriteFlags::CORK; + ASSERT_TRUE(isSet(flags, WriteFlags::CORK)); + ASSERT_FALSE(isSet(flags, WriteFlags::EOR)); + ASSERT_FALSE(isSet(flags, WriteFlags::WRITE_SHUTDOWN)); +} + +TEST(WriteFlagsTest, compoundAssignmentUnionOperator) { + WriteFlags flags = WriteFlags::NONE; + flags |= WriteFlags::CORK; + ASSERT_EQ(flags, WriteFlags::CORK); + + flags = WriteFlags::CORK; + flags |= WriteFlags::EOR; + ASSERT_EQ(flags, WriteFlags::CORK | WriteFlags::EOR); + + flags = WriteFlags::CORK | WriteFlags::EOR; + flags |= WriteFlags::CORK; + ASSERT_EQ(flags, WriteFlags::CORK | WriteFlags::EOR); +} + +TEST(WriteFlagsTest, compoundAssignmentIntersectionOperator) { + WriteFlags flags = WriteFlags::CORK | WriteFlags::EOR; + flags &= WriteFlags::CORK; + ASSERT_EQ(flags, WriteFlags::CORK); + + flags = WriteFlags::CORK | WriteFlags::EOR; + flags &= WriteFlags::EOR; + ASSERT_EQ(flags, WriteFlags::EOR); + + flags = WriteFlags::NONE; + flags &= WriteFlags::EOR; + ASSERT_EQ(flags, WriteFlags::NONE); +} + +} // namespace -- 2.34.1