Align C# implementation with Java and Dispose pattern

Summary:
- Align C# implementation with Java JNI implementation
  - AssertNativeInstance
    - is this instance disposed?
    - does this instance have native instance pointer?
- Align Dispose and destructor with Dispose pattern
  - https://msdn.microsoft.com/en-us/library/fs2xkftw(v=vs.110).aspx

    if (disposing)
    {
      Free maanged objects here
    }
    Free unmanaged objects here
    disposed = true;

Reviewed By: emilsjolander

Differential Revision: D3977015

fbshipit-source-id: 023cf5b15aacfada14a85c321576aa81d7f95125
This commit is contained in:
Kazuki Sakamoto
2016-10-06 06:04:56 -07:00
committed by Facebook Github Bot
parent 2870d3ce4d
commit 47bd1bb943

View File

@@ -33,12 +33,22 @@ namespace Facebook.CSSLayout
_printFunc = PrintInternal; _printFunc = PrintInternal;
} }
private void CheckDisposed() private void AssertNativeInstance()
{ {
if (_isDisposed) if (_isDisposed)
{ {
throw new ObjectDisposedException("CSSNode"); throw new ObjectDisposedException("CSSNode");
} }
if (_cssNode == IntPtr.Zero)
{
throw new InvalidOperationException("Null native pointer");
}
}
~CSSNode()
{
Dispose(false);
} }
public void Dispose() public void Dispose()
@@ -49,19 +59,43 @@ namespace Facebook.CSSLayout
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
if (!_isDisposed && disposing) if (!_isDisposed)
{ {
if (disposing)
{
FreeManaged();
}
FreeUnmanaged();
_isDisposed = true; _isDisposed = true;
}
}
private void FreeManaged()
{
_children = null;
_parent = null;
_measureFunction = null;
}
private void FreeUnmanaged()
{
if (_cssNode != IntPtr.Zero)
{
Native.CSSNodeFree(_cssNode); Native.CSSNodeFree(_cssNode);
GCHandle.FromIntPtr(_context).Free(); GCHandle.FromIntPtr(_context).Free();
_children = null; _cssNode = IntPtr.Zero;
_parent = null; _context = IntPtr.Zero;
_measureFunction = null;
} }
} }
public void Initialize() public void Initialize()
{ {
if (_cssNode != IntPtr.Zero)
{
throw new InvalidOperationException("Allready initialized node");
}
_cssNode = Native.CSSNodeNew(); _cssNode = Native.CSSNodeNew();
_context = (IntPtr)GCHandle.Alloc(this); _context = (IntPtr)GCHandle.Alloc(this);
Native.CSSNodeSetContext(_cssNode, _context); Native.CSSNodeSetContext(_cssNode, _context);
@@ -71,21 +105,23 @@ namespace Facebook.CSSLayout
public void Reset() public void Reset()
{ {
Dispose(true); AssertNativeInstance();
FreeManaged();
FreeUnmanaged();
} }
public bool IsDirty public bool IsDirty
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeIsDirty(_cssNode); return Native.CSSNodeIsDirty(_cssNode);
} }
} }
public virtual void MarkDirty() public virtual void MarkDirty()
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeMarkDirty(_cssNode); Native.CSSNodeMarkDirty(_cssNode);
} }
@@ -93,13 +129,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeGetIsTextnode(_cssNode); return Native.CSSNodeGetIsTextnode(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeSetIsTextnode(_cssNode, value); Native.CSSNodeSetIsTextnode(_cssNode, value);
} }
} }
@@ -108,14 +144,14 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeGetHasNewLayout(_cssNode); return Native.CSSNodeGetHasNewLayout(_cssNode);
} }
} }
public void MarkHasNewLayout() public void MarkHasNewLayout()
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeSetHasNewLayout(_cssNode, true); Native.CSSNodeSetHasNewLayout(_cssNode, true);
} }
@@ -123,7 +159,7 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return _parent; return _parent;
} }
} }
@@ -140,12 +176,12 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetDirection(_cssNode); return Native.CSSNodeStyleGetDirection(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetDirection(_cssNode, value); Native.CSSNodeStyleSetDirection(_cssNode, value);
} }
} }
@@ -154,13 +190,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetFlexDirection(_cssNode); return Native.CSSNodeStyleGetFlexDirection(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetFlexDirection(_cssNode, value); Native.CSSNodeStyleSetFlexDirection(_cssNode, value);
} }
} }
@@ -169,13 +205,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetJustifyContent(_cssNode); return Native.CSSNodeStyleGetJustifyContent(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetJustifyContent(_cssNode, value); Native.CSSNodeStyleSetJustifyContent(_cssNode, value);
} }
} }
@@ -184,13 +220,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetAlignItems(_cssNode); return Native.CSSNodeStyleGetAlignItems(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetAlignItems(_cssNode, value); Native.CSSNodeStyleSetAlignItems(_cssNode, value);
} }
} }
@@ -199,13 +235,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetAlignSelf(_cssNode); return Native.CSSNodeStyleGetAlignSelf(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetAlignSelf(_cssNode, value); Native.CSSNodeStyleSetAlignSelf(_cssNode, value);
} }
} }
@@ -214,13 +250,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetAlignContent(_cssNode); return Native.CSSNodeStyleGetAlignContent(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetAlignContent(_cssNode, value); Native.CSSNodeStyleSetAlignContent(_cssNode, value);
} }
} }
@@ -229,13 +265,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetPositionType(_cssNode); return Native.CSSNodeStyleGetPositionType(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetPositionType(_cssNode, value); Native.CSSNodeStyleSetPositionType(_cssNode, value);
} }
} }
@@ -244,13 +280,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetFlexWrap(_cssNode); return Native.CSSNodeStyleGetFlexWrap(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetFlexWrap(_cssNode, value); Native.CSSNodeStyleSetFlexWrap(_cssNode, value);
} }
} }
@@ -259,13 +295,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetFlex(_cssNode); return Native.CSSNodeStyleGetFlex(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetFlex(_cssNode, value); Native.CSSNodeStyleSetFlex(_cssNode, value);
} }
} }
@@ -274,13 +310,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetFlexGrow(_cssNode); return Native.CSSNodeStyleGetFlexGrow(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetFlexGrow(_cssNode, value); Native.CSSNodeStyleSetFlexGrow(_cssNode, value);
} }
} }
@@ -289,13 +325,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetFlexShrink(_cssNode); return Native.CSSNodeStyleGetFlexShrink(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetFlexShrink(_cssNode, value); Native.CSSNodeStyleSetFlexShrink(_cssNode, value);
} }
} }
@@ -304,20 +340,20 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetFlexBasis(_cssNode); return Native.CSSNodeStyleGetFlexBasis(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetFlexBasis(_cssNode, value); Native.CSSNodeStyleSetFlexBasis(_cssNode, value);
} }
} }
public Spacing GetMargin() public Spacing GetMargin()
{ {
CheckDisposed(); AssertNativeInstance();
var margin = new Spacing(); var margin = new Spacing();
margin.Set(Spacing.Left, Native.CSSNodeStyleGetMargin(_cssNode, CSSEdge.Left)); margin.Set(Spacing.Left, Native.CSSNodeStyleGetMargin(_cssNode, CSSEdge.Left));
@@ -332,13 +368,13 @@ namespace Facebook.CSSLayout
public void SetMargin(CSSEdge edge, float value) public void SetMargin(CSSEdge edge, float value)
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetMargin(_cssNode, edge, value); Native.CSSNodeStyleSetMargin(_cssNode, edge, value);
} }
public Spacing GetPadding() public Spacing GetPadding()
{ {
CheckDisposed(); AssertNativeInstance();
var padding = new Spacing(); var padding = new Spacing();
padding.Set(Spacing.Left, Native.CSSNodeStyleGetPadding(_cssNode, CSSEdge.Left)); padding.Set(Spacing.Left, Native.CSSNodeStyleGetPadding(_cssNode, CSSEdge.Left));
@@ -353,13 +389,13 @@ namespace Facebook.CSSLayout
public void SetPadding(CSSEdge edge, float padding) public void SetPadding(CSSEdge edge, float padding)
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetPadding(_cssNode, edge, padding); Native.CSSNodeStyleSetPadding(_cssNode, edge, padding);
} }
public Spacing GetBorder() public Spacing GetBorder()
{ {
CheckDisposed(); AssertNativeInstance();
var border = new Spacing(); var border = new Spacing();
border.Set(Spacing.Left, Native.CSSNodeStyleGetBorder(_cssNode, CSSEdge.Left)); border.Set(Spacing.Left, Native.CSSNodeStyleGetBorder(_cssNode, CSSEdge.Left));
@@ -374,13 +410,13 @@ namespace Facebook.CSSLayout
public void SetBorder(CSSEdge edge, float border) public void SetBorder(CSSEdge edge, float border)
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetBorder(_cssNode, edge, border); Native.CSSNodeStyleSetBorder(_cssNode, edge, border);
} }
public Spacing GetPosition() public Spacing GetPosition()
{ {
CheckDisposed(); AssertNativeInstance();
var position = new Spacing(); var position = new Spacing();
position.Set(Spacing.Left, Native.CSSNodeStyleGetPosition(_cssNode, CSSEdge.Left)); position.Set(Spacing.Left, Native.CSSNodeStyleGetPosition(_cssNode, CSSEdge.Left));
@@ -395,20 +431,20 @@ namespace Facebook.CSSLayout
public void SetPosition(CSSEdge edge, float position) public void SetPosition(CSSEdge edge, float position)
{ {
CheckDisposed(); AssertNativeInstance();
} }
public float StyleWidth public float StyleWidth
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetWidth(_cssNode); return Native.CSSNodeStyleGetWidth(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetWidth(_cssNode, value); Native.CSSNodeStyleSetWidth(_cssNode, value);
} }
} }
@@ -417,13 +453,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetHeight(_cssNode); return Native.CSSNodeStyleGetHeight(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetHeight(_cssNode, value); Native.CSSNodeStyleSetHeight(_cssNode, value);
} }
} }
@@ -432,13 +468,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetMaxWidth(_cssNode); return Native.CSSNodeStyleGetMaxWidth(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetMaxWidth(_cssNode, value); Native.CSSNodeStyleSetMaxWidth(_cssNode, value);
} }
} }
@@ -447,13 +483,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetMaxHeight(_cssNode); return Native.CSSNodeStyleGetMaxHeight(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetMaxHeight(_cssNode, value); Native.CSSNodeStyleSetMaxHeight(_cssNode, value);
} }
} }
@@ -462,13 +498,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetMinWidth(_cssNode); return Native.CSSNodeStyleGetMinWidth(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetMinWidth(_cssNode, value); Native.CSSNodeStyleSetMinWidth(_cssNode, value);
} }
} }
@@ -477,13 +513,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetMinHeight(_cssNode); return Native.CSSNodeStyleGetMinHeight(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetMinHeight(_cssNode, value); Native.CSSNodeStyleSetMinHeight(_cssNode, value);
} }
} }
@@ -492,7 +528,7 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeLayoutGetLeft(_cssNode); return Native.CSSNodeLayoutGetLeft(_cssNode);
} }
} }
@@ -501,7 +537,7 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeLayoutGetTop(_cssNode); return Native.CSSNodeLayoutGetTop(_cssNode);
} }
} }
@@ -510,7 +546,7 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeLayoutGetWidth(_cssNode); return Native.CSSNodeLayoutGetWidth(_cssNode);
} }
} }
@@ -519,7 +555,7 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeLayoutGetHeight(_cssNode); return Native.CSSNodeLayoutGetHeight(_cssNode);
} }
} }
@@ -528,7 +564,7 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeLayoutGetDirection(_cssNode); return Native.CSSNodeLayoutGetDirection(_cssNode);
} }
} }
@@ -537,13 +573,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return Native.CSSNodeStyleGetOverflow(_cssNode); return Native.CSSNodeStyleGetOverflow(_cssNode);
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeStyleSetOverflow(_cssNode, value); Native.CSSNodeStyleSetOverflow(_cssNode, value);
} }
} }
@@ -552,13 +588,13 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return _data; return _data;
} }
set set
{ {
CheckDisposed(); AssertNativeInstance();
_data = value; _data = value;
} }
} }
@@ -567,7 +603,7 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return _children[index]; return _children[index];
} }
} }
@@ -576,14 +612,14 @@ namespace Facebook.CSSLayout
{ {
get get
{ {
CheckDisposed(); AssertNativeInstance();
return _children.Count; return _children.Count;
} }
} }
public void MarkLayoutSeen() public void MarkLayoutSeen()
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeSetHasNewLayout(_cssNode, false); Native.CSSNodeSetHasNewLayout(_cssNode, false);
} }
@@ -599,7 +635,7 @@ namespace Facebook.CSSLayout
public void Insert(int index, CSSNode node) public void Insert(int index, CSSNode node)
{ {
CheckDisposed(); AssertNativeInstance();
_children.Insert(index, node); _children.Insert(index, node);
node._parent = this; node._parent = this;
Native.CSSNodeInsertChild(_cssNode, node._cssNode, (uint)index); Native.CSSNodeInsertChild(_cssNode, node._cssNode, (uint)index);
@@ -607,7 +643,7 @@ namespace Facebook.CSSLayout
public void RemoveAt(int index) public void RemoveAt(int index)
{ {
CheckDisposed(); AssertNativeInstance();
var child = _children[index]; var child = _children[index];
child._parent = null; child._parent = null;
_children.RemoveAt(index); _children.RemoveAt(index);
@@ -616,20 +652,20 @@ namespace Facebook.CSSLayout
public int IndexOf(CSSNode node) public int IndexOf(CSSNode node)
{ {
CheckDisposed(); AssertNativeInstance();
return _children.IndexOf(node); return _children.IndexOf(node);
} }
public void SetMeasureFunction(MeasureFunction measureFunction) public void SetMeasureFunction(MeasureFunction measureFunction)
{ {
CheckDisposed(); AssertNativeInstance();
_measureFunction = measureFunction; _measureFunction = measureFunction;
Native.CSSNodeSetMeasureFunc(_cssNode, measureFunction != null ? _measureFunc : null); Native.CSSNodeSetMeasureFunc(_cssNode, measureFunction != null ? _measureFunc : null);
} }
public void CalculateLayout() public void CalculateLayout()
{ {
CheckDisposed(); AssertNativeInstance();
Native.CSSNodeCalculateLayout(_cssNode, CSSConstants.Undefined, CSSConstants.Undefined, Native.CSSNodeStyleGetDirection(_cssNode)); Native.CSSNodeCalculateLayout(_cssNode, CSSConstants.Undefined, CSSConstants.Undefined, Native.CSSNodeStyleGetDirection(_cssNode));
} }