rippled
FunctionQueue.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_PEERFINDER_SIM_FUNCTIONQUEUE_H_INCLUDED
21 #define RIPPLE_PEERFINDER_SIM_FUNCTIONQUEUE_H_INCLUDED
22 
23 namespace ripple {
24 namespace PeerFinder {
25 namespace Sim {
26 
29 {
30 public:
31  explicit FunctionQueue() = default;
32 
33 private:
34  class BasicWork
35  {
36  public:
37  virtual ~BasicWork()
38  {
39  }
40  virtual void
41  operator()() = 0;
42  };
43 
44  template <typename Function>
45  class Work : public BasicWork
46  {
47  public:
48  explicit Work(Function f) : m_f(f)
49  {
50  }
51  void
53  {
54  (m_f)();
55  }
56 
57  private:
58  Function m_f;
59  };
60 
62 
63 public:
65  bool
67  {
68  return m_work.empty();
69  }
70 
75  template <typename Function>
76  void
77  post(Function f)
78  {
79  m_work.emplace_back(std::make_unique<Work<Function>>(f));
80  }
81 
85  void
86  run()
87  {
88  while (!m_work.empty())
89  {
90  (*m_work.front())();
91  m_work.pop_front();
92  }
93  }
94 };
95 
96 } // namespace Sim
97 } // namespace PeerFinder
98 } // namespace ripple
99 
100 #endif
ripple::PeerFinder::Sim::FunctionQueue::run
void run()
Run all pending functions.
Definition: FunctionQueue.h:86
ripple::PeerFinder::Sim::FunctionQueue::BasicWork::~BasicWork
virtual ~BasicWork()
Definition: FunctionQueue.h:37
std::list
STL class.
std::make_unique
T make_unique(T... args)
ripple::PeerFinder::Sim::FunctionQueue::m_work
std::list< std::unique_ptr< BasicWork > > m_work
Definition: FunctionQueue.h:61
ripple::PeerFinder::Sim::FunctionQueue::FunctionQueue
FunctionQueue()=default
ripple::PeerFinder::Sim::FunctionQueue::Work::m_f
Function m_f
Definition: FunctionQueue.h:58
ripple::PeerFinder::Sim::FunctionQueue
Maintains a queue of functors that can be called later.
Definition: FunctionQueue.h:28
ripple::PeerFinder::Sim::FunctionQueue::empty
bool empty()
Returns true if there is no remaining work.
Definition: FunctionQueue.h:66
ripple::PeerFinder::Sim::FunctionQueue::BasicWork
Definition: FunctionQueue.h:34
ripple::PeerFinder::Sim::FunctionQueue::Work
Definition: FunctionQueue.h:45
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerFinder::Sim::FunctionQueue::Work::Work
Work(Function f)
Definition: FunctionQueue.h:48
ripple::PeerFinder::Sim::FunctionQueue::BasicWork::operator()
virtual void operator()()=0
ripple::PeerFinder::Sim::FunctionQueue::Work::operator()
void operator()()
Definition: FunctionQueue.h:52
ripple::PeerFinder::Sim::FunctionQueue::post
void post(Function f)
Queue a function.
Definition: FunctionQueue.h:77