From: David Abrahams (dave_at_[hidden])
Date: 2002-11-19 21:51:04
Anthony Liguori <anthony_at_[hidden]> writes:
> 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;
Now can we rename "factory" to "metaclass"? I just want to see smoke
come out of peoples' ears!
> 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)
I sure hope that's new B(15).
> 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.
Very cool indeed!
> 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:
Well, I'm excited.
-- David Abrahams dave_at_[hidden] * http://www.boost-consulting.com Boost support, enhancements, training, and commercial distribution
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk