3#include <clingo/control.hh>
55 void add(std::string_view group, std::string_view option, std::string_view description,
Parser parser,
56 bool multi =
false, std::optional<std::string_view> argument = std::nullopt) {
57 parsers_->emplace_front(std::move(parser));
58 static constexpr auto cparser = [](
char const *
value,
size_t size,
void *data) ->
bool {
59 auto &parser = *
static_cast<Parser *
>(data);
61 parser({
value, size});
66 clingo_options_add(opts_, group.data(), group.size(), option.data(), option.size(), description.data(),
67 description.size(), cparser,
static_cast<void *
>(&parsers_->front()), multi,
68 argument ? argument->data() :
nullptr, argument ? argument->size() : 0));
82 void add_flag(std::string_view group, std::string_view option, std::string_view description,
bool &flag) {
84 description.data(), description.size(), &flag));
112 auto program_name() noexcept -> std::string_view {
return do_program_name(); }
127 void main(
Control const &control, std::span<std::string_view const> files) { do_main(control, files); }
148 virtual void do_main(
Control const &control, std::span<std::string_view const> files) {
152 virtual void do_print_model([[maybe_unused]] ConstModel model,
ModelPrinter const &printer) { printer(); }
153 virtual void do_register_options([[maybe_unused]] Options options) {}
154 virtual void do_validate_options() {}
155 virtual auto do_program_name() noexcept -> std::string_view {
return CLINGO_EXECUTABLE; }
156 virtual auto do_version() noexcept -> std::string_view {
return CLINGO_VERSION; }
168 auto &app_data = *
static_cast<AppData *
>(data);
170 name->data = str.data();
171 name->size = str.size();
174 auto &app_data = *
static_cast<AppData *
>(data);
175 auto str = app_data.app->program_version();
179 [](clingo_control_t *ctl,
clingo_string_t const *files,
size_t size,
void *data) ->
bool {
180 auto &app_data = *
static_cast<AppData *
>(data);
182 auto cpp_ctl = Control{ctl,
true};
184 transform(std::span{files, size}, [](
auto const &x) {
return std::string_view{x.data, x.size}; });
185 app_data.app->main(cpp_ctl, cpp_files);
190 auto &app_data = *
static_cast<AppData *
>(data);
192 app_data.app->print_model(ConstModel{
model},
193 [printer, printer_data]() { handle_error(printer(printer_data)); });
198 auto &app_data = *
static_cast<AppData *
>(data);
200 app_data.app->register_options(Options{options, app_data.parsers});
204 [](
void *data) ->
bool {
205 auto &app_data = *
static_cast<AppData *
>(data);
207 app_data.app->validate_options();
221inline auto main(
Library &lib, std::span<std::string_view const> arguments = {}, App *app =
nullptr,
222 bool raise_errors =
false) ->
int {
225 auto c_args = Detail::transform(arguments, [](
auto const &x) {
return clingo_string_t{x.
data(), x.size()}; });
226 auto data = Detail::AppData{app, {}};
227 Detail::handle_error_no_code(
clingo_main(c_cast(lib), c_args.data(), c_args.size(),
228 app !=
nullptr ? &Detail::c_app : nullptr,
229 app != nullptr ? static_cast<void *>(&data) : nullptr, &code));
230 }
catch (std::exception
const &e) {
235 fprintf(stderr,
"*** ERROR: (%.*s): %s\n",
static_cast<int>(name.size()), name.data(), e.what());
249inline auto main(
Library &lib, std::initializer_list<std::string_view const> arguments,
App *app =
nullptr,
250 bool raise_errors =
false) ->
int {
251 return main(lib, std::span{arguments}, app, raise_errors);
Interface to build applications on top of clingo.
Definition app.hh:96
App(App &&other)=delete
Disable copying and moving.
void print_model(ConstModel model, ModelPrinter const &printer)
Customize model printing.
Definition app.hh:135
App()=default
The default constructor.
auto operator=(App &&other) -> App &=delete
Disable copying and moving.
void main(Control const &control, std::span< std::string_view const > files)
Run the main control flow.
Definition app.hh:127
void validate_options()
Validate previously parsed options.
Definition app.hh:145
auto program_name() noexcept -> std::string_view
Get the name of the application.
Definition app.hh:112
void register_options(Options options)
Optionally register additional application options.
Definition app.hh:140
virtual ~App()=default
The default destructor.
auto program_version() noexcept -> std::string_view
Get the version of the application.
Definition app.hh:119
Class to provide an immutable view of a model.
Definition solve.hh:121
The main control class for grounding and solving logic programs.
Definition control.hh:228
void parse_files(StringSpan files) const
Parse files with the given paths.
Definition control.hh:291
void main() const
Run the default ground and solve flow.
Definition control.hh:445
The main library class for managing global information and logging.
Definition core.hh:471
Object to add command-line options.
Definition app.hh:17
void add(std::string_view group, std::string_view option, std::string_view description, Parser parser, bool multi=false, std::optional< std::string_view > argument=std::nullopt)
Add an option that is processed with a custom parser.
Definition app.hh:55
Options(clingo_options_t *opts, ParserList &parsers)
Construct an options object.
Definition app.hh:33
std::function< void(std::string_view value)> Parser
An option parser.
Definition app.hh:23
friend auto c_cast(Options const &x) -> clingo_options_t *
Convert the class to it's underlying C type.
Definition app.hh:36
std::forward_list< Parser > ParserList
A list of option parsers.
Definition app.hh:25
void add_flag(std::string_view group, std::string_view option, std::string_view description, bool &flag)
Add an option that is a simple flag.
Definition app.hh:82
bool(* clingo_default_model_printer_t)(void *data)
Callback to print a model in default format.
Definition app.h:62
struct clingo_options clingo_options_t
Object to add command-line options.
Definition app.h:46
CLINGO_VISIBILITY_DEFAULT bool clingo_options_add_flag(clingo_options_t *options, char const *group, size_t group_size, char const *option, size_t option_size, char const *description, size_t description_size, bool *target)
Add an option that is a simple flag.
CLINGO_VISIBILITY_DEFAULT bool clingo_main(clingo_lib_t *lib, clingo_string_t const *arguments, size_t size, clingo_application_t const *app, void *data, int *code)
Run an application with the given library and arguments.
CLINGO_VISIBILITY_DEFAULT bool clingo_options_add(clingo_options_t *options, char const *group, size_t group_size, char const *option, size_t option_size, char const *description, size_t description_size, clingo_option_parser_t parser, void *data, bool multi, char const *argument, size_t argument_size)
Add an option that is processed with a custom parser.
#define CLINGO_VERSION
String representation of version.
Definition core.h:68
#define CLINGO_EXECUTABLE
The name of the clingo executable.
Definition core.h:70
struct clingo_model clingo_model_t
Object representing a model.
Definition model.h:43
@ model
The model represents a stable model.
std::function< void()> ModelPrinter
A callback to print the current model.
Definition app.hh:93
auto main(Library &lib, std::span< std::string_view const > arguments={}, App *app=nullptr, bool raise_errors=false) -> int
Run a clingo based application with the given arguments.
Definition app.hh:221
@ value
The configuration entry is a double value.
auto version() -> std::tuple< int, int, int >
Get the version of the Clingo library as a tuple.
Definition core.hh:730
constexpr auto transform(std::optional< T > &x, F &&f) -> Detail::transform_result< T &, F >
Implemenatation of std::optional<T>::transform.
Definition optional.hh:28
This struct contains a set of functions to customize the clingo application.
Definition app.h:75
void(* program_name)(void *data, clingo_string_t *string)
callback to obtain program name
Definition app.h:76
Struct to capture strings that are not null-terminated.
Definition core.h:86
char const * data
pointer to the beginning of the string
Definition core.h:87