Skip to content

Commit 3a50ba0

Browse files
sherginfacebook-github-bot
authored andcommitted
Fabric: Unifying usage of autos
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
1 parent 3315314 commit 3a50ba0

22 files changed

+64
-66
lines changed

ReactCommon/fabric/attributedstring/AttributedString.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const std::vector<Fragment> &AttributedString::getFragments() const {
4040
}
4141

4242
std::string AttributedString::getString() const {
43-
std::string string;
43+
auto string = std::string {};
4444
for (const auto &fragment : fragments_) {
4545
string += fragment.string;
4646
}
@@ -50,7 +50,7 @@ std::string AttributedString::getString() const {
5050
#pragma mark - DebugStringConvertible
5151

5252
SharedDebugStringConvertibleList AttributedString::getDebugChildren() const {
53-
SharedDebugStringConvertibleList list = {};
53+
auto list = SharedDebugStringConvertibleList {};
5454

5555
for (auto &&fragment : fragments_) {
5656
auto propsList = fragment.textAttributes.DebugStringConvertible::getDebugProps();

ReactCommon/fabric/attributedstring/conversions.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ inline void fromDynamic(const folly::dynamic &value, FontVariant &result) {
8383
}
8484

8585
inline std::string toString(const FontVariant &fontVariant) {
86-
std::string result;
87-
std::string separator = ", ";
86+
auto result = std::string {};
87+
auto separator = std::string {", "};
8888
if ((int)fontVariant & (int)FontVariant::SmallCaps) { result += "small-caps" + separator; }
8989
if ((int)fontVariant & (int)FontVariant::OldstyleNums) { result += "oldstyle-nums" + separator; }
9090
if ((int)fontVariant & (int)FontVariant::LiningNums) { result += "lining-nums" + separator; }

ReactCommon/fabric/components/image/ImageShadowNode.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ ImageSource ImageShadowNode::getImageSource() const {
5959
auto targetImageArea = size.width * size.height * scale * scale;
6060
auto bestFit = kFloatMax;
6161

62-
ImageSource bestSource;
62+
auto bestSource = ImageSource {};
6363

6464
for (const auto &source : sources) {
6565
auto sourceSize = source.size;

ReactCommon/fabric/components/root/RootProps.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace facebook {
1414
namespace react {
1515

1616
static YGStyle yogaStyleFromLayoutConstraints(const LayoutConstraints &layoutConstraints) {
17-
YGStyle yogaStyle;
17+
auto yogaStyle = YGStyle {};
1818
yogaStyle.minDimensions[YGDimensionWidth] =
1919
yogaStyleValueFromFloat(layoutConstraints.minimumSize.width);
2020
yogaStyle.minDimensions[YGDimensionHeight] =

ReactCommon/fabric/components/scrollview/ScrollViewProps.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ ScrollViewProps::ScrollViewProps(const ScrollViewProps &sourceProps, const RawPr
4747
#pragma mark - DebugStringConvertible
4848

4949
SharedDebugStringConvertibleList ScrollViewProps::getDebugProps() const {
50-
ScrollViewProps defaultScrollViewProps;
50+
auto defaultScrollViewProps = ScrollViewProps {};
5151

5252
return
5353
ViewProps::getDebugProps() +

ReactCommon/fabric/components/scrollview/ScrollViewShadowNode.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const char ScrollViewComponentName[] = "ScrollView";
1919
void ScrollViewShadowNode::updateLocalData() {
2020
ensureUnsealed();
2121

22-
Rect contentBoundingRect;
22+
auto contentBoundingRect = Rect {};
2323
for (const auto &childNode : getLayoutableChildNodes()) {
2424
contentBoundingRect.unionInPlace(childNode->getLayoutMetrics().frame);
2525
}

ReactCommon/fabric/components/text/basetext/BaseTextProps.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace facebook {
1616
namespace react {
1717

1818
static TextAttributes convertRawProp(const RawProps &rawProps, const TextAttributes defaultTextAttributes) {
19-
TextAttributes textAttributes;
19+
auto textAttributes = TextAttributes {};
2020

2121
// Color
2222
textAttributes.foregroundColor = convertRawProp(rawProps, "color", defaultTextAttributes.foregroundColor);

ReactCommon/fabric/components/text/basetext/BaseTextShadowNode.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ AttributedString BaseTextShadowNode::getAttributedString(
2020
const TextAttributes &textAttributes,
2121
const SharedShadowNode &parentNode
2222
) const {
23-
AttributedString attributedString;
23+
auto attributedString = AttributedString {};
2424

2525
for (const auto &childNode : parentNode->getChildren()) {
2626
// RawShadowNode
2727
auto rawTextShadowNode = std::dynamic_pointer_cast<const RawTextShadowNode>(childNode);
2828
if (rawTextShadowNode) {
29-
AttributedString::Fragment fragment;
29+
auto fragment = AttributedString::Fragment {};
3030
fragment.string = rawTextShadowNode->getProps()->text;
3131
fragment.textAttributes = textAttributes;
3232
fragment.parentShadowNode = parentNode;
@@ -37,14 +37,14 @@ AttributedString BaseTextShadowNode::getAttributedString(
3737
// TextShadowNode
3838
auto textShadowNode = std::dynamic_pointer_cast<const TextShadowNode>(childNode);
3939
if (textShadowNode) {
40-
TextAttributes localTextAttributes = textAttributes;
40+
auto localTextAttributes = textAttributes;
4141
localTextAttributes.apply(textShadowNode->getProps()->textAttributes);
4242
attributedString.appendAttributedString(textShadowNode->getAttributedString(localTextAttributes, textShadowNode));
4343
continue;
4444
}
4545

4646
// Any other kind of ShadowNode
47-
AttributedString::Fragment fragment;
47+
auto fragment = AttributedString::Fragment {};
4848
fragment.shadowNode = childNode;
4949
fragment.textAttributes = textAttributes;
5050
attributedString.appendFragment(fragment);

ReactCommon/fabric/components/text/paragraph/ParagraphProps.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace facebook {
1515
namespace react {
1616

1717
static ParagraphAttributes convertRawProp(const RawProps &rawProps, const ParagraphAttributes &defaultParagraphAttributes) {
18-
ParagraphAttributes paragraphAttributes;
18+
auto paragraphAttributes = ParagraphAttributes {};
1919

2020
paragraphAttributes.maximumNumberOfLines = convertRawProp(rawProps, "numberOfLines", defaultParagraphAttributes.maximumNumberOfLines);
2121
paragraphAttributes.ellipsizeMode = convertRawProp(rawProps, "ellipsizeMode", defaultParagraphAttributes.ellipsizeMode);

ReactCommon/fabric/components/view/ConcreteViewShadowNode.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class ConcreteViewShadowNode:
116116
#pragma mark - DebugStringConvertible
117117

118118
SharedDebugStringConvertibleList getDebugProps() const override {
119-
SharedDebugStringConvertibleList list = {};
119+
auto list = SharedDebugStringConvertibleList {};
120120

121121
auto basePropsList = ShadowNode::getDebugProps();
122122
std::move(basePropsList.begin(), basePropsList.end(), std::back_inserter(list));

ReactCommon/fabric/components/view/conversions.h

+7-8
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ inline folly::Optional<Float> optionalFloatFromYogaValue(const YGValue &value, f
7272
}
7373

7474
inline LayoutMetrics layoutMetricsFromYogaNode(YGNode &yogaNode) {
75-
LayoutMetrics layoutMetrics;
76-
77-
YGLayout layout = yogaNode.getLayout();
75+
auto layoutMetrics = LayoutMetrics {};
76+
auto layout = yogaNode.getLayout();
7877

7978
layoutMetrics.frame = Rect {
8079
Point {
@@ -227,7 +226,7 @@ inline void fromDynamic(const folly::dynamic &value, YGFloatOptional &result) {
227226

228227
inline void fromDynamic(const folly::dynamic &value, Transform &result) {
229228
assert(value.isArray());
230-
Transform transformMatrix;
229+
auto transformMatrix = Transform {};
231230
for (const auto &tranformConfiguration : value) {
232231
assert(tranformConfiguration.isObject());
233232
auto pair = *tranformConfiguration.items().begin();
@@ -237,7 +236,7 @@ inline void fromDynamic(const folly::dynamic &value, Transform &result) {
237236
if (operation == "matrix") {
238237
assert(parameters.isArray());
239238
assert(parameters.size() == transformMatrix.matrix.size());
240-
int i = 0;
239+
auto i = 0;
241240
for (auto item : parameters) {
242241
transformMatrix.matrix[i++] = (Float)item.asDouble();
243242
}
@@ -407,10 +406,10 @@ inline std::string toString(const std::array<YGValue, YGEdgeCount> &value) {
407406
{"left", "top", "right", "bottom", "start", "end", "horizontal", "vertical", "all"}
408407
};
409408

410-
std::string result;
411-
std::string separator = ", ";
409+
auto result = std::string {};
410+
auto separator = std::string {", "};
412411

413-
for (int i = 0; i < YGEdgeCount; i++) {
412+
for (auto i = 0; i < YGEdgeCount; i++) {
414413
if (value[i].unit == YGUnitUndefined) {
415414
continue;
416415
}

ReactCommon/fabric/components/view/primitives.h

+14-15
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,40 @@ struct Transform {
2424
}};
2525

2626
static Transform Identity() {
27-
Transform transform;
28-
return transform;
27+
return {};
2928
}
3029

3130
static Transform Perspective(const Float &perspective) {
32-
Transform transform;
31+
auto transform = Transform {};
3332
transform.matrix[11] = -1.0 / perspective;
3433
return transform;
3534
}
3635

3736
static Transform Scale(const Float &factorX, const Float &factorY, const Float &factorZ) {
38-
Transform transform;
37+
auto transform = Transform {};
3938
transform.matrix[0] = factorX;
4039
transform.matrix[5] = factorY;
4140
transform.matrix[10] = factorZ;
4241
return transform;
4342
}
4443

4544
static Transform Translate(const Float &x, const Float &y, const Float &z) {
46-
Transform transform;
45+
auto transform = Transform {};
4746
transform.matrix[12] = x;
4847
transform.matrix[13] = y;
4948
transform.matrix[14] = z;
5049
return transform;
5150
}
5251

5352
static Transform Skew(const Float &x, const Float &y) {
54-
Transform transform;
53+
auto transform = Transform {};
5554
transform.matrix[4] = std::tan(x);
5655
transform.matrix[1] = std::tan(y);
5756
return transform;
5857
}
5958

6059
static Transform RotateX(const Float &radians) {
61-
Transform transform;
60+
auto transform = Transform {};
6261
transform.matrix[5] = std::cos(radians);
6362
transform.matrix[6] = std::sin(radians);
6463
transform.matrix[9] = -std::sin(radians);
@@ -67,7 +66,7 @@ struct Transform {
6766
}
6867

6968
static Transform RotateY(const Float &radians) {
70-
Transform transform;
69+
auto transform = Transform {};
7170
transform.matrix[0] = std::cos(radians);
7271
transform.matrix[2] = -std::sin(radians);
7372
transform.matrix[8] = std::sin(radians);
@@ -76,7 +75,7 @@ struct Transform {
7675
}
7776

7877
static Transform RotateZ(const Float &radians) {
79-
Transform transform;
78+
auto transform = Transform {};
8079
transform.matrix[0] = std::cos(radians);
8180
transform.matrix[1] = std::sin(radians);
8281
transform.matrix[4] = -std::sin(radians);
@@ -85,15 +84,15 @@ struct Transform {
8584
}
8685

8786
static Transform Rotate(const Float &x, const Float &y, const Float &z) {
88-
Transform transform;
87+
auto transform = Transform {};
8988
if (x != 0) { transform = transform * Transform::RotateX(x); }
9089
if (y != 0) { transform = transform * Transform::RotateY(y); }
9190
if (z != 0) { transform = transform * Transform::RotateZ(z); }
9291
return transform;
9392
}
9493

9594
bool operator ==(const Transform& rhs) const {
96-
for (int i = 0; i < 16; i++) {
95+
for (auto i = 0; i < 16; i++) {
9796
if (matrix[i] != rhs.matrix[i]) {
9897
return false;
9998
}
@@ -110,15 +109,15 @@ struct Transform {
110109
return rhs;
111110
}
112111

113-
const Transform &lhs = *this;
114-
Transform result;
112+
const auto &lhs = *this;
113+
auto result = Transform {};
115114

116-
Float lhs00 = lhs.matrix[0], lhs01 = lhs.matrix[1], lhs02 = lhs.matrix[2], lhs03 = lhs.matrix[3],
115+
auto lhs00 = lhs.matrix[0], lhs01 = lhs.matrix[1], lhs02 = lhs.matrix[2], lhs03 = lhs.matrix[3],
117116
lhs10 = lhs.matrix[4], lhs11 = lhs.matrix[5], lhs12 = lhs.matrix[6], lhs13 = lhs.matrix[7],
118117
lhs20 = lhs.matrix[8], lhs21 = lhs.matrix[9], lhs22 = lhs.matrix[10], lhs23 = lhs.matrix[11],
119118
lhs30 = lhs.matrix[12], lhs31 = lhs.matrix[13], lhs32 = lhs.matrix[14], lhs33 = lhs.matrix[15];
120119

121-
Float rhs0 = rhs.matrix[0], rhs1 = rhs.matrix[1], rhs2 = rhs.matrix[2], rhs3 = rhs.matrix[3];
120+
auto rhs0 = rhs.matrix[0], rhs1 = rhs.matrix[1], rhs2 = rhs.matrix[2], rhs3 = rhs.matrix[3];
122121
result.matrix[0] = rhs0 * lhs00 + rhs1 * lhs10 + rhs2 * lhs20 + rhs3 * lhs30;
123122
result.matrix[1] = rhs0 * lhs01 + rhs1 * lhs11 + rhs2 * lhs21 + rhs3 * lhs31;
124123
result.matrix[2] = rhs0 * lhs02 + rhs1 * lhs12 + rhs2 * lhs22 + rhs3 * lhs32;

ReactCommon/fabric/components/view/propsConversions.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static inline std::array<YGValue, 2> convertRawProp(
2020
const std::array<YGValue, 2> &sourceValue,
2121
const std::array<YGValue, 2> &defaultValue
2222
) {
23-
std::array<YGValue, 2> dimentions = defaultValue;
23+
auto dimentions = defaultValue;
2424
dimentions[YGDimensionWidth] = convertRawProp(rawProps, widthName, sourceValue[YGDimensionWidth], defaultValue[YGDimensionWidth]);
2525
dimentions[YGDimensionHeight] = convertRawProp(rawProps, heightName, sourceValue[YGDimensionHeight], defaultValue[YGDimensionWidth]);
2626
return dimentions;
@@ -33,7 +33,7 @@ static inline std::array<YGValue, YGEdgeCount> convertRawProp(
3333
const std::array<YGValue, YGEdgeCount> &sourceValue,
3434
const std::array<YGValue, YGEdgeCount> &defaultValue
3535
) {
36-
std::array<YGValue, YGEdgeCount> result = defaultValue;
36+
auto result = defaultValue;
3737
result[YGEdgeLeft] = convertRawProp(rawProps, prefix + "Left" + suffix, sourceValue[YGEdgeLeft], defaultValue[YGEdgeLeft]);
3838
result[YGEdgeTop] = convertRawProp(rawProps, prefix + "Top" + suffix, sourceValue[YGEdgeTop], defaultValue[YGEdgeTop]);
3939
result[YGEdgeRight] = convertRawProp(rawProps, prefix + "Right" + suffix, sourceValue[YGEdgeRight], defaultValue[YGEdgeRight]);
@@ -51,7 +51,7 @@ static inline std::array<YGValue, YGEdgeCount> convertRawProp(
5151
const std::array<YGValue, YGEdgeCount> &sourceValue,
5252
const std::array<YGValue, YGEdgeCount> &defaultValue
5353
) {
54-
std::array<YGValue, YGEdgeCount> result = defaultValue;
54+
auto result = defaultValue;
5555
result[YGEdgeLeft] = convertRawProp(rawProps, "left", sourceValue[YGEdgeLeft], defaultValue[YGEdgeLeft]);
5656
result[YGEdgeTop] = convertRawProp(rawProps, "top", sourceValue[YGEdgeTop], defaultValue[YGEdgeTop]);
5757
result[YGEdgeRight] = convertRawProp(rawProps, "right", sourceValue[YGEdgeRight], defaultValue[YGEdgeRight]);
@@ -62,7 +62,7 @@ static inline std::array<YGValue, YGEdgeCount> convertRawProp(
6262
}
6363

6464
static inline YGStyle convertRawProp(const RawProps &rawProps, const YGStyle &sourceValue) {
65-
YGStyle yogaStyle;
65+
auto yogaStyle = YGStyle {};
6666
yogaStyle.direction = convertRawProp(rawProps, "direction", sourceValue.direction, yogaStyle.direction);
6767
yogaStyle.flexDirection = convertRawProp(rawProps, "flexDirection", sourceValue.flexDirection, yogaStyle.flexDirection);
6868
yogaStyle.justifyContent = convertRawProp(rawProps, "justifyContent", sourceValue.justifyContent, yogaStyle.justifyContent);

ReactCommon/fabric/components/view/yoga/YogaStylableProps.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ YogaStylableProps::YogaStylableProps(const YogaStylableProps &sourceProps, const
2828
#pragma mark - DebugStringConvertible
2929

3030
SharedDebugStringConvertibleList YogaStylableProps::getDebugProps() const {
31-
YGStyle defaultYogaStyle;
31+
auto defaultYogaStyle = YGStyle {};
3232
return {
3333
debugStringConvertibleItem("direction", yogaStyle.direction, defaultYogaStyle.direction),
3434
debugStringConvertibleItem("flexDirection", yogaStyle.flexDirection, defaultYogaStyle.flexDirection),

ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ void LayoutableShadowNode::layout(LayoutContext layoutContext) {
7878
child->ensureUnsealed();
7979
child->setHasNewLayout(false);
8080

81-
const LayoutMetrics childLayoutMetrics = child->getLayoutMetrics();
81+
const auto childLayoutMetrics = child->getLayoutMetrics();
8282
if (childLayoutMetrics.displayType == DisplayType::None) {
8383
continue;
8484
}
8585

86-
LayoutContext childLayoutContext = LayoutContext(layoutContext);
86+
auto childLayoutContext = LayoutContext(layoutContext);
8787
childLayoutContext.absolutePosition += childLayoutMetrics.frame.origin;
8888

8989
child->layout(layoutContext);
@@ -95,7 +95,7 @@ void LayoutableShadowNode::layoutChildren(LayoutContext layoutContext) {
9595
}
9696

9797
SharedDebugStringConvertibleList LayoutableShadowNode::getDebugProps() const {
98-
SharedDebugStringConvertibleList list = {};
98+
auto list = SharedDebugStringConvertibleList {};
9999

100100
if (getHasNewLayout()) {
101101
list.push_back(std::make_shared<DebugStringConvertibleItem>("hasNewLayout"));
@@ -105,8 +105,8 @@ SharedDebugStringConvertibleList LayoutableShadowNode::getDebugProps() const {
105105
list.push_back(std::make_shared<DebugStringConvertibleItem>("dirty"));
106106
}
107107

108-
LayoutMetrics layoutMetrics = getLayoutMetrics();
109-
LayoutMetrics defaultLayoutMetrics = LayoutMetrics();
108+
auto layoutMetrics = getLayoutMetrics();
109+
auto defaultLayoutMetrics = LayoutMetrics();
110110

111111
list.push_back(std::make_shared<DebugStringConvertibleItem>("frame", toString(layoutMetrics.frame)));
112112

ReactCommon/fabric/core/shadownode/ShadowNode.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ std::string ShadowNode::getDebugValue() const {
154154
}
155155

156156
SharedDebugStringConvertibleList ShadowNode::getDebugChildren() const {
157-
SharedDebugStringConvertibleList debugChildren = {};
157+
auto debugChildren = SharedDebugStringConvertibleList {};
158158

159159
for (auto child : *children_) {
160160
auto debugChild = std::dynamic_pointer_cast<const DebugStringConvertible>(child);

ReactCommon/fabric/debug/DebugStringConvertible.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ std::string DebugStringConvertible::getDebugPropsDescription(DebugStringConverti
5656
}
5757

5858
std::string DebugStringConvertible::getDebugDescription(DebugStringConvertibleOptions options, int depth) const {
59-
std::string nameString = getDebugName();
60-
std::string valueString = getDebugValue();
61-
std::string childrenString = getDebugChildrenDescription(options, depth);
62-
std::string propsString = getDebugPropsDescription(options, depth);
59+
auto nameString = getDebugName();
60+
auto valueString = getDebugValue();
61+
auto childrenString = getDebugChildrenDescription(options, depth);
62+
auto propsString = getDebugPropsDescription(options, depth);
6363

64-
std::string leading = options.format ? std::string(depth * 2, ' ') : "";
65-
std::string trailing = options.format ? "\n" : "";
64+
auto leading = options.format ? std::string(depth * 2, ' ') : std::string {""};
65+
auto trailing = options.format ? std::string {"\n"} : std::string {""};
6666

6767
return leading + "<" + nameString +
6868
(valueString.empty() ? "" : "=" + valueString) +

ReactCommon/fabric/debug/debugStringConvertibleUtils.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline SharedDebugStringConvertible debugStringConvertibleItem(std::string name,
4545
}
4646

4747
inline SharedDebugStringConvertibleList operator+(const SharedDebugStringConvertibleList &lhs, const SharedDebugStringConvertibleList &rhs) {
48-
SharedDebugStringConvertibleList result = {};
48+
auto result = SharedDebugStringConvertibleList {};
4949
std::move(lhs.begin(), lhs.end(), std::back_inserter(result));
5050
std::move(rhs.begin(), rhs.end(), std::back_inserter(result));
5151
return result;

0 commit comments

Comments
 (0)