Advertisement
robert_muench

cgo / c interface / strange float value

Feb 2nd, 2021 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 5.06 KB | None | 0 0
  1. // Config Code
  2.  
  3. // type CloneNode func(oldNode *Node, parent *Node, childIndex int32) *Node
  4. type CloneNode func(configRef ConfigRef, oldRef NodeRef, parentRef NodeRef, childIndex int32) NodeRef
  5.  
  6. type Config struct {
  7.     ref ConfigRef
  8.     ctx interface{}
  9.  
  10.     onCloneNode CloneNode
  11. }
  12.  
  13. var configsByContext sync.Map
  14.  
  15. var DefaultConfig = newConfig(ConfigGetDefault())
  16.  
  17. func NewConfig() *Config {
  18.     return newConfig(ConfigNew())
  19. }
  20.  
  21. func configFromRef(ref ConfigRef) *Config {
  22.     if ptr := ConfigGetContext(ref); ptr != nil {
  23.         if value, ok := configsByContext.Load(ptr); ok {
  24.             if config, ok := value.(*Config); ok {
  25.                 return config
  26.             }
  27.         }
  28.     }
  29.     return nil
  30. }
  31.  
  32. func newConfig(ref ConfigRef) *Config {
  33.     config := configFromRef(ref)
  34.     if config == nil {
  35.         config = &Config{ref: ref}
  36.  
  37.         var ctx unsafe.Pointer = C.malloc(C.size_t(1))
  38.         ConfigSetContext(ref, ctx)
  39.         configsByContext.Store(ctx, config)
  40.     }
  41.     return config
  42. }
  43.  
  44. // ConfigGetContext function as declared in yoga/Yoga.h:352
  45. func ConfigGetContext(config ConfigRef) unsafe.Pointer {
  46.     cconfig, cconfigAllocMap := *(*C.YGConfigRef)(unsafe.Pointer(&config)), cgoAllocsUnknown
  47.     __ret := C.YGConfigGetContext(cconfig)
  48.     runtime.KeepAlive(cconfigAllocMap)
  49.     __v := *(*unsafe.Pointer)(unsafe.Pointer(&__ret))
  50.     return __v
  51. }
  52.  
  53. //
  54. // Node code
  55. //
  56. type Node struct {
  57.     ctx    interface{}
  58.     ref    NodeRef
  59.     style  *NodeStyle
  60.     layout *NodeLayout
  61.  
  62.     onMeasure  NodeMeasure
  63.     onBaseline NodeBaseline
  64.     onDirtied  NodeDirtied
  65.     onPrint    NodePrint
  66. }
  67.  
  68. var nodesByContext sync.Map
  69.  
  70. func NewNode() *Node {
  71.     ref := NodeNew()
  72.     return newNode(ref)
  73. }
  74.  
  75. func NewNodeWithConfig(config *Config) *Node {
  76.     ref := NodeNewWithConfig(config.ref)
  77.     return newNode(ref)
  78. }
  79.  
  80. func nodeFromRef(ref NodeRef) *Node {
  81.     if ptr := NodeGetContext(ref); ptr != nil {
  82.         if value, ok := nodesByContext.Load(ptr); ok {
  83.             if node, ok := value.(*Node); ok {
  84.                 return node
  85.             }
  86.         }
  87.     }
  88.     return nil
  89. }
  90.  
  91. func newNode(ref NodeRef) *Node {
  92.     node := nodeFromRef(ref)
  93.     if node == nil {
  94.         node = &Node{
  95.             ref: ref,
  96.             style: &NodeStyle{
  97.                 ref: ref,
  98.             },
  99.             layout: &NodeLayout{
  100.                 ref: ref,
  101.             },
  102.         }
  103.  
  104.         var ctx unsafe.Pointer = C.malloc(C.size_t(1))
  105.         NodeSetContext(ref, ctx)
  106.         nodesByContext.Store(ctx, node)
  107.     }
  108.     return node
  109. }
  110.  
  111. func (s *NodeStyle) SetPosition(edge Edge, position float32) {
  112.     if s.ref != nil {
  113.         NodeStyleSetPosition(s.ref, edge, position)
  114.     }
  115. }
  116.  
  117. func (l *NodeLayout) Height() float32 {
  118.     if l.ref != nil {
  119.         return NodeLayoutGetHeight(l.ref)
  120.     }
  121.     return Undefined
  122. }
  123.  
  124. func (n *Node) Layout() *NodeLayout {
  125.     return n.layout
  126. }
  127.  
  128. // NodeStyleSetPosition function as declared in yoga/Yoga.h:204
  129. func NodeStyleSetPosition(node NodeRef, edge Edge, position float32) {
  130.     cnode, cnodeAllocMap := *(*C.YGNodeRef)(unsafe.Pointer(&node)), cgoAllocsUnknown
  131.     cedge, cedgeAllocMap := (C.YGEdge)(edge), cgoAllocsUnknown
  132.     cposition, cpositionAllocMap := (C.float)(position), cgoAllocsUnknown
  133.     C.YGNodeStyleSetPosition(cnode, cedge, cposition)
  134.     runtime.KeepAlive(cpositionAllocMap)
  135.     runtime.KeepAlive(cedgeAllocMap)
  136.     runtime.KeepAlive(cnodeAllocMap)
  137. }
  138.  
  139. // NodeLayoutGetHeight function as declared in yoga/Yoga.h:284
  140. func NodeLayoutGetHeight(node NodeRef) float32 {
  141.     cnode, cnodeAllocMap := *(*C.YGNodeRef)(unsafe.Pointer(&node)), cgoAllocsUnknown
  142.     __ret := C.YGNodeLayoutGetHeight(cnode)
  143.     runtime.KeepAlive(cnodeAllocMap)
  144.     __v := (float32)(__ret)
  145.     return __v
  146. }
  147.  
  148. func getHeight(node *Node) float32 {
  149.     r0 := C.getCHeight(node.ref)
  150.     r1 := C.YGNodeLayoutGetHeight(node.ref)
  151.     _ = r1
  152.     return float32(r0)
  153. }
  154.  
  155. /*
  156. float getCHeight(YGNodeRef n) {
  157.    union ufloat {
  158.         float f;
  159.         unsigned u;
  160.     } r;
  161.  
  162.     r.f = YGNodeLayoutGetHeight(n);
  163.     printf("YGNodeLayoutGetHeight: %f\t%X\n", r.f, r.u);
  164.     return r.f;
  165. }
  166. */
  167.  
  168. //
  169. // Test
  170. //
  171. func TestRoundingInnerNodeControversyHorizontal(t *testing.T) {
  172.     config := NewConfig()
  173.     defer config.Free()
  174.  
  175.     root := NewNodeWithConfig(config)
  176.     defer root.Free()
  177.  
  178.     _ = getWidth(root)
  179.     _ = getHeight(root)
  180.  
  181.     root.Style().SetFlexDirection(FlexDirectionRow)
  182.     root.Style().SetWidth(320)
  183.  
  184.     c0 := NewNodeWithConfig(config)
  185.     defer c0.Free()
  186.     c0.Style().SetFlexGrow(1)
  187.     c0.Style().SetHeight(10)
  188.     root.InsertChild(c0, 0)
  189.  
  190.     c1 := NewNodeWithConfig(config)
  191.     defer c1.Free()
  192.     c1.Style().SetFlexGrow(1)
  193.     c1.Style().SetHeight(10)
  194.     root.InsertChild(c1, 1)
  195.  
  196.     c1c0 := NewNodeWithConfig(config)
  197.     defer c1c0.Free()
  198.     c1c0.Style().SetFlexGrow(1)
  199.     c1c0.Style().SetHeight(10)
  200.     c1.InsertChild(c1c0, 0)
  201.  
  202.     c2 := NewNodeWithConfig(config)
  203.     defer c2.Free()
  204.     c2.Style().SetFlexGrow(1)
  205.     c2.Style().SetHeight(10)
  206.     root.InsertChild(c2, 2)
  207.  
  208.     root.Style().SetDirection(DirectionLTR)
  209.     root.CalculateLayout(Undefined, Undefined, DirectionLTR)
  210.  
  211.     _ = getWidth(root)
  212.     _ = getHeight(root)
  213.  
  214.     assert.EqualValues(t, 0, root.Layout().Left())
  215.     assert.EqualValues(t, 0, root.Layout().Top())
  216.     assert.EqualValues(t, 320, root.Layout().Width())
  217.     assert.EqualValues(t, 320, getWidth(root))
  218.     assert.EqualValues(t, 10, root.Layout().Height()) // fails
  219.     assert.EqualValues(t, 10, getHeight(root)) // fails
  220. }
  221.  
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement