proxygen
folly::EventBaseLocal< T > Class Template Reference

#include <EventBase.h>

Inheritance diagram for folly::EventBaseLocal< T >:
folly::detail::EventBaseLocalBase folly::detail::EventBaseLocalBaseBase

Public Member Functions

 EventBaseLocal ()
 
Tget (EventBase &evb)
 
void emplace (EventBase &evb, T *ptr)
 
template<typename... Args>
void emplace (EventBase &evb, Args &&...args)
 
template<typename... Args>
TgetOrCreate (EventBase &evb, Args &&...args)
 
template<typename Func >
TgetOrCreateFn (EventBase &evb, Func fn)
 
- Public Member Functions inherited from folly::detail::EventBaseLocalBase
 EventBaseLocalBase ()
 
 ~EventBaseLocalBase () override
 
void erase (EventBase &evb)
 
void onEventBaseDestruction (EventBase &evb) override
 
- Public Member Functions inherited from folly::detail::EventBaseLocalBaseBase
virtual ~EventBaseLocalBaseBase ()=default
 

Additional Inherited Members

- Protected Member Functions inherited from folly::detail::EventBaseLocalBase
void setVoid (EventBase &evb, std::shared_ptr< void > &&ptr)
 
void * getVoid (EventBase &evb)
 
- Protected Attributes inherited from folly::detail::EventBaseLocalBase
folly::Synchronized< std::unordered_set< EventBase * > > eventBases_
 
std::size_t key_ {keyCounter_++}
 
- Static Protected Attributes inherited from folly::detail::EventBaseLocalBase
static std::atomic< std::size_t > keyCounter_ {0}
 

Detailed Description

template<typename T>
class folly::EventBaseLocal< T >

A storage abstraction for data that should be tied to an EventBase.

struct Foo { Foo(int a, int b); }; EventBaseLocal<Foo> 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<shared_ptr<...>>.

Definition at line 68 of file EventBase.h.

Constructor & Destructor Documentation

template<typename T>
folly::EventBaseLocal< T >::EventBaseLocal ( )
inline

Definition at line 76 of file EventBaseLocal.h.

Member Function Documentation

template<typename T>
void folly::EventBaseLocal< T >::emplace ( EventBase evb,
T ptr 
)
inline

Definition at line 82 of file EventBaseLocal.h.

References testing::Args(), folly::gen::move, and folly::detail::EventBaseLocalBase::setVoid().

Referenced by TEST().

82  {
83  std::shared_ptr<T> smartPtr(ptr);
84  setVoid(evb, std::move(smartPtr));
85  }
void * ptr
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void setVoid(EventBase &evb, std::shared_ptr< void > &&ptr)
template<typename T>
template<typename... Args>
void folly::EventBaseLocal< T >::emplace ( EventBase evb,
Args &&...  args 
)
inline

Definition at line 88 of file EventBaseLocal.h.

References testing::Args(), and folly::detail::EventBaseLocalBase::setVoid().

88  {
89  auto smartPtr = std::make_shared<T>(std::forward<Args>(args)...);
90  setVoid(evb, smartPtr);
91  }
void setVoid(EventBase &evb, std::shared_ptr< void > &&ptr)
template<typename T>
T* folly::EventBaseLocal< T >::get ( EventBase evb)
inline

Definition at line 78 of file EventBaseLocal.h.

References folly::detail::EventBaseLocalBase::getVoid(), and folly::T.

Referenced by TEST().

78  {
79  return static_cast<T*>(getVoid(evb));
80  }
void * getVoid(EventBase &evb)
folly::std T
template<typename T>
template<typename... Args>
T& folly::EventBaseLocal< T >::getOrCreate ( EventBase evb,
Args &&...  args 
)
inline

Definition at line 94 of file EventBaseLocal.h.

References folly::detail::EventBaseLocalBase::getVoid(), folly::gen::move, ptr, folly::detail::EventBaseLocalBase::setVoid(), and folly::T.

Referenced by TEST().

94  {
95  if (auto ptr = getVoid(evb)) {
96  return *static_cast<T*>(ptr);
97  }
98  auto smartPtr = std::make_shared<T>(std::forward<Args>(args)...);
99  auto& ref = *smartPtr;
100  setVoid(evb, std::move(smartPtr));
101  return ref;
102  }
void * ptr
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void * getVoid(EventBase &evb)
folly::std T
void setVoid(EventBase &evb, std::shared_ptr< void > &&ptr)
template<typename T>
template<typename Func >
T& folly::EventBaseLocal< T >::getOrCreateFn ( EventBase evb,
Func  fn 
)
inline

Definition at line 105 of file EventBaseLocal.h.

References folly::detail::EventBaseLocalBase::getVoid(), folly::gen::move, ptr, folly::detail::EventBaseLocalBase::setVoid(), and folly::T.

Referenced by TEST().

105  {
106  // If this looks like it's copy/pasted from above, that's because it is.
107  // gcc has a bug (fixed in 4.9) that doesn't allow capturing variadic
108  // params in a lambda.
109  if (auto ptr = getVoid(evb)) {
110  return *static_cast<T*>(ptr);
111  }
112  std::shared_ptr<T> smartPtr(fn());
113  auto& ref = *smartPtr;
114  setVoid(evb, std::move(smartPtr));
115  return ref;
116  }
void * ptr
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void * getVoid(EventBase &evb)
folly::std T
void setVoid(EventBase &evb, std::shared_ptr< void > &&ptr)

The documentation for this class was generated from the following files: