Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
shamalgs::ImplVariantGlobal< Alts > Class Template Reference

Drop-in replacement for the hand-rolled "global variable + enum + name mapping. More...

#include <shamalgs/include/shamalgs/ImplVariant.hpp>

Inheritance diagram for shamalgs::ImplVariantGlobal< Alts >:
Collaboration diagram for shamalgs::ImplVariantGlobal< Alts >:

Public Types

using Variant = std::variant<Alts...>
using AutoselectFn = std::function<void(const sham::DeviceScheduler_ptr &, ImplVariantGlobal &)>
 Callable selecting the default implementation, by calling set() on the selector.

Public Member Functions

 ImplVariantGlobal (AutoselectFn fn)
 Construct an unset selector, whose default implementation is picked by fn.
 ImplVariantGlobal (const ImplVariantGlobal &)=delete
ImplVariantGlobal & operator= (const ImplVariantGlobal &)=delete
 ImplVariantGlobal (ImplVariantGlobal &&)=delete
ImplVariantGlobal & operator= (ImplVariantGlobal &&)=delete
bool is_set () const override
 Whether an implementation has been selected yet.
void autoselect (const sham::DeviceScheduler_ptr &sched) override
 Select the algorithm's default implementation for the device behind sched.
const Variant & get () const
 Get the currently selected implementation. Requires is_set().
std::string get_current_config () const override
std::vector< std::string > get_default_config_list () const override
void set (Variant v)
 Directly select an alternative (e.g. to seed a default at the call site).
void set (std::string_view config_json) override
 Select an implementation from a {"implementation": ..., "parameters": ...} json string.

Detailed Description

template<class... Alts>
class shamalgs::ImplVariantGlobal< Alts >

Drop-in replacement for the hand-rolled "global variable + enum + name mapping.

  • 3 free functions" implementation-selection pattern. Holds the currently selected implementation as a std::variant<Alts...> and exposes it through the type-erased IImplVariant interface: - get_current_config() / get_default_config_list() / set(string_view) : a single <tt>{\"implementation\": ..., \"parameters\": ...}

json string ABI.

  • is_set() / autoselect(sched) : the unset state and the default selection.

Each instance is registered by name in shamalgs::impl_registry (see impl_registry.hpp), which exposes every algorithm's selection through that interface, so no per-algorithm free function or Python binding is needed.

No implementation is selected at construction: is_set() reports whether one has been picked yet. The rule picking the algorithm's default is however given at construction, as a callable of signature void(const sham::DeviceScheduler_ptr &, ImplVariantGlobal &) that calls set() on the selector it is handed. autoselect(sched) runs it, so that the default may depend on the device behind sched (see e.g. compute_histogram.cpp), or ignore it when it is a compile-time choice:

[](const sham::DeviceScheduler_ptr &, auto &self) {
self.set(AltA{});
}};
Drop-in replacement for the hand-rolled "global variable + enum + name mapping.

Call sites typically check is_set() and, when unset, call shamalgs::impl_registry::autoselect_impl right before dispatching (see e.g. segmented_sort_in_place.cpp). get() assumes is_set(); get_current_config() is the one exception and safely returns a json null instead of dereferencing an unset selection.

Instances are registered by name in shamalgs::impl_registry (which stores their address), so they are neither copyable nor movable.

Template Parameters
Altsthe alternative types, each requiring a static constexpr std::string_view variant_type_name

Definition at line 272 of file ImplVariant.hpp.

Member Typedef Documentation

◆ AutoselectFn

template<class... Alts>
using shamalgs::ImplVariantGlobal< Alts >::AutoselectFn = std::function<void(const sham::DeviceScheduler_ptr &, ImplVariantGlobal &)>

Callable selecting the default implementation, by calling set() on the selector.

Definition at line 277 of file ImplVariant.hpp.

◆ Variant

template<class... Alts>
using shamalgs::ImplVariantGlobal< Alts >::Variant = std::variant<Alts...>

Definition at line 274 of file ImplVariant.hpp.

Constructor & Destructor Documentation

◆ ImplVariantGlobal()

template<class... Alts>
shamalgs::ImplVariantGlobal< Alts >::ImplVariantGlobal ( AutoselectFn fn)
inlineexplicit

Construct an unset selector, whose default implementation is picked by fn.

Definition at line 280 of file ImplVariant.hpp.

Member Function Documentation

◆ autoselect()

template<class... Alts>
void shamalgs::ImplVariantGlobal< Alts >::autoselect ( const sham::DeviceScheduler_ptr & sched)
inlineoverridevirtual

Select the algorithm's default implementation for the device behind sched.

Implements shamalgs::IImplVariant.

Definition at line 297 of file ImplVariant.hpp.

◆ get()

template<class... Alts>
const Variant & shamalgs::ImplVariantGlobal< Alts >::get ( ) const
inline

Get the currently selected implementation. Requires is_set().

Definition at line 302 of file ImplVariant.hpp.

◆ get_current_config()

template<class... Alts>
std::string shamalgs::ImplVariantGlobal< Alts >::get_current_config ( ) const
inlineoverridevirtual

Get the currently selected implementation as a single config json string, or a json null if no implementation has been selected yet (see is_set())

Implements shamalgs::IImplVariant.

Definition at line 306 of file ImplVariant.hpp.

◆ get_default_config_list()

template<class... Alts>
std::vector< std::string > shamalgs::ImplVariantGlobal< Alts >::get_default_config_list ( ) const
inlineoverridevirtual

List the available implementations as config json strings. Most alternatives contribute exactly one entry; alternatives opting into HasCustomDefaults contribute one entry per instance in their variant_custom_defaults() list.

Implements shamalgs::IImplVariant.

Definition at line 316 of file ImplVariant.hpp.

◆ is_set()

template<class... Alts>
bool shamalgs::ImplVariantGlobal< Alts >::is_set ( ) const
inlineoverridevirtual

Whether an implementation has been selected yet.

Implements shamalgs::IImplVariant.

Definition at line 294 of file ImplVariant.hpp.

◆ set() [1/2]

template<class... Alts>
void shamalgs::ImplVariantGlobal< Alts >::set ( std::string_view config_json)
inlineoverridevirtual

Select an implementation from a {"implementation": ..., "parameters": ...} json string.

Implements shamalgs::IImplVariant.

Definition at line 324 of file ImplVariant.hpp.

◆ set() [2/2]

template<class... Alts>
void shamalgs::ImplVariantGlobal< Alts >::set ( Variant v)
inline

Directly select an alternative (e.g. to seed a default at the call site).

Definition at line 321 of file ImplVariant.hpp.


The documentation for this class was generated from the following file: