Skip to content

Instantly share code, notes, and snippets.

@Blecki
Created April 16, 2015 01:57
Show Gist options
  • Save Blecki/19684eaba0aaed1de2c1 to your computer and use it in GitHub Desktop.
Save Blecki/19684eaba0aaed1de2c1 to your computer and use it in GitHub Desktop.
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#@ include file="RuleGen.t4" #>
//This is generated code. Do not modify this file; modify the template that produces it.
using System;
namespace RMUD
{
public class RuleBuilder<TR>
{
public Rule<TR> Rule;
//The associated object is set by the MudObject rule factory functions and used to
// implement useful shortcuts on the rule builder types.
public MudObject AssociatedObject = null;
public RuleBuilder<TR> When(Func<bool> Clause)
{
if (Rule.WhenClause != null)
{
var oldClause = Rule.WhenClause;
Rule.WhenClause = RuleDelegateWrapper<bool>.MakeWrapper(
new Func<bool>(() => {
return oldClause.Invoke(null) && Clause();
})
);
}
else
Rule.WhenClause = RuleDelegateWrapper<bool>.MakeWrapper(Clause);
return this;
}
public RuleBuilder<TR> Do(Func<TR> Clause)
{
Rule.BodyClause = RuleDelegateWrapper<TR>.MakeWrapper(Clause);
return this;
}
public RuleBuilder<TR> Name(String Name)
{
Rule.DescriptiveName = Name;
return this;
}
public RuleBuilder<TR> ID(String ID)
{
Rule.ID = ID;
return this;
}
public RuleBuilder<TR> First {
get {
Rule.Priority = RulePriority.First;
return this;
}}
public RuleBuilder<TR> Last {
get {
Rule.Priority = RulePriority.Last;
return this;
}}
public RuleBuilder<TR> Associate(MudObject Object)
{
this.AssociatedObject = Object;
return this;
}
private class WrappedBoolean { public bool Value = false; }
public RuleBuilder<TR> FirstTimeOnly()
{
var beenCalled = new WrappedBoolean();
return this.When(() =>
{
if (beenCalled.Value) return false;
beenCalled.Value = true;
return true;
});
}
}
<#for (var i = 1; i < 5; ++i){#>
public class RuleBuilder<<# Args(i); #>, TR>
{
public Rule<TR> Rule;
//The associated object is set by the MudObject rule factory functions and used to
// implement useful shortcuts on the rule builder types.
public MudObject AssociatedObject = null;
public RuleBuilder<<#Args(i);#>, TR> When(Func<<#Args(i);#>, bool> Clause)
{
if (Rule.WhenClause != null)
{
var oldClause = Rule.WhenClause;
Rule.WhenClause = RuleDelegateWrapper<bool>.MakeWrapper(
new Func<<#Args(i);#>, bool>((<#Parms(i);#>) => {
return oldClause.Invoke(new Object[]{<#Parms(i);#>}) && Clause(<#Parms(i);#>);
})
);
}
else
Rule.WhenClause = RuleDelegateWrapper<bool>.MakeWrapper(Clause);
return this;
}
public RuleBuilder<<#Args(i);#>, TR> Do(Func<<#Args(i);#>, TR> Clause)
{
Rule.BodyClause = RuleDelegateWrapper<TR>.MakeWrapper(Clause);
return this;
}
public RuleBuilder<<#Args(i);#>, TR> Name(String Name)
{
Rule.DescriptiveName = Name;
return this;
}
public RuleBuilder<<#Args(i);#>, TR> ID(String ID)
{
Rule.ID = ID;
return this;
}
public RuleBuilder<<#Args(i);#>, TR> First {
get {
Rule.Priority = RulePriority.First;
return this;
}}
public RuleBuilder<<#Args(i);#>, TR> Last {
get {
Rule.Priority = RulePriority.Last;
return this;
}}
public RuleBuilder<<#Args(i);#>, TR> Associate(MudObject Object)
{
this.AssociatedObject = Object;
return this;
}
public RuleBuilder<<#Args(i);#>, TR> WhenThis()
{
if (AssociatedObject == null) throw new InvalidOperationException("WhenThis is only valid for rules declared on an object.");
return this.When((<#Parms(i);#>) =>
{
<#for (var x = 0; x < i; ++x){#>if (System.Object.ReferenceEquals(P<#=x#>, AssociatedObject)) return true;
<#}#>
return false;
});
}
private class WrappedBoolean { public bool Value = false; }
public RuleBuilder<<#Args(i);#>, TR> FirstTimeOnly()
{
var beenCalled = new WrappedBoolean();
return this.When((<#Parms(i);#>) =>
{
if (beenCalled.Value) return false;
beenCalled.Value = true;
return true;
});
}
}
<#}#>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment