Skip to content
Permalink
Browse files

Common/Analytics: Use deduction guides for std::lock_guard

Avoids needing to hardcode the type of mutex. We can also make use of
scoped_lock where two consecutive lock_guard instances are used.
  • Loading branch information...
lioncash committed Jun 3, 2019
1 parent 6df65d7 commit f813c4951a4e150b1f431085efdacf364b217dda
Showing with 7 additions and 8 deletions.
  1. +7 −8 Source/Core/Common/Analytics.h
@@ -61,16 +61,15 @@ class AnalyticsReportBuilder
AnalyticsReportBuilder(const AnalyticsReportBuilder& other) { *this = other; }
AnalyticsReportBuilder(AnalyticsReportBuilder&& other)
{
std::lock_guard<std::mutex> lk(other.m_lock);
std::lock_guard lk{other.m_lock};
m_report = std::move(other.m_report);
}

const AnalyticsReportBuilder& operator=(const AnalyticsReportBuilder& other)
{
if (this != &other)
{
std::lock_guard<std::mutex> lk(m_lock);
std::lock_guard<std::mutex> lk2(other.m_lock);
std::scoped_lock lk{m_lock, other.m_lock};
m_report = other.m_report;
}
return *this;
@@ -81,15 +80,15 @@ class AnalyticsReportBuilder
{
// Get before locking the object to avoid deadlocks with this += this.
std::string other_report = other.Get();
std::lock_guard<std::mutex> lk(m_lock);
std::lock_guard lk{m_lock};
m_report += other_report;
return *this;
}

template <typename T>
AnalyticsReportBuilder& AddData(const std::string& key, const T& value)
{
std::lock_guard<std::mutex> lk(m_lock);
std::lock_guard lk{m_lock};
AppendSerializedValue(&m_report, key);
AppendSerializedValue(&m_report, value);
return *this;
@@ -98,22 +97,22 @@ class AnalyticsReportBuilder
template <typename T>
AnalyticsReportBuilder& AddData(const std::string& key, const std::vector<T>& value)
{
std::lock_guard<std::mutex> lk(m_lock);
std::lock_guard lk{m_lock};
AppendSerializedValue(&m_report, key);
AppendSerializedValueVector(&m_report, value);
return *this;
}

std::string Get() const
{
std::lock_guard<std::mutex> lk(m_lock);
std::lock_guard lk{m_lock};
return m_report;
}

// More efficient version of Get().
std::string Consume()
{
std::lock_guard<std::mutex> lk(m_lock);
std::lock_guard lk{m_lock};
return std::move(m_report);
}

0 comments on commit f813c49

Please sign in to comment.
You can’t perform that action at this time.