folly: ReadMostlySharedPtr fix for `getStdShared()` method
authorSteve O'Brien <steveo@fb.com>
Tue, 3 Jan 2017 21:45:15 +0000 (13:45 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 3 Jan 2017 21:48:09 +0000 (13:48 -0800)
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<Foo, folly::TLRefCount>'
        return impl_->ptr_;
                      ^
  ... in instantiation of member function 'folly::ReadMostlySharedPtr<Foo, folly::TLRefCount>::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<T> 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
folly/experimental/test/ReadMostlySharedPtrTest.cpp

index 9e3a5d0a75f5b2823bf33eff5664956abe6b046b..bbe1628c6d28c8f33c3ab0ca3aa6387bdb9b58c4 100644 (file)
@@ -159,9 +159,9 @@ class ReadMostlyMainPtr {
     }
   }
 
-  std::shared_ptr<T> getStdShared() {
+  std::shared_ptr<T> getStdShared() const {
     if (impl_) {
-      return impl_->ptr_;
+      return impl_->getShared();
     } else {
       return {};
     }
@@ -320,7 +320,7 @@ class ReadMostlySharedPtr {
 
   std::shared_ptr<T> getStdShared() const {
     if (impl_) {
-      return impl_->ptr_;
+      return impl_->getShared();
     } else {
       return {};
     }
index 0b00bbb1472cc8f187244a6462a42bb026988cb7..e0d6c56dadc80bd591e231781cb95cc1b797b03a 100644 (file)
@@ -334,3 +334,16 @@ TEST_F(ReadMostlySharedPtrTest, nullptr) {
     EXPECT_TRUE(ptr);
   }
 }
+
+TEST_F(ReadMostlySharedPtrTest, getStdShared) {
+  const ReadMostlyMainPtr<int> rmmp1(std::make_shared<int>(42));
+
+  ReadMostlyMainPtr<int> rmmp2;
+  rmmp2.reset(rmmp1.getStdShared());
+
+  const ReadMostlySharedPtr<int> rmsp1 = rmmp1.getShared();
+  ReadMostlySharedPtr<int> rmsp2(rmsp1);
+
+  // No conditions to check; we just wanted to ensure this compiles.
+  SUCCEED();
+}