proxygen
proxygen::CobHelper Class Reference

#include <CobHelper.h>

Public Member Functions

 CobHelper (size_t itemsLeft, const std::function< void()> &cob, const std::function< void(const std::exception &)> &ecob)
 
void setError (const std::string &emsg)
 
void workerDone ()
 

Private Member Functions

void allDone ()
 

Private Attributes

std::atomic< uint32_titemsLeft_
 
std::string emsg_
 
std::function< void()> cob_
 
std::function< void(const std::exception &)> ecob_
 

Detailed Description

A helper class to schedule N async jobs in (possibly) different threads and invoke a final callback in a given thread once all are done.

Definition at line 21 of file CobHelper.h.

Constructor & Destructor Documentation

proxygen::CobHelper::CobHelper ( size_t  itemsLeft,
const std::function< void()> &  cob,
const std::function< void(const std::exception &)> &  ecob 
)
inline

Definition at line 23 of file CobHelper.h.

26  : itemsLeft_(itemsLeft)
27 , cob_(cob)
28 , ecob_(ecob) {}
std::function< void()> cob_
Definition: CobHelper.h:56
std::function< void(const std::exception &)> ecob_
Definition: CobHelper.h:57
std::atomic< uint32_t > itemsLeft_
Definition: CobHelper.h:54

Member Function Documentation

void proxygen::CobHelper::allDone ( )
inlineprivate

Definition at line 44 of file CobHelper.h.

References cob_, ecob_, and emsg_.

Referenced by workerDone().

44  {
45  if (!emsg_.empty()) {
46  ecob_(std::runtime_error(emsg_));
47  } else {
48  cob_();
49  }
50 
51  delete this;
52  }
std::function< void()> cob_
Definition: CobHelper.h:56
std::function< void(const std::exception &)> ecob_
Definition: CobHelper.h:57
std::string emsg_
Definition: CobHelper.h:55
void proxygen::CobHelper::setError ( const std::string emsg)
inline

Definition at line 30 of file CobHelper.h.

References emsg_.

30  {
31  CHECK(!emsg.empty());
32  emsg_ = emsg;
33  }
std::string emsg_
Definition: CobHelper.h:55
void proxygen::CobHelper::workerDone ( )
inline

Definition at line 35 of file CobHelper.h.

References allDone(), itemsLeft_, and uint32_t.

35  {
36  uint32_t oldValue = itemsLeft_.fetch_sub(1);
37  if (oldValue != 1) {
38  return;
39  }
40 
41  allDone();
42  }
std::atomic< uint32_t > itemsLeft_
Definition: CobHelper.h:54

Member Data Documentation

std::function<void()> proxygen::CobHelper::cob_
private

Definition at line 56 of file CobHelper.h.

Referenced by allDone().

std::function<void(const std::exception&)> proxygen::CobHelper::ecob_
private

Definition at line 57 of file CobHelper.h.

Referenced by allDone().

std::string proxygen::CobHelper::emsg_
private

Definition at line 55 of file CobHelper.h.

Referenced by allDone(), and setError().

std::atomic<uint32_t> proxygen::CobHelper::itemsLeft_
private

Definition at line 54 of file CobHelper.h.

Referenced by workerDone().


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