« Return to Thread: ChatterBean 00.007 is now available

Re: ChatterBean 00.007 is now available

by mehri :: Rate this Message:

Reply to Author | View in Thread

Problem is that from the xml I get the AIML tag name
as a String.  From the string I have to instantiate
the class with the same name.  With reflection this
can be done easily.  Without reflection it's painful.


For example, with C++ how can you do a:

String someClassName = "InnerTemplate";
return new someClassName;

I know of a few ways but none are elegant and all are
more complex then simply registering the classes in a
factory method:

Tag &tagClass createClass(String someClassName)
{
  if(someClassName == "InnerTemplate")
      return new InnerTemplate;
  else if(someClassName == "xxx")
      return new XXX
}

Right now I'm working on a dll layering scheme to
allow overriding or adding custom AIML tags.  The
creator of the dll will have to inherit from a factory
and write their own:

Tag &tagClass createClass(String someClassName)

method.


--- Helio Perroni Filho <xperroni@...> wrote:

> --- mehri <foreverlinux@...> escreveu:
>
> > //Boy I wish C++ had reflection :-(
> > Tag &createTag(String className)
> > {
> >    if(className == "category")
> >       return new Category;
> >    else if(className == "date")
> >       return new Date;
> >    else if // ... so on
> >
> > }
>
> Have you tried to use function pointers? For every
> tag, you would have a function responsible or
> returning a new instance of a given class, and then
> you could have a map collection mapping tag names to
> function pointers:
>
> // Include the library for map templates.
> #include <map>
>
> // ...
>
> /* Function pointer type for a tag-maker function.
> */
> typedef Tag& (*TagMaker)(void);
>
> /* Name-to-maker-function map. Must be filled
> somewhere else. */
> map<String, TagMaker> makers;
>
> // ...
>
> Tag &createTag(String className)
> {
>   /* Gets the function pointer from the map. */
>   TagMaker maker = makers[className];
>
>   return maker();
> }
>
> This might seem more complicated than Java
> reflection,
> but it is feasible, and for a lenghty set of
> possible
> tags, much more efficient than an if-then-else chain
> -- retrieving a value from the map takes a time of
> O(1) in assintotic notation, while going down a
> conditional chain takes O(n) in the worst case,
> where
> n is the length of the chain.
>
> --
> Ja mata ne.
> Helio Perroni Filho
>
>
>
>
>
>
>
>
>
>
_______________________________________________________
>
> Yahoo! doce lar. Fa?a do Yahoo! sua homepage.
> http://br.yahoo.com/homepageset.html 
>
> _______________________________________________
> alicebot-developer mailing list
> alicebot-developer@...
>
http://list.alicebot.org/mailman/listinfo/alicebot-developer
>


.................................................o' \,=./ `o
Mehri                                               (o o)
---=--=---=--=--=---=--=--=--=--=---=--=--=-----ooO--(_)--Ooo---

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
_______________________________________________
alicebot-developer mailing list
alicebot-developer@...
http://list.alicebot.org/mailman/listinfo/alicebot-developer

 « Return to Thread: ChatterBean 00.007 is now available

LightInTheBox - Buy quality products at wholesale price