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  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 
35 public:
36  void clear();
37  void update();
38  json serialize() const;
39  bool check_disabled(const Rule &rule);
40 
41  RulesCheckErrorLevel level = RulesCheckErrorLevel::NOT_RUN;
42  std::string comment;
43 
44  std::deque<RulesCheckError> errors;
45 };
46 
47 typedef std::function<void(const std::string &)> check_status_cb_t;
48 
49 class Rules {
50 public:
51  Rules();
52  virtual void load_from_json(const json &j) = 0;
53  virtual void import_rules(const json &j, const class RuleImportMap &import_map)
54  {
55  throw std::logic_error("import_rules not implemented");
56  }
57 
58 
59  virtual json serialize() const = 0;
60  virtual std::set<RuleID> get_rule_ids() const = 0;
61 
62  virtual const Rule *get_rule(RuleID id) const = 0;
63  Rule *get_rule(RuleID id);
64  Rule *get_rule_nc(RuleID id)
65  {
66  return get_rule(id);
67  }
68 
69  virtual const Rule *get_rule(RuleID id, const UUID &uu) const = 0;
70  Rule *get_rule(RuleID id, const UUID &uu);
71 
72  virtual std::map<UUID, const Rule *> get_rules(RuleID id) const = 0;
73  std::map<UUID, Rule *> get_rules(RuleID id);
74  std::map<UUID, Rule *> get_rules_nc(RuleID id)
75  {
76  return get_rules(id);
77  }
78 
79  template <typename T = Rule> std::vector<const T *> get_rules_sorted(RuleID id) const
80  {
81  auto rs = get_rules(id);
82  std::vector<const T *> rv;
83  rv.reserve(rs.size());
84  for (auto &it : rs) {
85  rv.push_back(dynamic_cast<const T *>(it.second));
86  }
87  std::sort(rv.begin(), rv.end(), [](auto a, auto b) { return a->order < b->order; });
88  return rv;
89  }
90 
91  template <typename T = Rule> std::vector<T *> get_rules_sorted(RuleID id)
92  {
93  std::vector<T *> r;
94  auto rs = static_cast<const Rules *>(this)->get_rules_sorted<T>(id);
95  r.reserve(rs.size());
96  std::transform(rs.begin(), rs.end(), std::back_inserter(r), [](auto x) { return const_cast<T *>(x); });
97  return r;
98  }
99 
100  virtual void remove_rule(RuleID id, const UUID &uu) = 0;
101  virtual Rule *add_rule(RuleID id) = 0;
102  void move_rule(RuleID id, const UUID &uu, int dir);
103 
104  virtual ~Rules();
105 
106  virtual bool can_export() const
107  {
108  return false;
109  }
110 
111 protected:
112  void fix_order(RuleID id);
113 };
114 } // namespace horizon
Definition: common.hpp:260
Definition: rule.hpp:33
Definition: rule.hpp:53
Definition: rules.hpp:19
Definition: rules.hpp:34
Definition: rules.hpp:49
This class encapsulates a UUID and allows it to be uses as a value type.
Definition: uuid.hpp:16
a class to store JSON values
Definition: json.hpp:170
basic_json<> json
default JSON class
Definition: json_fwd.hpp:62