Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
impl_registry.hpp File Reference

Name-keyed registry of the implementation selectors (IImplVariant) of every algorithm. More...

#include "shambase/call_lambda.hpp"
#include "shambase/unique_name_macro.hpp"
#include "shamalgs/ImplVariant.hpp"
#include "shambackends/DeviceScheduler.hpp"
#include <string_view>
#include <string>
#include <vector>
Include dependency graph for impl_registry.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  shamalgs
 namespace to contain everything implemented by shamalgs

Macros

#define SHAMALGS_REGISTER_IMPL(name, impl)
 Register the implementation selector impl under name at static initialization.

Functions

void shamalgs::impl_registry::register_impl (std::string name, IImplVariant &impl)
 Register an implementation selector under name.
std::vector< std::string > shamalgs::impl_registry::get_registered_algs ()
 Get the names of every registered algorithm, sorted.
std::vector< std::string > shamalgs::impl_registry::get_default_impl_list (std::string_view alg)
 Get the list of available implementations of alg, as config json strings.
std::string shamalgs::impl_registry::get_current_impl (std::string_view alg)
 Get the current implementation of alg as a config json string, "null" if unset.
bool shamalgs::impl_registry::is_impl_set (std::string_view alg)
 Whether an implementation of alg has been selected yet.
void shamalgs::impl_registry::set_impl (std::string_view alg, std::string_view impl)
 Select the implementation of alg from a config json string.
void shamalgs::impl_registry::autoselect_impl (std::string_view alg, const sham::DeviceScheduler_ptr &sched)
 Select the default implementation of alg for the device behind sched.

Detailed Description

Name-keyed registry of the implementation selectors (IImplVariant) of every algorithm.

Author
Timothée David–Cléris (tim.s.nosp@m.hamr.nosp@m.ock@p.nosp@m.roto.nosp@m.n.me)

Each algorithm registers its selector once, at static initialization, right after defining it:

constexpr std::string_view my_algo_impl_name = "my_algo";
[](const sham::DeviceScheduler_ptr &, auto &self) {
self.set(AltA{});
}};
// same translation unit, so initialized after my_algo_impl
SHAMALGS_REGISTER_IMPL(my_algo_impl_name, my_algo_impl);
Drop-in replacement for the hand-rolled "global variable + enum + name mapping.
#define SHAMALGS_REGISTER_IMPL(name, impl)
Register the implementation selector impl under name at static initialization.

The selection of any registered algorithm can then be read or changed by name. Every function taking an algorithm name throws std::invalid_argument if no algorithm is registered under it.

Definition in file impl_registry.hpp.

Macro Definition Documentation

◆ SHAMALGS_REGISTER_IMPL

#define SHAMALGS_REGISTER_IMPL ( name,
impl )
Value:
[[maybe_unused]] static const shambase::call_lambda __shamrock_unique_name( \
shamalgs_impl_registration_) { \
[] { \
shamalgs::impl_registry::register_impl(std::string(name), impl); \
} \
}
Execute a lambda when a call_lambda object is constructed.

Register the implementation selector impl under name at static initialization.

Meant for namespace scope in a .cpp file, right after the definition of impl: objects of one translation unit are initialized in definition order, so impl is constructed by then. See shamalgs::impl_registry::register_impl for the requirements and errors.

Usage :

SHAMALGS_REGISTER_IMPL(my_algo_impl_name, my_algo_impl);
Parameters
namethe registry name (string literal or std::string_view constant)
implthe namespace-scope IImplVariant to register

Definition at line 94 of file impl_registry.hpp.

Function Documentation

◆ autoselect_impl()

void shamalgs::impl_registry::autoselect_impl ( std::string_view alg,
const sham::DeviceScheduler_ptr & sched )

Select the default implementation of alg for the device behind sched.

Exceptions
std::runtime_errorif sched is null

Definition at line 99 of file impl_registry.cpp.

Here is the call graph for this function:

◆ get_current_impl()

std::string shamalgs::impl_registry::get_current_impl ( std::string_view alg)

Get the current implementation of alg as a config json string, "null" if unset.

Definition at line 86 of file impl_registry.cpp.

◆ get_default_impl_list()

std::vector< std::string > shamalgs::impl_registry::get_default_impl_list ( std::string_view alg)

Get the list of available implementations of alg, as config json strings.

Definition at line 82 of file impl_registry.cpp.

◆ get_registered_algs()

std::vector< std::string > shamalgs::impl_registry::get_registered_algs ( )

Get the names of every registered algorithm, sorted.

Definition at line 73 of file impl_registry.cpp.

◆ is_impl_set()

bool shamalgs::impl_registry::is_impl_set ( std::string_view alg)

Whether an implementation of alg has been selected yet.

Definition at line 90 of file impl_registry.cpp.

◆ register_impl()

void shamalgs::impl_registry::register_impl ( std::string name,
IImplVariant & impl )

Register an implementation selector under name.

The registry stores the address of impl, which must therefore outlive every later use of the registry (in practice, a namespace-scope global). There is no unregister.

Exceptions
std::invalid_argumentif name is already registered (nothing is stored then)

Definition at line 64 of file impl_registry.cpp.

Here is the call graph for this function:

◆ set_impl()

void shamalgs::impl_registry::set_impl ( std::string_view alg,
std::string_view impl )

Select the implementation of alg from a config json string.

Definition at line 92 of file impl_registry.cpp.

Here is the call graph for this function: