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 "util/uuid_vec.hpp"
8 #include <deque>
9 #include <set>
10 #include <functional>
11 
12 namespace horizon {
13 using json = nlohmann::json;
14 
15 enum class RulesCheckErrorLevel { NOT_RUN, PASS, WARN, FAIL, DISABLED, CANCELLED };
16 
17 Color rules_check_error_level_to_color(RulesCheckErrorLevel lev);
18 std::string rules_check_error_level_to_string(RulesCheckErrorLevel lev);
19 
21 public:
22  RulesCheckError(RulesCheckErrorLevel lev);
23  RulesCheckError(RulesCheckErrorLevel lev, const std::string &comment);
24 
25  RulesCheckErrorLevel level = RulesCheckErrorLevel::NOT_RUN;
26  UUID sheet;
27  UUIDVec instance_path;
28  Coordi location;
29  std::string comment;
30  bool has_location = false;
31  ClipperLib::Paths error_polygons;
32  std::set<int> layers;
33 
34  json serialize() const;
35 };
36 
38 public:
39  void clear();
40  void update();
41  json serialize() const;
42  bool check_disabled(const Rule &rule);
43  bool check_cancelled(bool cancel);
44 
45 
46  RulesCheckErrorLevel level = RulesCheckErrorLevel::NOT_RUN;
47 
48  std::deque<RulesCheckError> errors;
49 };
50 
51 typedef std::function<void(const std::string &)> check_status_cb_t;
52 
53 class Rules {
54 public:
55  Rules();
56  virtual void load_from_json(const json &j) = 0;
57  virtual void import_rules(const json &j, const class RuleImportMap &import_map)
58  {
59  throw std::logic_error("import_rules not implemented");
60  }
61 
62 
63  virtual json serialize() const = 0;
64  virtual std::vector<RuleID> get_rule_ids() const = 0;
65 
66  virtual const Rule &get_rule(RuleID id) const = 0;
67  Rule &get_rule(RuleID id);
68  Rule &get_rule_nc(RuleID id)
69  {
70  return get_rule(id);
71  }
72 
73  template <typename T> const T &get_rule_t() const
74  {
75  return dynamic_cast<const T &>(get_rule(T::id));
76  }
77 
78  template <typename T> T &get_rule_t()
79  {
80  return dynamic_cast<T &>(get_rule(T::id));
81  }
82 
83  virtual const Rule &get_rule(RuleID id, const UUID &uu) const = 0;
84  Rule &get_rule(RuleID id, const UUID &uu);
85 
86  template <typename T> const T &get_rule_t(const UUID &uu) const
87  {
88  return dynamic_cast<const T &>(get_rule(T::id, uu));
89  }
90  template <typename T> T &get_rule_t(const UUID &uu)
91  {
92  return dynamic_cast<T &>(get_rule(T::id, uu));
93  }
94 
95  virtual std::map<UUID, const Rule *> get_rules(RuleID id) const = 0;
96  std::map<UUID, Rule *> get_rules(RuleID id);
97  std::map<UUID, Rule *> get_rules_nc(RuleID id)
98  {
99  return get_rules(id);
100  }
101 
102  template <typename T = Rule> std::vector<const T *> get_rules_sorted(RuleID id) const
103  {
104  auto rs = get_rules(id);
105  std::vector<const T *> rv;
106  rv.reserve(rs.size());
107  for (auto &it : rs) {
108  rv.push_back(dynamic_cast<const T *>(it.second));
109  }
110  std::sort(rv.begin(), rv.end(), [](auto a, auto b) { return a->order < b->order; });
111  return rv;
112  }
113 
114  template <typename T> std::vector<const T *> get_rules_sorted() const
115  {
116  return get_rules_sorted<T>(T::id);
117  }
118 
119  template <typename T = Rule> std::vector<T *> get_rules_sorted(RuleID id)
120  {
121  std::vector<T *> r;
122  auto rs = static_cast<const Rules *>(this)->get_rules_sorted<T>(id);
123  r.reserve(rs.size());
124  std::transform(rs.begin(), rs.end(), std::back_inserter(r), [](auto x) { return const_cast<T *>(x); });
125  return r;
126  }
127 
128  template <typename T> std::vector<T *> get_rules_sorted()
129  {
130  return get_rules_sorted<T>(T::id);
131  }
132 
133  virtual void remove_rule(RuleID id, const UUID &uu) = 0;
134  template <typename T> T &add_rule_t()
135  {
136  return dynamic_cast<T &>(add_rule(T::id));
137  }
138  virtual Rule &add_rule(RuleID id) = 0;
139  void move_rule(RuleID id, const UUID &uu, int dir);
140 
141  virtual ~Rules();
142 
143  virtual bool can_export() const
144  {
145  return false;
146  }
147 
148 protected:
149  void fix_order(RuleID id);
150 };
151 } // namespace horizon
Definition: common.hpp:278
Definition: rule.hpp:37
Definition: rule.hpp:57
Definition: rules.hpp:20
Definition: rules.hpp:37
Definition: rules.hpp:53
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:177
defer< id, T > id
Definition: meta.hpp:585
_t< detail::sort_< L, Fn > > sort
Return a new meta::list that is sorted according to invocable predicate Fn.
Definition: meta.hpp:3277
_t< detail::transform_< list< Args... > >> transform
Return a new meta::list constructed by transforming all the elements in L with the unary invocable Fn...
Definition: meta.hpp:1855
basic_json<> json
default JSON class
Definition: json_fwd.hpp:62