Skip to content

Commit

Permalink
Allow makeStaticObserver(std::shared_ptr<const T>)
Browse files Browse the repository at this point in the history
Summary: Observers operate with `std::shared_ptr<const T>` internally so it's natural and convenient to allow `makeStaticObserver(std::shared_ptr<const T>)`.

Reviewed By: praihan

Differential Revision: D64764178

fbshipit-source-id: 4343dfdbe0af42defbe3b3d386b36b1c9b07dd79
  • Loading branch information
mshatalov authored and facebook-github-bot committed Oct 24, 2024
1 parent 2d0f8ed commit 2498c34
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion folly/observer/Observer-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Observer<T> makeStaticObserver(T value) {
}

template <typename T>
Observer<T> makeStaticObserver(std::shared_ptr<T> value) {
Observer<std::decay_t<T>> makeStaticObserver(std::shared_ptr<T> value) {
return makeObserver([value_2 = std::move(value)] { return value_2; });
}

Expand Down
2 changes: 1 addition & 1 deletion folly/observer/Observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ template <typename T>
Observer<T> makeStaticObserver(T value);

template <typename T>
Observer<T> makeStaticObserver(std::shared_ptr<T> value);
Observer<std::decay_t<T>> makeStaticObserver(std::shared_ptr<T> value);

template <typename T>
class AtomicObserver {
Expand Down
9 changes: 9 additions & 0 deletions folly/observer/test/ObserverTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,17 @@ TEST(Observer, MakeStaticObserver) {
makeStaticObserver<std::shared_ptr<int>>(std::make_shared<int>(5));
EXPECT_EQ(***explicitSharedPtrObserver, 5);

auto explicitSharedPtrToConstObserver =
makeStaticObserver<std::shared_ptr<const int>>(
std::make_shared<const int>(5));
EXPECT_EQ(***explicitSharedPtrToConstObserver, 5);

auto implicitSharedPtrObserver = makeStaticObserver(std::make_shared<int>(5));
EXPECT_EQ(**implicitSharedPtrObserver, 5);

auto implicitSharedPtrToConstObserver =
makeStaticObserver(std::make_shared<const int>(5));
EXPECT_EQ(**implicitSharedPtrToConstObserver, 5);
}

TEST(Observer, AtomicObserver) {
Expand Down

0 comments on commit 2498c34

Please sign in to comment.