|
2 | 2 |
|
3 | 3 | #include "solver_core.hpp" |
4 | 4 |
|
| 5 | +#ifdef ORATIO_ENABLE_LISTENERS |
| 6 | +#define NEW_FLAW(f) flaw_created(f) |
| 7 | +#define NEW_RESOLVER(r) resolver_created(r) |
| 8 | +#else |
| 9 | +#define NEW_FLAW(f) |
| 10 | +#define NEW_RESOLVER(r) |
| 11 | +#endif |
| 12 | + |
5 | 13 | namespace ratio |
6 | 14 | { |
| 15 | + class basic_solver; |
| 16 | + class resolver; |
| 17 | + |
| 18 | + class flaw |
| 19 | + { |
| 20 | + public: |
| 21 | + flaw(basic_solver &slv) noexcept; |
| 22 | + flaw(const flaw &) = delete; |
| 23 | + virtual ~flaw() = default; |
| 24 | + |
| 25 | + private: |
| 26 | + virtual void compute_resolvers() = 0; |
| 27 | + |
| 28 | + protected: |
| 29 | + basic_solver &slv; // The solver managing this flaw.. |
| 30 | + }; |
| 31 | + |
| 32 | + class resolver |
| 33 | + { |
| 34 | + public: |
| 35 | + resolver(flaw &flw, utils::rational &&intrinsic_cost) noexcept; |
| 36 | + resolver(const resolver &) = delete; |
| 37 | + virtual ~resolver() = default; |
| 38 | + |
| 39 | + private: |
| 40 | + virtual void apply() = 0; |
| 41 | + |
| 42 | + protected: |
| 43 | + flaw &flw; // The flaw this resolver addresses.. |
| 44 | + utils::rational intrinsic_cost; // The intrinsic cost of applying this resolver.. |
| 45 | + }; |
| 46 | + |
7 | 47 | class basic_solver : public solver_core |
8 | 48 | { |
9 | 49 | public: |
10 | 50 | basic_solver() noexcept; |
11 | 51 |
|
| 52 | + [[nodiscard]] riddle::expr new_enum(riddle::component_type &tp, std::vector<riddle::expr> &&values) override; |
| 53 | + |
| 54 | + /** |
| 55 | + * @brief Creates a new flaw of the given type. |
| 56 | + * |
| 57 | + * @tparam Tp The type of the flaw to create. |
| 58 | + * @tparam Args The types of the arguments to pass to the flaw |
| 59 | + * @param args The arguments to pass to the flaw |
| 60 | + * @return Tp& The created flaw |
| 61 | + */ |
| 62 | + template <typename Tp, typename... Args> |
| 63 | + Tp &new_flaw(Args &&...args) noexcept |
| 64 | + { |
| 65 | + static_assert(std::is_base_of_v<flaw, Tp>, "Tp must be a subclass of flaw"); |
| 66 | + auto f = std::make_unique<Tp>(std::forward<Args>(args)...); |
| 67 | + auto &f_ref = *f; |
| 68 | + NEW_FLAW(f_ref); |
| 69 | + flaws.emplace_back(std::move(f)); |
| 70 | + return f_ref; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @brief Creates a new resolver of the given type. |
| 75 | + * |
| 76 | + * @tparam Tp The type of the resolver to create. |
| 77 | + * @tparam Args The types of the arguments to pass to the resolver |
| 78 | + * @param args The arguments to pass to the resolver |
| 79 | + * @return Tp& The created resolver |
| 80 | + */ |
| 81 | + template <typename Tp, typename... Args> |
| 82 | + Tp &new_resolver(Args &&...args) noexcept |
| 83 | + { |
| 84 | + static_assert(std::is_base_of_v<resolver, Tp>, "Tp must be a subclass of resolver"); |
| 85 | + auto r = std::make_unique<Tp>(std::forward<Args>(args)...); |
| 86 | + auto &r_ref = *r; |
| 87 | + NEW_RESOLVER(r_ref); |
| 88 | + resolvers.emplace_back(std::move(r)); |
| 89 | + return r_ref; |
| 90 | + } |
| 91 | + |
12 | 92 | void solve() override; |
| 93 | + |
| 94 | +#ifdef ORATIO_ENABLE_LISTENERS |
| 95 | + private: |
| 96 | + /** |
| 97 | + * @brief Notifies that a new flaw has been created. |
| 98 | + * |
| 99 | + * This function is called whenever a new flaw is created in the solver. |
| 100 | + * |
| 101 | + * @param f The newly created flaw. |
| 102 | + */ |
| 103 | + virtual void flaw_created([[maybe_unused]] flaw &f) noexcept {} |
| 104 | + |
| 105 | + /** |
| 106 | + * @brief Notifies that a new resolver has been created. |
| 107 | + * |
| 108 | + * This function is called whenever a new resolver is created in the solver. |
| 109 | + * |
| 110 | + * @param r The newly created resolver. |
| 111 | + */ |
| 112 | + virtual void resolver_created([[maybe_unused]] resolver &r) noexcept {} |
| 113 | +#endif |
| 114 | + |
| 115 | + private: |
| 116 | + std::vector<std::unique_ptr<flaw>> flaws; // The set of flaws |
| 117 | + std::vector<std::unique_ptr<resolver>> resolvers; // The set of resolvers |
| 118 | + }; |
| 119 | + |
| 120 | + class enum_flaw final : public flaw |
| 121 | + { |
| 122 | + public: |
| 123 | + enum_flaw(basic_solver &slv, riddle::enum_expr var) noexcept; |
| 124 | + |
| 125 | + [[nodiscard]] const riddle::enum_expr &get_var() const noexcept { return var; } |
| 126 | + |
| 127 | + private: |
| 128 | + void compute_resolvers() override; |
| 129 | + |
| 130 | + private: |
| 131 | + riddle::enum_expr var; |
13 | 132 | }; |
14 | 133 | } // namespace ratio |
0 commit comments