YGNode.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the LICENSE
  5. * file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <stdio.h>
  9. #include "YGConfig.h"
  10. #include "YGLayout.h"
  11. #include "YGStyle.h"
  12. #include "Yoga-internal.h"
  13. struct YGNode {
  14. using MeasureWithContextFn =
  15. YGSize (*)(YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*);
  16. using BaselineWithContextFn = float (*)(YGNode*, float, float, void*);
  17. using PrintWithContextFn = void (*)(YGNode*, void*);
  18. private:
  19. void* context_ = nullptr;
  20. bool hasNewLayout_ : 1;
  21. bool isReferenceBaseline_ : 1;
  22. bool isDirty_ : 1;
  23. YGNodeType nodeType_ : 1;
  24. bool measureUsesContext_ : 1;
  25. bool baselineUsesContext_ : 1;
  26. bool printUsesContext_ : 1;
  27. union {
  28. YGMeasureFunc noContext;
  29. MeasureWithContextFn withContext;
  30. } measure_ = {nullptr};
  31. union {
  32. YGBaselineFunc noContext;
  33. BaselineWithContextFn withContext;
  34. } baseline_ = {nullptr};
  35. union {
  36. YGPrintFunc noContext;
  37. PrintWithContextFn withContext;
  38. } print_ = {nullptr};
  39. YGDirtiedFunc dirtied_ = nullptr;
  40. YGStyle style_ = {};
  41. YGLayout layout_ = {};
  42. uint32_t lineIndex_ = 0;
  43. YGNodeRef owner_ = nullptr;
  44. YGVector children_ = {};
  45. YGConfigRef config_ = nullptr;
  46. std::array<YGValue, 2> resolvedDimensions_ = {
  47. {YGValueUndefined, YGValueUndefined}};
  48. YGFloatOptional relativePosition(
  49. const YGFlexDirection axis,
  50. const float axisSize) const;
  51. void setMeasureFunc(decltype(measure_));
  52. void setBaselineFunc(decltype(baseline_));
  53. // DANGER DANGER DANGER!
  54. // If the the node assigned to has children, we'd either have to deallocate
  55. // them (potentially incorrect) or ignore them (danger of leaks). Only ever
  56. // use this after checking that there are no children.
  57. // DO NOT CHANGE THE VISIBILITY OF THIS METHOD!
  58. YGNode& operator=(YGNode&&) = default;
  59. public:
  60. YGNode()
  61. : hasNewLayout_{true},
  62. isReferenceBaseline_{false},
  63. isDirty_{false},
  64. nodeType_{YGNodeTypeDefault},
  65. measureUsesContext_{false},
  66. baselineUsesContext_{false},
  67. printUsesContext_{false} {}
  68. ~YGNode() = default; // cleanup of owner/children relationships in YGNodeFree
  69. explicit YGNode(const YGConfigRef newConfig) : config_(newConfig){};
  70. YGNode(YGNode&&);
  71. // Does not expose true value semantics, as children are not cloned eagerly.
  72. // Should we remove this?
  73. YGNode(const YGNode& node) = default;
  74. // assignment means potential leaks of existing children, or alternatively
  75. // freeing unowned memory, double free, or freeing stack memory.
  76. YGNode& operator=(const YGNode&) = delete;
  77. // Getters
  78. void* getContext() const {
  79. return context_;
  80. }
  81. void print(void*);
  82. bool getHasNewLayout() const {
  83. return hasNewLayout_;
  84. }
  85. YGNodeType getNodeType() const {
  86. return nodeType_;
  87. }
  88. bool hasMeasureFunc() const noexcept {
  89. return measure_.noContext != nullptr;
  90. }
  91. YGSize measure(float, YGMeasureMode, float, YGMeasureMode, void*);
  92. bool hasBaselineFunc() const noexcept {
  93. return baseline_.noContext != nullptr;
  94. }
  95. float baseline(float width, float height, void* layoutContext);
  96. YGDirtiedFunc getDirtied() const {
  97. return dirtied_;
  98. }
  99. // For Performance reasons passing as reference.
  100. YGStyle& getStyle() {
  101. return style_;
  102. }
  103. const YGStyle& getStyle() const {
  104. return style_;
  105. }
  106. // For Performance reasons passing as reference.
  107. YGLayout& getLayout() {
  108. return layout_;
  109. }
  110. const YGLayout& getLayout() const {
  111. return layout_;
  112. }
  113. uint32_t getLineIndex() const {
  114. return lineIndex_;
  115. }
  116. bool isReferenceBaseline() {
  117. return isReferenceBaseline_;
  118. }
  119. // returns the YGNodeRef that owns this YGNode. An owner is used to identify
  120. // the YogaTree that a YGNode belongs to. This method will return the parent
  121. // of the YGNode when a YGNode only belongs to one YogaTree or nullptr when
  122. // the YGNode is shared between two or more YogaTrees.
  123. YGNodeRef getOwner() const {
  124. return owner_;
  125. }
  126. // Deprecated, use getOwner() instead.
  127. YGNodeRef getParent() const {
  128. return getOwner();
  129. }
  130. const YGVector& getChildren() const {
  131. return children_;
  132. }
  133. // Applies a callback to all children, after cloning them if they are not
  134. // owned.
  135. template <typename T>
  136. void iterChildrenAfterCloningIfNeeded(T callback, void* cloneContext) {
  137. int i = 0;
  138. for (YGNodeRef& child : children_) {
  139. if (child->getOwner() != this) {
  140. child = config_->cloneNode(child, this, i, cloneContext);
  141. child->setOwner(this);
  142. }
  143. i += 1;
  144. callback(child, cloneContext);
  145. }
  146. }
  147. YGNodeRef getChild(uint32_t index) const {
  148. return children_.at(index);
  149. }
  150. YGConfigRef getConfig() const {
  151. return config_;
  152. }
  153. bool isDirty() const {
  154. return isDirty_;
  155. }
  156. std::array<YGValue, 2> getResolvedDimensions() const {
  157. return resolvedDimensions_;
  158. }
  159. YGValue getResolvedDimension(int index) const {
  160. return resolvedDimensions_[index];
  161. }
  162. // Methods related to positions, margin, padding and border
  163. YGFloatOptional getLeadingPosition(
  164. const YGFlexDirection axis,
  165. const float axisSize) const;
  166. bool isLeadingPositionDefined(const YGFlexDirection axis) const;
  167. bool isTrailingPosDefined(const YGFlexDirection axis) const;
  168. YGFloatOptional getTrailingPosition(
  169. const YGFlexDirection axis,
  170. const float axisSize) const;
  171. YGFloatOptional getLeadingMargin(
  172. const YGFlexDirection axis,
  173. const float widthSize) const;
  174. YGFloatOptional getTrailingMargin(
  175. const YGFlexDirection axis,
  176. const float widthSize) const;
  177. float getLeadingBorder(const YGFlexDirection flexDirection) const;
  178. float getTrailingBorder(const YGFlexDirection flexDirection) const;
  179. YGFloatOptional getLeadingPadding(
  180. const YGFlexDirection axis,
  181. const float widthSize) const;
  182. YGFloatOptional getTrailingPadding(
  183. const YGFlexDirection axis,
  184. const float widthSize) const;
  185. YGFloatOptional getLeadingPaddingAndBorder(
  186. const YGFlexDirection axis,
  187. const float widthSize) const;
  188. YGFloatOptional getTrailingPaddingAndBorder(
  189. const YGFlexDirection axis,
  190. const float widthSize) const;
  191. YGFloatOptional getMarginForAxis(
  192. const YGFlexDirection axis,
  193. const float widthSize) const;
  194. // Setters
  195. void setContext(void* context) {
  196. context_ = context;
  197. }
  198. void setPrintFunc(YGPrintFunc printFunc) {
  199. print_.noContext = printFunc;
  200. printUsesContext_ = false;
  201. }
  202. void setPrintFunc(PrintWithContextFn printFunc) {
  203. print_.withContext = printFunc;
  204. printUsesContext_ = true;
  205. }
  206. void setPrintFunc(std::nullptr_t) {
  207. setPrintFunc(YGPrintFunc{nullptr});
  208. }
  209. void setHasNewLayout(bool hasNewLayout) {
  210. hasNewLayout_ = hasNewLayout;
  211. }
  212. void setNodeType(YGNodeType nodeType) {
  213. nodeType_ = nodeType;
  214. }
  215. void setStyleFlexDirection(YGFlexDirection direction) {
  216. style_.flexDirection = direction;
  217. }
  218. void setStyleAlignContent(YGAlign alignContent) {
  219. style_.alignContent = alignContent;
  220. }
  221. void setMeasureFunc(YGMeasureFunc measureFunc);
  222. void setMeasureFunc(MeasureWithContextFn);
  223. void setMeasureFunc(std::nullptr_t) {
  224. return setMeasureFunc(YGMeasureFunc{nullptr});
  225. }
  226. void setBaselineFunc(YGBaselineFunc baseLineFunc) {
  227. baselineUsesContext_ = false;
  228. baseline_.noContext = baseLineFunc;
  229. }
  230. void setBaselineFunc(BaselineWithContextFn baseLineFunc) {
  231. baselineUsesContext_ = true;
  232. baseline_.withContext = baseLineFunc;
  233. }
  234. void setBaselineFunc(std::nullptr_t) {
  235. return setBaselineFunc(YGBaselineFunc{nullptr});
  236. }
  237. void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) {
  238. dirtied_ = dirtiedFunc;
  239. }
  240. void setStyle(const YGStyle& style) {
  241. style_ = style;
  242. }
  243. void setLayout(const YGLayout& layout) {
  244. layout_ = layout;
  245. }
  246. void setLineIndex(uint32_t lineIndex) {
  247. lineIndex_ = lineIndex;
  248. }
  249. void setIsReferenceBaseline(bool isReferenceBaseline) {
  250. isReferenceBaseline_ = isReferenceBaseline;
  251. }
  252. void setOwner(YGNodeRef owner) {
  253. owner_ = owner;
  254. }
  255. void setChildren(const YGVector& children) {
  256. children_ = children;
  257. }
  258. // TODO: rvalue override for setChildren
  259. void setConfig(YGConfigRef config) {
  260. config_ = config;
  261. }
  262. void setDirty(bool isDirty);
  263. void setLayoutLastOwnerDirection(YGDirection direction);
  264. void setLayoutComputedFlexBasis(const YGFloatOptional computedFlexBasis);
  265. void setLayoutComputedFlexBasisGeneration(
  266. uint32_t computedFlexBasisGeneration);
  267. void setLayoutMeasuredDimension(float measuredDimension, int index);
  268. void setLayoutHadOverflow(bool hadOverflow);
  269. void setLayoutDimension(float dimension, int index);
  270. void setLayoutDirection(YGDirection direction);
  271. void setLayoutMargin(float margin, int index);
  272. void setLayoutBorder(float border, int index);
  273. void setLayoutPadding(float padding, int index);
  274. void setLayoutPosition(float position, int index);
  275. void setPosition(
  276. const YGDirection direction,
  277. const float mainSize,
  278. const float crossSize,
  279. const float ownerWidth);
  280. void setAndPropogateUseLegacyFlag(bool useLegacyFlag);
  281. void setLayoutDoesLegacyFlagAffectsLayout(bool doesLegacyFlagAffectsLayout);
  282. void setLayoutDidUseLegacyFlag(bool didUseLegacyFlag);
  283. void markDirtyAndPropogateDownwards();
  284. // Other methods
  285. YGValue marginLeadingValue(const YGFlexDirection axis) const;
  286. YGValue marginTrailingValue(const YGFlexDirection axis) const;
  287. YGValue resolveFlexBasisPtr() const;
  288. void resolveDimension();
  289. YGDirection resolveDirection(const YGDirection ownerDirection);
  290. void clearChildren();
  291. /// Replaces the occurrences of oldChild with newChild
  292. void replaceChild(YGNodeRef oldChild, YGNodeRef newChild);
  293. void replaceChild(YGNodeRef child, uint32_t index);
  294. void insertChild(YGNodeRef child, uint32_t index);
  295. /// Removes the first occurrence of child
  296. bool removeChild(YGNodeRef child);
  297. void removeChild(uint32_t index);
  298. void cloneChildrenIfNeeded(void*);
  299. void markDirtyAndPropogate();
  300. float resolveFlexGrow();
  301. float resolveFlexShrink();
  302. bool isNodeFlexible();
  303. bool didUseLegacyFlag();
  304. bool isLayoutTreeEqualToNode(const YGNode& node) const;
  305. void reset();
  306. };