Skip to content
Permalink
Browse files

Common/Analytics: Convert std::string overload into std::string_view

Allows for both string types to be non-allocating. We can't remove the
const char* overload in this case due to the fact that pointers can
implicitly convert to bool, so if we removed the overload all const
char arrays passed in would begin executing the bool overload instead of
the string_view overload, which is definitely not what we want to occur.
  • Loading branch information...
lioncash committed Jun 3, 2019
1 parent 58e2cd5 commit 7935c27b5286f532eac35079765820f853a52b0b
Showing with 10 additions and 5 deletions.
  1. +6 −2 Source/Core/Common/Analytics.cpp
  2. +4 −3 Source/Core/Common/Analytics.h
@@ -76,15 +76,19 @@ AnalyticsReportBuilder::AnalyticsReportBuilder()
m_report.push_back(WIRE_FORMAT_VERSION);
}

void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, const std::string& v)
void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, std::string_view v)
{
AppendType(report, TypeId::STRING);
AppendBytes(report, reinterpret_cast<const u8*>(v.data()), static_cast<u32>(v.size()));
}

// We can't remove this overload despite the string_view overload due to the fact that
// pointers can implicitly convert to bool, so if we removed the overload, then all
// const char strings passed in would begin forwarding to the bool overload,
// which is definitely not what we want to occur.
void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, const char* v)
{
AppendSerializedValue(report, std::string(v));
AppendSerializedValue(report, std::string_view(v));
}

void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, bool v)
@@ -8,6 +8,7 @@
#include <memory>
#include <mutex>
#include <string>
#include <string_view>
#include <thread>
#include <utility>
#include <vector>
@@ -86,7 +87,7 @@ class AnalyticsReportBuilder
}

template <typename T>
AnalyticsReportBuilder& AddData(const std::string& key, const T& value)
AnalyticsReportBuilder& AddData(std::string_view key, const T& value)
{
std::lock_guard lk{m_lock};
AppendSerializedValue(&m_report, key);
@@ -95,7 +96,7 @@ class AnalyticsReportBuilder
}

template <typename T>
AnalyticsReportBuilder& AddData(const std::string& key, const std::vector<T>& value)
AnalyticsReportBuilder& AddData(std::string_view key, const std::vector<T>& value)
{
std::lock_guard lk{m_lock};
AppendSerializedValue(&m_report, key);
@@ -117,7 +118,7 @@ class AnalyticsReportBuilder
}

protected:
static void AppendSerializedValue(std::string* report, const std::string& v);
static void AppendSerializedValue(std::string* report, std::string_view v);
static void AppendSerializedValue(std::string* report, const char* v);
static void AppendSerializedValue(std::string* report, bool v);
static void AppendSerializedValue(std::string* report, u64 v);

0 comments on commit 7935c27

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