stats: use absl::optional to represent value

which is a more modern way to represent something that either has a value or is not set

BUG=webrtc:14544

Change-Id: I0a06b30b1c7f802247eb1f60e69271594b94a6f0
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/278421
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Commit-Queue: Philipp Hancke <phancke@microsoft.com>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#38443}
This commit is contained in:
Philipp Hancke
2022-10-13 13:14:01 +02:00
committed by WebRTC LUCI CQ
parent 3f519e0c89
commit 8e7a105c51
3 changed files with 63 additions and 73 deletions

View File

@ -708,6 +708,8 @@ rtc_source_set("rtc_stats_api") {
"../rtc_base/system:rtc_export", "../rtc_base/system:rtc_export",
"units:timestamp", "units:timestamp",
] ]
absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
} }
rtc_library("audio_options_api") { rtc_library("audio_options_api") {

View File

@ -20,6 +20,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "absl/types/optional.h"
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include "rtc_base/system/rtc_export.h" #include "rtc_base/system/rtc_export.h"
#include "rtc_base/system/rtc_export_template.h" #include "rtc_base/system/rtc_export_template.h"
@ -260,7 +261,7 @@ class RTCStatsMemberInterface {
virtual Type type() const = 0; virtual Type type() const = 0;
virtual bool is_sequence() const = 0; virtual bool is_sequence() const = 0;
virtual bool is_string() const = 0; virtual bool is_string() const = 0;
bool is_defined() const { return is_defined_; } virtual bool is_defined() const = 0;
// Is this part of the stats spec? Used so that chromium can easily filter // Is this part of the stats spec? Used so that chromium can easily filter
// out anything unstandardized. // out anything unstandardized.
virtual bool is_standardized() const = 0; virtual bool is_standardized() const = 0;
@ -296,13 +297,11 @@ class RTCStatsMemberInterface {
} }
protected: protected:
RTCStatsMemberInterface(const char* name, bool is_defined) explicit RTCStatsMemberInterface(const char* name) : name_(name) {}
: name_(name), is_defined_(is_defined) {}
virtual bool IsEqual(const RTCStatsMemberInterface& other) const = 0; virtual bool IsEqual(const RTCStatsMemberInterface& other) const = 0;
const char* const name_; const char* const name_;
bool is_defined_;
}; };
// Template implementation of `RTCStatsMemberInterface`. // Template implementation of `RTCStatsMemberInterface`.
@ -312,70 +311,58 @@ template <typename T>
class RTCStatsMember : public RTCStatsMemberInterface { class RTCStatsMember : public RTCStatsMemberInterface {
public: public:
explicit RTCStatsMember(const char* name) explicit RTCStatsMember(const char* name)
: RTCStatsMemberInterface(name, : RTCStatsMemberInterface(name), value_() {}
/*is_defined=*/false),
value_() {}
RTCStatsMember(const char* name, const T& value) RTCStatsMember(const char* name, const T& value)
: RTCStatsMemberInterface(name, : RTCStatsMemberInterface(name), value_(value) {}
/*is_defined=*/true),
value_(value) {}
RTCStatsMember(const char* name, T&& value) RTCStatsMember(const char* name, T&& value)
: RTCStatsMemberInterface(name, : RTCStatsMemberInterface(name), value_(std::move(value)) {}
/*is_defined=*/true), explicit RTCStatsMember(const RTCStatsMember<T>& other)
value_(std::move(value)) {} : RTCStatsMemberInterface(other.name_), value_(other.value_) {}
RTCStatsMember(const RTCStatsMember<T>& other) explicit RTCStatsMember(RTCStatsMember<T>&& other)
: RTCStatsMemberInterface(other.name_, other.is_defined_), : RTCStatsMemberInterface(other.name_), value_(std::move(other.value_)) {}
value_(other.value_) {}
RTCStatsMember(RTCStatsMember<T>&& other)
: RTCStatsMemberInterface(other.name_, other.is_defined_),
value_(std::move(other.value_)) {}
static Type StaticType(); static Type StaticType();
Type type() const override { return StaticType(); } Type type() const override { return StaticType(); }
bool is_sequence() const override; bool is_sequence() const override;
bool is_string() const override; bool is_string() const override;
bool is_defined() const override { return value_.has_value(); }
bool is_standardized() const override { return true; } bool is_standardized() const override { return true; }
std::string ValueToString() const override; std::string ValueToString() const override;
std::string ValueToJson() const override; std::string ValueToJson() const override;
template <typename U> template <typename U>
inline T ValueOrDefault(U default_value) const { inline T ValueOrDefault(U default_value) const {
if (is_defined()) { return value_.value_or(default_value);
return *(*this);
}
return default_value;
} }
// Assignment operators. // Assignment operators.
T& operator=(const T& value) { T& operator=(const T& value) {
value_ = value; value_ = value;
is_defined_ = true; return value_.value();
return value_;
} }
T& operator=(const T&& value) { T& operator=(const T&& value) {
value_ = std::move(value); value_ = std::move(value);
is_defined_ = true; return value_.value();
return value_;
} }
// Value getters. // Value getters.
T& operator*() { T& operator*() {
RTC_DCHECK(is_defined_); RTC_DCHECK(value_);
return value_; return *value_;
} }
const T& operator*() const { const T& operator*() const {
RTC_DCHECK(is_defined_); RTC_DCHECK(value_);
return value_; return *value_;
} }
// Value getters, arrow operator. // Value getters, arrow operator.
T* operator->() { T* operator->() {
RTC_DCHECK(is_defined_); RTC_DCHECK(value_);
return &value_; return &(*value_);
} }
const T* operator->() const { const T* operator->() const {
RTC_DCHECK(is_defined_); RTC_DCHECK(value_);
return &value_; return &(*value_);
} }
protected: protected:
@ -386,15 +373,11 @@ class RTCStatsMember : public RTCStatsMemberInterface {
return false; return false;
const RTCStatsMember<T>& other_t = const RTCStatsMember<T>& other_t =
static_cast<const RTCStatsMember<T>&>(other); static_cast<const RTCStatsMember<T>&>(other);
if (!is_defined_)
return !other_t.is_defined();
if (!other.is_defined())
return false;
return value_ == other_t.value_; return value_ == other_t.value_;
} }
private: private:
T value_; absl::optional<T> value_;
}; };
namespace rtc_stats_internal { namespace rtc_stats_internal {

View File

@ -187,12 +187,12 @@ RTCStats::MembersOfThisObjectAndAncestors(size_t additional_capacity) const {
} \ } \
template <> \ template <> \
std::string RTCStatsMember<T>::ValueToString() const { \ std::string RTCStatsMember<T>::ValueToString() const { \
RTC_DCHECK(is_defined_); \ RTC_DCHECK(value_.has_value()); \
return to_str; \ return to_str; \
} \ } \
template <> \ template <> \
std::string RTCStatsMember<T>::ValueToJson() const { \ std::string RTCStatsMember<T>::ValueToJson() const { \
RTC_DCHECK(is_defined_); \ RTC_DCHECK(value_.has_value()); \
return to_json; \ return to_json; \
} \ } \
template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) RTCStatsMember<T> template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) RTCStatsMember<T>
@ -201,93 +201,98 @@ WEBRTC_DEFINE_RTCSTATSMEMBER(bool,
kBool, kBool,
false, false,
false, false,
rtc::ToString(value_), rtc::ToString(*value_),
rtc::ToString(value_)); rtc::ToString(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t, WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t,
kInt32, kInt32,
false, false,
false, false,
rtc::ToString(value_), rtc::ToString(*value_),
rtc::ToString(value_)); rtc::ToString(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t, WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t,
kUint32, kUint32,
false, false,
false, false,
rtc::ToString(value_), rtc::ToString(*value_),
rtc::ToString(value_)); rtc::ToString(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t, WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t,
kInt64, kInt64,
false, false,
false, false,
rtc::ToString(value_), rtc::ToString(*value_),
ToStringAsDouble(value_)); ToStringAsDouble(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t, WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t,
kUint64, kUint64,
false, false,
false, false,
rtc::ToString(value_), rtc::ToString(*value_),
ToStringAsDouble(value_)); ToStringAsDouble(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(double, WEBRTC_DEFINE_RTCSTATSMEMBER(double,
kDouble, kDouble,
false, false,
false, false,
rtc::ToString(value_), rtc::ToString(*value_),
ToStringAsDouble(value_)); ToStringAsDouble(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true, value_, value_); WEBRTC_DEFINE_RTCSTATSMEMBER(std::string,
kString,
false,
true,
*value_,
*value_);
WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<bool>, WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<bool>,
kSequenceBool, kSequenceBool,
true, true,
false, false,
VectorToString(value_), VectorToString(*value_),
VectorToString(value_)); VectorToString(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int32_t>, WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int32_t>,
kSequenceInt32, kSequenceInt32,
true, true,
false, false,
VectorToString(value_), VectorToString(*value_),
VectorToString(value_)); VectorToString(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint32_t>, WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint32_t>,
kSequenceUint32, kSequenceUint32,
true, true,
false, false,
VectorToString(value_), VectorToString(*value_),
VectorToString(value_)); VectorToString(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int64_t>, WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int64_t>,
kSequenceInt64, kSequenceInt64,
true, true,
false, false,
VectorToString(value_), VectorToString(*value_),
VectorToStringAsDouble(value_)); VectorToStringAsDouble(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint64_t>, WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint64_t>,
kSequenceUint64, kSequenceUint64,
true, true,
false, false,
VectorToString(value_), VectorToString(*value_),
VectorToStringAsDouble(value_)); VectorToStringAsDouble(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<double>, WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<double>,
kSequenceDouble, kSequenceDouble,
true, true,
false, false,
VectorToString(value_), VectorToString(*value_),
VectorToStringAsDouble(value_)); VectorToStringAsDouble(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<std::string>, WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<std::string>,
kSequenceString, kSequenceString,
true, true,
false, false,
VectorOfStringsToString(value_), VectorOfStringsToString(*value_),
VectorOfStringsToString(value_)); VectorOfStringsToString(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(rtc_stats_internal::MapStringUint64, WEBRTC_DEFINE_RTCSTATSMEMBER(rtc_stats_internal::MapStringUint64,
kMapStringUint64, kMapStringUint64,
false, false,
false, false,
MapToString(value_), MapToString(*value_),
MapToStringAsDouble(value_)); MapToStringAsDouble(*value_));
WEBRTC_DEFINE_RTCSTATSMEMBER(rtc_stats_internal::MapStringDouble, WEBRTC_DEFINE_RTCSTATSMEMBER(rtc_stats_internal::MapStringDouble,
kMapStringDouble, kMapStringDouble,
false, false,
false, false,
MapToString(value_), MapToString(*value_),
MapToStringAsDouble(value_)); MapToStringAsDouble(*value_));
// Restricted members that expose hardware capabilites. // Restricted members that expose hardware capabilites.
template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT)