proxygen
folly::observer::detail::ObserverCreatorContext< Observable, Traits > Class Template Reference

#include <Observable-inl.h>

Public Member Functions

template<typename... Args>
 ObserverCreatorContext (Args &&...args)
 
 ~ObserverCreatorContext ()
 
void setCore (observer_detail::Core::WeakPtr coreWeak)
 
std::shared_ptr< const Tget ()
 
void update ()
 
template<typename F >
void subscribe (F &&callback)
 

Private Types

using T = typename Traits::element_type
 

Private Member Functions

bool updateValue ()
 

Private Attributes

folly::Synchronized< std::shared_ptr< const T > > value_
 
std::atomic< bool > updateRequested_ {false}
 
observer_detail::Core::WeakPtr coreWeak_
 
Observable observable_
 
std::mutex updateMutex_
 

Detailed Description

template<typename Observable, typename Traits>
class folly::observer::detail::ObserverCreatorContext< Observable, Traits >

Definition at line 24 of file Observable-inl.h.

Member Typedef Documentation

template<typename Observable , typename Traits >
using folly::observer::detail::ObserverCreatorContext< Observable, Traits >::T = typename Traits::element_type
private

Definition at line 25 of file Observable-inl.h.

Constructor & Destructor Documentation

template<typename Observable , typename Traits >
template<typename... Args>
folly::observer::detail::ObserverCreatorContext< Observable, Traits >::ObserverCreatorContext ( Args &&...  args)
inline
template<typename Observable , typename Traits >
folly::observer::detail::ObserverCreatorContext< Observable, Traits >::~ObserverCreatorContext ( )
inline

Member Function Documentation

template<typename Observable , typename Traits >
std::shared_ptr<const T> folly::observer::detail::ObserverCreatorContext< Observable, Traits >::get ( )
inline
template<typename Observable , typename Traits >
void folly::observer::detail::ObserverCreatorContext< Observable, Traits >::setCore ( observer_detail::Core::WeakPtr  coreWeak)
inline

Definition at line 40 of file Observable-inl.h.

References folly::observer::detail::ObserverCreatorContext< Observable, Traits >::coreWeak_, and folly::gen::move.

40  {
41  coreWeak_ = std::move(coreWeak);
42  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
observer_detail::Core::WeakPtr coreWeak_
template<typename Observable , typename Traits >
template<typename F >
void folly::observer::detail::ObserverCreatorContext< Observable, Traits >::subscribe ( F &&  callback)
inline

Definition at line 69 of file Observable-inl.h.

References folly::observer::detail::ObserverCreatorContext< Observable, Traits >::observable_, and subscribe().

69  {
70  Traits::subscribe(observable_, std::forward<F>(callback));
71  }
void subscribe(uint32_t iters, int N)
Definition: RxBenchmark.cpp:54
template<typename Observable , typename Traits >
void folly::observer::detail::ObserverCreatorContext< Observable, Traits >::update ( )
inline

Definition at line 49 of file Observable-inl.h.

References folly::observer::detail::ObserverCreatorContext< Observable, Traits >::coreWeak_, folly::observer_detail::ObserverManager::scheduleRefreshNewVersion(), folly::observer::detail::ObserverCreatorContext< Observable, Traits >::updateMutex_, folly::observer::detail::ObserverCreatorContext< Observable, Traits >::updateRequested_, and folly::observer::detail::ObserverCreatorContext< Observable, Traits >::updateValue().

49  {
50  // This mutex ensures there's no race condition between initial update()
51  // call and update() calls from the subsciption callback.
52  //
53  // Additionally it helps avoid races between two different subscription
54  // callbacks (getting new value from observable and storing it into value_
55  // is not atomic).
56  std::lock_guard<std::mutex> lg(updateMutex_);
57  if (!updateValue()) {
58  // Value didn't change, so we can skip the version update.
59  return;
60  }
61 
62  bool expected = false;
63  if (updateRequested_.compare_exchange_strong(expected, true)) {
65  }
66  }
static void scheduleRefreshNewVersion(Core::WeakPtr coreWeak)
observer_detail::Core::WeakPtr coreWeak_
template<typename Observable , typename Traits >
bool folly::observer::detail::ObserverCreatorContext< Observable, Traits >::updateValue ( )
inlineprivate

Definition at line 74 of file Observable-inl.h.

References folly::pushmi::operators::get, folly::observer::detail::ObserverCreatorContext< Observable, Traits >::observable_, folly::Synchronized< T, Mutex >::swap(), and folly::observer::detail::ObserverCreatorContext< Observable, Traits >::value_.

Referenced by folly::observer::detail::ObserverCreatorContext< Observable, Traits >::ObserverCreatorContext(), and folly::observer::detail::ObserverCreatorContext< Observable, Traits >::update().

74  {
75  auto newValue = Traits::get(observable_);
76  auto newValuePtr = newValue.get();
77  if (!newValue) {
78  throw std::logic_error("Observable returned nullptr.");
79  }
80  value_.swap(newValue);
81  return newValuePtr != newValue.get();
82  }
void swap(Synchronized &rhs)
Definition: Synchronized.h:684
folly::Synchronized< std::shared_ptr< const T > > value_
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391

Member Data Documentation

template<typename Observable , typename Traits >
std::mutex folly::observer::detail::ObserverCreatorContext< Observable, Traits >::updateMutex_
private
template<typename Observable , typename Traits >
std::atomic<bool> folly::observer::detail::ObserverCreatorContext< Observable, Traits >::updateRequested_ {false}
private

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