/* * Copyright 2015-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include namespace folly { namespace detail { class EventBaseLocalBase : public EventBaseLocalBaseBase, boost::noncopyable { public: EventBaseLocalBase() {} ~EventBaseLocalBase() override; void erase(EventBase& evb); void onEventBaseDestruction(EventBase& evb) override; protected: void setVoid(EventBase& evb, std::shared_ptr&& ptr); void* getVoid(EventBase& evb); folly::Synchronized> eventBases_; static std::atomic keyCounter_; std::size_t key_{keyCounter_++}; }; } // namespace detail /** * A storage abstraction for data that should be tied to an EventBase. * * struct Foo { Foo(int a, int b); }; * EventBaseLocal myFoo; * ... * EventBase evb; * myFoo.set(evb, new Foo(1, 2)); * myFoo.set(evb, 1, 2); * Foo* foo = myFoo.get(evb); * myFoo.erase(evb); * 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); }) * * The objects will be deleted when the EventBaseLocal or the EventBase is * destructed (whichever comes first). All methods must be called from the * EventBase thread. * * The user is responsible for throwing away invalid references/ptrs returned * by the get() method after set/erase is called. If shared ownership is * needed, use a EventBaseLocal>. */ template class EventBaseLocal : public detail::EventBaseLocalBase { public: EventBaseLocal() : EventBaseLocalBase() {} T* get(EventBase& evb) { return static_cast(getVoid(evb)); } void emplace(EventBase& evb, T* ptr) { std::shared_ptr smartPtr(ptr); setVoid(evb, std::move(smartPtr)); } template void emplace(EventBase& evb, Args&&... args) { auto smartPtr = std::make_shared(std::forward(args)...); setVoid(evb, smartPtr); } template T& getOrCreate(EventBase& evb, Args&&... args) { if (auto ptr = getVoid(evb)) { return *static_cast(ptr); } auto smartPtr = std::make_shared(std::forward(args)...); auto& ref = *smartPtr; setVoid(evb, std::move(smartPtr)); return ref; } template T& getOrCreateFn(EventBase& evb, Func& fn) { // If this looks like it's copy/pasted from above, that's because it is. // gcc has a bug (fixed in 4.9) that doesn't allow capturing variadic // params in a lambda. if (auto ptr = getVoid(evb)) { return *static_cast(ptr); } std::shared_ptr smartPtr(fn()); auto& ref = *smartPtr; setVoid(evb, std::move(smartPtr)); return ref; } }; } // namespace folly