Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow constructing values from any source sequence #13

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Bonsai.Sgen/CSharpClassTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,34 @@ public override void BuildType(CodeTypeDeclaration type)
"Defer"),
new CodeSnippetExpression(
@$"() => System.Reactive.Linq.Observable.Return(new {Model.ClassName}(this))"))));

var genericTypeParameter = new CodeTypeParameter("TSource");
var genericSourceParameter = new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(IObservable<>))
{
TypeArguments = { new CodeTypeReference(genericTypeParameter) }
}, "source");
var genericProcessMethod = new CodeMemberMethod
{
Name = "Process",
Attributes = MemberAttributes.Public | MemberAttributes.Final,
TypeParameters = { genericTypeParameter },
Parameters = { genericSourceParameter },
ReturnType = new CodeTypeReference(typeof(IObservable<>))
{
TypeArguments = { new CodeTypeReference(Model.ClassName) }
}
};
genericProcessMethod.Statements.Add(new CodeMethodReturnStatement(
new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodeTypeReferenceExpression("System.Reactive.Linq.Observable"),
"Select"),
new CodeVariableReferenceExpression(genericSourceParameter.Name),
new CodeSnippetExpression(
@$"_ => new {Model.ClassName}(this)"))));

type.Members.Add(processMethod);
type.Members.Add(genericProcessMethod);
type.CustomAttributes.Add(new CodeAttributeDeclaration(
new CodeTypeReference("Bonsai.CombinatorAttribute")));
type.CustomAttributes.Add(new CodeAttributeDeclaration(
Expand Down