about summary refs log tree commit diff
path: root/absl/types
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2018-06-08T15·14-0700
committerGennadiy Civil <misterg@google.com>2018-06-08T15·24-0400
commit92020a042c0cd46979db9f6f0cb32783dc07765e (patch)
tree5f4ebb5a8b0a8c4f0e5e2a061e63665b2071d92f /absl/types
parent7aacab8ae05d7115049923da9cfbf584dc1f8338 (diff)
- abacaab4b11a69dd4db627bd183571d7cabb8def Refinement to previous time.h edit (in this same github p... by Greg Falcon <gfalcon@google.com>
  - 64db19b773134c6c8004e3b23c9ca892efbf8bae Move SpinLock's adaptive spin count computation from a st... by Derek Mauro <dmauro@google.com>
  - 6f9533fb44a52485a7c2bbb9b4efc7bf8d6c359a Import of CCTZ from GitHub. by Abseil Team <absl-team@google.com>
  - a211d7255c986e8dd4ceada362c0d054a6a1969a Cleanup exception flags by Abseil Team <absl-team@google.com>
  - babdb29c590126fe9bba5229fe91034b5b5c358a Release time benchmarks. by Alex Strelnikov <strel@google.com>
  - 5803b32a3ff123d1fb57a0c471d199c818357c9f Release memutil microbenchmarks. by Alex Strelnikov <strel@google.com>
  - 5357d4890d30e80c53beb05af32500fb20e9402b Add parens around expansion of ABSL_PREDICT_{FALSE,TRUE} ... by Abseil Team <absl-team@google.com>
  - 32023f61a239a5f6b1c59e577bfe81b179bbcd2d Reformat build rule tag. by Alex Strelnikov <strel@google.com>
  - 833758ecf2b0cf7a42bbd50b5b127e416425c168 Release uint128 microbenchmarks. by Alex Strelnikov <strel@google.com>
  - c115a9bca1f944b90fdc78a56b2de176466b124f Disambiguate bitwise-not of size_type by Abseil Team <absl-team@google.com>
  - f6905f5b5f6e425792de646edafde440548d9346 Updates ConstructorTracker and TrackedObjects with 1) a m... by Abseil Team <absl-team@google.com>
  - 147c553bdd5d2db20a38f75c4d1ef973d6c709c5 Changes the absl::Duration factory functions to disallow ... by Greg Miller <jgm@google.com>
  - dba2b96d11b5264546b283ba452f2de1303b0f07 White space fix by Alex Strelnikov <strel@google.com>

GitOrigin-RevId: abacaab4b11a69dd4db627bd183571d7cabb8def
Change-Id: I6fa34f20d0b2f898e7b5475a603111413bb80a67
Diffstat (limited to 'absl/types')
-rw-r--r--absl/types/BUILD.bazel15
-rw-r--r--absl/types/optional_exception_safety_test.cc84
-rw-r--r--absl/types/span.h2
-rw-r--r--absl/types/variant_exception_safety_test.cc121
4 files changed, 109 insertions, 113 deletions
diff --git a/absl/types/BUILD.bazel b/absl/types/BUILD.bazel
index c26c68d0b35f..1fc4c098e9bd 100644
--- a/absl/types/BUILD.bazel
+++ b/absl/types/BUILD.bazel
@@ -28,7 +28,7 @@ licenses(["notice"])  # Apache 2.0
 cc_library(
     name = "any",
     hdrs = ["any.h"],
-    copts = ABSL_DEFAULT_COPTS + ABSL_EXCEPTIONS_FLAG,
+    copts = ABSL_DEFAULT_COPTS,
     deps = [
         ":bad_any_cast",
         "//absl/base:config",
@@ -40,9 +40,19 @@ cc_library(
 
 cc_library(
     name = "bad_any_cast",
-    srcs = ["bad_any_cast.cc"],
     hdrs = ["bad_any_cast.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = [":bad_any_cast_impl"],
+)
+
+cc_library(
+    name = "bad_any_cast_impl",
+    srcs = [
+        "bad_any_cast.cc",
+        "bad_any_cast.h",
+    ],
     copts = ABSL_EXCEPTIONS_FLAG + ABSL_DEFAULT_COPTS,
+    visibility = ["//visibility:private"],
     deps = [
         "//absl/base",
         "//absl/base:config",
@@ -206,7 +216,6 @@ cc_test(
     ],
 )
 
-
 cc_library(
     name = "variant",
     srcs = ["internal/variant.h"],
diff --git a/absl/types/optional_exception_safety_test.cc b/absl/types/optional_exception_safety_test.cc
index 7f6348e1b8e1..d2ef04b8d1b0 100644
--- a/absl/types/optional_exception_safety_test.cc
+++ b/absl/types/optional_exception_safety_test.cc
@@ -170,25 +170,22 @@ TEST(OptionalExceptionSafety, EverythingThrowsSwap) {
 
 TEST(OptionalExceptionSafety, NoThrowMoveSwap) {
   // Tests the nothrow guarantee for optional of T with non-throwing move
-  auto nothrow_test =
-      MakeExceptionSafetyTester().WithInvariants(testing::nothrow_guarantee);
-  auto nothrow_test_empty = nothrow_test.WithInitialValue(MoveOptional());
-  auto nothrow_test_nonempty =
-      nothrow_test.WithInitialValue(MoveOptional(kInitialInteger));
-
-  auto swap_empty = [](MoveOptional* optional_ptr) {
+  {
     auto empty = MoveOptional();
-    optional_ptr->swap(empty);
-  };
-  EXPECT_TRUE(nothrow_test_nonempty.Test(swap_empty));
-
-  auto swap_nonempty = [](MoveOptional* optional_ptr) {
-    auto nonempty =
-        MoveOptional(absl::in_place, kUpdatedInteger, testing::nothrow_ctor);
-    optional_ptr->swap(nonempty);
-  };
-  EXPECT_TRUE(nothrow_test_empty.Test(swap_nonempty));
-  EXPECT_TRUE(nothrow_test_nonempty.Test(swap_nonempty));
+    auto nonempty = MoveOptional(kInitialInteger);
+    EXPECT_TRUE(testing::TestNothrowOp([&]() { nonempty.swap(empty); }));
+  }
+  {
+    auto nonempty = MoveOptional(kUpdatedInteger);
+    auto empty = MoveOptional();
+    EXPECT_TRUE(testing::TestNothrowOp([&]() { empty.swap(nonempty); }));
+  }
+  {
+    auto nonempty_from = MoveOptional(kUpdatedInteger);
+    auto nonempty_to = MoveOptional(kInitialInteger);
+    EXPECT_TRUE(
+        testing::TestNothrowOp([&]() { nonempty_to.swap(nonempty_from); }));
+  }
 }
 
 TEST(OptionalExceptionSafety, CopyAssign) {
@@ -251,32 +248,33 @@ TEST(OptionalExceptionSafety, MoveAssign) {
 
 TEST(OptionalExceptionSafety, NothrowMoveAssign) {
   // Tests the nothrow guarantee for optional of T with non-throwing move
-  auto nothrow_test =
-      MakeExceptionSafetyTester().WithInvariants(testing::nothrow_guarantee);
-  auto nothrow_test_empty = nothrow_test.WithInitialValue(MoveOptional());
-  auto nothrow_test_nonempty =
-      nothrow_test.WithInitialValue(MoveOptional(kInitialInteger));
-
-  auto moveassign_empty = [](MoveOptional* optional_ptr) {
+  {
     auto empty = MoveOptional();
-    *optional_ptr = std::move(empty);
-  };
-  EXPECT_TRUE(nothrow_test_nonempty.Test(moveassign_empty));
-
-  auto moveassign_nonempty = [](MoveOptional* optional_ptr) {
-    auto nonempty =
-        MoveOptional(absl::in_place, kUpdatedInteger, testing::nothrow_ctor);
-    *optional_ptr = std::move(nonempty);
-  };
-  EXPECT_TRUE(nothrow_test_empty.Test(moveassign_nonempty));
-  EXPECT_TRUE(nothrow_test_nonempty.Test(moveassign_nonempty));
-
-  auto moveassign_thrower = [](MoveOptional* optional_ptr) {
-    auto thrower = MoveThrower(kUpdatedInteger, testing::nothrow_ctor);
-    *optional_ptr = std::move(thrower);
-  };
-  EXPECT_TRUE(nothrow_test_empty.Test(moveassign_thrower));
-  EXPECT_TRUE(nothrow_test_nonempty.Test(moveassign_thrower));
+    auto nonempty = MoveOptional(kInitialInteger);
+    EXPECT_TRUE(testing::TestNothrowOp([&]() { nonempty = std::move(empty); }));
+  }
+  {
+    auto nonempty = MoveOptional(kInitialInteger);
+    auto empty = MoveOptional();
+    EXPECT_TRUE(testing::TestNothrowOp([&]() { empty = std::move(nonempty); }));
+  }
+  {
+    auto nonempty_from = MoveOptional(kUpdatedInteger);
+    auto nonempty_to = MoveOptional(kInitialInteger);
+    EXPECT_TRUE(testing::TestNothrowOp(
+        [&]() { nonempty_to = std::move(nonempty_from); }));
+  }
+  {
+    auto thrower = MoveThrower(kUpdatedInteger);
+    auto empty = MoveOptional();
+    EXPECT_TRUE(testing::TestNothrowOp([&]() { empty = std::move(thrower); }));
+  }
+  {
+    auto thrower = MoveThrower(kUpdatedInteger);
+    auto nonempty = MoveOptional(kInitialInteger);
+    EXPECT_TRUE(
+        testing::TestNothrowOp([&]() { nonempty = std::move(thrower); }));
+  }
 }
 
 }  // namespace
diff --git a/absl/types/span.h b/absl/types/span.h
index f781353c2347..76be819ecca2 100644
--- a/absl/types/span.h
+++ b/absl/types/span.h
@@ -279,7 +279,7 @@ class Span {
   using size_type = size_t;
   using difference_type = ptrdiff_t;
 
-  static const size_type npos = ~size_type{0};
+  static const size_type npos = ~(size_type(0));
 
   constexpr Span() noexcept : Span(nullptr, 0) {}
   constexpr Span(pointer array, size_type length) noexcept
diff --git a/absl/types/variant_exception_safety_test.cc b/absl/types/variant_exception_safety_test.cc
index 377e4afac572..27c0b96ca6f3 100644
--- a/absl/types/variant_exception_safety_test.cc
+++ b/absl/types/variant_exception_safety_test.cc
@@ -27,8 +27,8 @@ namespace absl {
 namespace {
 
 using ::testing::MakeExceptionSafetyTester;
-using ::testing::nothrow_guarantee;
 using ::testing::strong_guarantee;
+using ::testing::TestNothrowOp;
 using ::testing::TestThrowingCtor;
 
 using Thrower = testing::ThrowingValue<>;
@@ -120,7 +120,11 @@ testing::AssertionResult CheckInvariants(ThrowingVariant* v) {
   return AssertionSuccess();
 }
 
-Thrower ExpectedThrower() { return Thrower(42); }
+template <typename... Args>
+Thrower ExpectedThrower(Args&&... args) {
+  return Thrower(42, args...);
+}
+
 ThrowerVec ExpectedThrowerVec() { return {Thrower(100), Thrower(200)}; }
 ThrowingVariant ValuelessByException() {
   ThrowingVariant v;
@@ -193,18 +197,14 @@ TEST(VariantExceptionSafetyTest, CopyAssign) {
   {
     // - neither *this nor rhs holds a value
     const ThrowingVariant rhs = ValuelessByException();
-    EXPECT_TRUE(MakeExceptionSafetyTester()
-                    .WithInitialValue(ValuelessByException())
-                    .WithInvariants(nothrow_guarantee)
-                    .Test([&rhs](ThrowingVariant* lhs) { *lhs = rhs; }));
+    ThrowingVariant lhs = ValuelessByException();
+    EXPECT_TRUE(TestNothrowOp([&]() { lhs = rhs; }));
   }
   {
     // - *this holds a value but rhs does not
     const ThrowingVariant rhs = ValuelessByException();
-    EXPECT_TRUE(MakeExceptionSafetyTester()
-                    .WithInitialValue(WithThrower())
-                    .WithInvariants(nothrow_guarantee)
-                    .Test([&rhs](ThrowingVariant* lhs) { *lhs = rhs; }));
+    ThrowingVariant lhs = WithThrower();
+    EXPECT_TRUE(TestNothrowOp([&]() { lhs = rhs; }));
   }
   // - index() == j
   {
@@ -237,10 +237,8 @@ TEST(VariantExceptionSafetyTest, CopyAssign) {
     // should not throw because emplace() invokes Tj's copy ctor
     // which should not throw.
     const ThrowingVariant rhs(CopyNothrow{});
-    EXPECT_TRUE(MakeExceptionSafetyTester()
-                    .WithInitialValue(WithThrower())
-                    .WithInvariants(nothrow_guarantee)
-                    .Test([&rhs](ThrowingVariant* lhs) { *lhs = rhs; }));
+    ThrowingVariant lhs = WithThrower();
+    EXPECT_TRUE(TestNothrowOp([&]() { lhs = rhs; }));
   }
   {
     // is_nothrow_copy_constructible<Tj> == false &&
@@ -281,23 +279,14 @@ TEST(VariantExceptionSafetyTest, MoveAssign) {
   {
     // - neither *this nor rhs holds a value
     ThrowingVariant rhs = ValuelessByException();
-
-    EXPECT_TRUE(MakeExceptionSafetyTester()
-                    .WithInitialValue(ValuelessByException())
-                    .WithInvariants(nothrow_guarantee)
-                    .Test([rhs](ThrowingVariant* lhs) mutable {
-                      *lhs = std::move(rhs);
-                    }));
+    ThrowingVariant lhs = ValuelessByException();
+    EXPECT_TRUE(TestNothrowOp([&]() { lhs = std::move(rhs); }));
   }
   {
     // - *this holds a value but rhs does not
     ThrowingVariant rhs = ValuelessByException();
-    EXPECT_TRUE(MakeExceptionSafetyTester()
-                    .WithInitialValue(WithThrower())
-                    .WithInvariants(nothrow_guarantee)
-                    .Test([rhs](ThrowingVariant* lhs) mutable {
-                      *lhs = std::move(rhs);
-                    }));
+    ThrowingVariant lhs = WithThrower();
+    EXPECT_TRUE(TestNothrowOp([&]() { lhs = std::move(rhs); }));
   }
   {
     // - index() == j
@@ -310,13 +299,14 @@ TEST(VariantExceptionSafetyTest, MoveAssign) {
     // Since Thrower's move assignment has basic guarantee, so should variant's.
     auto tester = MakeExceptionSafetyTester()
                       .WithInitialValue(WithThrower())
-                      .WithOperation([rhs](ThrowingVariant* lhs) mutable {
-                        *lhs = std::move(rhs);
+                      .WithOperation([&](ThrowingVariant* lhs) {
+                        auto copy = rhs;
+                        *lhs = std::move(copy);
                       });
     EXPECT_TRUE(tester
                     .WithInvariants(
                         CheckInvariants,
-                        [j](ThrowingVariant* lhs) { return lhs->index() == j; })
+                        [&](ThrowingVariant* lhs) { return lhs->index() == j; })
                     .Test());
     EXPECT_FALSE(tester.WithInvariants(strong_guarantee).Test());
   }
@@ -332,8 +322,9 @@ TEST(VariantExceptionSafetyTest, MoveAssign) {
                                     [](ThrowingVariant* lhs) {
                                       return lhs->valueless_by_exception();
                                     })
-                    .Test([rhs](ThrowingVariant* lhs) mutable {
-                      *lhs = std::move(rhs);
+                    .Test([&](ThrowingVariant* lhs) {
+                      auto copy = rhs;
+                      *lhs = std::move(copy);
                     }));
   }
 }
@@ -365,8 +356,9 @@ TEST(VariantExceptionSafetyTest, ValueAssign) {
     // move assign
     auto move_tester = MakeExceptionSafetyTester()
                            .WithInitialValue(WithThrower())
-                           .WithOperation([rhs](ThrowingVariant* lhs) mutable {
-                             *lhs = std::move(rhs);
+                           .WithOperation([&](ThrowingVariant* lhs) {
+                             auto copy = rhs;
+                             *lhs = std::move(copy);
                            });
     EXPECT_TRUE(move_tester
                     .WithInvariants(CheckInvariants,
@@ -388,19 +380,13 @@ TEST(VariantExceptionSafetyTest, ValueAssign) {
   // invokes the copy/move constructor and it should not throw.
   {
     const CopyNothrow rhs;
-    EXPECT_TRUE(MakeExceptionSafetyTester()
-                    .WithInitialValue(WithThrower())
-                    .WithInvariants(nothrow_guarantee)
-                    .Test([&rhs](ThrowingVariant* lhs) { *lhs = rhs; }));
+    ThrowingVariant lhs = WithThrower();
+    EXPECT_TRUE(TestNothrowOp([&]() { lhs = rhs; }));
   }
   {
     MoveNothrow rhs;
-    EXPECT_TRUE(MakeExceptionSafetyTester()
-                    .WithInitialValue(WithThrower())
-                    .WithInvariants(nothrow_guarantee)
-                    .Test([rhs](ThrowingVariant* lhs) mutable {
-                      *lhs = std::move(rhs);
-                    }));
+    ThrowingVariant lhs = WithThrower();
+    EXPECT_TRUE(TestNothrowOp([&]() { lhs = std::move(rhs); }));
   }
   // if is_nothrow_constructible_v<Tj, T> == false &&
   // is_nothrow_move_constructible<Tj> == false
@@ -423,8 +409,8 @@ TEST(VariantExceptionSafetyTest, ValueAssign) {
     // move
     auto move_tester = MakeExceptionSafetyTester()
                            .WithInitialValue(WithCopyNoThrow())
-                           .WithOperation([rhs](ThrowingVariant* lhs) mutable {
-                             *lhs = std::move(rhs);
+                           .WithOperation([](ThrowingVariant* lhs) {
+                             *lhs = ExpectedThrower(testing::nothrow_ctor);
                            });
     EXPECT_TRUE(move_tester
                     .WithInvariants(CheckInvariants,
@@ -477,21 +463,20 @@ TEST(VariantExceptionSafetyTest, Swap) {
   // if both are valueless_by_exception(), no effect
   {
     ThrowingVariant rhs = ValuelessByException();
-    EXPECT_TRUE(
-        MakeExceptionSafetyTester()
-            .WithInitialValue(ValuelessByException())
-            .WithInvariants(nothrow_guarantee)
-            .Test([rhs](ThrowingVariant* lhs) mutable { lhs->swap(rhs); }));
+    ThrowingVariant lhs = ValuelessByException();
+    EXPECT_TRUE(TestNothrowOp([&]() { lhs.swap(rhs); }));
   }
   // if index() == rhs.index(), calls swap(get<i>(*this), get<i>(rhs))
   // where i is index().
   {
     ThrowingVariant rhs = ExpectedThrower();
-    EXPECT_TRUE(
-        MakeExceptionSafetyTester()
-            .WithInitialValue(WithThrower())
-            .WithInvariants(CheckInvariants)
-            .Test([rhs](ThrowingVariant* lhs) mutable { lhs->swap(rhs); }));
+    EXPECT_TRUE(MakeExceptionSafetyTester()
+                    .WithInitialValue(WithThrower())
+                    .WithInvariants(CheckInvariants)
+                    .Test([&](ThrowingVariant* lhs) {
+                      auto copy = rhs;
+                      lhs->swap(copy);
+                    }));
   }
   // Otherwise, exchanges the value of rhs and *this. The exception safety
   // involves variant in moved-from state which is not specified in the
@@ -499,19 +484,23 @@ TEST(VariantExceptionSafetyTest, Swap) {
   // overall strong guarantee. So, we are only checking basic guarantee here.
   {
     ThrowingVariant rhs = ExpectedThrower();
-    EXPECT_TRUE(
-        MakeExceptionSafetyTester()
-            .WithInitialValue(WithCopyNoThrow())
-            .WithInvariants(CheckInvariants)
-            .Test([rhs](ThrowingVariant* lhs) mutable { lhs->swap(rhs); }));
+    EXPECT_TRUE(MakeExceptionSafetyTester()
+                    .WithInitialValue(WithCopyNoThrow())
+                    .WithInvariants(CheckInvariants)
+                    .Test([&](ThrowingVariant* lhs) {
+                      auto copy = rhs;
+                      lhs->swap(copy);
+                    }));
   }
   {
     ThrowingVariant rhs = ExpectedThrower();
-    EXPECT_TRUE(
-        MakeExceptionSafetyTester()
-            .WithInitialValue(WithCopyNoThrow())
-            .WithInvariants(CheckInvariants)
-            .Test([rhs](ThrowingVariant* lhs) mutable { rhs.swap(*lhs); }));
+    EXPECT_TRUE(MakeExceptionSafetyTester()
+                    .WithInitialValue(WithCopyNoThrow())
+                    .WithInvariants(CheckInvariants)
+                    .Test([&](ThrowingVariant* lhs) {
+                      auto copy = rhs;
+                      copy.swap(*lhs);
+                    }));
   }
 }