Commit 3f67719f authored by Philip Rogers's avatar Philip Rogers Committed by Commit Bot

Add OptionalOrNullptr helper for converting base::Optional<T> to T*

A common pattern for converting base::Optional<T> to T* is:
  optional ? &*optional : nullptr
This patch adds a helper to make the pattern simpler: OptionalOrNullptr.
This expression is relatively common, in part, because the styleguide
recommends T* parameters over Optional<T>:
https://chromium.googlesource.com/chromium/src/+/master/docs/optional.md#When-not-to-use

This patch also updates callsites in third_party/WebKit/* and cc/*.

Cq-Include-Trybots: master.tryserver.blink:linux_trusty_blink_rel;master.tryserver.chromium.android:android_optional_gpu_tests_rel;master.tryserver.chromium.linux:linux_layout_tests_slimming_paint_v2
Change-Id: I2c8b31b52480ad69228123df951486634edec006
Reviewed-on: https://chromium-review.googlesource.com/950226
Commit-Queue: Philip Rogers <pdr@chromium.org>
Reviewed-by: default avatardanakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#541993}
parent 69b4f568
......@@ -21,6 +21,7 @@
#include <vector>
#include "base/logging.h"
#include "base/optional.h"
namespace base {
......@@ -336,6 +337,17 @@ class IsNotIn {
const typename Collection::const_iterator end_;
};
// Helper for returning the optional value's address, or nullptr.
template <class T>
T* OptionalOrNullptr(base::Optional<T>& optional) {
return optional.has_value() ? &optional.value() : nullptr;
}
template <class T>
const T* OptionalOrNullptr(const base::Optional<T>& optional) {
return optional.has_value() ? &optional.value() : nullptr;
}
} // namespace base
#endif // BASE_STL_UTIL_H_
......@@ -438,5 +438,14 @@ TEST(ContainsValue, OrdinaryArrays) {
EXPECT_TRUE(ContainsValue(allowed_chars_including_nul, 0));
}
TEST(STLUtilTest, OptionalOrNullptr) {
Optional<float> optional;
EXPECT_EQ(nullptr, base::OptionalOrNullptr(optional));
optional = 0.1f;
EXPECT_EQ(&optional.value(), base::OptionalOrNullptr(optional));
EXPECT_NE(nullptr, base::OptionalOrNullptr(optional));
}
} // namespace
} // namespace base
......@@ -8,6 +8,7 @@
#include "base/containers/stack_container.h"
#include "base/logging.h"
#include "base/optional.h"
#include "base/stl_util.h"
#include "cc/paint/paint_export.h"
#include "cc/paint/paint_flags.h"
#include "cc/paint/paint_image.h"
......@@ -98,7 +99,7 @@ class CC_PAINT_EXPORT PaintFilter : public SkRefCnt {
return str.c_str();
}
const CropRect* crop_rect() const {
return crop_rect_ ? &*crop_rect_ : nullptr;
return base::OptionalOrNullptr(crop_rect_);
}
virtual size_t SerializedSize() const = 0;
......
......@@ -7,6 +7,7 @@
#include <stddef.h>
#include <algorithm>
#include "base/stl_util.h"
#include "cc/paint/image_transfer_cache_entry.h"
#include "cc/paint/paint_flags.h"
#include "cc/paint/paint_image_builder.h"
......@@ -666,7 +667,7 @@ void PaintOpReader::ReadColorFilterPaintFilter(
return;
filter->reset(new ColorFilterPaintFilter(std::move(color_filter),
std::move(input),
crop_rect ? &*crop_rect : nullptr));
base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadBlurPaintFilter(
......@@ -685,7 +686,7 @@ void PaintOpReader::ReadBlurPaintFilter(
return;
filter->reset(new BlurPaintFilter(sigma_x, sigma_y, tile_mode,
std::move(input),
crop_rect ? &*crop_rect : nullptr));
base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadDropShadowPaintFilter(
......@@ -714,7 +715,7 @@ void PaintOpReader::ReadDropShadowPaintFilter(
return;
filter->reset(new DropShadowPaintFilter(dx, dy, sigma_x, sigma_y, color,
shadow_mode, std::move(input),
crop_rect ? &*crop_rect : nullptr));
base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadMagnifierPaintFilter(
......@@ -730,7 +731,7 @@ void PaintOpReader::ReadMagnifierPaintFilter(
if (!valid_)
return;
filter->reset(new MagnifierPaintFilter(src_rect, inset, std::move(input),
crop_rect ? &*crop_rect : nullptr));
base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadComposePaintFilter(
......@@ -762,7 +763,7 @@ void PaintOpReader::ReadAlphaThresholdPaintFilter(
return;
filter->reset(new AlphaThresholdPaintFilter(
region, inner_min, outer_max, std::move(input),
crop_rect ? &*crop_rect : nullptr));
base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadXfermodePaintFilter(
......@@ -784,7 +785,7 @@ void PaintOpReader::ReadXfermodePaintFilter(
filter->reset(new XfermodePaintFilter(blend_mode, std::move(background),
std::move(foreground),
crop_rect ? &*crop_rect : nullptr));
base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadArithmeticPaintFilter(
......@@ -808,7 +809,7 @@ void PaintOpReader::ReadArithmeticPaintFilter(
return;
filter->reset(new ArithmeticPaintFilter(
k1, k2, k3, k4, enforce_pm_color, std::move(background),
std::move(foreground), crop_rect ? &*crop_rect : nullptr));
std::move(foreground), base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadMatrixConvolutionPaintFilter(
......@@ -848,7 +849,7 @@ void PaintOpReader::ReadMatrixConvolutionPaintFilter(
static_cast<MatrixConvolutionPaintFilter::TileMode>(tile_mode_int);
filter->reset(new MatrixConvolutionPaintFilter(
kernel_size, kernel.data(), gain, bias, kernel_offset, tile_mode,
convolve_alpha, std::move(input), crop_rect ? &*crop_rect : nullptr));
convolve_alpha, std::move(input), base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadDisplacementMapEffectPaintFilter(
......@@ -883,7 +884,7 @@ void PaintOpReader::ReadDisplacementMapEffectPaintFilter(
channel_y_int);
filter->reset(new DisplacementMapEffectPaintFilter(
channel_x, channel_y, scale, std::move(displacement), std::move(color),
crop_rect ? &*crop_rect : nullptr));
base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadImagePaintFilter(
......@@ -937,7 +938,7 @@ void PaintOpReader::ReadMergePaintFilter(
return;
filter->reset(new MergePaintFilter(inputs.data(),
static_cast<int>(input_count),
crop_rect ? &*crop_rect : nullptr));
base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadMorphologyPaintFilter(
......@@ -961,7 +962,7 @@ void PaintOpReader::ReadMorphologyPaintFilter(
static_cast<MorphologyPaintFilter::MorphType>(morph_type_int);
filter->reset(new MorphologyPaintFilter(morph_type, radius_x, radius_y,
std::move(input),
crop_rect ? &*crop_rect : nullptr));
base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadOffsetPaintFilter(
......@@ -977,7 +978,7 @@ void PaintOpReader::ReadOffsetPaintFilter(
if (!valid_)
return;
filter->reset(new OffsetPaintFilter(dx, dy, std::move(input),
crop_rect ? &*crop_rect : nullptr));
base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadTilePaintFilter(
......@@ -1022,7 +1023,7 @@ void PaintOpReader::ReadTurbulencePaintFilter(
static_cast<TurbulencePaintFilter::TurbulenceType>(turbulence_type_int);
filter->reset(new TurbulencePaintFilter(
turbulence_type, base_frequency_x, base_frequency_y, num_octaves, seed,
&tile_size, crop_rect ? &*crop_rect : nullptr));
&tile_size, base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadPaintFlagsPaintFilter(
......@@ -1034,7 +1035,7 @@ void PaintOpReader::ReadPaintFlagsPaintFilter(
if (!valid_)
return;
filter->reset(
new PaintFlagsPaintFilter(flags, crop_rect ? &*crop_rect : nullptr));
new PaintFlagsPaintFilter(flags, base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadMatrixPaintFilter(
......@@ -1083,7 +1084,7 @@ void PaintOpReader::ReadLightingDistantPaintFilter(
static_cast<PaintFilter::LightingType>(lighting_type_int);
filter->reset(new LightingDistantPaintFilter(
lighting_type, direction, light_color, surface_scale, kconstant,
shininess, std::move(input), crop_rect ? &*crop_rect : nullptr));
shininess, std::move(input), base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadLightingPointPaintFilter(
......@@ -1114,7 +1115,7 @@ void PaintOpReader::ReadLightingPointPaintFilter(
static_cast<PaintFilter::LightingType>(lighting_type_int);
filter->reset(new LightingPointPaintFilter(
lighting_type, location, light_color, surface_scale, kconstant, shininess,
std::move(input), crop_rect ? &*crop_rect : nullptr));
std::move(input), base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::ReadLightingSpotPaintFilter(
......@@ -1153,7 +1154,7 @@ void PaintOpReader::ReadLightingSpotPaintFilter(
filter->reset(new LightingSpotPaintFilter(
lighting_type, location, target, specular_exponent, cutoff_angle,
light_color, surface_scale, kconstant, shininess, std::move(input),
crop_rect ? &*crop_rect : nullptr));
base::OptionalOrNullptr(crop_rect)));
}
void PaintOpReader::Read(sk_sp<PaintRecord>* record) {
......
......@@ -288,7 +288,7 @@ void PaintShader::CreateSkShader(ImageProvider* image_provider) {
points, colors_.data(),
positions_.empty() ? nullptr : positions_.data(),
static_cast<int>(colors_.size()), tx_, flags_,
local_matrix_ ? &*local_matrix_ : nullptr);
base::OptionalOrNullptr(local_matrix_));
break;
}
case Type::kRadialGradient:
......@@ -296,26 +296,26 @@ void PaintShader::CreateSkShader(ImageProvider* image_provider) {
center_, start_radius_, colors_.data(),
positions_.empty() ? nullptr : positions_.data(),
static_cast<int>(colors_.size()), tx_, flags_,
local_matrix_ ? &*local_matrix_ : nullptr);
base::OptionalOrNullptr(local_matrix_));
break;
case Type::kTwoPointConicalGradient:
cached_shader_ = SkGradientShader::MakeTwoPointConical(
start_point_, start_radius_, end_point_, end_radius_, colors_.data(),
positions_.empty() ? nullptr : positions_.data(),
static_cast<int>(colors_.size()), tx_, flags_,
local_matrix_ ? &*local_matrix_ : nullptr);
base::OptionalOrNullptr(local_matrix_));
break;
case Type::kSweepGradient:
cached_shader_ = SkGradientShader::MakeSweep(
center_.x(), center_.y(), colors_.data(),
positions_.empty() ? nullptr : positions_.data(),
static_cast<int>(colors_.size()), tx_, start_degrees_, end_degrees_,
flags_, local_matrix_ ? &*local_matrix_ : nullptr);
flags_, base::OptionalOrNullptr(local_matrix_));
break;
case Type::kImage:
if (image_) {
cached_shader_ = image_.GetSkImage()->makeShader(
tx_, ty_, local_matrix_ ? &*local_matrix_ : nullptr);
tx_, ty_, base::OptionalOrNullptr(local_matrix_));
}
break;
case Type::kPaintRecord: {
......@@ -328,7 +328,7 @@ void PaintShader::CreateSkShader(ImageProvider* image_provider) {
case ScalingBehavior::kRasterAtScale:
cached_shader_ = SkShader::MakePictureShader(
std::move(picture), tx_, ty_,
local_matrix_ ? &*local_matrix_ : nullptr, nullptr);
base::OptionalOrNullptr(local_matrix_), nullptr);
break;
// For fixed scale, we create an image shader with an image backed by
// the picture.
......@@ -338,7 +338,7 @@ void PaintShader::CreateSkShader(ImageProvider* image_provider) {
nullptr, nullptr, SkImage::BitDepth::kU8,
SkColorSpace::MakeSRGB());
cached_shader_ = image->makeShader(
tx_, ty_, local_matrix_ ? &*local_matrix_ : nullptr);
tx_, ty_, base::OptionalOrNullptr(local_matrix_));
break;
}
}
......
......@@ -9,6 +9,7 @@
#include <vector>
#include "base/optional.h"
#include "base/stl_util.h"
#include "cc/paint/paint_export.h"
#include "cc/paint/paint_image.h"
#include "third_party/skia/include/core/SkImage.h"
......@@ -115,7 +116,7 @@ class CC_PAINT_EXPORT PaintShader : public SkRefCnt {
}
const gfx::SizeF* tile_scale() const {
return tile_scale_ ? &*tile_scale_ : nullptr;
return base::OptionalOrNullptr(tile_scale_);
}
const sk_sp<PaintRecord>& paint_record() const { return record_; }
bool GetRasterizationTileRect(const SkMatrix& ctm, SkRect* tile_rect) const;
......
......@@ -100,11 +100,9 @@ void PrePaintTreeWalk::Walk(LocalFrameView& frame_view) {
FrameViewPaintPropertyTreeBuilder::Update(frame_view,
*context().tree_builder_context);
}
paint_invalidator_.InvalidatePaint(frame_view,
context().tree_builder_context
? &*context().tree_builder_context
: nullptr,
context().paint_invalidator_context);
paint_invalidator_.InvalidatePaint(
frame_view, WTF::OptionalOrNullptr(context().tree_builder_context),
context().paint_invalidator_context);
if (LayoutView* view = frame_view.GetLayoutView()) {
#ifndef NDEBUG
......@@ -210,8 +208,7 @@ void PrePaintTreeWalk::WalkInternal(const LayoutObject& object,
}
paint_invalidator_.InvalidatePaint(
object,
context.tree_builder_context ? &*context.tree_builder_context : nullptr,
object, WTF::OptionalOrNullptr(context.tree_builder_context),
context.paint_invalidator_context);
if (context.tree_builder_context) {
......
......@@ -188,7 +188,7 @@ scoped_refptr<cc::PictureLayer> ContentLayerClientImpl::UpdateCcPictureLayer(
paint_chunks, layer_state, layer_bounds.OffsetFromOrigin(),
paint_artifact.GetDisplayItemList(),
cc::DisplayItemList::kTopLevelDisplayItemList,
params ? &*params : nullptr);
WTF::OptionalOrNullptr(params));
if (paint_chunks[0]->size()) {
cc_picture_layer_->SetBackgroundColor(DisplayItemBackgroundColor(
......
......@@ -15,6 +15,7 @@ include_rules = [
"+base/optional.h",
"+base/process/process_metrics.h",
"+base/rand_util.h",
"+base/stl_util.h",
"+base/strings",
"+base/template_util.h",
"+base/threading/thread_checker.h",
......
......@@ -6,6 +6,7 @@
#define Optional_h
#include "base/optional.h"
#include "base/stl_util.h"
namespace WTF {
......@@ -24,6 +25,11 @@ constexpr Optional<typename std::decay<T>::type> make_optional(T&& value) {
return base::make_optional(std::forward<T>(value));
}
template <typename T>
T* OptionalOrNullptr(Optional<T>& optional) {
return base::OptionalOrNullptr(optional);
}
} // namespace WTF
using WTF::Optional;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment