67#include "TasksConfig.h"
71template <
typename T =
void>
78 Token(std::string in_name)
79 : name(std::move(in_name))
87template <
typename tData>
90 DataToken(std::string in_name, tData in_data)
91 : name(std::move(in_name))
92 , data(std::move(in_data))
100inline std::shared_ptr<Token>
MakeToken(std::string in_name)
102 return std::make_shared<Token>(std::move(in_name));
106template <
typename tData>
107std::shared_ptr<DataToken<tData>>
MakeToken(std::string in_name, tData in_data)
109 return std::make_shared<DataToken<tData>>(std::move(in_name), std::move(in_data));
122 template <typename U = T, typename std::enable_if_t<std::is_void<U>::value>* =
nullptr>
123 static std::shared_ptr<Token>
MakeToken(std::string in_name)
125 return std::make_shared<Token>(std::move(in_name));
129 template <typename U = T, typename std::enable_if_t<!std::is_void<U>::value>* =
nullptr>
130 static std::shared_ptr<Token>
MakeToken(std::string in_name, U in_data)
132 return std::make_shared<Token>(std::move(in_name), std::move(in_data));
136 template <typename U = T, typename std::enable_if_t<std::is_void<U>::value>* =
nullptr>
137 SQUID_NODISCARD std::shared_ptr<Token>
TakeToken(std::string in_name)
139 return AddTokenInternal(
MakeToken(std::move(in_name)));
143 template <typename U = T, typename std::enable_if_t<!std::is_void<U>::value>* =
nullptr>
144 SQUID_NODISCARD std::shared_ptr<Token>
TakeToken(std::string in_name, U in_data)
146 return AddTokenInternal(
MakeToken(std::move(in_name), std::move(in_data)));
150 std::shared_ptr<Token>
AddToken(std::shared_ptr<Token> in_token)
152 SQUID_RUNTIME_CHECK(in_token,
"Cannot add null token");
153 auto foundIter = std::find_if(m_tokens.begin(), m_tokens.end(), [&in_token](
const std::weak_ptr<Token> in_iterToken){
154 return in_iterToken.lock() == in_token;
157 if(foundIter == m_tokens.end())
159 return AddTokenInternal(in_token);
170 m_tokens.erase(std::remove_if(m_tokens.begin(), m_tokens.end(), [&in_token](
const std::weak_ptr<Token>& in_otherToken) {
171 return !in_otherToken.owner_before(in_token) && !in_token.owner_before(in_otherToken);
177 operator bool()
const
186 for(
auto i = (int32_t)(m_tokens.size() - 1); i >= 0; --i)
188 const auto& token = m_tokens[i];
201 std::vector<T> tokenData;
202 for(
const auto& tokenWeak : m_tokens)
204 if(
auto token = tokenWeak.lock())
206 tokenData.push_back(token->data);
220 return m_tokens.size() ? m_tokens.front().lock()->data : std::optional<T>{};
227 return m_tokens.size() ? m_tokens.back().lock()->data : std::optional<T>{};
233 std::optional<T> ret;
234 SanitizeAndProcessData([&ret](
const T& in_data) {
235 if(!ret || in_data < ret.value())
246 std::optional<T> ret;
247 SanitizeAndProcessData([&ret](
const T& in_data) {
248 if(!ret || in_data > ret.value())
259 std::optional<double> ret;
260 std::optional<double> total;
261 SanitizeAndProcessData([&total](
const T& in_data) {
262 total = total.value_or(0.0) + (
double)in_data;
266 ret = total.value() / m_tokens.size();
272 template <typename U = T, typename std::enable_if_t<!std::is_void<U>::value>* =
nullptr>
275 bool containsData =
false;
276 SanitizeAndProcessData([&in_searchData, &containsData](
const T& in_data) {
277 if(in_searchData == in_data)
289 std::vector<std::string> tokenStrings;
290 std::string debugStr;
291 for(
const auto& token : m_tokens)
295 if(debugStr.size() > 0)
299 debugStr += token.lock()->name;
302 if(debugStr.size() == 0)
304 debugStr =
"[no tokens]";
311 std::shared_ptr<Token> AddTokenInternal(std::shared_ptr<Token> in_token)
314 m_tokens.push_back(in_token);
319 void Sanitize()
const
324 m_tokens.erase(std::remove_if(m_tokens.begin(), m_tokens.end(), [](
const std::weak_ptr<Token>& in_token) {
325 return in_token.expired();
329 template <
typename tFn>
330 void SanitizeAndProcessData(tFn in_dataFn)
const
335 m_tokens.erase(std::remove_if(m_tokens.begin(), m_tokens.end(), [&in_dataFn](
const std::weak_ptr<Token>& in_token) {
336 if(in_token.expired())
340 if(
auto token = in_token.lock())
342 in_dataFn(token->data);
350 mutable std::vector<std::weak_ptr<Token>> m_tokens;
Container for tracking decentralized state across multiple tasks. (See Token List for more info....
Definition: TokenList.h:116
std::optional< double > GetMean() const
Returns arithmetic mean of all associated data from the set of live tokens.
Definition: TokenList.h:257
typename std::conditional_t< std::is_void< T >::value, Token, DataToken< T > > Token
Type of Token tracked by this container.
Definition: TokenList.h:119
std::shared_ptr< Token > TakeToken(std::string in_name, U in_data)
Create and add a token with the specified debug name and associated data.
Definition: TokenList.h:144
std::optional< T > GetMax() const
Returns largest associated data from the set of live tokens.
Definition: TokenList.h:244
std::shared_ptr< Token > AddToken(std::shared_ptr< Token > in_token)
Add an existing token to this container.
Definition: TokenList.h:150
std::optional< T > GetLeastRecent() const
Returns associated data from the least-recently-added live token.
Definition: TokenList.h:217
bool HasTokens() const
Returns whether this container holds any live tokens.
Definition: TokenList.h:183
bool Contains(const U &in_searchData) const
Returns whether the set of live tokens contains at least one token associated with the specified data...
Definition: TokenList.h:273
std::shared_ptr< Token > TakeToken(std::string in_name)
Create and add a token with the specified debug name.
Definition: TokenList.h:137
std::optional< T > GetMostRecent() const
Returns associated data from the most-recently-added live token.
Definition: TokenList.h:224
std::vector< T > GetTokenData() const
Returns an array of all live token data.
Definition: TokenList.h:199
std::optional< T > GetMin() const
Returns smallest associated data from the set of live tokens.
Definition: TokenList.h:231
static std::shared_ptr< Token > MakeToken(std::string in_name, U in_data)
Create a token with the specified debug name and associated data.
Definition: TokenList.h:130
void RemoveToken(std::shared_ptr< Token > in_token)
Explicitly remove a token from this container.
Definition: TokenList.h:165
static std::shared_ptr< Token > MakeToken(std::string in_name)
Create a token with the specified debug name.
Definition: TokenList.h:123
std::string GetDebugString() const
Returns a debug string containing a list of the debug names of all live tokens.
Definition: TokenList.h:287
std::shared_ptr< Token > MakeToken(std::string in_name)
Create a token with the specified debug name.
Definition: TokenList.h:100
Handle to a TokenList element that stores both a debug name and associated data.
Definition: TokenList.h:89
Handle to a TokenList element that stores a debug name.
Definition: TokenList.h:77