Skip to content

Commit 5ae50a6

Browse files
committed
perf: a faster way of change notifications
1 parent 709feae commit 5ae50a6

7 files changed

Lines changed: 197 additions & 147 deletions

File tree

.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*.cs]
4+
indent_style = space
5+
tab_width = 4
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
spelling_languages = en-us,de-de
10+
dotnet_sort_system_directives_first = true

CADability/GeoObject.cs

Lines changed: 77 additions & 100 deletions
Large diffs are not rendered by default.

CADability/GeoObjectChange.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
3+
4+
/// <summary>
5+
/// Namespace GeoObject
6+
/// </summary>
7+
namespace CADability.GeoObject
8+
{
9+
/// <summary>
10+
/// This class is used as a parameter in the <see cref="ChangeDelegate"/> event of IGeoObject
11+
/// (see <see cref="IGeoObject.WillChangeEvent"/>).
12+
/// </summary>
13+
public class GeoObjectChange : ReversibleChange
14+
{
15+
/// <summary>
16+
/// Notifies that only an attribute was changed in contrast to a change of the geometry.
17+
/// </summary>
18+
public bool OnlyAttributeChanged;
19+
/// <summary>
20+
/// Notifies that this change doesn't require an undo operation
21+
/// </summary>
22+
public bool NoUndoNecessary;
23+
/// <summary>
24+
/// Creates a new GeoObjectChange object. See the appropriate constructor of <see cref="ReversibleChange"/> for details.
25+
/// </summary>
26+
/// <param name="objectToChange">The object which will be or was changed</param>
27+
/// <param name="interfaceForMethod">the interface on which contains the method or property</param>
28+
/// <param name="methodOrPropertyName">the case sensitive name of the method or property</param>
29+
/// <param name="parameters">The parameters needed to call this method or property</param>
30+
public GeoObjectChange(IGeoObject objectToChange, Type interfaceForMethod, string methodOrPropertyName, params object[] parameters)
31+
: base(objectToChange, interfaceForMethod, methodOrPropertyName, parameters)
32+
{ }
33+
34+
/// <summary>
35+
/// Creates a new GeoObjectChange object. See the appropriate constructor of <see cref="ReversibleChange"/> for details.
36+
/// </summary>
37+
/// <param name="objectToChange">The object which will be or was changed</param>
38+
/// <param name="methodOrPropertyName">the case sensitive name of the method or property</param>
39+
/// <param name="parameters">The parameters needed to call this method or property</param>
40+
public GeoObjectChange(IGeoObject objectToChange, string methodOrPropertyName, params object[] parameters)
41+
: base(objectToChange, methodOrPropertyName, parameters)
42+
{ }
43+
44+
public GeoObjectChange(IGeoObject objectToChange, string methodOrPropertyName, object parameter)
45+
: base(objectToChange, methodOrPropertyName, parameter)
46+
{ }
47+
48+
/// <summary>
49+
/// Creates a new GeoObjectChange object that reflects a modification by the <see cref="ModOp"/> m.
50+
/// </summary>
51+
/// <param name="objectToChange">The object which will be or was changed</param>
52+
/// <param name="m">the ModOp that changes or changed the object</param>
53+
public GeoObjectChange(IGeoObject objectToChange, ModOp m)
54+
: base(objectToChange, "ModifyInverse", m)
55+
{ }
56+
57+
// Wenn das gebraucht wird, dann mit anderen Parametern versehen sonst droht verwechslung mit obiger Methode (methodOrPropertyName)
58+
//public GeoObjectChange(IGeoObject objectToChange, string key, object oldValue)
59+
// : base(objectToChange.UserData, "Add", key, oldValue)
60+
//{
61+
// OnlyAttributeChanged = true;
62+
// NoUndoNecessary = false;
63+
//}
64+
}
65+
}

CADability/Line.cs

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using CADability.Curve2D;
1+
using CADability.Curve2D;
22
using System;
33
#if WEBASSEMBLY
44
using CADability.WebDrawing;
@@ -34,7 +34,7 @@ public class Line : IGeoObjectImpl, IColorDef, ILineWidth, ILinePattern,
3434
/// <returns>A Line or Line derived class</returns>
3535
public delegate Line ConstructionDelegate();
3636
/// <summary>
37-
/// Provide a delegate here if you want you Line derived class to be
37+
/// Provide a delegate here if you want you Line derived class to be
3838
/// created each time CADability creates a line.
3939
/// </summary>
4040
public static ConstructionDelegate Constructor;
@@ -71,13 +71,10 @@ public static Line TwoPoints(GeoPoint sp, GeoPoint ep)
7171
/// </summary>
7272
public virtual GeoPoint StartPoint
7373
{
74-
get
75-
{
76-
return startPoint;
77-
}
74+
get => startPoint;
7875
set
7976
{
80-
using (new Changing(this, "StartPoint"))
77+
using (Changing.Create(this, startPoint))
8178
{
8279
startPoint = value;
8380
}
@@ -89,13 +86,10 @@ public virtual GeoPoint StartPoint
8986
/// </summary>
9087
public virtual GeoPoint EndPoint
9188
{
92-
get
93-
{
94-
return endPoint;
95-
}
89+
get => endPoint;
9690
set
9791
{
98-
using (new Changing(this, "EndPoint"))
92+
using (Changing.Create(this, endPoint))
9993
{
10094
endPoint = value;
10195
}
@@ -122,13 +116,10 @@ public void SetTwoPoints(GeoPoint startPoint, GeoPoint endPoint)
122116
/// </summary>
123117
public double Length
124118
{
125-
get
126-
{
127-
return Geometry.Dist(startPoint, endPoint);
128-
}
119+
get => Geometry.Dist(startPoint, endPoint);
129120
set
130121
{
131-
using (new Changing(this, "Length"))
122+
using (Changing.Create(this, Length))
132123
{
133124
GeoVector v = endPoint - startPoint;
134125
if (!v.IsNullVector()) v.Norm();
@@ -154,7 +145,7 @@ public GeoPoint LengthFixPoint
154145
/// <param name="m">the operator for the modification</param>
155146
public override void Modify(ModOp m)
156147
{
157-
using (new Changing(this, "ModifyInverse", m))
148+
using (new Changing(this, nameof(ModifyInverse), m))
158149
{
159150
startPoint = m * startPoint;
160151
endPoint = m * endPoint;
@@ -447,13 +438,10 @@ public override IPropertyEntry GetShowProperties(IFrame Frame)
447438
private ColorDef colorDef;
448439
public ColorDef ColorDef
449440
{
450-
get
451-
{
452-
return colorDef;
453-
}
441+
get => colorDef;
454442
set
455443
{
456-
using (new ChangingAttribute(this, "ColorDef", colorDef))
444+
using (ChangingAttribute.Create(this, colorDef))
457445
{
458446
colorDef = value;
459447
}
@@ -754,13 +742,10 @@ bool ICurve.TryPointDeriv2At(double position, out GeoPoint point, out GeoVector
754742
#region ILineWidth Members
755743
public LineWidth LineWidth
756744
{
757-
get
758-
{
759-
return lineWidth;
760-
}
745+
get => lineWidth;
761746
set
762747
{
763-
using (new ChangingAttribute(this, "LineWidth", lineWidth))
748+
using (ChangingAttribute.Create(this, lineWidth))
764749
{
765750
lineWidth = value;
766751
}
@@ -772,13 +757,10 @@ public LineWidth LineWidth
772757

773758
public LinePattern LinePattern
774759
{
775-
get
776-
{
777-
return linePattern;
778-
}
760+
get => linePattern;
779761
set
780762
{
781-
using (new ChangingAttribute(this, "LinePattern", linePattern))
763+
using (ChangingAttribute.Create(this, linePattern))
782764
{
783765
linePattern = value;
784766
}
@@ -853,7 +835,7 @@ int IExportStep.Export(ExportStep export, bool topLevel)
853835
int sp = (startPoint as IExportStep).Export(export, false);
854836
int ep = (endPoint as IExportStep).Export(export, false);
855837
int tc = export.WriteDefinition("TRIMMED_CURVE('',#" + ln.ToString() + ",(#" + sp.ToString() + ",PARAMETER_VALUE(0.0)),(#" + ep.ToString() + ",PARAMETER_VALUE("+export.ToString(Length)+")),.T.,.CARTESIAN.)");
856-
int gcs = export.WriteDefinition("GEOMETRIC_CURVE_SET('',(#" + tc.ToString() + "))"); // is a Representation_Item
838+
int gcs = export.WriteDefinition("GEOMETRIC_CURVE_SET('',(#" + tc.ToString() + "))"); // is a Representation_Item
857839
ColorDef cd = ColorDef;
858840
if (cd == null) cd = new ColorDef("Black", Color.Black);
859841
cd.MakeStepStyle(gcs, export);

CADability/Model.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ public void Add(GeoObjectList ListToAdd)
984984
}
985985
}
986986
}
987-
Undo.AddUndoStep(new ReversibleChange(this, "Remove", new object[] { ListToAdd.Clone() }));
987+
Undo.AddUndoStep(new ReversibleChange(this, "Remove", ListToAdd.Clone()));
988988
if (AddingGeoObjectsEvent != null) AddingGeoObjectsEvent(ListToAdd);
989989
if (octTree != null && octTree.IsEmpty && extent != null) octTree = new OctTree<IGeoObject>(extent.Value, displayListPrecision);
990990
for (int i = 0; i < ListToAdd.Count; ++i)
@@ -1069,7 +1069,7 @@ public void Remove(GeoObjectList ToRemove)
10691069
// den Aufruf von RemovingGeoObjectsEvent an den Anfang gesetzt, damit man darin einen UndoFrame machen kann
10701070
// den man bei GeoObjectsRemovedEvent wieder zu macht. Ob das stört?
10711071
if (RemovingGeoObjectsEvent != null) RemovingGeoObjectsEvent(ToRemove);
1072-
Undo.AddUndoStep(new ReversibleChange(this, "Add", new object[] { ToRemove.Clone() }));
1072+
Undo.AddUndoStep(new ReversibleChange(this, "Add", ToRemove.Clone()));
10731073
// kein Undoframe, denn Remove wird selbst von Undo aufgerufen
10741074
for (int i = ToRemove.Count - 1; i >= 0; i--)
10751075
{

CADability/ReversibleChange.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Globalization;
33
using System.Reflection;
44
using System.Threading;
@@ -14,8 +14,9 @@ namespace CADability
1414

1515
public class ReversibleChange
1616
{
17-
private object objectToChange;
18-
private string methodOrPropertyName;
17+
private readonly object objectToChange;
18+
private readonly string methodOrPropertyName;
19+
private readonly object parameter; // for the case of a single parameter, this is used to find the property or method
1920
private object[] parameters;
2021
private Type interfaceForMethod;
2122
private Func<bool> undo; // this is the modern way to do it, all data is captured
@@ -35,7 +36,8 @@ public ReversibleChange(object objectToChange, Type interfaceForMethod, string m
3536
{
3637
this.objectToChange = objectToChange;
3738
this.methodOrPropertyName = methodOrPropertyName;
38-
this.parameters = (object[])parameters.Clone();
39+
this.parameter = null;
40+
this.parameters = parameters;
3941
this.interfaceForMethod = interfaceForMethod;
4042
}
4143
/// <summary>
@@ -53,7 +55,17 @@ public ReversibleChange(object objectToChange, string methodOrPropertyName, para
5355
{
5456
this.objectToChange = objectToChange;
5557
this.methodOrPropertyName = methodOrPropertyName;
56-
this.parameters = (object[])parameters.Clone();
58+
this.parameter = null;
59+
this.parameters = parameters;
60+
this.interfaceForMethod = null;
61+
}
62+
63+
public ReversibleChange(object objectToChange, string methodOrPropertyName, object parameter)
64+
{
65+
this.objectToChange = objectToChange;
66+
this.methodOrPropertyName = methodOrPropertyName;
67+
this.parameter = parameter;
68+
this.parameters = null;
5769
this.interfaceForMethod = null;
5870
}
5971
/// <summary>
@@ -122,6 +134,10 @@ private MethodInfo FindMethod(object o, string methodname, Type[] ret)
122134
public bool Undo()
123135
{
124136
if (undo != null) return undo(); // this is the modern way
137+
138+
// lazy initialization of parameters, because the parameter is only used for the case of a single parameter
139+
if (parameters == null)
140+
parameters = new object[] { parameter };
125141
if (parameters.Length == 1)
126142
{
127143
try

CADability/Style.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ public ColorDef ColorDef
570570
}
571571
set
572572
{
573-
object[] param = new object[] { colorDef };
573+
var param = colorDef;
574574
colorDef = value;
575575
if (parent != null)
576576
(parent as IAttributeList).AttributeChanged(this, new ReversibleChange(this, "ColorDef", param));
@@ -594,7 +594,7 @@ public Layer Layer
594594
}
595595
set
596596
{
597-
object[] param = new object[] { layer };
597+
var param = layer;
598598
layer = value;
599599
if (parent != null)
600600
(parent as IAttributeList).AttributeChanged(this, new ReversibleChange(this, "Layer", param));
@@ -609,7 +609,7 @@ public LineWidth LineWidth
609609
}
610610
set
611611
{
612-
object[] param = new object[] { lineWidth };
612+
var param = lineWidth;
613613
lineWidth = value;
614614
if (parent != null)
615615
(parent as IAttributeList).AttributeChanged(this, new ReversibleChange(this, "LineWidth", param));
@@ -625,7 +625,7 @@ public LinePattern LinePattern
625625
}
626626
set
627627
{
628-
object[] param = new object[] { linePattern };
628+
var param = linePattern;
629629
linePattern = value;
630630
if (parent != null)
631631
(parent as IAttributeList).AttributeChanged(this, new ReversibleChange(this, "LinePattern", param));
@@ -640,7 +640,7 @@ public HatchStyle HatchStyle
640640
}
641641
set
642642
{
643-
object[] param = new object[] { hatchStyle };
643+
var param = hatchStyle;
644644
hatchStyle = value;
645645
if (parent != null)
646646
(parent as IAttributeList).AttributeChanged(this, new ReversibleChange(this, "HatchStyle", param));
@@ -651,7 +651,7 @@ public DimensionStyle DimensionStyle
651651
get { return dimStyle; }
652652
set
653653
{
654-
object[] param = new object[] { dimStyle };
654+
var param = dimStyle;
655655
dimStyle = value;
656656
if (parent != null)
657657
(parent as IAttributeList).AttributeChanged(this, new ReversibleChange(this, "DimensionStyle", param));

0 commit comments

Comments
 (0)