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.
This commit is contained in:
Lioncash 2019-06-03 18:35:26 -04:00
parent 58e2cd5486
commit 7935c27b52
2 changed files with 10 additions and 5 deletions

View File

@ -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)

View File

@ -8,6 +8,7 @@
#include <memory>
#include <mutex>
#include <string>
#include <string_view>
#include <thread>
#include <utility>
#include <vector>
@ -86,7 +87,7 @@ public:
}
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 @@ public:
}
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 @@ public:
}
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);