Skip to content

Commit

Permalink
Fabric: Unifying usage of autos
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shergin authored and facebook-github-bot committed Sep 10, 2018
1 parent 3315314 commit 3a50ba0
Show file tree
Hide file tree
Showing 22 changed files with 64 additions and 66 deletions.
4 changes: 2 additions & 2 deletions ReactCommon/fabric/attributedstring/AttributedString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions ReactCommon/fabric/attributedstring/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/fabric/components/image/ImageShadowNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/fabric/components/root/RootProps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class ConcreteViewShadowNode:
#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));
Expand Down
15 changes: 7 additions & 8 deletions ReactCommon/fabric/components/view/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
}
Expand Down
29 changes: 14 additions & 15 deletions ReactCommon/fabric/components/view/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,40 @@ 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;
return 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;
return 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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -85,15 +84,15 @@ 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); }
return 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;
}
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions ReactCommon/fabric/components/view/propsConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
10 changes: 5 additions & 5 deletions ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"));
Expand All @@ -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)));

Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/fabric/core/shadownode/ShadowNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions ReactCommon/fabric/debug/DebugStringConvertible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) +
Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/fabric/debug/debugStringConvertibleUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 3a50ba0

Please sign in to comment.