[drape] [Booking] Exclude the possibility of a race condition MAPSME-14325

This commit is contained in:
Anatoliy Tomilov 2020-08-13 14:18:52 +05:00 committed by Tatiana Yan
parent ae69fde178
commit e6eb7c6195
2 changed files with 33 additions and 36 deletions

View file

@ -840,7 +840,10 @@ void SearchMarks::SetPrices(std::vector<FeatureID> const & features, std::vector
void SearchMarks::OnActivate(FeatureID const & featureId)
{
m_selected = featureId;
{
std::scoped_lock<std::mutex> lock(m_lock);
m_selected = featureId;
}
ProcessMarks([&featureId](SearchMarkPoint * mark) -> base::ControlFlow
{
if (featureId != mark->GetFeatureID())
@ -862,6 +865,11 @@ void SearchMarks::OnActivate(FeatureID const & featureId)
void SearchMarks::OnDeactivate(FeatureID const & featureId)
{
{
std::scoped_lock<std::mutex> lock(m_lock);
m_visited.insert(featureId);
m_selected = {};
}
ProcessMarks([&featureId](SearchMarkPoint * mark) -> base::ControlFlow
{
if (featureId != mark->GetFeatureID())
@ -871,20 +879,14 @@ void SearchMarks::OnDeactivate(FeatureID const & featureId)
mark->SetSelected(false);
return base::ControlFlow::Break;
});
m_visited.insert(featureId);
m_selected = {};
}
bool SearchMarks::IsVisited(FeatureID const & id) const
{
return m_visited.find(id) != m_visited.cend();
}
void SearchMarks::ClearVisited() { m_visited.clear(); }
void SearchMarks::SetUnavailable(SearchMarkPoint & mark, std::string const & reasonKey)
{
m_unavailable.insert_or_assign(mark.GetFeatureID(), reasonKey);
{
std::scoped_lock<std::mutex> lock(m_lock);
m_unavailable.insert_or_assign(mark.GetFeatureID(), reasonKey);
}
mark.SetAvailable(false);
mark.SetReason(platform::GetLocalizedString(reasonKey));
}
@ -904,33 +906,32 @@ void SearchMarks::SetUnavailable(std::vector<FeatureID> const & features,
});
}
bool SearchMarks::IsVisited(FeatureID const & id) const
{
std::scoped_lock<std::mutex> lock(m_lock);
return m_visited.find(id) != m_visited.cend();
}
bool SearchMarks::IsUnavailable(FeatureID const & id) const
{
std::scoped_lock<std::mutex> lock(m_lock);
return m_unavailable.find(id) != m_unavailable.cend();
}
void SearchMarks::ClearUnavailable()
bool SearchMarks::IsSelected(FeatureID const & id) const
{
m_unavailable.clear();
ProcessMarks([](SearchMarkPoint * mark) -> base::ControlFlow
{
mark->SetAvailable(true);
mark->SetReason({});
return base::ControlFlow::Continue;
});
std::scoped_lock<std::mutex> lock(m_lock);
return id == m_selected;
}
bool SearchMarks::IsSelected(FeatureID const & id) { return id == m_selected; }
void SearchMarks::ClearTrackedProperties()
{
ClearVisited();
ClearUnavailable();
ClearSelected();
std::scoped_lock<std::mutex> lock(m_lock);
m_visited.clear();
m_unavailable.clear();
m_selected = {};
}
void SearchMarks::ClearSelected() { m_selected = {}; }
void SearchMarks::ProcessMarks(
std::function<base::ControlFlow(SearchMarkPoint *)> && processor) const
{

View file

@ -19,6 +19,7 @@
#include <string>
#include <utility>
#include <vector>
#include <mutex>
class BookmarkManager;
@ -124,13 +125,12 @@ public:
void OnActivate(FeatureID const & featureId);
void OnDeactivate(FeatureID const & featureId);
bool IsVisited(FeatureID const & id) const;
void SetUnavailable(SearchMarkPoint & mark, std::string const & reasonKey);
void SetUnavailable(std::vector<FeatureID> const & features, std::string const & reasonKey);
bool IsUnavailable(FeatureID const & id) const;
bool IsSelected(FeatureID const & id);
bool IsVisited(FeatureID const & id) const;
bool IsUnavailable(FeatureID const & id) const;
bool IsSelected(FeatureID const & id) const;
void ClearTrackedProperties();
@ -140,17 +140,13 @@ public:
private:
void ProcessMarks(std::function<base::ControlFlow(SearchMarkPoint *)> && processor) const;
void ClearVisited();
void ClearUnavailable();
void ClearSelected();
BookmarkManager * m_bmManager;
df::DrapeEngineSafePtr m_drapeEngine;
static std::map<std::string, m2::PointF> m_searchMarksSizes;
mutable std::mutex m_lock;
std::set<FeatureID> m_visited;
// The value is localized string key for unavailability reason.
std::map<FeatureID, std::string> m_unavailable;
std::map<FeatureID, std::string /* SearchMarkPoint::m_reason */> m_unavailable;
FeatureID m_selected;
};