Skip to content

Commit a9795b0

Browse files
committed
RDS: improve quality of generated code for scripts
1 parent fb0dcbc commit a9795b0

36 files changed

+292
-103
lines changed

sources/RevitDBExplorer/Domain/DataModel/Accessors/IAccessor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ namespace RevitDBExplorer.Domain.DataModel.Accessors
77
internal interface IAccessor
88
{
99
IValueViewModel CreatePresenter(SnoopableContext context, object @object);
10+
string UniqueId { get; set; }
11+
string DefaultInvocation { get; set; }
1012
}
1113
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md
2+
3+
namespace RevitDBExplorer.Domain.DataModel.Accessors
4+
{
5+
internal interface IAccessorWithCodeGeneration : IAccessor
6+
{
7+
string GenerateInvocationForScript();
8+
}
9+
}

sources/RevitDBExplorer/Domain/DataModel/Accessors/IAccessorWithSnoop.cs renamed to sources/RevitDBExplorer/Domain/DataModel/Accessors/IAccessorWithReadAndSnoop.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace RevitDBExplorer.Domain.DataModel.Accessors
77
{
8-
internal interface IAccessorWithSnoop
8+
internal interface IAccessorWithReadAndSnoop : IAccessor
99
{
1010
ReadResult Read(SnoopableContext context, object @object);
1111
IEnumerable<SnoopableObject> Snoop(SnoopableContext context, object @object, IValueContainer state);

sources/RevitDBExplorer/Domain/DataModel/MemberAccessors/GenericFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public Func<object, object> CreateCompiledLambda(MethodInfo getMethod)
101101
public IAccessor CreateMemberAccessorByRefCompiled(MethodInfo getMethod)
102102
{
103103
var func = CreateLambda(getMethod);
104-
var accessor = new MemberAccessorByRefCompiled<TSnoopedObjectType, TReturnType>(func);
104+
var accessor = new MemberAccessorByRefCompiled<TSnoopedObjectType, TReturnType>(getMethod, func);
105105

106106
return accessor;
107107
}

sources/RevitDBExplorer/Domain/DataModel/MemberAccessors/MemberAccessorByFunc.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace RevitDBExplorer.Domain.DataModel.MemberAccessors
1010
{
11-
internal class MemberAccessorByFunc<TSnoopedObjectType, TReturnType> : MemberAccessorTyped<TSnoopedObjectType>
11+
internal class MemberAccessorByFunc<TSnoopedObjectType, TReturnType> : MemberAccessorTypedWithReadAndSnoop<TSnoopedObjectType>
1212
{
1313
private readonly Func<Document, TSnoopedObjectType, TReturnType> get;
1414
private readonly Func<Document, TSnoopedObjectType, IEnumerable<SnoopableObject>> snoop;

sources/RevitDBExplorer/Domain/DataModel/MemberAccessors/MemberAccessorByIteration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace RevitDBExplorer.Domain.DataModel.MemberAccessors
1212
{
13-
internal sealed class MemberAccessorByIteration<TSnoopedObjectType, TReturnType> : MemberAccessorTyped<TSnoopedObjectType>
13+
internal sealed class MemberAccessorByIteration<TSnoopedObjectType, TReturnType> : MemberAccessorTypedWithReadAndSnoop<TSnoopedObjectType>
1414
{
1515
private readonly string getMethodReturnTypeName;
1616
private readonly ParameterInfo getMethodParameter;

sources/RevitDBExplorer/Domain/DataModel/MemberAccessors/MemberAccessorByRef.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44
using Autodesk.Revit.DB;
55
using RevitDBExplorer.Domain.DataModel.Accessors;
66
using RevitDBExplorer.Domain.DataModel.ValueContainers.Base;
7+
using RevitDBExplorer.Domain.RevitDatabaseScripting;
78

89
// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md
910

1011
namespace RevitDBExplorer.Domain.DataModel.MemberAccessors
1112
{
12-
internal sealed class MemberAccessorByRef : MemberAccessorTyped<object>
13+
internal sealed class MemberAccessorByRef : MemberAccessorTypedWithReadAndSnoop<object>, IAccessorWithCodeGeneration
1314
{
14-
private readonly MethodInfo getMethod;
15-
private readonly MethodInfo setMethod;
15+
private readonly MethodInfo getMethod;
1616

1717

18-
public MemberAccessorByRef(MethodInfo getMethod, MethodInfo setMethod)
18+
public MemberAccessorByRef(MethodInfo getMethod)
1919
{
20-
this.getMethod = getMethod;
21-
this.setMethod = setMethod;
20+
this.getMethod = getMethod;
2221
}
2322

2423

@@ -80,6 +79,12 @@ private object[] ResolveArguments(ParameterInfo[] paramsDef, Document doc, objec
8079
}
8180
}
8281
return args;
83-
}
82+
}
83+
84+
85+
public string GenerateInvocationForScript()
86+
{
87+
return new MemberInvocation_Template().Evaluate(getMethod, null);
88+
}
8489
}
8590
}

sources/RevitDBExplorer/Domain/DataModel/MemberAccessors/MemberAccessorByRefCompiled.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Reflection;
34
using RevitDBExplorer.Domain.DataModel.Accessors;
45
using RevitDBExplorer.Domain.DataModel.ValueContainers.Base;
6+
using RevitDBExplorer.Domain.RevitDatabaseScripting;
57

68
// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md
79

810
namespace RevitDBExplorer.Domain.DataModel.MemberAccessors
911
{
10-
internal sealed class MemberAccessorByRefCompiled<TSnoopedObjectType, TReturnType> : MemberAccessorTyped<TSnoopedObjectType>
12+
internal sealed class MemberAccessorByRefCompiled<TSnoopedObjectType, TReturnType> : MemberAccessorTypedWithReadAndSnoop<TSnoopedObjectType>, IAccessorWithCodeGeneration
1113
{
14+
private readonly MethodInfo getMethod;
1215
private readonly Func<TSnoopedObjectType, TReturnType> func;
1316

14-
public MemberAccessorByRefCompiled(Func<TSnoopedObjectType, TReturnType> func)
17+
public MemberAccessorByRefCompiled(MethodInfo getMethod, Func<TSnoopedObjectType, TReturnType> func)
1518
{
19+
this.getMethod = getMethod;
1620
this.func = func;
1721
}
1822

@@ -28,5 +32,11 @@ public override IEnumerable<SnoopableObject> Snoop(SnoopableContext context, TSn
2832
{
2933
return state.Snoop();
3034
}
35+
36+
37+
public string GenerateInvocationForScript()
38+
{
39+
return new MemberInvocation_Template().Evaluate(getMethod, null);
40+
}
3141
}
3242
}

sources/RevitDBExplorer/Domain/DataModel/MemberAccessors/MemberAccessorByType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace RevitDBExplorer.Domain.DataModel.MemberAccessors
1111
{
12-
internal abstract class MemberAccessorByType<TSnoopedObjectType> : MemberAccessorTyped<TSnoopedObjectType> where TSnoopedObjectType : class
12+
internal abstract class MemberAccessorByType<TSnoopedObjectType> : MemberAccessorTypedWithReadAndSnoop<TSnoopedObjectType> where TSnoopedObjectType : class
1313
{
1414
public sealed override ReadResult Read(SnoopableContext context, TSnoopedObjectType @object)
1515
{

sources/RevitDBExplorer/Domain/DataModel/MemberAccessors/MemberAccessorFactory.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,28 @@ private static IEnumerable<T> GetAllInstancesThatImplement<T>() where T : class
3737
var instances = types.Select(x => Activator.CreateInstance(x) as T);
3838
return instances;
3939
}
40-
4140

42-
41+
42+
4343
public static IAccessor CreateMemberAccessor(MethodInfo getMethod, MethodInfo setMethod)
4444
{
45-
if (getMethod.ReturnType == typeof(void) && getMethod.Name != "GetOverridableHookParameters") return null;
45+
var memberAccessor = CreateMemberAccessor(getMethod);
46+
memberAccessor.UniqueId = getMethod.GetUniqueId();
47+
memberAccessor.DefaultInvocation = getMethod.GenerateInvocation();
48+
return memberAccessor;
49+
}
50+
51+
private static IAccessor CreateMemberAccessor(MethodInfo getMethod)
52+
{
53+
if (getMethod.IsStatic)
54+
{
55+
return new MemberAccessorForStatic(getMethod);
56+
}
57+
58+
if (getMethod.ReturnType == typeof(void) && getMethod.Name != "GetOverridableHookParameters")
59+
{
60+
return new MemberAccessorForNotExposed(getMethod);
61+
}
4662

4763
if (forTypeMembers.TryGetValue(getMethod.GetUniqueId(), out Func<IAccessor> factory))
4864
{
@@ -69,10 +85,10 @@ public static IAccessor CreateMemberAccessor(MethodInfo getMethod, MethodInfo se
6985
}
7086
if (@params.All(x => MemberAccessorByRef.HandledParameterTypes.Contains(x.ParameterType)))
7187
{
72-
return new MemberAccessorByRef(getMethod, setMethod);
88+
return new MemberAccessorByRef(getMethod);
7389
}
7490

75-
return null;
91+
return new MemberAccessorForNotExposed(getMethod);
7692
}
7793
}
7894

0 commit comments

Comments
 (0)