2 Implement a more natural wrap for factory methods, for example, if
5 ---- geometry.h --------
12 virtual ~Geometry() {}
13 virtual int draw() = 0;
16 // Factory method for all the Geometry objects
18 static Geometry *create(GeomType i);
21 struct Point : Geometry {
22 int draw() { return 1; }
23 double width() { return 1.0; }
26 struct Circle : Geometry {
27 int draw() { return 2; }
28 double radius() { return 1.5; }
32 // Factory method for all the Geometry objects
34 Geometry *Geometry::create(GeomType type) {
36 case POINT: return new Point();
37 case CIRCLE: return new Circle();
41 ---- geometry.h --------
44 You can use the %factory with the Geometry::create method as follows:
46 %newobject Geometry::create;
47 %factory(Geometry *Geometry::create, Point, Circle);
50 and Geometry::create will return a 'Point' or 'Circle' instance
51 instead of the plain 'Geometry' type. For example, in python:
53 circle = Geometry.create(Geometry.CIRCLE)
56 where circle is a Circle proxy instance.
58 NOTES: remember to fully qualify all the type names and don't
59 use %factory inside a namespace declaration, ie, instead of
62 %factory(Geometry *Geometry::create, Point, Circle);
67 %factory(Foo::Geometry *Foo::Geometry::create, Foo::Point, Foo::Circle);
72 /* for loop for macro with one argument */
73 %define %_formacro_1(macro, arg1,...)macro(arg1)
74 #if #__VA_ARGS__ != "__fordone__"
75 %_formacro_1(macro, __VA_ARGS__)
79 /* for loop for macro with one argument */
80 %define %formacro_1(macro,...)%_formacro_1(macro,__VA_ARGS__,__fordone__)%enddef
81 %define %formacro(macro,...)%_formacro_1(macro,__VA_ARGS__,__fordone__)%enddef
83 /* for loop for macro with two arguments */
84 %define %_formacro_2(macro, arg1, arg2, ...)macro(arg1, arg2)
85 #if #__VA_ARGS__ != "__fordone__"
86 %_formacro_2(macro, __VA_ARGS__)
90 /* for loop for macro with two arguments */
91 %define %formacro_2(macro,...)%_formacro_2(macro, __VA_ARGS__, __fordone__)%enddef
93 %define %_factory_dispatch(Type)
95 Type *dobj = dynamic_cast<Type *>($1);
98 SWIG_SetPointerZval(return_value, SWIG_as_voidptr(dobj),$descriptor(Type *), $owner);
102 %define %factory(Method,Types...)
103 %typemap(out) Method {
105 %formacro(%_factory_dispatch, Types)
107 SWIG_SetPointerZval(return_value, SWIG_as_voidptr($1),$descriptor, $owner);