Commit 1486429b authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

let EventBaseLocal::getOrCreateFn function return value

Summary:
Let the function passed to `EventBaseLocal::getOrCreateFn` directly return the value to be stored, rather than allocating it and returning a pointer.

Details of its storage should be internal to `EventBaseLocal` and should not leak out into the creator functions.

Differential Revision: D26246341

fbshipit-source-id: d72a62998985e42ef517eede3654324dc9f48709
parent 029aff28
......@@ -62,7 +62,7 @@ class EventBaseLocalBase : public EventBaseLocalBaseBase {
* Foo& foo = myFoo.getOrCreate(evb, 1, 2); // ctor
* Foo& foo = myFoo.getOrCreate(evb, 1, 2); // no ctor
* myFoo.erase(evb);
* Foo& foo = myFoo.getOrCreateFn(evb, [] () { return new Foo(3, 4); })
* Foo& foo = myFoo.getOrCreateFn(evb, [] { return Foo(3, 4); })
*
* The objects will be deleted when the EventBaseLocal or the EventBase is
* destructed (whichever comes first). All methods must be called from the
......@@ -112,8 +112,7 @@ class EventBaseLocal : public detail::EventBaseLocalBase {
if (auto ptr = getVoid(evb)) {
return *static_cast<T*>(ptr);
}
std::shared_ptr<T> smartPtr(fn());
DCHECK(smartPtr.get() != nullptr);
auto smartPtr = std::shared_ptr<T>(new T(fn()));
auto& ref = *smartPtr;
setVoid(evb, std::move(smartPtr));
return ref;
......
......@@ -68,7 +68,7 @@ TEST(EventBaseLocalTest, getOrCreate) {
folly::EventBase evb2;
EXPECT_EQ(ints.getOrCreate(evb2, 5), 5);
ints.erase(evb2);
EXPECT_EQ(4, ints.getOrCreateFn(evb2, []() { return new int(4); }));
EXPECT_EQ(4, ints.getOrCreateFn(evb2, [] { return 4; }));
}
using IntPtr = std::unique_ptr<int>;
......
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