Adding 'compound assignment union operator' for folly::WriteFlags enum class
authorMaxim Georgiev <maxgeorg@fb.com>
Wed, 18 Jan 2017 22:30:57 +0000 (14:30 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 18 Jan 2017 22:32:57 +0000 (14:32 -0800)
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
folly/io/async/AsyncTransport.h
folly/io/async/test/WriteFlagsTest.cpp [new file with mode: 0644]

index 6395ef5..db62312 100644 (file)
@@ -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_);
index f686d95..8af868f 100644 (file)
@@ -71,6 +71,14 @@ inline WriteFlags operator|(WriteFlags a, WriteFlags b) {
     static_cast<uint32_t>(a) | static_cast<uint32_t>(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<uint32_t>(a) & static_cast<uint32_t>(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 (file)
index 0000000..0ae9fd9
--- /dev/null
@@ -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 <folly/io/async/AsyncTransport.h>
+
+//#include <folly/io/async/test/MockAsyncTransport.h>
+#include <folly/portability/GTest.h>
+
+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<WriteFlags>(
+      static_cast<uint32_t>(WriteFlags::CORK) |
+      static_cast<uint32_t>(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<WriteFlags>(
+      static_cast<uint32_t>(WriteFlags::CORK) |
+      static_cast<uint32_t>(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