Coverage Report

Created: 2026-12-03 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/yoga/yoga/style/StyleValueHandle.h
Line
Count
Source
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 */
7
8
#pragma once
9
10
#include <cstdint>
11
12
#include <yoga/Yoga.h>
13
14
#include <yoga/numeric/FloatOptional.h>
15
#include <yoga/style/SmallValueBuffer.h>
16
#include <yoga/style/StyleLength.h>
17
18
namespace facebook::yoga {
19
20
#pragma pack(push)
21
#pragma pack(1)
22
23
/**
24
 * StyleValueHandle is a small (16-bit) handle to a length or number in a style.
25
 * The value may be embedded directly in the handle if simple, or the handle may
26
 * instead point to an index within a StyleValuePool.
27
 *
28
 * To read or write a value from a StyleValueHandle, use
29
 * `StyleValuePool::store()`, and `StyleValuePool::getLength()`/
30
 * `StyleValuePool::getNumber()`.
31
 */
32
class StyleValueHandle {
33
 public:
34
5.08M
  static constexpr StyleValueHandle ofAuto() {
35
5.08M
    StyleValueHandle handle;
36
5.08M
    handle.setType(Type::Auto);
37
5.08M
    return handle;
38
5.08M
  }
39
40
2.40G
  constexpr bool isUndefined() const {
41
2.40G
    return type() == Type::Undefined;
42
2.40G
  }
43
44
1.61G
  constexpr bool isDefined() const {
45
1.61G
    return !isUndefined();
46
1.61G
  }
47
48
229M
  constexpr bool isAuto() const {
49
229M
    return type() == Type::Auto;
50
229M
  }
51
52
348M
  constexpr bool isPercent() const {
53
348M
    return type() == Type::Percent;
54
348M
  }
55
56
30.2M
  constexpr bool isPoint() const {
57
30.2M
    return type() == Type::Point;
58
30.2M
  }
59
60
484M
  constexpr bool isResolvableLength() const {
61
484M
    return (repr_ & kLengthTypeMask) != 0;
62
484M
  }
63
64
 private:
65
  friend class StyleValuePool;
66
67
  static constexpr uint16_t kHandleTypeMask = 0b0000'0000'0000'0111;
68
  static constexpr uint16_t kHandleIndexedMask = 0b0000'0000'0000'1000;
69
  static constexpr uint16_t kHandleValueMask = 0b1111'1111'1111'0000;
70
71
  static constexpr uint16_t kLengthTypeMask = 0b100;
72
73
  // isResolvableLength() tests only kLengthTypeMask, so a type must set that
74
  // bit if and only if StyleValuePool::resolveLength() can resolve it
75
  enum class Type : uint8_t {
76
    Undefined = 0b000,
77
    Auto = 0b001,
78
    Keyword = 0b010,
79
    Number = 0b011,
80
    Point = 0b100,
81
    Percent = 0b110
82
  };
83
84
  static_assert(
85
      (static_cast<uint16_t>(Type::Undefined) & kLengthTypeMask) == 0 &&
86
          (static_cast<uint16_t>(Type::Auto) & kLengthTypeMask) == 0 &&
87
          (static_cast<uint16_t>(Type::Keyword) & kLengthTypeMask) == 0 &&
88
          (static_cast<uint16_t>(Type::Number) & kLengthTypeMask) == 0 &&
89
          (static_cast<uint16_t>(Type::Point) & kLengthTypeMask) != 0 &&
90
          (static_cast<uint16_t>(Type::Percent) & kLengthTypeMask) != 0,
91
      "Only Point and Percent may set kLengthTypeMask");
92
93
  // Intentionally leaving out auto as a fast path
94
  enum class Keyword : uint8_t { MaxContent, FitContent, Stretch };
95
96
3.30G
  constexpr Type type() const {
97
3.30G
    return static_cast<Type>(repr_ & kHandleTypeMask);
98
3.30G
  }
99
100
10.1M
  constexpr void setType(Type handleType) {
101
10.1M
    repr_ &= (~kHandleTypeMask);
102
10.1M
    repr_ |= static_cast<uint8_t>(handleType);
103
10.1M
  }
104
105
81.9M
  constexpr uint16_t value() const {
106
81.9M
    return repr_ >> 4;
107
81.9M
  }
108
109
5.08M
  constexpr void setValue(uint16_t value) {
110
5.08M
    repr_ &= (~kHandleValueMask);
111
5.08M
    repr_ |= (value << 4);
112
5.08M
  }
113
114
87.0M
  constexpr bool isValueIndexed() const {
115
87.0M
    return (repr_ & kHandleIndexedMask) != 0;
116
87.0M
  }
117
118
4.74M
  constexpr void setValueIsIndexed() {
119
4.74M
    repr_ |= kHandleIndexedMask;
120
4.74M
  }
121
122
  uint16_t repr_{0};
123
};
124
125
#pragma pack(pop)
126
127
} // namespace facebook::yoga