![]() |
Shamrock 2025.10.0
Astrophysical Code
|
Generic std::variant-based implementation selector. More...
#include "shambase/exception.hpp"#include "sham/format/format.hpp"#include "shambackends/DeviceScheduler.hpp"#include <fmt/ranges.h>#include <nlohmann/json.hpp>#include <string_view>#include <concepts>#include <functional>#include <optional>#include <string>#include <utility>#include <variant>#include <vector>Go to the source code of this file.
Classes | |
| struct | shamalgs::ImplVariantParams< Alt > |
| Customization point controlling how an alternative's fields (if any) are serialized to / parsed from the "parameters" json value carried alongside its name. More... | |
| struct | shamalgs::details::impl_variant_alts< std::variant< Alts... > > |
| class | shamalgs::IImplVariant |
| Non-template virtual interface exposed by ImplVariantGlobal, for code that needs to hold or pass around an implementation selector without knowing its alternative types. More... | |
| class | shamalgs::ImplVariantGlobal< Alts > |
| Drop-in replacement for the hand-rolled "global variable + enum + name mapping. More... | |
Namespaces | |
| namespace | shamalgs |
| namespace to contain everything implemented by shamalgs | |
Concepts | |
| concept | shamalgs::HasCustomDefaults |
| Detects whether Alt opts into exposing more than one default instance of itself (e.g. the same alternative with different tunable field values) via a static variant_custom_defaults() method, for example: | |
Functions | |
| template<class Variant> | |
| std::string | shamalgs::variant_to_config_string (const Variant &v) |
| Serialize the currently active alternative of a variant to a single config json string. | |
| template<class Alt> | |
| std::vector< Alt > | shamalgs::details::alt_default_list () |
| template<class Variant> | |
| Variant | shamalgs::variant_from_config_string (std::string_view s) |
| Parse a variant back from a config string produced by variant_to_config_string. | |
| template<class Variant> | |
| std::vector< std::string > | shamalgs::variant_default_type_names () |
| List the variant_type_name of every alternative of a variant, with no params. | |
Generic std::variant-based implementation selector.
Lets an algorithm expose its set of possible implementations as a plain std::variant<Alt1, Alt2, ...> of small structs (mirroring a Rust enum, variants with fields included) instead of hand-writing an enum plus the name <-> enum switch/if-else chains that come with it. Each alternative only needs a static constexpr std::string_view variant_type_name (the same convention already used by the std::variant-based configs in shammodels, e.g. AVConfig.hpp).
The free functions variant_to_config_string / variant_from_config_string / variant_default_type_names turn any such variant into a single config string ABI: {"implementation": "<name>", "parameters": <alternative's own json>}. ImplVariantGlobal builds on top of them to replace the hand-rolled "global variable + enum + name mapping + 3 free functions" pattern used to hold an algorithm's currently selected implementation; see its own doc comment for the config string ABI it exposes, how the unset state works and how the default implementation is picked (autoselect).
Dispatch on the selected implementation is then a plain std::visit(shambase::overloaded{...}, variant) instead of a switch.
Definition in file ImplVariant.hpp.
|
inline |
The instances of Alt to expose as "available implementations": a single default-constructed one, unless Alt opts into HasCustomDefaults.
Definition at line 123 of file ImplVariant.hpp.