constructors vs factory functions (was Re: [Xapian-discuss] TCL Binding working, but db_lock doesn't get removed?)

Olly Betts olly at survex.com
Wed Dec 1 07:10:43 GMT 2004


On Sat, Nov 06, 2004 at 10:37:58AM -0800, Eric Parusel wrote:
> [SWIG Tcl8 not calling WritableDatabase destructor bug]
> I joined, and sent an email, but it never made it to the list...
> I emailed the list maintainer, and never got a reply.

Well, I reduced it down to a really simple testcase and sent it to the
swig list (took me 3 attempts to post - it seems to just throw away any
mail with attachments):

http://article.gmane.org/gmane.comp.programming.swig/5422

But I've not had a single reply, either on or off the list.  I guess
that this isn't going to get fixed in SWIG any time soon...

So I thought about how we could work around this.  The problem is that
Tcl8 doesn't cope with a factory function which returns an object.  But
we could give WritableDatabase a normal constructor instead of using a
factory function.

And in fact thinking about this, it's probably a more natural design
for the API user.

I'm trying to remember quite why I chose factory functions originally.
The change is here:

http://cvs.xapian.org/xapian/xapian-core/api/Attic/omdatabaseinternal.cc.diff?r1=1.27&r2=1.28

http://cvs.xapian.org/xapian/xapian-core/include/om/Attic/omdatabase.h.diff?r1=1.51&r2=1.52

Previously the database type and parameters were encapsulated in an
OmSettings object, and you passed this to an OmDatabase constructor.

I think the main advantage of using a factory function was that it
avoided trying to provide several constructors which wanted to have the
same parameters.

That's still valid reasoning, but we could add a constructor which
is equivalent to Xapian::Auto::open(), as that's what most users
will want most of the time.  If they explicitly want to use a
particular database type, or want to pass particular parameters,
then the specific factory functions such as Xapian::Quartz::open
can be used.

Does that seem sensible?  Xapian::Auto::open() can be left as a
(probably deprecated) alternative way of achieving the same thing.

An example will probably make this clearer.  Instead of:

    Xapian::WritableDatabase db = Xapian::Auto::open(path, Xapian::DB_CREATE);

you'd write:

    Xapian::WritableDatabase db(path, Xapian::DB_CREATE);

And instead of:

    Xapian::Database db;
    while (*++argv) {
        db.add_database(Xapian::Auto::open(*argv));
    }

you'd write:

    Xapian::Database db;
    while (*++argv) {
        db.add_database(Xapian::Database(*argv));
    }

Cheers,
    Olly



More information about the Xapian-discuss mailing list