From 38894440d462e055099699723c26cb8551c1e4a3 Mon Sep 17 00:00:00 2001 From: Steve O'Brien Date: Tue, 3 Jan 2017 13:45:15 -0800 Subject: [PATCH] folly: ReadMostlySharedPtr fix for `getStdShared()` method Summary: Fix this error: In file included from FooUtils.cpp:2: folly/experimental/ReadMostlySharedPtr.h:323:21: error: 'ptr_' is a private member of 'folly::detail::ReadMostlySharedPtrCore' return impl_->ptr_; ^ ... in instantiation of member function 'folly::ReadMostlySharedPtr::getStdShared' requested here: getSomeReadMostlySharedPtr().getStdShared(); ^ buck-out/dev/gen/folly/__default_headers__#default,headers/folly/experimental/ReadMostlySharedPtr.h:94:22: note: declared private here std::shared_ptr ptr_; ^ 1 error generated. The added test case repro's the above error (and the changes to the class fixes it). Alternatives include just making `ReadMostlySharedPtr` a friend class of `ReadMostlySharedPtrCore`, but that seems uglier than this fix, which was to simply use the public `getShared` method which already exists. As luck would have it, I had tried that, and also found that a `const ReadMostlySharedPtr` would still give some trouble because `getStdShared` was not marked `const`. Fixed that too. (I assume if a copy of a const `shared_ptr` member / such a member of a `const` instance is permissible, then the method should be const as well. Plus it's const in the other `ReadMostlySharedPtr` class.) Reviewed By: djwatson Differential Revision: D4377690 fbshipit-source-id: 8e9e778ca991fd04b0eb1e5762795d871ce0ee8d --- folly/experimental/ReadMostlySharedPtr.h | 6 +++--- folly/experimental/test/ReadMostlySharedPtrTest.cpp | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/folly/experimental/ReadMostlySharedPtr.h b/folly/experimental/ReadMostlySharedPtr.h index 9e3a5d0a..bbe1628c 100644 --- a/folly/experimental/ReadMostlySharedPtr.h +++ b/folly/experimental/ReadMostlySharedPtr.h @@ -159,9 +159,9 @@ class ReadMostlyMainPtr { } } - std::shared_ptr getStdShared() { + std::shared_ptr getStdShared() const { if (impl_) { - return impl_->ptr_; + return impl_->getShared(); } else { return {}; } @@ -320,7 +320,7 @@ class ReadMostlySharedPtr { std::shared_ptr getStdShared() const { if (impl_) { - return impl_->ptr_; + return impl_->getShared(); } else { return {}; } diff --git a/folly/experimental/test/ReadMostlySharedPtrTest.cpp b/folly/experimental/test/ReadMostlySharedPtrTest.cpp index 0b00bbb1..e0d6c56d 100644 --- a/folly/experimental/test/ReadMostlySharedPtrTest.cpp +++ b/folly/experimental/test/ReadMostlySharedPtrTest.cpp @@ -334,3 +334,16 @@ TEST_F(ReadMostlySharedPtrTest, nullptr) { EXPECT_TRUE(ptr); } } + +TEST_F(ReadMostlySharedPtrTest, getStdShared) { + const ReadMostlyMainPtr rmmp1(std::make_shared(42)); + + ReadMostlyMainPtr rmmp2; + rmmp2.reset(rmmp1.getStdShared()); + + const ReadMostlySharedPtr rmsp1 = rmmp1.getShared(); + ReadMostlySharedPtr rmsp2(rmsp1); + + // No conditions to check; we just wanted to ensure this compiles. + SUCCEED(); +} -- 2.34.1