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 auto operator=(Script &&other) -> Script & = delete;
29 virtual ~Script() = default;
30
34 void execute(std::string_view code) { do_execute(code); }
35
42 auto call(Library &lib, std::string_view name, SymbolSpan arguments) -> SymbolVector {
43 return do_call(lib, name, arguments);
44 }
45
51 auto callable(std::string_view name, size_t arguments) -> bool { return do_callable(name, arguments); }
52
57 void main(Library &lib, const Control &ctl) { do_main(lib, ctl); }
58
62 auto name() -> std::string_view { return do_name(); }
63
67 auto version() -> std::string_view { return do_version(); }
68
69 private:
70 virtual void do_execute(std::string_view code) = 0;
71 virtual auto do_call(Library &lib, std::string_view name, SymbolSpan arguments) -> SymbolVector = 0;
72 virtual auto do_callable(std::string_view name, size_t arguments) -> bool = 0;
73 virtual void do_main(Library &lib, const Control &ctl) = 0;
74 virtual auto do_name() -> std::string_view = 0;
75 virtual auto do_version() -> std::string_view = 0;
76};
77
85template <Detail::UserData<Script, false> T> auto register_script(Library const &lib, T &&script) -> decltype(auto) {
86 using UserData = Detail::UserDataTraits<T>;
87 auto user_data = UserData::create(std::forward<T>(script));
88 if (!UserData::has_value(user_data)) {
89 throw std::invalid_argument("script cannot be null");
90 }
91
92 static constexpr clingo_script_t c_script = {
93 [](char const *code, size_t size, void *data) -> bool {
94 CLINGO_TRY {
95 UserData::cast(data)->execute(std::string_view{code, size});
96 }
97 CLINGO_CATCH;
98 },
99 [](clingo_lib_t *lib, [[maybe_unused]] clingo_location_t const *loc, char const *name, size_t name_size,
100 clingo_symbol_t const *arguments, size_t arguments_size, clingo_symbol_callback_t symbol_callback,
101 void *symbol_callback_data, void *data) -> bool {
102 CLINGO_TRY {
103 auto args =
104 Detail::transform(arguments, std::next(arguments, static_cast<std::ptrdiff_t>(arguments_size)),
105 [](auto sym) { return Symbol{sym, true}; });
106 auto cpp_lib = Library{lib, true};
107 auto syms = UserData::cast(data)->call(cpp_lib, {name, name_size}, args);
108 auto const *c_syms = c_cast(syms.data());
109 return symbol_callback(c_syms, syms.size(), symbol_callback_data);
110 }
111 CLINGO_CATCH;
112 },
113 [](char const *name, size_t size, size_t arguments, bool *result, void *data) -> bool {
114 CLINGO_TRY {
115 *result = UserData::cast(data)->callable({name, size}, arguments);
116 }
117 CLINGO_CATCH;
118 },
119 [](clingo_lib_t *lib, clingo_control_t *control, void *data) -> bool {
120 CLINGO_TRY {
121 auto cpp_lib = Library{lib, true};
122 auto cpp_ctl = Control{control, true};
123 UserData::cast(data)->main(cpp_lib, cpp_ctl);
124 }
125 CLINGO_CATCH;
126 },
127 [](void *data, clingo_string_t *name) {
128 auto str = UserData::cast(data)->name();
129 name->data = str.data();
130 name->size = str.size();
131 },
132 [](void *data, clingo_string_t *version) {
133 auto str = UserData::cast(data)->version();
134 version->data = str.data();
135 version->size = str.size();
136 },
137 [](void *data) { UserData::free(data); },
138 };
139 auto &res = *UserData::get(user_data);
140 Detail::handle_error(clingo_script_register(c_cast(lib), &c_script, UserData::release(user_data)));
141 return res;
142}
143
145
146} // namespace Clingo
The main control class for grounding and solving logic programs.
Definition control.hh:159
void main() const
Run the default ground and solve flow.
Definition control.hh:465
The main library class for managing global information and logging.
Definition core.hh:564
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:67
auto name() -> std::string_view
Get the name of the script.
Definition script.hh:62
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:51
void execute(std::string_view code)
Callback to execute the given code.
Definition script.hh:34
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:42
Script(Script &&other)=delete
Disable copy and move operations.
auto operator=(Script &&other) -> Script &=delete
Disable copy and move operations.
void main(Library &lib, const Control &ctl)
Callback to customize the main function.
Definition script.hh:57
Class modeling a symbol in Clingo.
Definition symbol.hh:68
struct clingo_lib clingo_lib_t
A library object storing global information.
Definition core.h:171
struct clingo_location clingo_location_t
Represents a source code location marking its beginning and end.
Definition core.h:354
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
auto version() -> std::tuple< int, int, int >
Get the version of the Clingo library as a tuple.
Definition core.hh:823
auto register_script(Library const &lib, T &&script) -> decltype(auto)
Register the given script with the library.
Definition script.hh:85
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
Custom scripting language to run functions during grounding.
Definition script.h:18
Struct to capture strings that are not null-terminated.
Definition core.h:86