1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
/*
* CQuery.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "QueriesProcessor.h"
#include "CQuery.h"
void QueriesProcessor::popQuery(PlayerColor player, QueryPtr query)
{
LOG_TRACE_PARAMS(logGlobal, "player='%s', query='%s'", player % query);
if(topQuery(player) != query)
{
logGlobal->trace("Cannot remove, not a top!");
return;
}
queries[player] -= query;
auto nextQuery = topQuery(player);
query->onRemoval(player);
//Exposure on query below happens only if removal didn't trigger any new query
if(nextQuery && nextQuery == topQuery(player))
nextQuery->onExposure(query);
}
void QueriesProcessor::popQuery(const CQuery &query)
{
LOG_TRACE_PARAMS(logGlobal, "query='%s'", query);
assert(query.players.size());
for(auto player : query.players)
{
auto top = topQuery(player);
if(top.get() == &query)
popQuery(top);
else
{
logGlobal->trace("Cannot remove query %s", query.toString());
logGlobal->trace("Queries found:");
for(auto q : queries[player])
{
logGlobal->trace(q->toString());
}
}
}
}
void QueriesProcessor::popQuery(QueryPtr query)
{
for(auto player : query->players)
popQuery(player, query);
}
void QueriesProcessor::addQuery(QueryPtr query)
{
for(auto player : query->players)
addQuery(player, query);
for(auto player : query->players)
query->onAdded(player);
}
void QueriesProcessor::addQuery(PlayerColor player, QueryPtr query)
{
LOG_TRACE_PARAMS(logGlobal, "player='%d', query='%s'", player.getNum() % query);
query->onAdding(player);
queries[player].push_back(query);
}
QueryPtr QueriesProcessor::topQuery(PlayerColor player)
{
return vstd::backOrNull(queries[player]);
}
void QueriesProcessor::popIfTop(QueryPtr query)
{
LOG_TRACE_PARAMS(logGlobal, "query='%d'", query);
if(!query)
logGlobal->error("The query is nullptr! Ignoring.");
popIfTop(*query);
}
void QueriesProcessor::popIfTop(const CQuery & query)
{
for(PlayerColor color : query.players)
if(topQuery(color).get() == &query)
popQuery(color, topQuery(color));
}
std::vector<std::shared_ptr<const CQuery>> QueriesProcessor::allQueries() const
{
std::vector<std::shared_ptr<const CQuery>> ret;
for(auto & playerQueries : queries)
for(auto & query : playerQueries.second)
ret.push_back(query);
return ret;
}
std::vector<QueryPtr> QueriesProcessor::allQueries()
{
//TODO code duplication with const function :(
std::vector<QueryPtr> ret;
for(auto & playerQueries : queries)
for(auto & query : playerQueries.second)
ret.push_back(query);
return ret;
}
QueryPtr QueriesProcessor::getQuery(QueryID queryID)
{
for(auto & playerQueries : queries)
for(auto & query : playerQueries.second)
if(query->queryID == queryID)
return query;
return nullptr;
}
|