Commit 15906ea2 authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook GitHub Bot

Add upcasting support

Summary: Store a raw pointer to the managed object outside of the ReadMostlySharedPtrCore to make casting easier.

Reviewed By: yfeldblum

Differential Revision: D23920580

fbshipit-source-id: 4d3d3423f4ef3a78ebbd5efd45bb365ca0f72a18
parent a52c2b20
This diff is collapsed.
...@@ -346,3 +346,51 @@ TEST_F(ReadMostlySharedPtrTest, getStdShared) { ...@@ -346,3 +346,51 @@ TEST_F(ReadMostlySharedPtrTest, getStdShared) {
// No conditions to check; we just wanted to ensure this compiles. // No conditions to check; we just wanted to ensure this compiles.
SUCCEED(); SUCCEED();
} }
struct Base {
virtual ~Base() = default;
virtual std::string getName() const {
return "Base";
}
};
struct Derived : public Base {
std::string getName() const override {
return "Derived";
}
};
TEST_F(ReadMostlySharedPtrTest, casts) {
ReadMostlyMainPtr<Derived> rmmp(std::make_shared<Derived>());
ReadMostlySharedPtr<Derived> rmsp(rmmp);
{
ReadMostlySharedPtr<Base> rmspbase(rmmp);
EXPECT_EQ("Derived", rmspbase->getName());
EXPECT_EQ("Derived", rmspbase.getStdShared()->getName());
}
{
ReadMostlySharedPtr<Base> rmspbase(rmsp);
EXPECT_EQ("Derived", rmspbase->getName());
EXPECT_EQ("Derived", rmspbase.getStdShared()->getName());
}
{
ReadMostlySharedPtr<Base> rmspbase;
rmspbase = rmsp;
EXPECT_EQ("Derived", rmspbase->getName());
EXPECT_EQ("Derived", rmspbase.getStdShared()->getName());
}
{
auto rmspcopy = rmsp;
ReadMostlySharedPtr<Base> rmspbase(std::move(rmspcopy));
EXPECT_EQ("Derived", rmspbase->getName());
EXPECT_EQ("Derived", rmspbase.getStdShared()->getName());
}
{
auto rmspcopy = rmsp;
ReadMostlySharedPtr<Base> rmspbase;
rmspbase = std::move(rmspcopy);
EXPECT_EQ("Derived", rmspbase->getName());
EXPECT_EQ("Derived", rmspbase.getStdShared()->getName());
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment