On Wed, 24 May 2000, Peter Haworth wrote:

> Jeffrey W. Baker wrote:
> > I have read all of the messages in regarding the zygotic
> > HTML::Forms/FormGen project, and I like the idea.  However, I hope that
> > the inplementation of such a beast isn't in Perl.  To ensure that a
> > quality product results, I propose that this library be written in C, and
> > also in an object-oriented fashion.
> > 
> > What I propose is an object interface to the various form input types, as
> > well as some higher level functionality that encapsulated what people
> > frequently do with HTML forms.  For example, I would expect the C
> > interface to look something like this:
> > 
> > myInput = hfTextInputNew();
> > myInput->addAttr(myInput, "name", "first_name");
> > myInput->addAttr(myInput, "value", "Jeffrey");
> > printf("%s\n", myInput->render(myInput));
> > myInput->destroy(myInput);
> > 
> > I would expect a similar interface with more methods for a select box,
> > e.g. mySelect->addOption(mySelect, myOption), and myOption->select.
> 
> Where do you store those attributes before you do the rendering? If you tie
> yourself to Apache, you could use tables. If you tie yourself to perl, you can
> use hashes. Otherwise you have to do your own hashes. Blech, especially if you
> then use the library with Apache and/or perl - wasted effort.

Actually, there is another way, and that is to simply build a linked list
of attributes.  It doesn't matter what order you put the attributes in, so
I just add them to the head of the list for performance.  Adding to the
head of a list is faster than hashing anyway.  If you use the
setAttr(self *, char *, char *) method, the list has to be scanned, but is
is likely to be so short as to not matter.

The other advantage here is storage space.  An attribute in this
implementation takes up only 4*sizeof(void *) + strlen(name) +
strlen(value) + 2.  On 32-bit architectures, the attribute
name="myfield" only requires 29 bytes.  Only 45 bytes are required on
64-bit architectures.  Compare with Perl, where $hash{name} = 'myfield' is
costing several kilobytes at runtime.

> Bear in mind that it's nice to be able to support arbitrary attributes for
> things like onMouseClick and the like, horrible though they are. This means
> that you can't just define structs with members dedicated to specific
> attributes.

Done.  The same approach is also used for adding options to a select box,
although a select box will have an additional sort() method.

-jwb

Reply via email to