Skip to content

Commit

Permalink
Parse and save the name from a named creator
Browse files Browse the repository at this point in the history
Differential Revision: D68113986

fbshipit-source-id: a413040eaf9e0bdb1f46c57597d0843a0bbeadf1
  • Loading branch information
Tao Chen (RPC) authored and facebook-github-bot committed Jan 14, 2025
1 parent 32dc0bf commit b4e6ab9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
10 changes: 6 additions & 4 deletions folly/observer/Observer-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ observer::Observer<ResultOfUnwrapSharedPtr<F>> makeObserver(
F&& creator,
std::optional<Core::CreatorContext> creatorContext = std::nullopt) {
if (!creatorContext) {
creatorContext = Core::CreatorContext{&typeid(F)};
creatorContext = Core::CreatorContext::create(std::forward<F>(creator));
}
auto core = Core::create(
[creator_2 = std::forward<F>(creator)]() mutable {
Expand All @@ -41,19 +41,20 @@ observer::Observer<ResultOfUnwrapSharedPtr<F>> makeObserver(

template <typename F>
observer::Observer<ResultOfNoObserverUnwrap<F>> makeObserver(F&& creator) {
auto creatorContext = Core::CreatorContext::create(std::forward<F>(creator));
return makeObserver(
[creator_2 = std::forward<F>(creator)]() mutable {
return std::make_shared<ResultOfNoObserverUnwrap<F>>(creator_2());
},
Core::CreatorContext{&typeid(F)});
std::move(creatorContext));
}

template <typename F>
observer::Observer<ResultOfUnwrapSharedPtr<F>> makeValueObserver(
F&& creator,
std::optional<Core::CreatorContext> creatorContext = std::nullopt) {
if (!creatorContext) {
creatorContext = Core::CreatorContext{&typeid(F)};
creatorContext = Core::CreatorContext::create(std::forward<F>(creator));
}
return makeObserver(
[activeValue = std::shared_ptr<const ResultOfUnwrapSharedPtr<F>>(),
Expand All @@ -69,11 +70,12 @@ observer::Observer<ResultOfUnwrapSharedPtr<F>> makeValueObserver(

template <typename F>
observer::Observer<ResultOfNoObserverUnwrap<F>> makeValueObserver(F&& creator) {
auto creatorContext = Core::CreatorContext::create(std::forward<F>(creator));
return makeValueObserver(
[creator_2 = std::forward<F>(creator)]() mutable {
return std::make_shared<ResultOfNoObserverUnwrap<F>>(creator_2());
},
Core::CreatorContext{&typeid(F)});
std::move(creatorContext));
}
} // namespace observer_detail

Expand Down
5 changes: 5 additions & 0 deletions folly/observer/Observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions folly/observer/detail/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
namespace folly {
namespace observer_detail {

#define DEFINE_HAS_MEMBER_FUNC(Member) \
template <typename T, typename = std::void_t<>> \
struct Has_##Member##T : std::false_type {}; \
template <typename T> \
struct Has_##Member##T<T, std::void_t<decltype(std::declval<T>().Member())>> \
: std::true_type {}; \
template <typename T> \
constexpr bool Has_##Member##T_v = Has_##Member##T<T>::value;

DEFINE_HAS_MEMBER_FUNC(getName)

class ObserverManager;

/**
Expand All @@ -43,6 +54,17 @@ class Core : public std::enable_shared_from_this<Core> {

struct CreatorContext {
const std::type_info* typeInfo;
std::string name;

template <typename F>
static CreatorContext create(const F& creator) {
CreatorContext context;
context.typeInfo = &typeid(F);
if constexpr (Has_getNameT_v<F>) {
context.name = creator.getName();
}
return context;
}
};
/**
* Blocks until creator is successfully run by ObserverManager
Expand Down
28 changes: 28 additions & 0 deletions folly/observer/test/ObserverTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ namespace {
template <typename T>
struct AltAtomic : std::atomic<T> {};

template <typename F>
struct NamedCreator {
std::string name;
F creator;

NamedCreator(std::string nameP, F&& creatorP)
: name(std::move(nameP)), creator(std::forward<F>(creatorP)) {}

const std::string& getName() const { return name; }

auto operator()() const { return creator(); }
};
} // namespace

namespace folly {
Expand Down Expand Up @@ -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

0 comments on commit b4e6ab9

Please sign in to comment.