Boost logo

Boost :

From: Edward Diener (eddielee_at_[hidden])
Date: 2004-12-05 17:11:39


Yes, I am interested in it. It appears to provide a way to do reflection on
a limited basis by using a name to create an object. Hajo Kirchhoff in his
LitWindow library also provides a way to do reflection based on names
although I do not believe he has the ability which you have to dynamically
create objects based on names. The general ideas that you both use,
providing a way to register information about types, seems similar. I am
interested, more for the ideas involved than for current practical use, in
any general mechanism which provides reflection capabilities, where a name
can be used to find out type information or variable information at
run-time, or where an object can be created based on its name at run-time.

The only nit I have to pick is calling your implementation a virtual
constructor lib. I do not see where polymorphism comes into play in the
actual construction of the objects from its name. Perhaps a "name
constructor lib" would be more accurate.

Roland Schwarz wrote:
> As a byproduct of my current work I factored out a small utility
> library that implements a virtual constructor.
>
> The implementation is based on boost::any and typeid(...).name().
> It features dynamic creation of objects by name (string), and allows
> for default and custom constructors beeing called.
>
> It could be used as the basic step to object deserialization from
> a file, creation of operating system dependent implementations,
> or allow a library to instantiate user-pluggable classes.
>
> The design is inspired by the Io_obj example from Stroustrup's book
> on C++ programming.
>
> A small example to show the usage:
>
> #include <string>
> #include <iostream>
> #include "dynanew.hpp"
>
> using namespace dynanew;
>
> // a simple class hierarchy with abstact base class
> struct shape {
> virtual void draw() = 0;
> };
>
> struct square : public shape {
> virtual void draw() {
> std::cout << "I am a square" << std::endl;
> }
> };
>
> struct circle : public shape {
> circle() : radius(0) {}
> circle(double dr) : radius(dr) {}
> virtual void draw() {
> std::cout << "I am a circle of radius: " << radius <<
> std::endl; }
> double radius;
> };
>
> // create the type map
> TYPE_MAP(shape);
> // square with default constructor
> TYPE_REGISTER(shape, square);
> // circle with custom constructor taking a double parameter
> TYPE_REGISTER1(shape, circle, double);
> // circle with default constructor
> TYPE_REGISTER(shape, circle);
>
> int main(int argc, char* argv[])
> {
> std::string type;
> type = "square";
> shape* ps1 = dynamic_new<shape>(type);
> shape* ps2 = dynamic_new<shape>("circle", 20.0);
> shape* ps3 = dynamic_new<shape>("circle");
> shape* ps4 = dynamic_new<shape>("square", 10.0);
>
> if (ps1) ps1->draw(); // prints: I am a square
> if (ps2) ps2->draw(); // prints: I am a circle of radius: 20.0
> if (ps3) ps3->draw(); // prints: I am a circle of radius: 0.0
> if (ps4) ps4->draw(); // does nothing since no ctor available
>
> delete ps1;
> delete ps2;
> delete ps3;
> delete ps4;
>
> return 0;
> }
>
> regards,
> Roland
>
>
> _______________________________________________
> 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