Saturday, December 25, 2010

Easier to Write Interpreters

This is like a Christmas gift for some, mostly for plugin writers. In short is just this: if you subclass (derive) from the new class: NaroDataInterpreter instead of AttributeDataInterpreter and you have just integer and double properties, they will be serialized/deserialized automatically via Reflection. Probably more automatic types will come but in case you had somelike simple like an store class for integers you will just have to do like this code:
public class IntegerInterpreter : NaroDataInterpreter
{
public int Value
{
get { return _value; }
set
{
_value = value;
OnModified();
}
}
private int _value;
}
Compared with (old code):
public class IntegerInterpreter : AttributeInterpreterBase
{
#region Properties

public int Value
{
get { return _value; }
set
{
_value = value;
OnModified();
}
}

public override void Serialize(AttributeData data)
{
data.WriteAttribute("Value", _value);
}

public override void Deserialize(AttributeData data)
{
Value = data.ReadAttributeInteger("Value");
}

private int _value;
}

Of course you can have other types/extra logic on serialize/deserialize and in those cases you will be able to override and to use the basic code for properties that you want and to add your custom code right in the same very place. This will benefit mostly the plugin writers that will be able to define custom elements inside of NaroCAD's document tree data.

No comments: