proxygen
CobHelper.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11 
12 #include <atomic>
13 #include <functional>
14 
15 namespace proxygen {
16 
21 class CobHelper {
22  public:
23  CobHelper(size_t itemsLeft,
24  const std::function<void()>& cob,
25  const std::function<void(const std::exception&)>& ecob)
26  : itemsLeft_(itemsLeft)
27 , cob_(cob)
28 , ecob_(ecob) {}
29 
30  void setError(const std::string& emsg) {
31  CHECK(!emsg.empty());
32  emsg_ = emsg;
33  }
34 
35  void workerDone() {
36  uint32_t oldValue = itemsLeft_.fetch_sub(1);
37  if (oldValue != 1) {
38  return;
39  }
40 
41  allDone();
42  }
43  private:
44  void allDone() {
45  if (!emsg_.empty()) {
46  ecob_(std::runtime_error(emsg_));
47  } else {
48  cob_();
49  }
50 
51  delete this;
52  }
53 
54  std::atomic<uint32_t> itemsLeft_;
56  std::function<void()> cob_;
57  std::function<void(const std::exception&)> ecob_;
58 };
59 
60 }
std::function< void()> cob_
Definition: CobHelper.h:56
void setError(const std::string &emsg)
Definition: CobHelper.h:30
std::function< void(const std::exception &)> ecob_
Definition: CobHelper.h:57
std::string emsg_
Definition: CobHelper.h:55
CobHelper(size_t itemsLeft, const std::function< void()> &cob, const std::function< void(const std::exception &)> &ecob)
Definition: CobHelper.h:23
std::atomic< uint32_t > itemsLeft_
Definition: CobHelper.h:54
const char * string
Definition: Conv.cpp:212