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.
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 a single config json string ABI, so that an algorithm's get_default_impl_list_X / get_current_impl_X / set_impl_X free functions become one-liners: - get_current_config() / get_default_config_list() / set(string_view) : a single <tt>{\"implementation\": ..., \"parameters\": ...}

json string ABI.

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.hpp), 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 call autoselect() 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.

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

Definition at line 265 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 270 of file ImplVariant.hpp.

◆ Variant

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

Definition at line 267 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 273 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 279 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 284 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 288 of file ImplVariant.hpp.

Here is the call graph for this function:

◆ 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 298 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 276 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 306 of file ImplVariant.hpp.

Here is the call graph for this function:

◆ 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 303 of file ImplVariant.hpp.


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