From c478c13cf03fe993bb0affa5f462b664da046675 Mon Sep 17 00:00:00 2001 From: Logan Johnson Date: Wed, 30 Apr 2003 22:22:11 +0000 Subject: [PATCH] Various and sundry updates for the Pike module. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4758 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Doc/Manual/Pike.html | 94 +++++++++++++++++++++++++++------ Examples/pike/check.list | 1 + Examples/pike/class/example.cxx | 10 ++-- Examples/pike/class/example.h | 12 ++--- Examples/pike/class/runme.pike | 36 +++++-------- Source/Modules/pike.cxx | 53 +++++++++++++++---- 6 files changed, 147 insertions(+), 59 deletions(-) diff --git a/Doc/Manual/Pike.html b/Doc/Manual/Pike.html index c258c0a23..d1ace2050 100644 --- a/Doc/Manual/Pike.html +++ b/Doc/Manual/Pike.html @@ -1,13 +1,33 @@ - SWIG and Pike - +SWIG and Pike - -

SWIG and Pike
-

+ + +

5 SWIG and Pike

+ + + + + + This chapter describes SWIG support for Pike. As of this writing, the SWIG Pike module is still under development and is not considered ready for prime time. The Pike module is being developed against the @@ -19,8 +39,12 @@ are covered in less depth than in earlier chapters. At the very least, make sure you read the "SWIG Basics" chapter.
-

Preliminaries

-

Running SWIG

+

5.1 Preliminaries

+ + +

5.1.1 Running SWIG

+ + Suppose that you defined a SWIG module such as the following:
%module example

%{
#include "example.h"
%}

int fact(int n);
@@ -46,7 +70,9 @@ can use the -o option:
$ swig -pike -o pseudonym.c example.i
-

Getting the right header files

+

5.1.2 Getting the right header files

+ + In order to compile the C/C++ wrappers, the compiler needs to know the path to the Pike header files. These files are usually contained in a directory such as @@ -59,7 +85,9 @@ location of these files, so you may need to hunt around for them. You're looking for files with the names global.h, program.h and so on. -

Using your module

+

5.1.3 Using your module

+ + To use your module, simply use Pike's import statement:
@@ -70,16 +98,21 @@ Pike v7.4 release 10 running Hilfe v3.5 (Incremental Pike Frontend)
 (1) Result: 24
 
-

Basic C/C++ Mapping

+

5.2 Basic C/C++ Mapping

+ + +

5.2.1 Modules

+ -

Modules

All of the code for a given SWIG module is wrapped into a single Pike module. Since the name of the shared library that implements your module ultimately determines the module's name (as far as Pike is concerned), SWIG's %module directive doesn't really have any significance. -

Functions

+

5.2.2 Functions

+ + Global functions are wrapped as new Pike built-in functions. For example, @@ -98,7 +131,8 @@ exactly as you'd expect it to: (1) Result: 24
-

Global variables

+

5.2.3 Global variables

+ Global variables are currently wrapped as a pair of of functions, one to get the current value of the variable and another to set it. For example, the @@ -122,8 +156,38 @@ will result in two functions, Foo_get() and Foo_set(): (3) Result: 3.141590 -

Constants and enumerated types

+

5.2.4 Constants and enumerated types

+ Enumerated types in C/C++ declarations are wrapped as Pike constants, not as Pike enums. +

5.2.5 Constructors and Destructors

+ + +Constructors are wrapped as create() methods, and destructors are +wrapped as destroy() methods, for Pike classes. + +

5.2.6 Static Members

+ + +Since Pike doesn't support static methods or data for Pike classes, static +member functions in your C++ classes are wrapped as regular functions and +static member variables are wrapped as pairs of functions (one to get the +value of the static member variable, and another to set it). The names of +these functions are prepended with the name of the class. +For example, given this C++ class declaration: + +
+class Shape
+{
+public:
+    static void print();
+    static int nshapes;
+};
+
+ +SWIG will generate a Shape_print() method that invokes the static +Shape::print() member function, as well as a pair of methods, +Shape_nshapes_get() and Shape_nshapes_set(), to get and set +the value of Shape::nshapes. \ No newline at end of file diff --git a/Examples/pike/check.list b/Examples/pike/check.list index a335c5498..a8d348bf1 100644 --- a/Examples/pike/check.list +++ b/Examples/pike/check.list @@ -1,4 +1,5 @@ # see top-level Makefile.in +class constants enum simple diff --git a/Examples/pike/class/example.cxx b/Examples/pike/class/example.cxx index c1708aa90..c7a3194a4 100755 --- a/Examples/pike/class/example.cxx +++ b/Examples/pike/class/example.cxx @@ -11,7 +11,6 @@ int Shape::nshapes = 0; // Constructor Shape::Shape() { - // printf("Shape::Shape(), this = 0x%08x\n", this); nshapes++; } @@ -23,26 +22,25 @@ void Shape::move(double dx, double dy) { // Destructor Shape::~Shape() { - // printf("Shape::~Shape(), this = 0x%08x\n", this); nshapes--; } // Circle area -double Circle::area() { +double Circle::area() const { return M_PI*radius*radius; } // Circle perimeter -double Circle::perimeter() { +double Circle::perimeter() const { return 2*M_PI*radius; } // Square area -double Square::area() { +double Square::area() const { return width*width; } // Square perimeter -double Square::perimeter() { +double Square::perimeter() const { return 4*width; } diff --git a/Examples/pike/class/example.h b/Examples/pike/class/example.h index 6f5ac06d9..f74a4fefc 100755 --- a/Examples/pike/class/example.h +++ b/Examples/pike/class/example.h @@ -6,8 +6,8 @@ public: virtual ~Shape(); double x, y; void move(double dx, double dy); - virtual double area(void) = 0; - virtual double perimeter(void) = 0; + virtual double area() const = 0; + virtual double perimeter() const = 0; static int nshapes; }; @@ -16,8 +16,8 @@ private: double radius; public: Circle(double r) : radius(r) { }; - virtual double area(void); - virtual double perimeter(void); + virtual double area() const; + virtual double perimeter() const; }; class Square : public Shape { @@ -25,8 +25,8 @@ private: double width; public: Square(double w) : width(w) { }; - virtual double area(void); - virtual double perimeter(void); + virtual double area() const; + virtual double perimeter() const; }; diff --git a/Examples/pike/class/runme.pike b/Examples/pike/class/runme.pike index dd334accb..a6377600e 100755 --- a/Examples/pike/class/runme.pike +++ b/Examples/pike/class/runme.pike @@ -6,55 +6,47 @@ int main() write("Creating some objects:\n"); Circle c = Circle(10.0); - // write(" Created circle " + (string) c + ".\n"); write(" Created circle.\n"); Square s = Square(10.0); - // write(" Created square " + (string) s + ".\n"); write(" Created square.\n"); // ----- Access a static member ----- - // write("\nA total of " + Shape->nshapes + " shapes were created\n"); + write("\nA total of " + Shape_nshapes_get() + " shapes were created\n"); // ----- Member data access ----- // Set the location of the object - // c->x = 20.0; - // c->y = 30.0; c->x_set(20.0); c->y_set(30.0); - // s->x = -10.0; - // s->y = 5.0; s->x_set(-10.0); s->y_set(5.0); write("\nHere is their current position:\n"); - // write(sprintf(" Circle = (%f, %f)\n", c->x, c->y)); - // write(sprintf(" Square = (%f, %f)\n", s->x, s->y)); - write(sprintf(" Circle = (%f, %f)\n", c->x_get(), c->y_get())); - write(sprintf(" Square = (%f, %f)\n", s->x_get(), s->y_get())); + write(" Circle = (%f, %f)\n", c->x_get(), c->y_get()); + write(" Square = (%f, %f)\n", s->x_get(), s->y_get()); // ----- Call some methods ----- write("\nHere are some properties of the shapes:\n"); write(" The circle:\n"); - write(sprintf(" area = %f.\n", c->area())); - write(sprintf(" perimeter = %f.\n", c->perimeter())); - // write(" " + (string) s + ".\n"); + write(" area = %f.\n", c->area()); + write(" perimeter = %f.\n", c->perimeter()); write(" The square:\n"); - write(sprintf(" area = %f.\n", s->area())); - write(sprintf(" perimeter = %f.\n", s->perimeter())); + write(" area = %f.\n", s->area()); + write(" perimeter = %f.\n", s->perimeter()); write("\nGuess I'll clean up now\n"); - // Note: this invokes the virtual destructor - // del c; - // del s; - - // s = 3; - // write(sprintf("%d shapes remain\n", Shape->nshapes)); + /* See if we can force 's' to be garbage-collected */ + s = 0; + + /* Now we should be down to only 1 shape */ + write("%d shapes remain\n", Shape_nshapes_get()); + + /* Done */ write("Goodbye\n"); return 0; diff --git a/Source/Modules/pike.cxx b/Source/Modules/pike.cxx index 86559bef3..062827d68 100644 --- a/Source/Modules/pike.cxx +++ b/Source/Modules/pike.cxx @@ -9,6 +9,13 @@ * struct svalue), we're just calling the appropriate push_XXX * (e.g. push_int) to push the return value onto the stack. * + * - Pike classes can't have static member functions or data, so we need + * to find some other appropriate mapping for C++ static member functions + * and data. + * + * - Pike doesn't seem to provide any default way to print the memory + * address, etc. for extension objects. Should we do something here? + * ***********************************************************************/ char cvsroot_pike_cxx[] = "$Header$"; @@ -32,6 +39,8 @@ private: File *f_header; File *f_wrappers; File *f_init; + File *f_classInit; + String *PrefixPlusUnderscore; int current; @@ -60,6 +69,7 @@ public: f_header = 0; f_wrappers = 0; f_init = 0; + f_classInit = 0; PrefixPlusUnderscore = 0; current = NO_CPP; } @@ -118,6 +128,7 @@ public: SWIG_exit(EXIT_FAILURE); } f_init = NewString(""); + f_classInit = NewString(""); f_header = NewString(""); f_wrappers = NewString(""); @@ -126,6 +137,7 @@ public: Swig_register_filebyname("wrapper", f_wrappers); Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("init", f_init); + Swig_register_filebyname("classInit", f_classInit); /* Standard stuff for the SWIG runtime section */ Swig_banner(f_runtime); @@ -154,9 +166,12 @@ public: Dump(f_header, f_runtime); Dump(f_wrappers, f_runtime); Wrapper_pretty_print(f_init, f_runtime); + Delete(f_header); Delete(f_wrappers); Delete(f_init); + Delete(f_classInit); + Close(f_runtime); Delete(f_runtime); @@ -221,12 +236,28 @@ public: void add_method(Node *n, const DOHString_or_char *name, const DOHString_or_char *function, const DOHString_or_char *description) { String *rename; - if (current != NO_CPP) { - rename = strip(name); - } else { - rename = NewString(name); + switch (current) { + case NO_CPP: + rename = NewString(name); + Printf(f_init, "ADD_FUNCTION(\"%s\", %s, tFunc(%s), 0);\n", rename, function, description); + break; + case STATIC_FUNC: + case STATIC_VAR: + rename = NewString(name); + Printf(f_init, "ADD_FUNCTION(\"%s\", %s, tFunc(%s), 0);\n", rename, function, description); + break; + case CONSTRUCTOR: + case DESTRUCTOR: + case MEMBER_FUNC: + case MEMBER_VAR: + rename = strip(name); + Printf(f_classInit, "ADD_FUNCTION(\"%s\", %s, tFunc(%s), 0);\n", rename, function, description); + break; + case CLASS_CONST: + assert(false); // shouldn't have gotten here for CLASS_CONST nodes + default: + assert(false); // what is this? } - Printf(f_init, "ADD_FUNCTION(\"%s\", %s, tFunc(%s), 0);\n", rename, function, description); Delete(rename); } @@ -606,7 +637,7 @@ public: PrefixPlusUnderscore = NewStringf("%s_", getClassPrefix()); - Printf(f_init, "start_new_program();\n"); + Printf(f_classInit, "start_new_program();\n"); /* Handle inheritance */ List *baselist = Getattr(n,"bases"); @@ -621,13 +652,13 @@ public: SwigType_add_pointer(basetype); SwigType_remember(basetype); String *basemangle = SwigType_manglestr(basetype); - Printf(f_init, "low_inherit((struct program *) SWIGTYPE%s->clientdata, 0, 0, 0, 0, 0);\n", basemangle); + Printf(f_classInit, "low_inherit((struct program *) SWIGTYPE%s->clientdata, 0, 0, 0, 0, 0);\n", basemangle); Delete(basemangle); Delete(basetype); base = Nextitem(baselist); } } else { - Printf(f_init, "ADD_STORAGE(swig_object_wrapper);\n"); + Printf(f_classInit, "ADD_STORAGE(swig_object_wrapper);\n"); } Language::classHandler(n); @@ -640,8 +671,10 @@ public: } */ - /* Done, close the class */ - Printf(f_init, "add_program_constant(\"%s\", pr = end_program(), 0);\n", symname); + /* Done, close the class and dump its definition to the init function */ + Printf(f_classInit, "add_program_constant(\"%s\", pr = end_program(), 0);\n", symname); + Dump(f_classInit, f_init); + Clear(f_classInit); SwigType *tt = NewString(symname); SwigType_add_pointer(tt);