hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
answer.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_ANSWER_HPP
7 #define IROHA_ANSWER_HPP
8 
9 #include <ciso646>
10 #include <map>
11 #include <vector>
12 
13 #include <boost/range/numeric.hpp>
14 #include "utils/string_builder.hpp"
15 
16 namespace shared_model {
17  namespace validation {
18 
19  using ConcreteReasonType = std::string;
20  using GroupedReasons = std::vector<ConcreteReasonType>;
21  using ReasonsGroupName = std::string;
22  using ReasonsGroupType = std::pair<ReasonsGroupName, GroupedReasons>;
23 
28  class Answer {
29  public:
30  operator bool() const {
31  return not reasons_map_.empty();
32  }
33 
37  std::string reason() const {
38  return boost::accumulate(
39  reasons_map_,
40  std::string{},
41  [](auto &&acc, const auto &command_reasons) {
43  .init(command_reasons.first)
44  .appendAll(command_reasons.second,
45  [](auto &element) { return element; })
46  .finalize()
47  + "\n";
48  return std::forward<decltype(acc)>(acc);
49  });
50  }
51 
56  bool hasErrors() {
57  return not reasons_map_.empty();
58  }
59 
64  void addReason(ReasonsGroupType &&reasons) {
65  reasons_map_.insert(std::move(reasons));
66  }
67 
68  std::map<ReasonsGroupName, GroupedReasons> getReasonsMap() {
69  return reasons_map_;
70  };
71 
72  private:
73  std::map<ReasonsGroupName, GroupedReasons> reasons_map_;
74  };
75 
76  } // namespace validation
77 } // namespace shared_model
78 
79 #endif // IROHA_ANSWER_HPP
std::string reason() const
Definition: answer.hpp:37
PrettyStringBuilder & init(const std::string &name)
Definition: string_builder.cpp:18
void addReason(ReasonsGroupType &&reasons)
Definition: answer.hpp:64
std::string finalize()
Definition: string_builder.cpp:52
bool hasErrors()
Definition: answer.hpp:56
std::string ConcreteReasonType
Definition: answer.hpp:19
PrettyStringBuilder & appendAll(Collection &&c, Transform &&t)
Definition: string_builder.hpp:56
std::string ReasonsGroupName
Definition: answer.hpp:21
Definition: string_builder.hpp:16
std::map< ReasonsGroupName, GroupedReasons > getReasonsMap()
Definition: answer.hpp:68
std::vector< ConcreteReasonType > GroupedReasons
Definition: answer.hpp:20
std::pair< ReasonsGroupName, GroupedReasons > ReasonsGroupType
Definition: answer.hpp:22
Definition: command_executor.hpp:12
Definition: answer.hpp:28