From b4e6ab95b22fb44ecf578f19677876fea4832cc6 Mon Sep 17 00:00:00 2001 From: "Tao Chen (RPC)" Date: Mon, 13 Jan 2025 23:49:43 -0800 Subject: [PATCH] Parse and save the name from a named creator Differential Revision: D68113986 fbshipit-source-id: a413040eaf9e0bdb1f46c57597d0843a0bbeadf1 --- folly/observer/Observer-inl.h | 10 ++++++---- folly/observer/Observer.h | 5 +++++ folly/observer/detail/Core.h | 22 ++++++++++++++++++++++ folly/observer/test/ObserverTest.cpp | 28 ++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/folly/observer/Observer-inl.h b/folly/observer/Observer-inl.h index b3b38443e7a..5b5aac06683 100644 --- a/folly/observer/Observer-inl.h +++ b/folly/observer/Observer-inl.h @@ -26,7 +26,7 @@ observer::Observer> makeObserver( F&& creator, std::optional creatorContext = std::nullopt) { if (!creatorContext) { - creatorContext = Core::CreatorContext{&typeid(F)}; + creatorContext = Core::CreatorContext::create(std::forward(creator)); } auto core = Core::create( [creator_2 = std::forward(creator)]() mutable { @@ -41,11 +41,12 @@ observer::Observer> makeObserver( template observer::Observer> makeObserver(F&& creator) { + auto creatorContext = Core::CreatorContext::create(std::forward(creator)); return makeObserver( [creator_2 = std::forward(creator)]() mutable { return std::make_shared>(creator_2()); }, - Core::CreatorContext{&typeid(F)}); + std::move(creatorContext)); } template @@ -53,7 +54,7 @@ observer::Observer> makeValueObserver( F&& creator, std::optional creatorContext = std::nullopt) { if (!creatorContext) { - creatorContext = Core::CreatorContext{&typeid(F)}; + creatorContext = Core::CreatorContext::create(std::forward(creator)); } return makeObserver( [activeValue = std::shared_ptr>(), @@ -69,11 +70,12 @@ observer::Observer> makeValueObserver( template observer::Observer> makeValueObserver(F&& creator) { + auto creatorContext = Core::CreatorContext::create(std::forward(creator)); return makeValueObserver( [creator_2 = std::forward(creator)]() mutable { return std::make_shared>(creator_2()); }, - Core::CreatorContext{&typeid(F)}); + std::move(creatorContext)); } } // namespace observer_detail diff --git a/folly/observer/Observer.h b/folly/observer/Observer.h index 0dcf06a6378..5570ad0960d 100644 --- a/folly/observer/Observer.h +++ b/folly/observer/Observer.h @@ -249,6 +249,11 @@ class Observer { const std::type_info* getCreatorTypeInfo() const { return core_->getCreatorContext().typeInfo; } + + const std::string& getCreatorName() const { + return core_->getCreatorContext().name; + } + folly::observer_detail::Core& getCore() const { return *core_; } private: diff --git a/folly/observer/detail/Core.h b/folly/observer/detail/Core.h index fcd00565d78..9580a4c0341 100644 --- a/folly/observer/detail/Core.h +++ b/folly/observer/detail/Core.h @@ -30,6 +30,17 @@ namespace folly { namespace observer_detail { +#define DEFINE_HAS_MEMBER_FUNC(Member) \ + template > \ + struct Has_##Member##T : std::false_type {}; \ + template \ + struct Has_##Member##T().Member())>> \ + : std::true_type {}; \ + template \ + constexpr bool Has_##Member##T_v = Has_##Member##T::value; + +DEFINE_HAS_MEMBER_FUNC(getName) + class ObserverManager; /** @@ -43,6 +54,17 @@ class Core : public std::enable_shared_from_this { struct CreatorContext { const std::type_info* typeInfo; + std::string name; + + template + static CreatorContext create(const F& creator) { + CreatorContext context; + context.typeInfo = &typeid(F); + if constexpr (Has_getNameT_v) { + context.name = creator.getName(); + } + return context; + } }; /** * Blocks until creator is successfully run by ObserverManager diff --git a/folly/observer/test/ObserverTest.cpp b/folly/observer/test/ObserverTest.cpp index aace87638ee..dfcc1415605 100644 --- a/folly/observer/test/ObserverTest.cpp +++ b/folly/observer/test/ObserverTest.cpp @@ -42,6 +42,18 @@ namespace { template struct AltAtomic : std::atomic {}; +template +struct NamedCreator { + std::string name; + F creator; + + NamedCreator(std::string nameP, F&& creatorP) + : name(std::move(nameP)), creator(std::forward(creatorP)) {} + + const std::string& getName() const { return name; } + + auto operator()() const { return creator(); } +}; } // namespace namespace folly { @@ -1293,5 +1305,21 @@ TEST(Observer, TestSimpleObservableWithTypeInfo) { StartsWith(prefix)); } +TEST(Observer, TestObserverWithNamedCreator) { + { + auto observer = + folly::observer::makeObserver(NamedCreator("foo", [] { return 42; })); + EXPECT_EQ(42, **observer); + EXPECT_EQ(observer.getCreatorName(), "foo"); + } + { + auto observer = folly::observer::makeValueObserver(NamedCreator("foo", [] { + return 42; + })); + EXPECT_EQ(42, **observer); + EXPECT_EQ(observer.getCreatorName(), "foo"); + } +} + } // namespace observer } // namespace folly