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 
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 void import_rules(const json &j, const class RuleImportMap &import_map)
53  {
54  throw std::logic_error("import_rules not implemented");
55  }
56 
57 
58  virtual json serialize() const = 0;
59  virtual std::set<RuleID> get_rule_ids() const = 0;
60 
61  virtual const Rule *get_rule(RuleID id) const = 0;
62  Rule *get_rule(RuleID id);
63  Rule *get_rule_nc(RuleID id)
64  {
65  return get_rule(id);
66  }
67 
68  virtual const Rule *get_rule(RuleID id, const UUID &uu) const = 0;
69  Rule *get_rule(RuleID id, const UUID &uu);
70 
71  virtual std::map<UUID, const Rule *> get_rules(RuleID id) const = 0;
72  std::map<UUID, Rule *> get_rules(RuleID id);
73  std::map<UUID, Rule *> get_rules_nc(RuleID id)
74  {
75  return get_rules(id);
76  }
77 
78  template <typename T = Rule> std::vector<const T *> get_rules_sorted(RuleID id) const
79  {
80  auto rs = get_rules(id);
81  std::vector<const T *> rv;
82  rv.reserve(rs.size());
83  for (auto &it : rs) {
84  rv.push_back(dynamic_cast<const T *>(it.second));
85  }
86  std::sort(rv.begin(), rv.end(), [](auto a, auto b) { return a->order < b->order; });
87  return rv;
88  }
89 
90  template <typename T = Rule> std::vector<T *> get_rules_sorted(RuleID id)
91  {
92  std::vector<T *> r;
93  auto rs = static_cast<const Rules *>(this)->get_rules_sorted<T>(id);
94  r.reserve(rs.size());
95  std::transform(rs.begin(), rs.end(), std::back_inserter(r), [](auto x) { return const_cast<T *>(x); });
96  return r;
97  }
98 
99  virtual void remove_rule(RuleID id, const UUID &uu) = 0;
100  virtual Rule *add_rule(RuleID id) = 0;
101  void move_rule(RuleID id, const UUID &uu, int dir);
102 
103  virtual ~Rules();
104 
105  virtual bool can_export() const
106  {
107  return false;
108  }
109 
110 protected:
111  void fix_order(RuleID id);
112 };
113 } // namespace horizon
Definition: rule.hpp:33
Definition: rule.hpp:53
Definition: rules.hpp:19
Definition: rules.hpp:34
Definition: rules.hpp:48
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:166
basic_json<> json
default JSON class
Definition: json_fwd.hpp:61