Horizon
rules.hpp
1 #pragma once
2 #include "clipper/clipper.hpp"
3 #include "common/common.hpp"
4 #include "nlohmann/json_fwd.hpp"
5 #include "rule.hpp"
6 #include "util/uuid.hpp"
7 #include <deque>
8 #include <set>
9 #include <functional>
10 
11 namespace horizon {
12 using json = nlohmann::json;
13 
14 enum class RulesCheckErrorLevel { NOT_RUN, PASS, WARN, FAIL, DISABLED };
15 
16 Color rules_check_error_level_to_color(RulesCheckErrorLevel lev);
17 std::string rules_check_error_level_to_string(RulesCheckErrorLevel lev);
18 
19 class RulesCheckError {
20 public:
21  RulesCheckError(RulesCheckErrorLevel lev);
22  RulesCheckError(RulesCheckErrorLevel lev, const std::string &comment);
23 
24  RulesCheckErrorLevel level = RulesCheckErrorLevel::NOT_RUN;
25  UUID sheet;
26  Coordi location;
27  std::string comment;
28  bool has_location = false;
29  ClipperLib::Paths error_polygons;
30 
31  json serialize() const;
32 };
33 
34 class RulesCheckResult {
35 public:
36  void clear();
37  void update();
38  json serialize() const;
39 
40  RulesCheckErrorLevel level = RulesCheckErrorLevel::NOT_RUN;
41  std::string comment;
42 
43  std::deque<RulesCheckError> errors;
44 };
45 
46 typedef std::function<void(const std::string &)> check_status_cb_t;
47 
48 class Rules {
49 public:
50  Rules();
51  virtual void load_from_json(const json &j) = 0;
52  virtual json serialize() const = 0;
53  virtual std::set<RuleID> get_rule_ids() const = 0;
54 
55  virtual const Rule *get_rule(RuleID id) const = 0;
56  Rule *get_rule(RuleID id);
57  Rule *get_rule_nc(RuleID id)
58  {
59  return get_rule(id);
60  }
61 
62  virtual const Rule *get_rule(RuleID id, const UUID &uu) const = 0;
63  Rule *get_rule(RuleID id, const UUID &uu);
64 
65  virtual std::map<UUID, const Rule *> get_rules(RuleID id) const = 0;
66  std::map<UUID, Rule *> get_rules(RuleID id);
67  std::map<UUID, Rule *> get_rules_nc(RuleID id)
68  {
69  return get_rules(id);
70  }
71 
72  template <typename T = Rule> std::vector<const T *> get_rules_sorted(RuleID id) const
73  {
74  auto rs = get_rules(id);
75  std::vector<const T *> rv;
76  rv.reserve(rs.size());
77  for (auto &it : rs) {
78  rv.push_back(dynamic_cast<const T *>(it.second));
79  }
80  std::sort(rv.begin(), rv.end(), [](auto a, auto b) { return a->order < b->order; });
81  return rv;
82  }
83 
84  template <typename T = Rule> std::vector<T *> get_rules_sorted(RuleID id)
85  {
86  std::vector<T *> r;
87  auto rs = static_cast<const Rules *>(this)->get_rules_sorted<T>(id);
88  r.reserve(rs.size());
89  std::transform(rs.begin(), rs.end(), std::back_inserter(r), [](auto x) { return const_cast<T *>(x); });
90  return r;
91  }
92 
93  virtual void remove_rule(RuleID id, const UUID &uu) = 0;
94  virtual Rule *add_rule(RuleID id) = 0;
95  void move_rule(RuleID id, const UUID &uu, int dir);
96 
97  virtual ~Rules();
98 
99 protected:
100  void fix_order(RuleID id);
101 };
102 } // namespace horizon
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:166
nlohmann::json
basic_json<> json
default JSON class
Definition: json_fwd.hpp:61