Scorum
tasks_base.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <functional>
5 #include <boost/type_index.hpp>
6 #include <fc/log/logger.hpp>
7 
8 namespace scorum {
9 namespace chain {
10 
11 template <typename ContextType> struct task_reentrance_guard_i
12 {
14  {
15  }
16 
17  virtual bool is_allowed(ContextType& ctx) = 0;
18  virtual void apply(ContextType& ctx) = 0;
19 };
20 
21 template <typename ContextType> class dummy_reentrance_guard : public task_reentrance_guard_i<ContextType>
22 {
23 public:
24  virtual bool is_allowed(ContextType&)
25  {
26  return true;
27  }
28  virtual void apply(ContextType&)
29  {
30  }
31 };
32 
33 struct data_service_factory_i;
34 
35 template <typename ContextType = data_service_factory_i,
36  typename ReentranceGuardType = dummy_reentrance_guard<ContextType>>
37 class task
38 {
39 public:
40  virtual ~task()
41  {
42  }
43 
44  task& after(task& impl)
45  {
46  _after.push_back(std::ref(impl));
47  return *this;
48  }
49  task& before(task& impl)
50  {
51  _before.push_back(std::ref(impl));
52  return *this;
53  }
54 
55  void apply(ContextType& ctx)
56  {
57  if (!_guard.is_allowed(ctx))
58  return;
59  _guard.apply(ctx);
60 
61  for (task& r : _after)
62  {
63  r.apply(ctx);
64  }
65 
66  auto impl = boost::typeindex::type_id_runtime(*this).pretty_name();
67 
68  on_apply(ctx);
69 
70  for (task& r : _before)
71  {
72  r.apply(ctx);
73  }
74  }
75 
76 protected:
77  virtual void on_apply(ContextType&) = 0;
78 
79 private:
80  using tasks_reqired_type = std::vector<std::reference_wrapper<task>>;
81 
82  tasks_reqired_type _after;
83  tasks_reqired_type _before;
84  ReentranceGuardType _guard;
85 };
86 }
87 }
virtual void apply(ContextType &)
Definition: tasks_base.hpp:28
virtual bool is_allowed(ContextType &)
Definition: tasks_base.hpp:24
task & after(task &impl)
Definition: tasks_base.hpp:44
void apply(ContextType &ctx)
Definition: tasks_base.hpp:55
task & before(task &impl)
Definition: tasks_base.hpp:49
virtual void on_apply(ContextType &)=0
Definition: asset.cpp:15
virtual void apply(ContextType &ctx)=0
virtual bool is_allowed(ContextType &ctx)=0