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