Source release 17.1.0
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
// Copyright 2017 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
//
|
||||
// This file contains the declarations for the Metric class and related
|
||||
// types.
|
||||
#ifndef WVCDM_METRICS_VALUE_METRIC_H_
|
||||
#define WVCDM_METRICS_VALUE_METRIC_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
@@ -13,114 +15,103 @@
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
// Internal namespace for helper methods.
|
||||
namespace impl {
|
||||
|
||||
namespace internal {
|
||||
// Helper function for setting a value in the proto.
|
||||
template <typename T>
|
||||
void SetValue(drm_metrics::ValueMetric *value_proto, const T &value);
|
||||
|
||||
} // namespace impl
|
||||
void SetValue(drm_metrics::ValueMetric* value_proto, const T& value);
|
||||
} // namespace internal
|
||||
|
||||
// The ValueMetric class supports storing a single, overwritable value or an
|
||||
// error code. This class can be serialized to a drm_metrics::ValueMetric proto.
|
||||
// If an error or value was not provided, this metric will serialize to nullptr.
|
||||
//
|
||||
// Example Usage:
|
||||
// Metric<string> cdm_version().Record("a.b.c.d");
|
||||
// ValueMetric<string> cdm_version().Record("a.b.c.d");
|
||||
// std::unique_ptr<drm_metrics::ValueMetric> mymetric(
|
||||
// cdm_version.ToProto());
|
||||
//
|
||||
// Example Error Usage:
|
||||
//
|
||||
// Metric<string> cdm_version().SetError(error_code);
|
||||
// ValueMetric<string> cdm_version().SetError(error_code);
|
||||
// std::unique_ptr<drm_metrics::ValueMetric> mymetric(
|
||||
// cdm_version.ToProto());
|
||||
//
|
||||
template <typename T>
|
||||
class ValueMetric {
|
||||
public:
|
||||
// Constructs a metric with the given metric name.
|
||||
explicit ValueMetric()
|
||||
: error_code_(0), has_error_(false), has_value_(false) {}
|
||||
ValueMetric() {}
|
||||
|
||||
// Record the value of the metric.
|
||||
void Record(const T &value) {
|
||||
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||
void Record(const T& value) {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
value_ = value;
|
||||
has_value_ = true;
|
||||
has_error_ = false;
|
||||
state_ = kHasValue;
|
||||
}
|
||||
|
||||
// Set the error code if an error was encountered.
|
||||
void SetError(int error_code) {
|
||||
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
error_code_ = error_code;
|
||||
has_value_ = false;
|
||||
has_error_ = true;
|
||||
state_ = kHasError;
|
||||
}
|
||||
|
||||
bool HasValue() const {
|
||||
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||
return has_value_;
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
return state_ == kHasValue;
|
||||
}
|
||||
const T &GetValue() const {
|
||||
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||
const T& GetValue() const {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
return value_;
|
||||
}
|
||||
|
||||
bool HasError() const {
|
||||
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||
return has_error_;
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
return state_ == kHasError;
|
||||
}
|
||||
int GetError() const {
|
||||
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
return error_code_;
|
||||
}
|
||||
|
||||
// Clears the indicators that the metric or error was set.
|
||||
void Clear() {
|
||||
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||
has_value_ = false;
|
||||
has_error_ = false;
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
state_ = kNone;
|
||||
}
|
||||
|
||||
// Returns a new ValueMetric proto containing the metric value or the
|
||||
// error code. If neither the error or value are set, it returns nullptr.
|
||||
drm_metrics::ValueMetric *ToProto() const {
|
||||
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||
if (has_error_) {
|
||||
drm_metrics::ValueMetric *value_proto = new drm_metrics::ValueMetric;
|
||||
value_proto->set_error_code(error_code_);
|
||||
return value_proto;
|
||||
} else if (has_value_) {
|
||||
drm_metrics::ValueMetric *value_proto = new drm_metrics::ValueMetric;
|
||||
impl::SetValue(value_proto, value_);
|
||||
return value_proto;
|
||||
drm_metrics::ValueMetric* ToProto() const {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
switch (state_) {
|
||||
case kHasValue: {
|
||||
drm_metrics::ValueMetric* value_proto = new drm_metrics::ValueMetric;
|
||||
internal::SetValue(value_proto, value_);
|
||||
return value_proto;
|
||||
}
|
||||
case kHasError: {
|
||||
drm_metrics::ValueMetric* value_proto = new drm_metrics::ValueMetric;
|
||||
value_proto->set_error_code(error_code_);
|
||||
return value_proto;
|
||||
}
|
||||
case kNone:
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
enum ValueState { kNone, kHasValue, kHasError };
|
||||
|
||||
T value_;
|
||||
int error_code_;
|
||||
bool has_error_;
|
||||
bool has_value_;
|
||||
int error_code_ = 0;
|
||||
ValueState state_ = kNone;
|
||||
|
||||
/*
|
||||
* This locks the internal state of the value metric to ensure safety
|
||||
* across multiple threads preventing the caller from worrying about
|
||||
* locking.
|
||||
*
|
||||
* This field must be declared mutable because the lock must be takeable even
|
||||
* in const methods.
|
||||
*/
|
||||
mutable std::mutex internal_lock_;
|
||||
// This locks the internal state of the value metric to ensure safety
|
||||
// across multiple threads preventing the caller from worrying about
|
||||
// locking.
|
||||
mutable std::mutex mutex_;
|
||||
};
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_VALUE_METRIC_H_
|
||||
|
||||
Reference in New Issue
Block a user