Scorum
evaluator_registry.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
5 namespace scorum {
6 namespace chain {
7 
8 class database;
9 
10 template <typename OperationType, typename DbType = data_service_factory_i> class evaluator_registry
11 {
12 public:
13  typedef OperationType operation_type;
14 
15  evaluator_registry(DbType& d)
16  : _db(d)
17  {
18  for (int i = 0; i < OperationType::count(); i++)
19  _op_evaluators.emplace_back();
20  }
21 
22  template <typename EvaluatorType, typename... Args> void register_evaluator(Args&&... args)
23  {
24  _op_evaluators[OperationType::template tag<typename EvaluatorType::operation_type>::value].reset(
25  new EvaluatorType(_db, std::forward<Args>(args)...));
26  }
27 
28  template <typename EvaluatorType> void register_evaluator(EvaluatorType* e)
29  {
30  _op_evaluators[OperationType::template tag<typename EvaluatorType::operation_type>::value].reset(e);
31  }
32 
33  evaluator<OperationType>& get_evaluator(const OperationType& op)
34  {
35  int i_which = op.which();
36  uint64_t u_which = uint64_t(i_which);
37  if (i_which < 0)
38  assert("Negative operation tag" && false);
39  if (u_which >= _op_evaluators.size())
40  assert("No registered evaluator for this operation" && false);
41  std::unique_ptr<evaluator<OperationType>>& eval = _op_evaluators[u_which];
42  if (!eval)
43  assert("No registered evaluator for this operation" && false);
44  return *eval;
45  }
46 
47  std::vector<std::unique_ptr<evaluator<OperationType>>> _op_evaluators;
48  DbType& _db;
49 };
50 
51 } // namespace chain
52 } // namespace scorum
void register_evaluator(Args &&... args)
std::vector< std::unique_ptr< evaluator< OperationType > > > _op_evaluators
evaluator< OperationType > & get_evaluator(const OperationType &op)
void register_evaluator(EvaluatorType *e)
Definition: asset.cpp:15