Template Class CallbackRegistry#
Defined in File CallbackRegistry.h
Class Documentation#
-
template<typename Return, typename ...Args>
class CallbackRegistry# This class stores functions to be called with templatized Return type and Args, and it can call them in order (or reverse order).
The template parameters define the type of function that will be stored on the CallbackRegistry. Functions are identified by a unique name and are stored in the order they were added. They can be added or modified the same way you would modify a map entry. They can be retrieved by name or by index. The functions registered can be called in forward or reverse order using the execute or executeReverse methods.
Public Functions
-
CallbackRegistry() = default#
-
CallbackRegistry(const CallbackRegistry<Return, Args...>&) = delete#
-
CallbackRegistry &operator=(const CallbackRegistry<Return, Args...>&) = delete#
-
inline const f &operator[](const std::string &name) const#
Get the callback function associated with name.
- Parameters:
name – The associated with the callback function.
- Returns:
f A placeholder for the callback function.
-
inline f &operator[](const std::string &name)#
Set the callback function associated with name.
- Parameters:
name – The associated with the callback function.
- Returns:
f The callback function.
-
inline f &operator[](const uint index)#
Set the callback function at the given index.
- Parameters:
index – The index to insert the callback function at.
- Returns:
f A placeholder for the callback function.
-
inline const f &operator[](const uint index) const#
Get the callback function at the given index.
- Parameters:
index – The index of the callback function to get.
- Returns:
f The callback function at index.
-
inline void insertBefore(const std::string &item, const std::string &name, const f &fn)#
Insert a function before another in the registry.
- Parameters:
item – The name of the item to insert this new one before.
name – The name of the new item.
fn – The callback associated with the new item.
-
inline void insertBefore(const uint index, const std::string &name, const f &fn)#
Insert a function before another in the registry.
- Parameters:
index – The index of the item to insert this one before.
name – The name of the new item.
fn – The callback associated with the new item.
-
inline void insertAfter(const std::string &item, const std::string &name, const f &fn)#
Insert a function after another in the registry.
- Parameters:
item – The name of the item to insert this new one after.
name – The name of the new item.
fn – The callback associated with the new item.
-
inline void insertAfter(const uint index, const std::string &name, const f &fn)#
Insert a function before another in the registry.
- Parameters:
index – The index of the item to insert this one before.
name – The name of the new item.
fn – The callback associated with the new item.
-
inline void addDependency(size_t parent, size_t child)#
Add a dependency to the the execution graph.
- Parameters:
parent – The index of the callback that must finish first.
child – The index of the callback that waits for the parent.
-
inline void addDependency(const std::string &parent, const std::string &child)#
Defines a directed edge in the execution graph.
- Parameters:
parent – The name of the callback that must finish first.
child – The name of the callback that waits for the parent.
-
inline void removeDependency(size_t parent, size_t child)#
Remove the dependency in the execution graph.
- Parameters:
parent – The index of the callback that must finish first.
child – The index of the callback that waits for the parent.
-
inline void removeAllDependencies()#
Remove all dependencies from the execution graph.
This causes the execution graph to run all callbacks in parallel.
-
inline void removeAllDependencies(size_t index)#
Remove all dependencies from the given callback index.
The dependencies are the parent edges in the DAG that this callback depends on before it can execute.
- Parameters:
index – The index of the callback to remove all dependencies from.
-
inline void removeAllDependencies(const std::string &name)#
Remove all dependencies from the given callback.
The dependencies are the parent edges in the DAG that this callback depends on before it can execute.
- Parameters:
name – The name of the callback to remove all dependencies from.
-
inline void removeAllDependents(size_t index)#
Remove all dependents from the given callback index.
The dependents are the children edges in the DAG that depend on this callback executing before they can execute.
- Parameters:
index – The index of the callback to remove all dependents from.
-
inline void removeAllDependents(const std::string &name)#
Remove all dependents from the given callback.
The dependents are the children edges in the DAG that depend on this callback executing before they can execute.
- Parameters:
name – The name of the callback to remove all dependents from.
-
inline void removeDependency(const std::string &parent, const std::string &child)#
Remove the dependency in the execution graph.
- Parameters:
parent – The index of the callback that must finish first.
child – The index of the callback that waits for the parent.
-
inline std::vector<std::size_t> getDependents(size_t index)#
Get the dependents for the given callback index.
The dependents are the children in the DAG that depend on this callback completing before they can run.
- Parameters:
index – The index of the callback to get dependents for.
- Returns:
The dependents associated with the given callback index.
-
inline std::vector<std::size_t> getDependents(const std::string &name)#
Get the dependents for the given callback.
The dependents are the children in the DAG that depend on this callback completing before they can run.
- Parameters:
name – The name of the callback to get dependents for.
- Returns:
The dependents associated with the given callback.
-
inline std::vector<std::size_t> getDependencies(size_t index)#
Get the dependencies for the given callback index.
The dependencies are the parent edges in the DAG that this callback depends on before it can execute.
- Parameters:
index – The index of the callback to get dependencies for.
- Returns:
The dependencies associated with the given callback index.
-
inline std::vector<std::size_t> getDependencies(const std::string &name)#
Get the dependencies for the given callback.
The dependencies are the parent edges in the DAG that this callback depends on before it can execute.
- Parameters:
name – The name of the callback to get dependencies for.
- Returns:
The dependencies associated with the given callback.
-
inline void runInParallel(const std::vector<size_t> &indices)#
This runs the callbacks with the associated indices in parallel.
This modifies the DAG as follows:
A “base” callback is found. This is the callback with the shallowest depth in the DAG. If more than one callback has the same depth, then the callback with the lowest index is used.
All the callbacks associated with the given indices are modified to have the same parents/children as the base.
The callbacks whose edges were modified have their previous parents tied to their previous children.
- Parameters:
indices – The indices of the callbacks to run in parallel.
-
inline void runInParallel(const std::vector<std::string> &names)#
This runs the given callbacks in parallel.
This modifies the DAG as follows:
A “base” callback is found. This is the callback with the shallowest depth in the DAG. If more than one callback has the same depth, then the callback with the lowest index is used.
All the callbacks associated with the given indices are modified to have the same parents/children as the base.
The callbacks whose edges were modified have their previous parents tied to their previous children.
- Parameters:
names – The names of the callbacks to run in parallel.
-
inline void transitiveReduction()#
Perform transitive reduction on the callbacks DAG.
This preserves the reachability between all callbacks in the DAG while removing all redundant, transitive edges
-
inline void execute(Args... args)#
Execute the callbacks in order.
- Parameters:
args – The arguments passed to the callbacks.
-
inline std::vector<Return> execute(Args... args)
Execute the callbacks in order.
- Parameters:
args – The arguments passed to the callbacks.
- Returns:
A vector of the return types from each function.
-
inline void executeReverse(Args... args)#
Execute the callbacks in reverse order.
- Parameters:
args – The arguments passed to the callbacks.
-
inline std::vector<Return> executeReverse(Args... args)
Execute the callbacks in reverse order.
- Parameters:
args – The arguments passed to the callbacks.
- Returns:
A vector of the return types from each function.
-
inline void executeAndPopReverse(Args... args)#
Execute the callbacks in reverse order, and delete them as we go.
- Parameters:
args – The arguments passed to the callbacks.
-
inline std::vector<Return> executeAndPopReverse(Args... args)
Execute the callbacks in reverse order and delete them as we go.
- Parameters:
args – The arguments passed to the callbacks.
- Returns:
A vector of the return types from each function.
-
inline size_t size() const#
Returns the number of registered functions.
- Returns:
The number of registered functions
-
inline std::string dumpString(std::string_view prefix = "") const#
Create a string that lists all the functions by name.
- Parameters:
prefix – A string to use as prefix for each output line
- Returns:
A dump string of the CallbackRegistry.
-
inline void dump(std::string_view prefix = "") const#
Print out the string from
dumpString. SeedumpStringfor more details.- Parameters:
prefix – A string to use as prefix for each output line
-
inline void erase(const std::string &name, bool okay_not_exists = false)#
Erase the callback with the associated name from the registry.
- Parameters:
name – The name of the callback to erase.
okay_not_exists – If true, then do not error if the callback doesn’t exist. If false, then throw an error if the callback doesn’t exist.
-
inline void erase(size_t index)#
Erase the callback at the provided index.
- Parameters:
index – The index of the callback.
-
inline f pop(const std::string &name)#
Pop the callback with the associated name from the registry.
- Parameters:
name – The name of the callback to pop.
- Returns:
The function associated with name.
-
inline f pop(size_t index)#
Pop the callback at the provided index.
- Parameters:
index – The index of the callback.
- Returns:
The function at the given index.
-
inline void clear()#
Clear out the entire CallbackRegistry. All callbacks will be removed.
-
inline bool contains(const std::string &name)#
Determine whether the CallbackRegistry contains a function with the provided name.
- Parameters:
name – The name of the callback to check for.
- Returns:
true if a callback with the given name is in the registry, false otherwise.
Public Members
-
bool trace_callback_registry = false#
If true, the print trace messages for this callback registry.
-
CallbackRegistry() = default#