Clingo
Loading...
Searching...
No Matches
config.hh
1#pragma once
2
3#include <clingo/core.hh>
4
5#include <clingo/config.h>
6
7namespace Clingo {
8
17
29
31class ConfigArray;
32class ConstConfigMap;
33class ConfigMap;
34
37 public:
42 explicit ConstConfig(clingo_config_t const *stats, ProgramId key) : cfg_{stats}, key_{key} {}
43
48 friend auto c_cast(ConstConfig const &stats) -> clingo_config_t const * { return stats.cfg_; }
49
54 return static_cast<ConfigType>(Detail::call<clingo_config_type>(cfg_, key_));
55 }
56
61
66 [[nodiscard]] auto at(size_t index) const -> ConstConfig;
67
69 [[nodiscard]] auto operator[](size_t index) const -> ConstConfig { return at(index); }
70
74 [[nodiscard]] auto map() const -> ConstConfigMap;
75
81
83 [[nodiscard]] auto operator[](std::string_view name) const -> ConstConfig { return get(name); }
84
91 [[nodiscard]] auto value() const -> std::optional<std::string_view> {
92 if (intersects(type(), ConfigType::value)) {
94 bool assigned = false;
95 Detail::handle_error(clingo_config_value_get(cfg_, key_, &value, &assigned));
96 if (assigned) {
97 return std::string_view{value.data, value.size};
98 }
99 return std::nullopt;
100 }
101 throw std::logic_error{"not a value"};
102 }
103
105 [[nodiscard]] auto operator*() const -> std::optional<std::string_view> { return value(); }
106
114 auto [data, size] = Detail::call<clingo_config_description>(cfg_, key_);
115 return {data, size};
116 }
117
124 [[nodiscard]] auto to_string() const -> std::string {
125 auto bld = StringBuilder{};
126 Detail::handle_error(clingo_config_to_string(cfg_, key_, c_cast(bld)));
127 return std::string{bld.str()};
128 }
129
130 private:
131 friend class Config;
132
133 clingo_config_t const *cfg_;
134 clingo_id_t key_;
135};
136
138class Config : public ConstConfig {
139 public:
144 explicit Config(clingo_config_t *stats, ProgramId key) : ConstConfig{stats, key} {}
145
150 friend auto c_cast(Config const &stats) -> clingo_config_t * { return stats.cfg_(); }
151
153 [[nodiscard]] auto array() const -> ConfigArray;
155 [[nodiscard]] auto at(size_t index) const -> Config;
157 [[nodiscard]] auto operator[](size_t index) const -> Config { return at(index); }
159 [[nodiscard]] auto map() const -> ConfigMap;
161 [[nodiscard]] auto get(std::string_view name) const -> Config;
163 [[nodiscard]] auto operator[](std::string_view name) const -> Config { return get(name); }
164 using ConstConfig::value;
168 void value(std::string_view value) const {
169 if (intersects(type(), ConfigType::value)) {
170 Detail::handle_error(clingo_config_value_set(cfg_(), key_, value.data(), value.size()));
171 } else {
172 throw std::logic_error{"not a value"};
173 }
174 }
177 auto operator=(std::string_view value) const -> Config { // NOLINT
178 this->value(value);
179 return *this;
180 }
181
182 private:
183 [[nodiscard]] auto cfg_() const -> clingo_config_t * {
184 // NOLINTNEXTLINE
185 return const_cast<clingo_config_t *>(ConstConfig::cfg_);
186 }
187};
188
191 public:
195 using size_type = std::size_t;
197 using difference_type = std::ptrdiff_t;
201 using pointer = Detail::ArrowProxy<value_type>;
203 using iterator = Detail::RandomAccessIterator<ConstConfigArray>;
204
209 explicit ConstConfigArray(clingo_config_t const *stats, ProgramId key) : cfg_{stats}, key_{key} {}
210
212 [[nodiscard]] auto at(size_t index) const -> ConstConfig { return ConstConfig{cfg_, at_(index)}; }
214 [[nodiscard]] auto operator[](size_t index) const -> ConstConfig { return at(index); }
218 [[nodiscard]] auto size() const -> size_t { return Detail::call<clingo_config_array_size>(cfg_, key_); }
222 [[nodiscard]] auto begin() const -> iterator { return iterator{*this, 0}; }
226 [[nodiscard]] auto end() const -> iterator { return iterator{*this, size()}; }
227
228 private:
229 friend class ConfigArray;
230 [[nodiscard]] auto at_(size_t index) const -> clingo_id_t {
231 return Detail::call<clingo_config_array_at>(cfg_, key_, index);
232 }
233
234 clingo_config_t const *cfg_;
235 clingo_id_t key_;
236};
237
239 if (intersects(type(), ConfigType::array)) {
240 return ConstConfigArray{cfg_, key_};
241 }
242 throw std::logic_error{"not an array"};
243}
244
245inline auto ConstConfig::at(size_t index) const -> ConstConfig {
246 return array().at(index);
247}
248
251 public:
255 using size_type = std::size_t;
257 using difference_type = std::ptrdiff_t;
261 using pointer = Detail::ArrowProxy<value_type>;
263 using iterator = Detail::RandomAccessIterator<ConfigArray>;
264
267
269 [[nodiscard]] auto at(size_t index) const -> Config { return Config{cfg_(), at_(index)}; }
271 [[nodiscard]] auto operator[](size_t index) const -> Config { return at(index); }
273 [[nodiscard]] auto begin() const -> iterator { return iterator{*this, 0}; }
275 [[nodiscard]] auto end() const -> iterator { return iterator{*this, size()}; }
276
277 private:
278 [[nodiscard]] auto cfg_() const -> clingo_config_t * {
279 // NOLINTNEXTLINE
280 return const_cast<clingo_config_t *>(ConstConfigArray::cfg_);
281 }
282};
283
285 if (intersects(type(), ConfigType::array)) {
286 return ConfigArray{cfg_(), key_};
287 }
288 throw std::logic_error{"not an array"};
289}
290
291inline auto Config::at(size_t index) const -> Config {
292 return array().at(index);
293}
294
297 public:
299 using key_type = std::string_view;
303 using value_type = std::pair<key_type, mapped_type>;
305 using size_type = std::size_t;
307 using difference_type = std::ptrdiff_t;
311 using pointer = Detail::ArrowProxy<value_type>;
313 using iterator = Detail::RandomAccessIterator<ConstConfigMap>;
314
319 explicit ConstConfigMap(clingo_config_t const *stats, ProgramId key) : cfg_{stats}, key_{key} {}
320
324 [[nodiscard]] auto size() const -> size_t { return Detail::call<clingo_config_map_size>(cfg_, key_); }
325
330 [[nodiscard]] auto at(size_t index) const -> value_type {
331 auto [name, subkey] = at_(index);
332 return {name, ConstConfig{cfg_, subkey}};
333 }
334
339 [[nodiscard]] auto get(std::string_view name) const -> ConstConfig { return ConstConfig{cfg_, at_(name)}; }
340
342 [[nodiscard]] auto operator[](std::string_view name) const -> ConstConfig { return get(name); }
343
348 [[nodiscard]] auto contains(std::string_view name) const -> bool {
349 return Detail::call<clingo_config_map_has_subkey>(cfg_, key_, name.data(), name.size());
350 }
351
355 [[nodiscard]] auto begin() const -> iterator { return iterator{*this, 0}; }
356
360 [[nodiscard]] auto end() const -> iterator { return iterator{*this, size()}; }
361
362 private:
363 friend class ConfigMap;
364
365 [[nodiscard]] auto at_(std::string_view name) const -> clingo_id_t {
366 return Detail::call<clingo_config_map_at>(cfg_, key_, name.data(), name.size());
367 }
368
369 [[nodiscard]] auto at_(size_t index) const -> std::pair<std::string_view, clingo_id_t> {
370 auto [data, size] = Detail::call<clingo_config_map_subkey_name>(cfg_, key_, index);
371 auto str = std::string_view{data, size};
372 return {str, at_(str)};
373 }
374
375 clingo_config_t const *cfg_;
376 clingo_id_t key_;
377};
378
380 if (intersects(type(), ConfigType::map)) {
381 return ConstConfigMap{cfg_, key_};
382 }
383 throw std::logic_error{"not a map"};
384}
385
386inline auto ConstConfig::get(std::string_view name) const -> ConstConfig {
387 return map().get(name);
388}
389
391class ConfigMap : public ConstConfigMap {
392 public:
394 using key_type = std::string_view;
398 using value_type = std::pair<key_type, mapped_type>;
400 using size_type = std::size_t;
402 using difference_type = std::ptrdiff_t;
406 using pointer = Detail::ArrowProxy<value_type>;
408 using iterator = Detail::RandomAccessIterator<ConfigMap>;
409
412
414 [[nodiscard]] auto at(size_t index) const -> value_type {
415 auto [name, subkey] = at_(index);
416 return {name, Config{cfg_(), subkey}};
417 }
419 [[nodiscard]] auto get(std::string_view name) const -> Config { return Config{cfg_(), at_(name)}; }
421 [[nodiscard]] auto operator[](std::string_view name) const -> Config { return get(name); }
423 [[nodiscard]] auto begin() const -> iterator { return iterator{*this, 0}; }
425 [[nodiscard]] auto end() const -> iterator { return iterator{*this, size()}; }
426
427 private:
428 [[nodiscard]] auto cfg_() const -> clingo_config_t * {
429 // NOLINTNEXTLINE
430 return const_cast<clingo_config_t *>(ConstConfigMap::cfg_);
431 }
432};
433
434inline auto Config::map() const -> ConfigMap {
435 if (intersects(type(), ConfigType::map)) {
436 return ConfigMap{cfg_(), key_};
437 }
438 throw std::logic_error{"not a map"};
439}
440
441inline auto Config::get(std::string_view name) const -> Config {
442 return map().get(name);
443}
444
446
447} // namespace Clingo
Class modeling a mutable array of configuration entries.
Definition config.hh:250
auto begin() const -> iterator
Get an iterator to the beginning of the array.
Definition config.hh:273
ConfigArray(clingo_config_t *stats, ProgramId key)
Construct from the underlying C API type and a key.
Definition config.hh:266
auto operator[](size_t index) const -> Config
Get the configuration entry at the given index in the array.
Definition config.hh:271
auto end() const -> iterator
Get an iterator to the beginning of the array.
Definition config.hh:275
Detail::ArrowProxy< value_type > pointer
The pointer type.
Definition config.hh:261
auto at(size_t index) const -> Config
Get the configuration entry at the given index in the array.
Definition config.hh:269
Detail::RandomAccessIterator< ConfigArray > iterator
The iterator type.
Definition config.hh:263
std::size_t size_type
The size type.
Definition config.hh:255
std::ptrdiff_t difference_type
The difference type.
Definition config.hh:257
Config value_type
The value type.
Definition config.hh:253
Class modeling a mutable map of configuration entries.
Definition config.hh:391
auto at(size_t index) const -> value_type
Get the name configuration entry pair at the given index in the map.
Definition config.hh:414
Detail::RandomAccessIterator< ConfigMap > iterator
The iterator type.
Definition config.hh:408
std::pair< key_type, mapped_type > value_type
The value type.
Definition config.hh:398
auto begin() const -> iterator
Get an iterator to the beginning of the map.
Definition config.hh:423
std::string_view key_type
The key type.
Definition config.hh:394
value_type reference
The reference type.
Definition config.hh:404
std::ptrdiff_t difference_type
The difference type.
Definition config.hh:402
auto get(std::string_view name) const -> Config
Get the configuration entry with the given name in the map.
Definition config.hh:419
auto end() const -> iterator
Get an iterator to the end of the map.
Definition config.hh:425
auto operator[](std::string_view name) const -> Config
Get the configuration entry with the given name in the map.
Definition config.hh:421
Detail::ArrowProxy< value_type > pointer
The pointer type.
Definition config.hh:406
std::size_t size_type
The size type.
Definition config.hh:400
ConfigMap(clingo_config_t *stats, ProgramId key)
Construct from the underlying C API type and a key.
Definition config.hh:411
Class modeling a mutable configuration entry.
Definition config.hh:138
auto value() const -> std::optional< std::string_view >
Get the value of the configuration entry.
Definition config.hh:91
friend auto c_cast(Config const &stats) -> clingo_config_t *
Cast the configuration to the underlying C API type.
Definition config.hh:150
void value(std::string_view value) const
Set the value of the configuration entry.
Definition config.hh:168
auto operator=(std::string_view value) const -> Config
Set the value of the configuration entry.
Definition config.hh:177
Config(clingo_config_t *stats, ProgramId key)
Construct from the underlying C API type and a key.
Definition config.hh:144
Class modeling an immutable array of configuration entries.
Definition config.hh:190
std::size_t size_type
The size type.
Definition config.hh:195
ConstConfigArray(clingo_config_t const *stats, ProgramId key)
Construct from the underlying C API type and a key.
Definition config.hh:209
auto operator[](size_t index) const -> ConstConfig
Get the configuration entry at the given index in the array.
Definition config.hh:214
Detail::ArrowProxy< value_type > pointer
The pointer type.
Definition config.hh:201
auto end() const -> iterator
Get an iterator to the end of the array.
Definition config.hh:226
auto size() const -> size_t
Get the size of the array.
Definition config.hh:218
ConstConfig value_type
The value type.
Definition config.hh:193
auto at(size_t index) const -> ConstConfig
Get the configuration entry at the given index in the array.
Definition config.hh:212
auto begin() const -> iterator
Get an iterator to the beginning of the array.
Definition config.hh:222
Detail::RandomAccessIterator< ConstConfigArray > iterator
The iterator type.
Definition config.hh:203
std::ptrdiff_t difference_type
The difference type.
Definition config.hh:197
Class modeling an immutable map of configuration entries.
Definition config.hh:296
std::pair< key_type, mapped_type > value_type
The value type.
Definition config.hh:303
ConstConfigMap(clingo_config_t const *stats, ProgramId key)
Construct from the underlying C API type and a key.
Definition config.hh:319
std::size_t size_type
The size type.
Definition config.hh:305
auto end() const -> iterator
Get an iterator to the end of the map.
Definition config.hh:360
auto begin() const -> iterator
Get an iterator to the beginning of the map.
Definition config.hh:355
value_type reference
The reference type.
Definition config.hh:309
auto contains(std::string_view name) const -> bool
Check if the map contains a configuration entry with the given name.
Definition config.hh:348
std::string_view key_type
The key type.
Definition config.hh:299
auto operator[](std::string_view name) const -> ConstConfig
Get the configuration entry with the given name in the map.
Definition config.hh:342
Detail::ArrowProxy< value_type > pointer
The pointer type.
Definition config.hh:311
auto at(size_t index) const -> value_type
Get the name configuration entry pair at the given index in the map.
Definition config.hh:330
auto size() const -> size_t
Get the size of the map.
Definition config.hh:324
std::ptrdiff_t difference_type
The difference type.
Definition config.hh:307
Detail::RandomAccessIterator< ConstConfigMap > iterator
The iterator type.
Definition config.hh:313
auto get(std::string_view name) const -> ConstConfig
Get the configuration entry with the given name in the map.
Definition config.hh:339
Class modeling an immutable configuration entry.
Definition config.hh:36
friend auto c_cast(ConstConfig const &stats) -> clingo_config_t const *
Cast the configuration to the underlying C API type.
Definition config.hh:48
auto description() const -> std::string_view
Get the description of the configuration entry.
Definition config.hh:113
auto value() const -> std::optional< std::string_view >
Get the value of the configuration entry.
Definition config.hh:91
auto type() const -> ConfigType
Get the type of the configuration entry.
Definition config.hh:53
auto to_string() const -> std::string
Get a string representation of the configuration entry.
Definition config.hh:124
auto operator*() const -> std::optional< std::string_view >
Get the value of the configuration entry.
Definition config.hh:105
ConstConfig(clingo_config_t const *stats, ProgramId key)
Construct from the underlying C API type and a key.
Definition config.hh:42
A string builder for constructing strings.
Definition core.hh:524
struct clingo_config clingo_config_t
Handle for to the solver configuration.
Definition config.h:46
CLINGO_VISIBILITY_DEFAULT bool clingo_config_value_get(clingo_config_t const *config, clingo_id_t key, clingo_string_t *value, bool *has_value)
Get the string value of the given entry.
unsigned clingo_config_type_bitset_t
Bitset for values of type clingo_config_type_e.
Definition config.h:43
CLINGO_VISIBILITY_DEFAULT bool clingo_config_value_set(clingo_config_t *config, clingo_id_t key, char const *value, size_t size)
Set the value of an entry.
CLINGO_VISIBILITY_DEFAULT bool clingo_config_to_string(clingo_config_t const *config, clingo_id_t key, clingo_string_builder_t *builder)
Get the string representation of the given theory element.
@ clingo_config_type_array
the entry is an array
Definition config.h:39
@ clingo_config_type_map
the entry is a map
Definition config.h:40
@ clingo_config_type_value
the entry is a (string) value
Definition config.h:38
uint32_t clingo_id_t
Unsigned integer type used in various places.
Definition core.h:82
@ tuple
Theory tuples "(t1,...,tn)".
ConfigType
Enumeration of configuration types.
Definition config.hh:19
auto array() const -> ConstConfigArray
Access the configuration entry as an array.
Definition config.hh:238
auto get(std::string_view name) const -> Config
Get the configuration entry with the given name in the map.
Definition config.hh:441
auto array() const -> ConfigArray
Access the configuration entry as an array.
Definition config.hh:284
auto map() const -> ConstConfigMap
Access the configuration entry as a map.
Definition config.hh:379
auto at(size_t index) const -> Config
Get the configuration entry at the given index in the array.
Definition config.hh:291
auto get(std::string_view name) const -> ConstConfig
Get the configuration entry with the given name in the map.
Definition config.hh:386
auto at(size_t index) const -> ConstConfig
Get the configuration entry at the given index in the array.
Definition config.hh:245
auto map() const -> ConfigMap
Access the configuration entry as a map.
Definition config.hh:434
@ map
The configuration entry is a map of configurations.
@ value
The configuration entry is a double value.
@ array
The configuration entry is a array of configurations.
clingo_id_t ProgramId
A program id used for various kinds of indices.
Definition core.hh:382
#define CLINGO_ENABLE_BITSET_ENUM(E,...)
Opt-in macro for enabling bit operations for a given enum type.
Definition enum.hh:18
Struct to capture strings that are not null-terminated.
Definition core.h:91