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 
20 public:
21  RulesCheckError(RulesCheckErrorLevel lev);
22 
23  RulesCheckErrorLevel level = RulesCheckErrorLevel::NOT_RUN;
24  UUID sheet;
25  Coordi location;
26  std::string comment;
27  bool has_location = false;
28  ClipperLib::Paths error_polygons;
29 
30  json serialize() const;
31 };
32 
34 public:
35  void clear();
36  void update();
37  json serialize() const;
38 
39  RulesCheckErrorLevel level = RulesCheckErrorLevel::NOT_RUN;
40  std::string comment;
41 
42  std::deque<RulesCheckError> errors;
43 };
44 
45 typedef std::function<void(const std::string &)> check_status_cb_t;
46 
47 class Rules {
48 public:
49  Rules();
50  virtual void load_from_json(const json &j) = 0;
51  virtual json serialize() const = 0;
52  virtual std::set<RuleID> get_rule_ids() const = 0;
53 
54  virtual const Rule *get_rule(RuleID id) const = 0;
55  Rule *get_rule(RuleID id);
56 
57  virtual const Rule *get_rule(RuleID id, const UUID &uu) const = 0;
58  Rule *get_rule(RuleID id, const UUID &uu);
59 
60  virtual std::map<UUID, const Rule *> get_rules(RuleID id) const = 0;
61  std::map<UUID, Rule *> get_rules(RuleID id);
62  std::map<UUID, Rule *> get_rules_nc(RuleID id)
63  {
64  return get_rules(id);
65  }
66 
67  template <typename T = Rule> std::vector<const T *> get_rules_sorted(RuleID id) const
68  {
69  auto rs = get_rules(id);
70  std::vector<const T *> rv;
71  rv.reserve(rs.size());
72  for (auto &it : rs) {
73  rv.push_back(dynamic_cast<const T *>(it.second));
74  }
75  std::sort(rv.begin(), rv.end(), [](auto a, auto b) { return a->order < b->order; });
76  return rv;
77  }
78 
79  template <typename T = Rule> std::vector<T *> get_rules_sorted(RuleID id)
80  {
81  std::vector<T *> r;
82  auto rs = static_cast<const Rules *>(this)->get_rules_sorted<T>(id);
83  r.reserve(rs.size());
84  std::transform(rs.begin(), rs.end(), std::back_inserter(r), [](auto x) { return const_cast<T *>(x); });
85  return r;
86  }
87 
88  virtual void remove_rule(RuleID id, const UUID &uu) = 0;
89  virtual Rule *add_rule(RuleID id) = 0;
90  void move_rule(RuleID id, const UUID &uu, int dir);
91 
92  virtual ~Rules();
93 
94 protected:
95  void fix_order(RuleID id);
96 };
97 } // namespace horizon
nlohmann::json
basic_json<> json
default JSON class
Definition: json_fwd.hpp:61
horizon::Rules
Definition: rules.hpp:47
horizon::Rule
Definition: rule.hpp:30
horizon::RulesCheckError
Definition: rules.hpp:19
horizon::Coord< int64_t >
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:166
horizon::UUID
This class encapsulates a UUID and allows it to be uses as a value type.
Definition: uuid.hpp:16
horizon::RulesCheckResult
Definition: rules.hpp:33