Clingo
Loading...
Searching...
No Matches
script.hh
1#pragma once
2
3#include <clingo/control.hh>
4#include <clingo/core.hh>
5#include <clingo/symbol.hh>
6
7#include <clingo/script.h>
8
9namespace Clingo {
10
18
20class Script {
21 public:
23 Script() = default;
25 Script(Script &&other) = delete;
27 virtual ~Script() = default;
28
32 void execute(std::string_view code) { do_execute(code); }
33
40 auto call(Library &lib, std::string_view name, SymbolSpan arguments) -> SymbolVector {
41 return do_call(lib, name, arguments);
42 }
43
49 auto callable(std::string_view name, size_t arguments) -> bool { return do_callable(name, arguments); }
50
55 void main(Library &lib, const Control &ctl) { do_main(lib, ctl); }
56
60 auto name() -> std::string_view { return do_name(); }
61
65 auto version() -> std::string_view { return do_version(); }
66
67 private:
68 virtual void do_execute(std::string_view code) = 0;
69 virtual auto do_call(Library &lib, std::string_view name, SymbolSpan arguments) -> SymbolVector = 0;
70 virtual auto do_callable(std::string_view name, size_t arguments) -> bool = 0;
71 virtual void do_main(Library &lib, const Control &ctl) = 0;
72 virtual auto do_name() -> std::string_view = 0;
73 virtual auto do_version() -> std::string_view = 0;
74};
75
77
78namespace Detail {
79
80static constexpr clingo_script_t c_script = {
81 [](char const *code, size_t size, void *data) -> bool {
82 CLINGO_TRY {
83 static_cast<Script *>(data)->execute(std::string_view{code, size});
84 }
85 CLINGO_CATCH;
86 },
87 [](clingo_lib_t *lib, [[maybe_unused]] clingo_location_t const *loc, char const *name, size_t name_size,
89 void *symbol_callback_data, void *data) -> bool {
90 CLINGO_TRY {
91 auto &self = *static_cast<Script *>(data);
92 auto args = transform(arguments, std::next(arguments, static_cast<std::ptrdiff_t>(arguments_size)),
93 [](auto sym) { return Symbol{sym, true}; });
94 auto cpp_lib = Library{lib, true};
95 auto syms = self.call(cpp_lib, {name, name_size}, args);
96 auto const *c_syms = c_cast(syms.data());
98 }
99 CLINGO_CATCH;
100 },
101 [](char const *name, size_t size, size_t arguments, bool *result, void *data) -> bool {
102 CLINGO_TRY {
103 auto &self = *static_cast<Script *>(data);
104 *result = self.callable({name, size}, arguments);
105 }
106 CLINGO_CATCH;
107 },
108 [](clingo_lib_t *lib, clingo_control_t *control, void *data) -> bool {
109 CLINGO_TRY {
110 auto &self = *static_cast<Script *>(data);
111 auto cpp_lib = Library{lib, true};
112 auto cpp_ctl = Control{control, true};
113 self.main(cpp_lib, cpp_ctl);
114 }
115 CLINGO_CATCH;
116 },
117 [](void *data, clingo_string_t *name) {
118 auto &self = *static_cast<Script *>(data);
119 auto str = self.name();
120 name->data = str.data();
121 name->size = str.size();
122 },
123 [](void *data, clingo_string_t *version) {
124 auto &self = *static_cast<Script *>(data);
125 auto str = self.name();
126 version->data = str.data();
127 version->size = str.size();
128 },
129 [](void *data) { std::ignore = std::unique_ptr<Script>(static_cast<Script *>(data)); },
130};
131
132} // namespace Detail
133
134template <std::derived_from<Script> T> auto register_script(Library const &lib, std::unique_ptr<T> script) -> T & {
135 auto &res = *script;
136 Detail::handle_error(clingo_script_register(c_cast(lib), &Detail::c_script, script.release()));
137 return res;
138}
139
140} // namespace Clingo
The main control class for grounding and solving logic programs.
Definition control.hh:179
The main library class for managing global information and logging.
Definition core.hh:471
Interface for custom scripts.
Definition script.hh:20
Script()=default
The default constructor.
auto version() -> std::string_view
Get the version of the script.
Definition script.hh:65
auto name() -> std::string_view
Get the name of the script.
Definition script.hh:60
virtual ~Script()=default
The default destructor.
auto callable(std::string_view name, size_t arguments) -> bool
Callback to check if the given signature is callable.
Definition script.hh:49
void execute(std::string_view code)
Callback to execute the given code.
Definition script.hh:32
auto call(Library &lib, std::string_view name, SymbolSpan arguments) -> SymbolVector
Callback to call the function with the given name and arguments.
Definition script.hh:40
Script(Script &&other)=delete
Disable copy and move operations.
void main(Library &lib, const Control &ctl)
Callback to customize the main function.
Definition script.hh:55
struct clingo_lib clingo_lib_t
A library object storing global information.
Definition core.h:176
struct clingo_location clingo_location_t
Represents a source code location marking its beginning and end.
Definition core.h:359
CLINGO_VISIBILITY_DEFAULT bool clingo_script_register(clingo_lib_t *lib, clingo_script_t const *script, void *data)
Add a custom scripting language to a control object.
bool(* clingo_symbol_callback_t)(clingo_symbol_t const *symbols, size_t symbols_size, void *data)
Callback function to inject symbols.
Definition symbol.h:60
uint64_t clingo_symbol_t
Type to represent a symbol.
Definition symbol.h:51
@ tuple
Theory tuples "(t1,...,tn)".
auto version() -> std::tuple< int, int, int >
Get the version of the Clingo library as a tuple.
Definition core.hh:730
std::vector< Symbol > SymbolVector
A vector of symbols.
Definition symbol.hh:42
std::span< Symbol const > SymbolSpan
A span of symbols, which is a view on a contiguous sequence of symbols.
Definition symbol.hh:38
constexpr auto transform(std::optional< T > &x, F &&f) -> Detail::transform_result< T &, F >
Implemenatation of std::optional<T>::transform.
Definition optional.hh:28
Custom scripting language to run functions during grounding.
Definition script.h:18
Struct to capture strings that are not null-terminated.
Definition core.h:91