41#include <fmt/ranges.h>
42#include <nlohmann/json.hpp>
86 static inline nlohmann::json
to_json(
const Alt &) {
return nlohmann::json::object(); }
88 static inline Alt
from_json(
const nlohmann::json &) {
return Alt{}; }
112 { Alt::variant_custom_defaults() } -> std::convertible_to<std::vector<Alt>>;
116 template<
class Variant>
125 return Alt::variant_custom_defaults();
132 template<
class Variant>
135 template<
class... Alts>
137 static inline std::vector<std::string> default_type_names() {
138 return {std::string(Alts::variant_type_name)...};
145 std::vector<std::string> out;
146 auto add_alt = [&]<
class Alt>() {
150 std::variant<Alts...>{std::move(alt)}));
153 (add_alt.template operator()<Alts>(), ...);
157 static inline std::variant<Alts...> from_config_string(std::string_view s) {
158 nlohmann::json j = nlohmann::json::parse(s);
159 std::string name = j.at(
"implementation").get<std::string>();
160 nlohmann::json params = j.value(
"parameters", nlohmann::json::object());
162 std::optional<std::variant<Alts...>> result;
163 (void) ((name == Alts::variant_type_name
172 "invalid implementation : {}, possible implementations : {}",
174 default_type_names()));
182 template<
class Variant>
185 [](
const auto &alt) {
186 using Alt = std::decay_t<
decltype(alt)>;
188 j[
"implementation"] = std::string(Alt::variant_type_name);
196 template<
class Variant>
202 template<
class Variant>
223 virtual void set(std::string_view config_json) = 0;
229 virtual void autoselect(
const sham::DeviceScheduler_ptr &sched) = 0;
264 template<
class... Alts>
267 using Variant = std::variant<Alts...>;
277 inline bool is_set()
const override {
return current.has_value(); }
280 inline void autoselect(
const sham::DeviceScheduler_ptr &sched)
override {
281 autoselect_fn(sched, *
this);
285 inline const Variant &
get()
const {
return *current; }
291 return nlohmann::json(
nullptr).dump();
304 inline void set(Variant v) { current = std::move(v); }
307 inline void set(std::string_view config_json)
override {
312 std::optional<Variant> current;
std::vector< Alt > alt_default_list()
Non-template virtual interface exposed by ImplVariantGlobal, for code that needs to hold or pass arou...
virtual std::vector< std::string > get_default_config_list() const =0
List the available implementations as config json strings, one per alternative.
virtual void autoselect(const sham::DeviceScheduler_ptr &sched)=0
Select the algorithm's default implementation for the device behind sched.
virtual bool is_set() const =0
Whether an implementation has been selected yet.
virtual void set(std::string_view config_json)=0
Select an implementation from a {"implementation": ..., "parameters": ...} json string.
virtual std::string get_current_config() const =0
std::vector< std::string > get_default_config_list() const override
void autoselect(const sham::DeviceScheduler_ptr &sched) override
Select the algorithm's default implementation for the device behind sched.
void set(Variant v)
Directly select an alternative (e.g. to seed a default at the call site).
ImplVariantGlobal(AutoselectFn fn)
Construct an unset selector, whose default implementation is picked by fn.
bool is_set() const override
Whether an implementation has been selected yet.
std::string get_current_config() const override
const Variant & get() const
Get the currently selected implementation. Requires is_set().
std::function< void(const sham::DeviceScheduler_ptr &, ImplVariantGlobal &)> AutoselectFn
Callable selecting the default implementation, by calling set() on the selector.
Detects whether Alt opts into exposing more than one default instance of itself (e....
This header file contains utility functions related to exception handling in the code.
Namespace for internal details of the logs module.
namespace to contain everything implemented by shamalgs
std::string variant_to_config_string(const Variant &v)
Serialize the currently active alternative of a variant to a single config json string.
std::vector< std::string > variant_default_type_names()
List the variant_type_name of every alternative of a variant, with no params.
Variant variant_from_config_string(std::string_view s)
Parse a variant back from a config string produced by variant_to_config_string.
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
Customization point controlling how an alternative's fields (if any) are serialized to / parsed from ...
static Alt from_json(const nlohmann::json &)
Parse the alternative's fields back (default: no fields, ignored).
static nlohmann::json to_json(const Alt &)
Serialize the alternative's fields (default: no fields, empty object).
static std::vector< std::string > default_config_list()
Partial specialization target: extracts the Alts... pack out of std::variant<Alts....