Fabric: Unifying usage of `auto`s

Summary:
I was watching a classic magnificent talk about modern C++ by Herb Sutter and I was totally sold on double down on using `auto` in our codebase. Surprisingly, 95% of the code base already follows Herb's guidence; I just changed the last 5% to make it consistent.
All those changes must work *exactly* like it was before.
The talk: https://youtu.be/xnqTKD8uD64?t=28m25s

Reviewed By: mdvacca

Differential Revision: D9753301

fbshipit-source-id: 9629aa485a5d6e51806cc96306c297284d4f90b8
This commit is contained in:
Valentin Shergin 2018-09-10 16:33:48 -07:00 коммит произвёл Facebook Github Bot
Родитель 37d19aaae3
Коммит 9570d7d490
22 изменённых файлов: 64 добавлений и 66 удалений

Просмотреть файл

@ -40,7 +40,7 @@ const std::vector<Fragment> &AttributedString::getFragments() const {
}
std::string AttributedString::getString() const {
std::string string;
auto string = std::string {};
for (const auto &fragment : fragments_) {
string += fragment.string;
}
@ -50,7 +50,7 @@ std::string AttributedString::getString() const {
#pragma mark - DebugStringConvertible
SharedDebugStringConvertibleList AttributedString::getDebugChildren() const {
SharedDebugStringConvertibleList list = {};
auto list = SharedDebugStringConvertibleList {};
for (auto &&fragment : fragments_) {
auto propsList = fragment.textAttributes.DebugStringConvertible::getDebugProps();

Просмотреть файл

@ -83,8 +83,8 @@ inline void fromDynamic(const folly::dynamic &value, FontVariant &result) {
}
inline std::string toString(const FontVariant &fontVariant) {
std::string result;
std::string separator = ", ";
auto result = std::string {};
auto separator = std::string {", "};
if ((int)fontVariant & (int)FontVariant::SmallCaps) { result += "small-caps" + separator; }
if ((int)fontVariant & (int)FontVariant::OldstyleNums) { result += "oldstyle-nums" + separator; }
if ((int)fontVariant & (int)FontVariant::LiningNums) { result += "lining-nums" + separator; }

Просмотреть файл

@ -59,7 +59,7 @@ ImageSource ImageShadowNode::getImageSource() const {
auto targetImageArea = size.width * size.height * scale * scale;
auto bestFit = kFloatMax;
ImageSource bestSource;
auto bestSource = ImageSource {};
for (const auto &source : sources) {
auto sourceSize = source.size;

Просмотреть файл

@ -14,7 +14,7 @@ namespace facebook {
namespace react {
static YGStyle yogaStyleFromLayoutConstraints(const LayoutConstraints &layoutConstraints) {
YGStyle yogaStyle;
auto yogaStyle = YGStyle {};
yogaStyle.minDimensions[YGDimensionWidth] =
yogaStyleValueFromFloat(layoutConstraints.minimumSize.width);
yogaStyle.minDimensions[YGDimensionHeight] =

Просмотреть файл

@ -47,7 +47,7 @@ ScrollViewProps::ScrollViewProps(const ScrollViewProps &sourceProps, const RawPr
#pragma mark - DebugStringConvertible
SharedDebugStringConvertibleList ScrollViewProps::getDebugProps() const {
ScrollViewProps defaultScrollViewProps;
auto defaultScrollViewProps = ScrollViewProps {};
return
ViewProps::getDebugProps() +

Просмотреть файл

@ -19,7 +19,7 @@ const char ScrollViewComponentName[] = "ScrollView";
void ScrollViewShadowNode::updateLocalData() {
ensureUnsealed();
Rect contentBoundingRect;
auto contentBoundingRect = Rect {};
for (const auto &childNode : getLayoutableChildNodes()) {
contentBoundingRect.unionInPlace(childNode->getLayoutMetrics().frame);
}

Просмотреть файл

@ -16,7 +16,7 @@ namespace facebook {
namespace react {
static TextAttributes convertRawProp(const RawProps &rawProps, const TextAttributes defaultTextAttributes) {
TextAttributes textAttributes;
auto textAttributes = TextAttributes {};
// Color
textAttributes.foregroundColor = convertRawProp(rawProps, "color", defaultTextAttributes.foregroundColor);

Просмотреть файл

@ -20,13 +20,13 @@ AttributedString BaseTextShadowNode::getAttributedString(
const TextAttributes &textAttributes,
const SharedShadowNode &parentNode
) const {
AttributedString attributedString;
auto attributedString = AttributedString {};
for (const auto &childNode : parentNode->getChildren()) {
// RawShadowNode
auto rawTextShadowNode = std::dynamic_pointer_cast<const RawTextShadowNode>(childNode);
if (rawTextShadowNode) {
AttributedString::Fragment fragment;
auto fragment = AttributedString::Fragment {};
fragment.string = rawTextShadowNode->getProps()->text;
fragment.textAttributes = textAttributes;
fragment.parentShadowNode = parentNode;
@ -37,14 +37,14 @@ AttributedString BaseTextShadowNode::getAttributedString(
// TextShadowNode
auto textShadowNode = std::dynamic_pointer_cast<const TextShadowNode>(childNode);
if (textShadowNode) {
TextAttributes localTextAttributes = textAttributes;
auto localTextAttributes = textAttributes;
localTextAttributes.apply(textShadowNode->getProps()->textAttributes);
attributedString.appendAttributedString(textShadowNode->getAttributedString(localTextAttributes, textShadowNode));
continue;
}
// Any other kind of ShadowNode
AttributedString::Fragment fragment;
auto fragment = AttributedString::Fragment {};
fragment.shadowNode = childNode;
fragment.textAttributes = textAttributes;
attributedString.appendFragment(fragment);

Просмотреть файл

@ -15,7 +15,7 @@ namespace facebook {
namespace react {
static ParagraphAttributes convertRawProp(const RawProps &rawProps, const ParagraphAttributes &defaultParagraphAttributes) {
ParagraphAttributes paragraphAttributes;
auto paragraphAttributes = ParagraphAttributes {};
paragraphAttributes.maximumNumberOfLines = convertRawProp(rawProps, "numberOfLines", defaultParagraphAttributes.maximumNumberOfLines);
paragraphAttributes.ellipsizeMode = convertRawProp(rawProps, "ellipsizeMode", defaultParagraphAttributes.ellipsizeMode);

Просмотреть файл

@ -116,7 +116,7 @@ public:
#pragma mark - DebugStringConvertible
SharedDebugStringConvertibleList getDebugProps() const override {
SharedDebugStringConvertibleList list = {};
auto list = SharedDebugStringConvertibleList {};
auto basePropsList = ShadowNode::getDebugProps();
std::move(basePropsList.begin(), basePropsList.end(), std::back_inserter(list));

Просмотреть файл

@ -72,9 +72,8 @@ inline folly::Optional<Float> optionalFloatFromYogaValue(const YGValue &value, f
}
inline LayoutMetrics layoutMetricsFromYogaNode(YGNode &yogaNode) {
LayoutMetrics layoutMetrics;
YGLayout layout = yogaNode.getLayout();
auto layoutMetrics = LayoutMetrics {};
auto layout = yogaNode.getLayout();
layoutMetrics.frame = Rect {
Point {
@ -227,7 +226,7 @@ inline void fromDynamic(const folly::dynamic &value, YGFloatOptional &result) {
inline void fromDynamic(const folly::dynamic &value, Transform &result) {
assert(value.isArray());
Transform transformMatrix;
auto transformMatrix = Transform {};
for (const auto &tranformConfiguration : value) {
assert(tranformConfiguration.isObject());
auto pair = *tranformConfiguration.items().begin();
@ -237,7 +236,7 @@ inline void fromDynamic(const folly::dynamic &value, Transform &result) {
if (operation == "matrix") {
assert(parameters.isArray());
assert(parameters.size() == transformMatrix.matrix.size());
int i = 0;
auto i = 0;
for (auto item : parameters) {
transformMatrix.matrix[i++] = (Float)item.asDouble();
}
@ -407,10 +406,10 @@ inline std::string toString(const std::array<YGValue, YGEdgeCount> &value) {
{"left", "top", "right", "bottom", "start", "end", "horizontal", "vertical", "all"}
};
std::string result;
std::string separator = ", ";
auto result = std::string {};
auto separator = std::string {", "};
for (int i = 0; i < YGEdgeCount; i++) {
for (auto i = 0; i < YGEdgeCount; i++) {
if (value[i].unit == YGUnitUndefined) {
continue;
}

Просмотреть файл

@ -24,18 +24,17 @@ struct Transform {
}};
static Transform Identity() {
Transform transform;
return transform;
return {};
}
static Transform Perspective(const Float &perspective) {
Transform transform;
auto transform = Transform {};
transform.matrix[11] = -1.0 / perspective;
return transform;
}
static Transform Scale(const Float &factorX, const Float &factorY, const Float &factorZ) {
Transform transform;
auto transform = Transform {};
transform.matrix[0] = factorX;
transform.matrix[5] = factorY;
transform.matrix[10] = factorZ;
@ -43,7 +42,7 @@ struct Transform {
}
static Transform Translate(const Float &x, const Float &y, const Float &z) {
Transform transform;
auto transform = Transform {};
transform.matrix[12] = x;
transform.matrix[13] = y;
transform.matrix[14] = z;
@ -51,14 +50,14 @@ struct Transform {
}
static Transform Skew(const Float &x, const Float &y) {
Transform transform;
auto transform = Transform {};
transform.matrix[4] = std::tan(x);
transform.matrix[1] = std::tan(y);
return transform;
}
static Transform RotateX(const Float &radians) {
Transform transform;
auto transform = Transform {};
transform.matrix[5] = std::cos(radians);
transform.matrix[6] = std::sin(radians);
transform.matrix[9] = -std::sin(radians);
@ -67,7 +66,7 @@ struct Transform {
}
static Transform RotateY(const Float &radians) {
Transform transform;
auto transform = Transform {};
transform.matrix[0] = std::cos(radians);
transform.matrix[2] = -std::sin(radians);
transform.matrix[8] = std::sin(radians);
@ -76,7 +75,7 @@ struct Transform {
}
static Transform RotateZ(const Float &radians) {
Transform transform;
auto transform = Transform {};
transform.matrix[0] = std::cos(radians);
transform.matrix[1] = std::sin(radians);
transform.matrix[4] = -std::sin(radians);
@ -85,7 +84,7 @@ struct Transform {
}
static Transform Rotate(const Float &x, const Float &y, const Float &z) {
Transform transform;
auto transform = Transform {};
if (x != 0) { transform = transform * Transform::RotateX(x); }
if (y != 0) { transform = transform * Transform::RotateY(y); }
if (z != 0) { transform = transform * Transform::RotateZ(z); }
@ -93,7 +92,7 @@ struct Transform {
}
bool operator ==(const Transform& rhs) const {
for (int i = 0; i < 16; i++) {
for (auto i = 0; i < 16; i++) {
if (matrix[i] != rhs.matrix[i]) {
return false;
}
@ -110,15 +109,15 @@ struct Transform {
return rhs;
}
const Transform &lhs = *this;
Transform result;
const auto &lhs = *this;
auto result = Transform {};
Float lhs00 = lhs.matrix[0], lhs01 = lhs.matrix[1], lhs02 = lhs.matrix[2], lhs03 = lhs.matrix[3],
auto lhs00 = lhs.matrix[0], lhs01 = lhs.matrix[1], lhs02 = lhs.matrix[2], lhs03 = lhs.matrix[3],
lhs10 = lhs.matrix[4], lhs11 = lhs.matrix[5], lhs12 = lhs.matrix[6], lhs13 = lhs.matrix[7],
lhs20 = lhs.matrix[8], lhs21 = lhs.matrix[9], lhs22 = lhs.matrix[10], lhs23 = lhs.matrix[11],
lhs30 = lhs.matrix[12], lhs31 = lhs.matrix[13], lhs32 = lhs.matrix[14], lhs33 = lhs.matrix[15];
Float rhs0 = rhs.matrix[0], rhs1 = rhs.matrix[1], rhs2 = rhs.matrix[2], rhs3 = rhs.matrix[3];
auto rhs0 = rhs.matrix[0], rhs1 = rhs.matrix[1], rhs2 = rhs.matrix[2], rhs3 = rhs.matrix[3];
result.matrix[0] = rhs0 * lhs00 + rhs1 * lhs10 + rhs2 * lhs20 + rhs3 * lhs30;
result.matrix[1] = rhs0 * lhs01 + rhs1 * lhs11 + rhs2 * lhs21 + rhs3 * lhs31;
result.matrix[2] = rhs0 * lhs02 + rhs1 * lhs12 + rhs2 * lhs22 + rhs3 * lhs32;

Просмотреть файл

@ -20,7 +20,7 @@ static inline std::array<YGValue, 2> convertRawProp(
const std::array<YGValue, 2> &sourceValue,
const std::array<YGValue, 2> &defaultValue
) {
std::array<YGValue, 2> dimentions = defaultValue;
auto dimentions = defaultValue;
dimentions[YGDimensionWidth] = convertRawProp(rawProps, widthName, sourceValue[YGDimensionWidth], defaultValue[YGDimensionWidth]);
dimentions[YGDimensionHeight] = convertRawProp(rawProps, heightName, sourceValue[YGDimensionHeight], defaultValue[YGDimensionWidth]);
return dimentions;
@ -33,7 +33,7 @@ static inline std::array<YGValue, YGEdgeCount> convertRawProp(
const std::array<YGValue, YGEdgeCount> &sourceValue,
const std::array<YGValue, YGEdgeCount> &defaultValue
) {
std::array<YGValue, YGEdgeCount> result = defaultValue;
auto result = defaultValue;
result[YGEdgeLeft] = convertRawProp(rawProps, prefix + "Left" + suffix, sourceValue[YGEdgeLeft], defaultValue[YGEdgeLeft]);
result[YGEdgeTop] = convertRawProp(rawProps, prefix + "Top" + suffix, sourceValue[YGEdgeTop], defaultValue[YGEdgeTop]);
result[YGEdgeRight] = convertRawProp(rawProps, prefix + "Right" + suffix, sourceValue[YGEdgeRight], defaultValue[YGEdgeRight]);
@ -51,7 +51,7 @@ static inline std::array<YGValue, YGEdgeCount> convertRawProp(
const std::array<YGValue, YGEdgeCount> &sourceValue,
const std::array<YGValue, YGEdgeCount> &defaultValue
) {
std::array<YGValue, YGEdgeCount> result = defaultValue;
auto result = defaultValue;
result[YGEdgeLeft] = convertRawProp(rawProps, "left", sourceValue[YGEdgeLeft], defaultValue[YGEdgeLeft]);
result[YGEdgeTop] = convertRawProp(rawProps, "top", sourceValue[YGEdgeTop], defaultValue[YGEdgeTop]);
result[YGEdgeRight] = convertRawProp(rawProps, "right", sourceValue[YGEdgeRight], defaultValue[YGEdgeRight]);
@ -62,7 +62,7 @@ static inline std::array<YGValue, YGEdgeCount> convertRawProp(
}
static inline YGStyle convertRawProp(const RawProps &rawProps, const YGStyle &sourceValue) {
YGStyle yogaStyle;
auto yogaStyle = YGStyle {};
yogaStyle.direction = convertRawProp(rawProps, "direction", sourceValue.direction, yogaStyle.direction);
yogaStyle.flexDirection = convertRawProp(rawProps, "flexDirection", sourceValue.flexDirection, yogaStyle.flexDirection);
yogaStyle.justifyContent = convertRawProp(rawProps, "justifyContent", sourceValue.justifyContent, yogaStyle.justifyContent);

Просмотреть файл

@ -28,7 +28,7 @@ YogaStylableProps::YogaStylableProps(const YogaStylableProps &sourceProps, const
#pragma mark - DebugStringConvertible
SharedDebugStringConvertibleList YogaStylableProps::getDebugProps() const {
YGStyle defaultYogaStyle;
auto defaultYogaStyle = YGStyle {};
return {
debugStringConvertibleItem("direction", yogaStyle.direction, defaultYogaStyle.direction),
debugStringConvertibleItem("flexDirection", yogaStyle.flexDirection, defaultYogaStyle.flexDirection),

Просмотреть файл

@ -78,12 +78,12 @@ void LayoutableShadowNode::layout(LayoutContext layoutContext) {
child->ensureUnsealed();
child->setHasNewLayout(false);
const LayoutMetrics childLayoutMetrics = child->getLayoutMetrics();
const auto childLayoutMetrics = child->getLayoutMetrics();
if (childLayoutMetrics.displayType == DisplayType::None) {
continue;
}
LayoutContext childLayoutContext = LayoutContext(layoutContext);
auto childLayoutContext = LayoutContext(layoutContext);
childLayoutContext.absolutePosition += childLayoutMetrics.frame.origin;
child->layout(layoutContext);
@ -95,7 +95,7 @@ void LayoutableShadowNode::layoutChildren(LayoutContext layoutContext) {
}
SharedDebugStringConvertibleList LayoutableShadowNode::getDebugProps() const {
SharedDebugStringConvertibleList list = {};
auto list = SharedDebugStringConvertibleList {};
if (getHasNewLayout()) {
list.push_back(std::make_shared<DebugStringConvertibleItem>("hasNewLayout"));
@ -105,8 +105,8 @@ SharedDebugStringConvertibleList LayoutableShadowNode::getDebugProps() const {
list.push_back(std::make_shared<DebugStringConvertibleItem>("dirty"));
}
LayoutMetrics layoutMetrics = getLayoutMetrics();
LayoutMetrics defaultLayoutMetrics = LayoutMetrics();
auto layoutMetrics = getLayoutMetrics();
auto defaultLayoutMetrics = LayoutMetrics();
list.push_back(std::make_shared<DebugStringConvertibleItem>("frame", toString(layoutMetrics.frame)));

Просмотреть файл

@ -154,7 +154,7 @@ std::string ShadowNode::getDebugValue() const {
}
SharedDebugStringConvertibleList ShadowNode::getDebugChildren() const {
SharedDebugStringConvertibleList debugChildren = {};
auto debugChildren = SharedDebugStringConvertibleList {};
for (auto child : *children_) {
auto debugChild = std::dynamic_pointer_cast<const DebugStringConvertible>(child);

Просмотреть файл

@ -56,13 +56,13 @@ std::string DebugStringConvertible::getDebugPropsDescription(DebugStringConverti
}
std::string DebugStringConvertible::getDebugDescription(DebugStringConvertibleOptions options, int depth) const {
std::string nameString = getDebugName();
std::string valueString = getDebugValue();
std::string childrenString = getDebugChildrenDescription(options, depth);
std::string propsString = getDebugPropsDescription(options, depth);
auto nameString = getDebugName();
auto valueString = getDebugValue();
auto childrenString = getDebugChildrenDescription(options, depth);
auto propsString = getDebugPropsDescription(options, depth);
std::string leading = options.format ? std::string(depth * 2, ' ') : "";
std::string trailing = options.format ? "\n" : "";
auto leading = options.format ? std::string(depth * 2, ' ') : std::string {""};
auto trailing = options.format ? std::string {"\n"} : std::string {""};
return leading + "<" + nameString +
(valueString.empty() ? "" : "=" + valueString) +

Просмотреть файл

@ -45,7 +45,7 @@ inline SharedDebugStringConvertible debugStringConvertibleItem(std::string name,
}
inline SharedDebugStringConvertibleList operator+(const SharedDebugStringConvertibleList &lhs, const SharedDebugStringConvertibleList &rhs) {
SharedDebugStringConvertibleList result = {};
auto result = SharedDebugStringConvertibleList {};
std::move(lhs.begin(), lhs.end(), std::back_inserter(result));
std::move(rhs.begin(), rhs.end(), std::back_inserter(result));
return result;

Просмотреть файл

@ -20,7 +20,7 @@ namespace react {
* (e.g. "layout" becames "topLayout").
*/
static std::string normalizeEventType(const std::string &type) {
std::string prefixedType = type;
auto prefixedType = type;
prefixedType[0] = toupper(prefixedType[0]);
prefixedType.insert(0, "top");
return prefixedType;
@ -51,7 +51,7 @@ void EventEmitter::dispatchEvent(
folly::dynamic extendedPayload = folly::dynamic::object("target", tag_);
extendedPayload.merge_patch(payload);
std::weak_ptr<const EventEmitter> weakEventEmitter = shared_from_this();
auto weakEventEmitter = std::weak_ptr<const EventEmitter> {shared_from_this()};
eventDispatcher->dispatchEvent(
RawEvent(

Просмотреть файл

@ -100,10 +100,10 @@ struct Rect {
Float getMinY() const { return size.height >= 0 ? origin.y : origin.y + size.height; }
void unionInPlace(const Rect &rect) {
Float x1 = std::min(getMinX(), rect.getMinX());
Float y1 = std::min(getMinY(), rect.getMinY());
Float x2 = std::max(getMaxX(), rect.getMaxX());
Float y2 = std::max(getMaxY(), rect.getMaxY());
auto x1 = std::min(getMinX(), rect.getMinX());
auto y1 = std::min(getMinY(), rect.getMinY());
auto x2 = std::max(getMaxX(), rect.getMaxX());
auto y2 = std::max(getMaxY(), rect.getMaxY());
origin = {x1, y1};
size = {x2 - x1, y2 - y1};
}

Просмотреть файл

@ -24,7 +24,7 @@ inline void fromDynamic(const folly::dynamic &value, SharedColor &result) {
if (value.isNumber()) {
auto argb = value.asInt();
float ratio = 256;
auto ratio = 256.f;
alpha = ((argb >> 24) & 0xFF) / ratio;
red = ((argb >> 16) & 0xFF) / ratio;
green = ((argb >> 8) & 0xFF) / ratio;
@ -45,7 +45,7 @@ inline void fromDynamic(const folly::dynamic &value, SharedColor &result) {
inline std::string toString(const SharedColor &value) {
ColorComponents components = colorComponentsFromColor(value);
const float ratio = 256;
auto ratio = 256.f;
return "rgba(" +
folly::to<std::string>(round(components.red * ratio)) + ", " +
folly::to<std::string>(round(components.green * ratio)) + ", " +

Просмотреть файл

@ -18,7 +18,7 @@ SharedColor colorFromComponents(ColorComponents components) {
components.alpha
};
CGColorRef color = CGColorCreate(
auto color = CGColorCreate(
CGColorSpaceCreateDeviceRGB(),
componentsArray
);
@ -32,7 +32,7 @@ ColorComponents colorComponentsFromColor(SharedColor color) {
return ColorComponents {0, 0, 0, 0};
}
int numberOfComponents = CGColorGetNumberOfComponents(color.get());
auto numberOfComponents = CGColorGetNumberOfComponents(color.get());
assert(numberOfComponents == 4);
const CGFloat *components = CGColorGetComponents(color.get());
return ColorComponents {