[stringtemplate-interest] IAttributeRenderer and Type Inheritancein C#

Jonathan Buhacoff jonathan at buhacoff.net
Thu Oct 22 10:22:07 PDT 2009


If it's integrated with the class renderers as I showed in my diff,   
there's a little extra work to do. On the other hand,  most classes  
only implement handful of interfaces, and since this code doesn't do  
any string processing it will probably be fast enough.

Or, if it's implemented as a separate map of renderers, then it will  
only slow down processing if the programmer has registered any  
interface renderers.  But that seems a little messy to me, maybe more  
messy than the speed savings is worth.

Finally, the other issue with interface renderers is priority -  as  
proposed now, we have  no control over which renderer will be found  
first, and if a class implements more than one interface with a  
registered renderer, the output will be non-deterministic.  I think  
that's a bad thing.

The way to control that would be to have a complete interface to  
manage the priority of interface renderers, or to have ST implement  
something like  
registerInterfaceAttributeRenderers(List<AttributeRenderer>  
renderers);     so the programmer can specify the order of the  
renderers.

So the loop would change to iterating through the registered interface  
renderers and checking if the value implements each one (and they  
would be in priority order).

That makes things deterministic again, and would require the use of a  
list of interface renders separate from the map of class renderers,  
which means only people who use interface renderers would incur any  
performance penalty.

I'm still trying to think of a case where this scheme would be better  
than wrapping everything with an adapter and registering a renderer  
for that, because automatically wrapping certain classes that  
implement an interface automatically is so easy to do by subclassing  
StringTemplate.   I'm still thinking along the lines of  
internationalization, units of measure, stuff like that.   Vincent can  
you provide a real world case?


On Oct 22, 2009, at 9:45 AM, Sam Harwell wrote:

> Hi Vincent,
>
> My first thought is this changes an O(1) algorithm that has to run for
> every rendered item into an O(n) algorithm. I would have to run this
> through the instrumenting profiler to make sure it isn't slowing down
> the rendering process before I could include it. There are a few other
> ways to approach the problem, but I'm not yet sure which one would  
> work
> out best.
>
> Sam
>
> -----Original Message-----
> From: stringtemplate-interest-bounces at antlr.org
> [mailto:stringtemplate-interest-bounces at antlr.org] On Behalf Of  
> Vincent
> DARON
> Sent: Thursday, October 22, 2009 10:12 AM
> To: stringtemplate-interest at antlr.org
> Subject: [stringtemplate-interest] IAttributeRenderer and Type
> Inheritancein C#
>
> Hi
>
> I would like to be able to register an IAttributeRenderer for objects
> implementing an Interface.
>
> Part of current implementation of GetAttributeRenderer
>
>        public virtual IAttributeRenderer GetAttributeRenderer( Type
> attributeClassType )
>        {
>            IAttributeRenderer renderer = null;
>            if ( _attributeRenderers != null )
>            {
>                if ( !_attributeRenderers.TryGetValue(
> attributeClassType, out renderer ) )
>                    renderer = null;
>            }
> // ...snip ... //
>        }
>
>
> My proposal (untested, but should be ok)
>
>        public virtual IAttributeRenderer GetAttributeRenderer( Type
> attributeClassType )
>        {
>            IAttributeRenderer renderer = null;
>            if ( _attributeRenderers != null )
>            {
> 		foreach(Type key in _attributeRenderers.Keys)
> 		{
> 			if(key.IsAssignableFrom(attributeClassType))
> 			{
> 				renderer = _attributeRenderers[key];
> 				break;
> 			}
> 		}
>            }
> // ...snip ... //
>        }
>
>
> It will allow to register Attribute Renderer for objects implementing
> interfaces
>
>    RegisterAttributeRenderer(typeof(IFormatProvider), ...);
>
> Goot idea ?
>
> Second question, is it possible to have access to source using Source
> Control (Subversion, Hg, Git, ...) ? It would be far easier to test  
> and
> create patches.
>
> Thanks for answer/comments
>
> Vincent
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest



More information about the stringtemplate-interest mailing list