Squid::Tasks 1.0.0
C++14 coroutine-based task library for games
|
Data structure for tracking decentralized state across multiple tasks. More...
Classes | |
class | TokenList< T > |
Container for tracking decentralized state across multiple tasks. (See Token List for more info...) More... | |
struct | Token |
Handle to a TokenList element that stores a debug name. More... | |
struct | DataToken< tData > |
Handle to a TokenList element that stores both a debug name and associated data. More... | |
Functions | |
std::shared_ptr< Token > | MakeToken (std::string in_name) |
Create a token with the specified debug name. | |
template<typename tData > | |
std::shared_ptr< DataToken< tData > > | MakeToken (std::string in_name, tData in_data) |
Create a token with the specified debug name and associated data. | |
Data structure for tracking decentralized state across multiple tasks.
Token objects can be created using TokenList::MakeToken(), returning a shared pointer to a new Token. This new Token can then be added to the TokenList using TokenList::AddToken(). TokenList::TakeToken() can be used to make + add a new token with a single function call.
Because TokenList uses weak pointers to track its elements, Token objects are logically removed from the list once they are destroyed. As such, it is usually unnecessary to explicitly call TokenList::RemoveToken() to remove a Token from the list. Instead, it is idiomatic to consider the Token to be a sort of "scope guard" that will remove itself from all TokenList objects when it leaves scope.
The TokenList class is included as part of Squid::Tasks to provide a simple mechanism for robustly sharing aribtrary state between multiple tasks. Consider this example of a poison damage-over-time system:
As the above example shows, this mechanism is well-suited for coroutines, as they can hold a Token across multiple frames. Also note that Token objects can optionally hold data. The TokenList class has query functions (e.g. GetMin()/GetMax()) that can be used to aggregate the data from the set of live tokens. This is used above to quickly find the highest DPS poison instance.