Clingo
Loading...
Searching...
No Matches
core.hh
1#pragma once
2
3#include <clingo/core.h>
4#include <clingo/shared.h>
5
6#include <algorithm>
7#include <cassert>
8#include <functional>
9#include <iterator>
10#include <memory>
11#include <span>
12#include <stdexcept>
13#include <utility>
14#include <vector>
15
16namespace Clingo {
17
18namespace Detail {
19
20template <typename> inline constexpr bool always_false = false;
21
22#ifndef __clang_analyzer__
23#define CLINGO_ENABLE_BITSET_ENUM(E, ...) \
24 [[nodiscard]] CLINGO_ENUM_OP(~, (E a), __VA_ARGS__)->E { \
25 return static_cast<E>(~static_cast<std::underlying_type_t<E>>(a)); \
26 } \
27 [[nodiscard]] CLINGO_ENUM_OP(|, (E a, E b), __VA_ARGS__)->E { \
28 return static_cast<E>(static_cast<std::underlying_type_t<E>>(a) | static_cast<std::underlying_type_t<E>>(b)); \
29 } \
30 CLINGO_ENUM_OP(|=, (E & a, E b), __VA_ARGS__)->E & { \
31 return a = a | b; \
32 } \
33 [[nodiscard]] CLINGO_ENUM_OP(&, (E a, E b), __VA_ARGS__)->E { \
34 return static_cast<E>(static_cast<std::underlying_type_t<E>>(a) & static_cast<std::underlying_type_t<E>>(b)); \
35 } \
36 CLINGO_ENUM_OP(&=, (E & a, E b), __VA_ARGS__)->E & { \
37 return a = a & b; \
38 } \
39 [[nodiscard]] CLINGO_ENUM_OP(-, (E a, E b), __VA_ARGS__)->E { \
40 return static_cast<E>(static_cast<std::underlying_type_t<E>>(a) & static_cast<std::underlying_type_t<E>>(~b)); \
41 } \
42 CLINGO_ENUM_OP(-=, (E & a, E b), __VA_ARGS__)->E & { \
43 return a = a - b; \
44 } \
45 [[nodiscard]] CLINGO_ENUM_OP(^, (E a, E b), __VA_ARGS__)->E { \
46 return static_cast<E>(static_cast<std::underlying_type_t<E>>(a) ^ static_cast<std::underlying_type_t<E>>(b)); \
47 } \
48 CLINGO_ENUM_OP(^=, (E & a, E b), __VA_ARGS__)->E & { \
49 return a = a ^ b; \
50 } \
51 [[nodiscard]] [[maybe_unused]] inline __VA_ARGS__ constexpr auto intersects(E a, E b) -> bool { \
52 return static_cast<std::underlying_type_t<E>>(a & b) != 0; \
53 } \
54 static_assert(std::is_enum_v<E>)
55#define CLINGO_ENUM_OP(op, arg, ...) [[maybe_unused]] inline __VA_ARGS__ constexpr auto operator op arg noexcept
56#else
57#define CLINGO_ENABLE_BITSET_ENUM(E, ...) \
58 [[nodiscard]] CLINGO_ENUM_OP(~, (E a), __VA_ARGS__)->E; \
59 [[nodiscard]] CLINGO_ENUM_OP(|, (E a, E b), __VA_ARGS__)->E; \
60 CLINGO_ENUM_OP(|=, (E & a, E b), __VA_ARGS__)->E &; \
61 [[nodiscard]] CLINGO_ENUM_OP(&, (E a, E b), __VA_ARGS__)->E; \
62 CLINGO_ENUM_OP(&=, (E & a, E b), __VA_ARGS__)->E &; \
63 [[nodiscard]] CLINGO_ENUM_OP(-, (E a, E b), __VA_ARGS__)->E; \
64 CLINGO_ENUM_OP(-=, (E & a, E b), __VA_ARGS__)->E &; \
65 [[nodiscard]] CLINGO_ENUM_OP(^, (E a, E b), __VA_ARGS__)->E; \
66 CLINGO_ENUM_OP(^=, (E & a, E b), __VA_ARGS__)->E &; \
67 [[nodiscard]] [[maybe_unused]] __VA_ARGS__ auto intersects(E a, E b) -> bool; \
68 static_assert(std::is_enum_v<E>)
69#define CLINGO_ENUM_OP(op, arg, ...) [[maybe_unused]] __VA_ARGS__ auto operator op arg noexcept
70#endif
71
72#define CLINGO_TRY try
73#define CLINGO_CATCH \
74 catch (...) { \
75 return Clingo::Detail::store_error(); \
76 } \
77 return true
78
80inline auto store_error() -> bool {
81 try {
82 throw;
83 } catch (std::out_of_range const &e) {
84 auto msg = std::string_view{e.what()};
85 return clingo_set_error(clingo_result_range, msg.data(), msg.size());
86 } catch (std::invalid_argument const &e) {
87 auto msg = std::string_view{e.what()};
88 return clingo_set_error(clingo_result_invalid, msg.data(), msg.size());
89 } catch (std::bad_alloc const &e) {
90 auto msg = std::string_view{e.what()};
91 return clingo_set_error(clingo_result_bad_alloc, msg.data(), msg.size());
92 } catch (std::logic_error const &e) {
93 auto msg = std::string_view{e.what()};
94 return clingo_set_error(clingo_result_logic, msg.data(), msg.size());
95 } catch (std::exception const &e) {
96 auto msg = std::string_view{e.what()};
97 return clingo_set_error(clingo_result_runtime, msg.data(), msg.size());
98 } catch (...) {
99 auto msg = std::string_view{"no message"};
100 return clingo_set_error(clingo_result_runtime, msg.data(), msg.size());
101 }
102}
103
105inline void raise_error() {
106 clingo_string_t str;
108 clingo_get_error(&rc, &str);
109 switch (rc) {
111 throw std::bad_alloc{};
112 }
113 case clingo_result_logic: {
114 throw std::logic_error{std::string{str.data, str.size}};
115 }
117 throw std::invalid_argument{std::string{str.data, str.size}};
118 }
119 case clingo_result_range: {
120 throw std::out_of_range{std::string{str.data, str.size}};
121 }
122 default: {
123 throw std::runtime_error{std::string{str.data, str.size}};
124 }
125 }
126}
127
131inline void handle_error(bool res) {
132 if (!res) {
133 raise_error();
134 }
135}
136
137// Similar to handle_error but also reraises if the code is success.
138inline void handle_error_no_code(bool res) {
139 if (!res) {
140 raise_error();
141 }
142 clingo_string_t str;
144 clingo_get_error(&rc, &str);
145 if (rc != clingo_result_success) {
146 raise_error();
147 }
148}
149
150template <typename> struct funptr_traits;
151
152template <typename R, typename... As> struct funptr_traits<R (*)(As...)> {
153 using return_type = R;
154 using args_tuple = std::tuple<As...>;
155 template <std::size_t N> using arg = std::tuple_element_t<N, args_tuple>;
156
157 static constexpr std::size_t arity = sizeof...(As);
158 using last = arg<arity - 1>;
159};
160
161template <auto F, class... As> auto call(As &&...args) {
162 using Traits = funptr_traits<decltype(F)>;
163 auto res = std::remove_pointer_t<typename Traits::last>{};
164 if constexpr (std::is_same_v<typename Traits::return_type, void>) {
165 F(std::forward<As>(args)..., &res);
166 } else {
167 handle_error(F(std::forward<As>(args)..., &res));
168 }
169 return res;
170}
171
172template <auto F> struct Free {
173 template <typename Ptr> void operator()(Ptr p) const noexcept { F(p); }
174};
175
176template <class T> struct value_handle {
177 public:
178 using pointer = typename T::pointer;
179
180 value_handle() = default;
181
182 value_handle(const value_handle &other) : ptr_{other ? T::copy(other.ptr_) : nullptr} {}
183
184 value_handle(value_handle &&other) noexcept : ptr_{std::exchange(other.ptr_, nullptr)} {}
185
186 explicit value_handle(pointer ptr, bool copy) : ptr_{copy && ptr != nullptr ? T::copy(ptr) : ptr} {}
187
188 ~value_handle() {
189 if (*this) {
190 T::free(ptr_);
191 }
192 }
193
194 auto operator=(const value_handle &other) -> value_handle & {
195 if (this != &other) {
196 if (*this) {
197 T::free(ptr_);
198 }
199 ptr_ = other ? T::copy(other.ptr_) : nullptr;
200 }
201 return *this;
202 }
203
204 auto operator=(value_handle &&other) noexcept -> value_handle & {
205 if (this != &other) {
206 if (*this) {
207 T::free(ptr_);
208 }
209 ptr_ = std::exchange(other.ptr_, nullptr);
210 }
211 return *this;
212 }
213
214 [[nodiscard]] auto get() const -> pointer { return ptr_; }
215 explicit operator bool() const { return ptr_ != nullptr; }
216
217 private:
218 pointer ptr_ = nullptr;
219};
220
221template <auto Copy, auto Free> struct value_handle_traits {
222 using pointer = std::remove_pointer_t<typename funptr_traits<decltype(Copy)>::template arg<1>>;
223 using const_pointer = typename funptr_traits<decltype(Copy)>::template arg<0>;
224 static auto copy(pointer p) -> pointer {
225 auto res = pointer{};
226 handle_error(Copy(p, &res));
227 return res;
228 }
229 static void free(const_pointer p) noexcept { Free(p); }
230};
231
232template <class T, auto F> using unique_handle = std::unique_ptr<T, Free<F>>;
233
235template <std::input_iterator It, std::sentinel_for<It> S, class Pred> auto transform(It begin, S end, Pred pred) {
236 auto p = std::vector<std::invoke_result_t<Pred, std::iter_reference_t<It>>>{};
237 p.reserve(std::distance(begin, end));
238 std::transform(begin, end, std::back_inserter(p), pred);
239 return p;
240}
241
243template <std::ranges::input_range Rng, class Pred> auto transform(Rng &&rng, Pred pred) { // NOLINT
244 return transform(std::ranges::begin(rng), std::ranges::end(rng), pred);
245}
246
250template <class T> auto hash_value(T const &x) {
251 return std::hash<T>{}(x);
252}
253
255inline auto hash_combine(size_t a, size_t b) -> size_t {
256 // NOLINTBEGIN
257 auto p = std::make_pair(a, b);
258 return std::hash<std::string_view>{}(std::string_view(reinterpret_cast<char const *>(&p), sizeof(decltype(p))));
259 // NOLINTEND
260}
261
262template <class T, class P> class intrusive_handle {
263 public:
264 intrusive_handle() = default;
265 explicit intrusive_handle(P *ptr, bool inc = true) : ptr_{ptr} {
266 if (inc) {
267 T::acquire(ptr);
268 }
269 }
270 intrusive_handle(intrusive_handle const &other) : intrusive_handle{other.ptr_} {}
271 intrusive_handle(intrusive_handle &&other) noexcept : ptr_{std::exchange(other.ptr_, nullptr)} {}
272 ~intrusive_handle() noexcept { T::release(ptr_); }
273 auto operator=(intrusive_handle const &other) -> intrusive_handle & {
274 T::acquire(other.ptr_);
275 T::release(ptr_);
276 ptr_ = other.ptr_;
277 return *this;
278 }
279 auto operator=(intrusive_handle &&other) noexcept -> intrusive_handle & {
280 T::release(ptr_);
281 ptr_ = std::exchange(other.ptr_, nullptr);
282 return *this;
283 }
284 [[nodiscard]] auto get() const noexcept -> P * { return ptr_; }
285 void reset(std::nullptr_t = nullptr) noexcept {
286 T::release(ptr_);
287 ptr_ = nullptr;
288 }
289 void reset(P *ptr, bool inc = true) {
290 T::release(ptr_);
291 ptr_ = ptr;
292 if (inc) {
293 T::acquire(ptr_);
294 }
295 }
296 friend auto operator==(intrusive_handle const &p, std::nullptr_t) noexcept -> bool { return p.ptr_ == nullptr; }
297 friend auto operator!=(intrusive_handle const &p, std::nullptr_t) noexcept -> bool { return p.ptr_ != nullptr; }
298 friend auto operator==(std::nullptr_t, intrusive_handle const &p) noexcept -> bool { return p.ptr_ == nullptr; }
299 friend auto operator!=(std::nullptr_t, intrusive_handle const &p) noexcept -> bool { return p.ptr_ != nullptr; }
300
301 private:
302 P *ptr_ = nullptr;
303};
304
305template <typename T> class ArrowProxy {
306 public:
307 constexpr ArrowProxy(T value) : value_(std::move(value)) {}
308 constexpr auto operator->() -> T * { return &value_; }
309
310 private:
311 T value_;
312};
313
314template <class Seq> class RandomAccessIterator {
315 public:
316 using iterator_category = std::random_access_iterator_tag;
317 using value_type = typename Seq::value_type;
318 using size_type = typename Seq::size_type;
319 using difference_type = typename Seq::difference_type;
320 using pointer = typename Seq::pointer;
321 using reference = typename Seq::reference;
322
323 // NOTE: Added to fulfill the sentinel_for concept; should not be used.
324 constexpr RandomAccessIterator() : view_{throw std::logic_error("invalid iterator")}, index_{0} {}
325 constexpr RandomAccessIterator(Seq container, size_t index) noexcept : view_{std::move(container)}, index_{index} {}
326 constexpr auto operator*() const -> reference { return view_.at(index_); }
327 constexpr auto operator->() const -> pointer { return view_.at(index_); }
328 constexpr auto operator++() -> RandomAccessIterator & {
329 ++index_;
330 return *this;
331 }
332 constexpr auto operator++(int) -> RandomAccessIterator {
333 auto tmp = *this;
334 ++index_;
335 return tmp;
336 }
337 constexpr auto operator--() -> RandomAccessIterator & {
338 --index_;
339 return *this;
340 }
341 constexpr auto operator--(int) -> RandomAccessIterator {
342 auto tmp = *this;
343 --index_;
344 return tmp;
345 }
346 constexpr auto operator-(const RandomAccessIterator &other) const -> difference_type {
347 return static_cast<difference_type>(index_) - static_cast<difference_type>(other.index_);
348 }
349 constexpr auto operator+(difference_type n) const -> RandomAccessIterator {
350 return RandomAccessIterator(view_, index_ + n);
351 }
352 friend constexpr auto operator+(difference_type n, RandomAccessIterator it) -> RandomAccessIterator {
353 return RandomAccessIterator(it.view_, it.index_ + n);
354 }
355 constexpr auto operator-(difference_type n) const -> RandomAccessIterator {
356 return RandomAccessIterator(view_, index_ - n);
357 }
358 constexpr auto operator+=(difference_type n) -> RandomAccessIterator & {
359 index_ += n;
360 return *this;
361 }
362 constexpr auto operator-=(difference_type n) -> RandomAccessIterator & {
363 index_ -= n;
364 return *this;
365 }
366 constexpr auto operator==(const RandomAccessIterator &other) const -> bool { return index_ == other.index_; }
367 constexpr auto operator<=>(const RandomAccessIterator &other) const { return index_ <=> other.index_; }
368 constexpr auto operator[](difference_type n) const -> reference { return view_.at(index_ + n); }
369
370 private:
371 Seq view_;
372 size_t index_;
373};
374
375} // namespace Detail
376
380
384using ProgramIdSpan = std::span<ProgramId const>;
385
389using ProgramAtomSpan = std::span<ProgramAtom const>;
390
394using ProgramLiteralSpan = std::span<ProgramLiteral const>;
396using ProgramLiteralVector = std::vector<ProgramLiteral>;
397
401using SolverLiteralSpan = std::span<SolverLiteral const>;
403using SolverLiteralList = std::initializer_list<SolverLiteral const>;
405using SolverLiteralVector = std::vector<SolverLiteral>;
406
410using WeightSpan = std::span<clingo_weight_t const>;
411
415using WeightedLiteralSpan = std::span<WeightedLiteral const>;
416
418using Sum = int64_t;
420using SumSpan = std::span<Sum const>;
421
423using StringSpan = std::span<std::string_view const>;
425using StringList = std::initializer_list<std::string_view const>;
426
439
448
458
460inline constexpr size_t default_message_limit = 25;
461
465using Logger = std::function<void(MessageCode, std::string_view)>;
466
471class Library {
472 public:
480 size_t limit = default_message_limit) {
481 auto log = logger ? std::make_unique<Logger>(std::move(logger)) : nullptr;
482 auto has_log = static_cast<bool>(log);
483 rep_.reset(Detail::call<clingo_lib_new>(static_cast<clingo_lib_flags_t>(flags),
484 static_cast<clingo_log_level_t>(level), has_log ? &c_logger : nullptr,
485 log.release(), limit),
486 false);
487 }
494 explicit Library(clingo_lib_t *rep, bool acquire) : rep_{rep, acquire} {}
495
500 [[nodiscard]] friend auto c_cast(Library const &lib) -> clingo_lib_t * { return lib.rep_.get(); }
501
502 private:
503 friend class Detail::intrusive_handle<Library, clingo_lib_t>;
504
505 static constexpr clingo_logger_t c_logger = {
506 [](clingo_message_t code, char const *message, size_t size, void *data) {
507 (*static_cast<Logger *>(data))(static_cast<MessageCode>(code), {message, size});
508 },
509 [](void *data) noexcept { std::unique_ptr<Logger>(static_cast<Logger *>(data)); },
510 };
511
512 static auto acquire(clingo_lib_t *ptr) { clingo_lib_acquire(ptr); }
513 static auto release(clingo_lib_t *ptr) { clingo_lib_release(ptr); }
514
515 Detail::intrusive_handle<Library, clingo_lib_t> rep_;
516};
517
525 public:
527 explicit StringBuilder() : bld_{Detail::call<clingo_string_builder_new>(), false} {}
528
533 [[nodiscard]] friend auto c_cast(StringBuilder &bld) -> clingo_string_builder_t * { return bld.bld_.get(); }
534
538 [[nodiscard]] auto str() const -> std::string_view {
539 auto [data, size] = Detail::call<clingo_string_builder_string>(bld_.get());
540 return {data, size};
541 }
542
545
546 private:
547 using Traits = Detail::value_handle_traits<clingo_string_builder_copy, clingo_string_builder_free>;
548 Detail::value_handle<Traits> bld_;
549};
550
558
568
572class Position {
573 public:
579 explicit Position(clingo_position_t const *pos) : pos_{pos, true} {}
580
587 explicit Position(Library const &lib, std::string_view file, size_t line, size_t column)
588 : pos_{Detail::call<clingo_position_new>(c_cast(lib), file.data(), file.size(), line, column), false} {}
589
594 friend auto c_cast(Position const &x) -> clingo_position_t const * { return x.pos_.get(); }
595
599 [[nodiscard]] auto file() const -> std::string_view {
600 auto [data, size] = Detail::call<clingo_position_file>(pos_.get());
601 return {data, size};
602 }
603
607 [[nodiscard]] auto line() const -> size_t { return clingo_position_line(pos_.get()); }
608
612 [[nodiscard]] auto column() const -> size_t { return clingo_position_column(pos_.get()); }
613
617 [[nodiscard]] auto to_string() const -> std::string {
618 auto bld = StringBuilder{};
619 Detail::handle_error(clingo_position_to_string(pos_.get(), c_cast(bld)));
620 return std::string{bld.str()};
621 }
622
628 [[nodiscard]] auto hash() const noexcept -> size_t { return clingo_position_hash(pos_.get()); }
629
635 friend auto operator==(Position const &a, Position const &b) noexcept -> bool {
636 return clingo_position_equal(a.pos_.get(), b.pos_.get());
637 }
638
644 friend auto operator<=>(Position const &a, Position const &b) noexcept -> std::strong_ordering {
645 return clingo_position_compare(a.pos_.get(), b.pos_.get()) <=> 0;
646 }
647
648 private:
649 using Traits = Detail::value_handle_traits<clingo_position_copy, clingo_position_free>;
650 Detail::value_handle<Traits> pos_;
651};
652
656class Location {
657 public:
659 explicit Location(clingo_location_t const *loc) : loc_{loc, true} {}
660
665 explicit Location(Position const &begin, Position const &end)
666 : loc_{Detail::call<clingo_location_new>(c_cast(begin), c_cast(end)), false} {}
667
671 friend auto c_cast(Location const &x) -> clingo_location_t const * { return x.loc_.get(); }
672
676 [[nodiscard]] auto begin() const -> Position { return Position{clingo_location_begin(loc_.get())}; }
677
681 [[nodiscard]] auto end() const -> Position { return Position{clingo_location_end(loc_.get())}; }
682
686 [[nodiscard]] auto to_string() const -> std::string {
687 auto bld = StringBuilder{};
688 Detail::handle_error(clingo_location_to_string(loc_.get(), c_cast(bld)));
689 return std::string{bld.str()};
690 }
691
693 [[nodiscard]] auto hash() const noexcept -> size_t { return clingo_location_hash(loc_.get()); }
694
700 friend auto operator==(Location const &a, Location const &b) noexcept -> bool {
701 return clingo_location_equal(a.loc_.get(), b.loc_.get());
702 }
703
709 friend auto operator<=>(Location const &a, Location const &b) noexcept -> std::strong_ordering {
710 return clingo_location_compare(a.loc_.get(), b.loc_.get()) <=> 0;
711 }
712
713 private:
714 using Traits = Detail::value_handle_traits<clingo_location_copy, clingo_location_free>;
715 Detail::value_handle<Traits> loc_;
716};
717
718namespace Version {
720inline constexpr int major = CLINGO_VERSION_MAJOR;
722inline constexpr int minor = CLINGO_VERSION_MINOR;
724inline constexpr int revision = CLINGO_VERSION_REVISION;
725} // namespace Version
726
730inline auto version() -> std::tuple<int, int, int> {
731 int major = 0;
732 int minor = 0;
733 int revision = 0;
734 clingo_version(&major, &minor, &revision);
735 return {major, minor, revision};
736}
737
739
740} // namespace Clingo
The main library class for managing global information and logging.
Definition core.hh:471
Library(LibraryFlags flags=LibraryFlags::none, Logger logger=nullptr, LogLevel level=LogLevel::info, size_t limit=default_message_limit)
Constructs a library object.
Definition core.hh:479
friend auto c_cast(Library const &lib) -> clingo_lib_t *
Casts the library object to its C representation.
Definition core.hh:500
Library(clingo_lib_t *rep, bool acquire)
Constructs a library object from an existing C representation.
Definition core.hh:494
Class representing a range in a file.
Definition core.hh:656
auto begin() const -> Position
Get the position marking the beginning of the location.
Definition core.hh:676
friend auto operator==(Location const &a, Location const &b) noexcept -> bool
Compare two locations for equality.
Definition core.hh:700
friend auto operator<=>(Location const &a, Location const &b) noexcept -> std::strong_ordering
Compare two locations.
Definition core.hh:709
friend auto c_cast(Location const &x) -> clingo_location_t const *
Cast the location to its C representation.
Definition core.hh:671
auto end() const -> Position
Get the position marking the end of the location.
Definition core.hh:681
Location(clingo_location_t const *loc)
Constructs a location from an existing C representation.
Definition core.hh:659
auto to_string() const -> std::string
Convert the location to a string representation.
Definition core.hh:686
auto hash() const noexcept -> size_t
Get a hash value for the location.
Definition core.hh:693
Location(Position const &begin, Position const &end)
Constructs a location from a begin and end position.
Definition core.hh:665
Class representing a position in a file.
Definition core.hh:572
Position(Library const &lib, std::string_view file, size_t line, size_t column)
Constructs a position from a file, line, and column.
Definition core.hh:587
friend auto operator==(Position const &a, Position const &b) noexcept -> bool
Compare two positions for equality.
Definition core.hh:635
friend auto c_cast(Position const &x) -> clingo_position_t const *
Cast a position to its C representation.
Definition core.hh:594
friend auto operator<=>(Position const &a, Position const &b) noexcept -> std::strong_ordering
Compare two positions.
Definition core.hh:644
Position(clingo_position_t const *pos)
Constructs a position from an existing C representation.
Definition core.hh:579
auto to_string() const -> std::string
Convert the position to a string representation.
Definition core.hh:617
auto file() const -> std::string_view
Get the file name of the position.
Definition core.hh:599
auto line() const -> size_t
Get the line number of the position.
Definition core.hh:607
auto hash() const noexcept -> size_t
Compute the hash of the position.
Definition core.hh:628
auto column() const -> size_t
Get the column number of the position.
Definition core.hh:612
A string builder for constructing strings.
Definition core.hh:524
StringBuilder()
Construct an empty string builder.
Definition core.hh:527
auto str() const -> std::string_view
Get a view of the string built by the string builder.
Definition core.hh:538
friend auto c_cast(StringBuilder &bld) -> clingo_string_builder_t *
Cast the string builder to its C representation.
Definition core.hh:533
void clear() noexcept
Clear the string builder, removing all content.
Definition core.hh:544
CLINGO_VISIBILITY_DEFAULT clingo_position_t const * clingo_location_end(clingo_location_t const *loc)
Get the end of the location.
CLINGO_VISIBILITY_DEFAULT void clingo_lib_release(clingo_lib_t *lib)
Release a library object created with clingo_lib_new().
CLINGO_VISIBILITY_DEFAULT int clingo_position_compare(clingo_position_t const *a, clingo_position_t const *b)
Compare two positions.
CLINGO_VISIBILITY_DEFAULT bool clingo_position_new(clingo_lib_t *lib, char const *file, size_t size, size_t line, size_t column, clingo_position_t const **pos)
Create a new source position object.
CLINGO_VISIBILITY_DEFAULT void clingo_version(int *major, int *minor, int *revision)
Obtain the clingo version.
CLINGO_VISIBILITY_DEFAULT bool clingo_set_error(clingo_result_t code, char const *message, size_t size)
Set an error in the current thread.
struct clingo_weighted_literal clingo_weighted_literal_t
A literal with an associated weight.
CLINGO_VISIBILITY_DEFAULT bool clingo_location_new(clingo_position_t const *begin, clingo_position_t const *end, clingo_location_t const **loc)
Create a new source location object.
CLINGO_VISIBILITY_DEFAULT bool clingo_position_to_string(clingo_position_t const *pos, clingo_string_builder_t *str)
Convert the given position into a string.
CLINGO_VISIBILITY_DEFAULT size_t clingo_position_column(clingo_position_t const *pos)
Get the column number of the position.
CLINGO_VISIBILITY_DEFAULT void clingo_string_builder_clear(clingo_string_builder_t *bld)
Clear the string in the builder.
int clingo_log_level_t
Corresponding type to clingo_log_level_e.
Definition core.h:154
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 void clingo_get_error(clingo_result_t *code, clingo_string_t *message)
Get the error set in the current thread.
uint32_t clingo_atom_t
Unsigned integer type used for aspif atoms.
Definition core.h:80
CLINGO_VISIBILITY_DEFAULT int clingo_location_compare(clingo_location_t const *a, clingo_location_t const *b)
Compare two locations.
struct clingo_string_builder clingo_string_builder_t
A builder for strings.
Definition core.h:257
CLINGO_VISIBILITY_DEFAULT size_t clingo_position_line(clingo_position_t const *pos)
Get the line number of the position.
#define CLINGO_VERSION_MINOR
Minor version number.
Definition core.h:69
#define CLINGO_VERSION_MAJOR
Major version number.
Definition core.h:67
CLINGO_VISIBILITY_DEFAULT bool clingo_position_equal(clingo_position_t const *a, clingo_position_t const *b)
Check if two positions are equal.
CLINGO_VISIBILITY_DEFAULT bool clingo_location_to_string(clingo_location_t const *loc, clingo_string_builder_t *str)
Convert the given location into a string.
CLINGO_VISIBILITY_DEFAULT size_t clingo_position_hash(clingo_position_t const *pos)
Compute a hash of the position.
int32_t clingo_literal_t
Signed integer type used for aspif and solver literals.
Definition core.h:78
CLINGO_VISIBILITY_DEFAULT clingo_position_t const * clingo_location_begin(clingo_location_t const *loc)
Get the beginning of the location.
int clingo_message_t
Corresponding type to clingo_message_e.
Definition core.h:143
uint32_t clingo_id_t
Unsigned integer type used in various places.
Definition core.h:82
int clingo_result_t
Corresponding type to clingo_result_e.
Definition core.h:111
uint32_t clingo_lib_flags_t
Bitset of clingo_lib_flags_e.
Definition core.h:185
#define CLINGO_VERSION_REVISION
Revision number.
Definition core.h:71
struct clingo_position clingo_position_t
Represents a cursor position in source code.
Definition core.h:291
CLINGO_VISIBILITY_DEFAULT bool clingo_string_builder_new(clingo_string_builder_t **bld)
Create a new string builder.
CLINGO_VISIBILITY_DEFAULT bool clingo_location_equal(clingo_location_t const *a, clingo_location_t const *b)
Check if two locations are equal.
int32_t clingo_weight_t
Signed integer type for weights in sum aggregates and minimize constraints.
Definition core.h:84
CLINGO_VISIBILITY_DEFAULT size_t clingo_location_hash(clingo_location_t const *loc)
Compute a hash of the location.
CLINGO_VISIBILITY_DEFAULT void clingo_lib_acquire(clingo_lib_t *lib)
Increment the reference count of the given library.
@ clingo_message_file_included
same file included multiple times
Definition core.h:137
@ clingo_message_error
to report multiple errors; a corresponding runtime error is raised later
Definition core.h:140
@ clingo_message_info
an info message
Definition core.h:134
@ clingo_message_atom_undefined
undefined atom in program
Definition core.h:136
@ clingo_message_global_variable
global variable in tuple of aggregate element
Definition core.h:138
@ clingo_message_operation_undefined
undefined operation in program
Definition core.h:135
@ clingo_message_trace
a trace message
Definition core.h:132
@ clingo_message_debug
a debug message
Definition core.h:133
@ clingo_message_warn
a warning message
Definition core.h:139
@ clingo_log_level_warn
the warning level
Definition core.h:150
@ clingo_log_level_debug
the debug level
Definition core.h:148
@ clingo_log_level_info
the info level
Definition core.h:149
@ clingo_log_level_error
the error level (least verbose)
Definition core.h:151
@ clingo_log_level_trace
the trace level (most verbose)
Definition core.h:147
@ clingo_result_range
result out of range
Definition core.h:108
@ clingo_result_runtime
errors only detectable at runtime like invalid input
Definition core.h:104
@ clingo_result_success
successful API calls
Definition core.h:103
@ clingo_result_invalid
invalid arguments passed to function
Definition core.h:107
@ clingo_result_bad_alloc
memory could not be allocated
Definition core.h:106
@ clingo_result_logic
wrong usage of the clingo API
Definition core.h:105
@ clingo_lib_flags_shared
create symbols in a thread-safe manner
Definition core.h:181
@ clingo_lib_flags_slotted
use custom allocator for storing symbols
Definition core.h:180
@ clingo_lib_flags_fast_release
whether to enable fast release of libraries
Definition core.h:182
int clingo_external_type_t
Corresponding type to clingo_external_type_e.
Definition shared.h:32
int clingo_heuristic_type_t
Corresponding type to clingo_heuristic_type_e.
Definition shared.h:22
@ clingo_heuristic_type_false
set the level of an atom and choose a negative sign
Definition shared.h:19
@ clingo_heuristic_type_init
modify the initial VSIDS score of an atom
Definition shared.h:17
@ clingo_heuristic_type_true
set the level of an atom and choose a positive sign
Definition shared.h:18
@ clingo_heuristic_type_factor
modify VSIDS factor of an atom
Definition shared.h:16
@ clingo_heuristic_type_level
set the level of an atom
Definition shared.h:14
@ clingo_heuristic_type_sign
configure which sign to chose for an atom
Definition shared.h:15
@ clingo_external_type_free
allow an external to be assigned freely
Definition shared.h:26
@ clingo_external_type_true
assign an external to true
Definition shared.h:27
@ clingo_external_type_false
assign an external to false
Definition shared.h:28
@ clingo_external_type_release
no longer treat an atom as external
Definition shared.h:29
auto operator+(Sign a, Sign b) -> Sign
Combine two signs.
auto operator+=(Sign &a, Sign b) -> Sign &
Combine two signs.
auto operator-(Sign a) -> Sign
Negate the sign.
@ tuple
Theory tuples "(t1,...,tn)".
@ value
The configuration entry is a double value.
std::span< std::string_view const > StringSpan
A span of string views.
Definition core.hh:423
clingo_literal_t SolverLiteral
A solver literal.
Definition core.hh:399
std::initializer_list< std::string_view const > StringList
A list of string views.
Definition core.hh:425
constexpr size_t default_message_limit
The default message limit for the logger.
Definition core.hh:460
std::vector< SolverLiteral > SolverLiteralVector
A vector of solver literals.
Definition core.hh:405
LibraryFlags
Flags to create library objects.
Definition core.hh:450
std::function< void(MessageCode, std::string_view)> Logger
A callback function type for logging messages.
Definition core.hh:465
std::span< Sum const > SumSpan
A span of sums.
Definition core.hh:420
clingo_atom_t ProgramAtom
A program atom.
Definition core.hh:387
std::span< ProgramId const > ProgramIdSpan
A span of program ids.
Definition core.hh:384
std::initializer_list< SolverLiteral const > SolverLiteralList
A list of solver literals.
Definition core.hh:403
HeuristicType
Enumeration of heuristic types.
Definition core.hh:560
std::span< ProgramAtom const > ProgramAtomSpan
A span of program atoms.
Definition core.hh:389
auto version() -> std::tuple< int, int, int >
Get the version of the Clingo library as a tuple.
Definition core.hh:730
int64_t Sum
A sum representing the sum of weights.
Definition core.hh:418
MessageCode
Enumeration of message codes.
Definition core.hh:428
std::vector< ProgramLiteral > ProgramLiteralVector
A vector of program literals.
Definition core.hh:396
clingo_weight_t Weight
A weight used in sum aggregates and minimize constraints.
Definition core.hh:408
clingo_literal_t ProgramLiteral
A program literal.
Definition core.hh:392
std::span< SolverLiteral const > SolverLiteralSpan
A span of solver literals.
Definition core.hh:401
std::span< WeightedLiteral const > WeightedLiteralSpan
A span of weighted literals.
Definition core.hh:415
clingo_id_t ProgramId
A program id used for various kinds of indices.
Definition core.hh:382
std::span< ProgramLiteral const > ProgramLiteralSpan
A span of program literals.
Definition core.hh:394
ExternalType
Enumeration of control modes.
Definition core.hh:552
std::span< clingo_weight_t const > WeightSpan
A span of weights.
Definition core.hh:410
LogLevel
Enumeration of log levels.
Definition core.hh:441
@ none
no flags set
@ fast_release
whether to enable fast release of libraries
@ shared
create symbols in a thread-safe manner
@ slotted
use custom allocator for storing symbols
@ sign
Configure which sign to chose for an atom.
@ factor
Modify VSIDS factor of an atom.
@ level
Set the level of an atom.
@ init
Modify the initial VSIDS score of an atom.
@ trace
a trace message
@ global_variable
global variable in tuple of aggregate element
@ warn
a warning message
@ file_included
same file included multiple times
@ atom_undefined
undefined atom in program
@ operation_undefined
undefined operation in program
@ debug
a debug message
@ info
an info message
@ error
to report multiple errors; a corresponding runtime error is raised later
@ release
No longer treat an atom as external.
@ true_
Assign an external to true.
@ free
Allow an external to be assigned freely.
@ false_
Assign an external to false.
@ info
the info level
@ wart
the warning level
@ none
The empty set of flags.
Definition propagate.hh:38
#define CLINGO_ENABLE_BITSET_ENUM(E,...)
Opt-in macro for enabling bit operations for a given enum type.
Definition enum.hh:18
auto hash_combine(T... a) -> size_t
Combine the given hashes.
Definition hash.hh:239
constexpr auto transform(std::optional< T > &x, F &&f) -> Detail::transform_result< T &, F >
Implemenatation of std::optional<T>::transform.
Definition optional.hh:28
The clingo logger.
Definition core.h:157
Struct to capture strings that are not null-terminated.
Definition core.h:91
size_t size
the length of the string
Definition core.h:93
char const * data
pointer to the beginning of the string
Definition core.h:92
A literal with an associated weight.
Definition core.h:86