Boost logo

Boost :

From: Anthony Liguori (anthony_at_[hidden])
Date: 2002-11-19 17:14:11


One thing has always bothered me about Loki's class factories (namely,
one paragraph in Modern C++ Design). The fact that virtual
constructions aren't considered possible in C++. Specifically, in
chapter 8.2 where Andrei gives an example of code that doesn't work in C++:

// Warning-this is NOT C++
// Assumes Class is a class that's also an object

Class Read(const char *filename);
Document *DocumentManager::OpenDocument(const char *filename)
{
    Class theClass = Read(filename);
    Document *pDoc = new theClass;
}

So I wrote up a class that provided virtual constructor functionality.
 It actually allows for types to be treated as objects including storing
types in any STL container. This allows for really advanced factory
algorithms and all sorts of fun stuff. The basic usage is as follows:

struct A
{
    A(int a);
};

struct B : public A
{
    B(int a);
};

typedef factory<A, int> FactoryA;

FactoryA b = FactoryA::create<B>();
FactoryA c;

A *a = b(10); // Returns new B(10)
c = b;
delete a;
a = c(15); // Returns new B(10)

The other interesting characteristic is that factories are compatiable
with functions so bind functions can be used. If a subclass has
additional constructor parameters that had reasonable defaults, those
parameters could be bind'ed which is something that no other factory
implementation is capable of.

I haven't submitted yet because it hasn't been updated to the new
function class and the documentation isn't finished but since the topic
came up, I thought I'd see what people thought.

Here's the site:
http://clam.rutgers.edu/~aliguori/factories/

Regards,
Anthony Liguori

David B. Held wrote:

> Christophe Meessen wrote:
>
>> [...]
>> Would there be any interrest in such thing for boost ? If there are
>> better solution I would be happy to know about it.
>
>
> How does your library compare to Loki's class factories? Consult
> Modern C++ Design for an explanation.
>
> Dave
>
>
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk