[ Pobierz całość w formacie PDF ]
.SWIG supports thefundamental C data types, enums, and #defined constant values.Variables of complex or user-definedtypes are automatically mapped to a pair of get/set accessor functions.PointersEvery pointer is treated as a void * by default, regardless of whether it is a char** or Matrix* or double***.This strategy works especially well for user-defined types, because most C libraries don't expect youto dereference such pointers.For example, fopen returns a FILE *, which is simply handed over tofread() and fwrite().In Perl, this pointer is available as a scalar, and Perl doesn't have to know whether thepointer refers to an array, structure, or a typedef.On the other hand, if you want a Vector * to a list ofinteger-valued scalars, you will have to help SWIG out by providing a typemap.TypemapsNot every data type is a simple conversion from Perl to C or vice versa.SWIG (like xsubpp) provides away for you to write arbitrary transformations, such as converting a Perl array to a 10-by-10 matrix.Towrite a typemap, you need to know Perl's API for accessing its internal data types, so we'll cover thistopic in the section "SWIG Typemaps" in Chapter 20.Typemaps can be applied not just to functionparameters, but also to structure members and global variables.You can also optionally create namedtypemaps, which apply to specific named entities (function arguments, variable names, function names),instead of all entities of that type.ArraysBoth simple arrays (vector[100]) and multidimensional arrays (vector[10][10]) are mapped to a simplepointer (vector *).Typemap support for arrays exists, but there are still a number of thorny issues forwhich SWIG cannot provide a general solution; please read the SWIG documentation for details.Structures and C++ classesSWIG automatically creates accessor functions for each member of a structure or class defined in theinterface file.As with the other facilities, these declarations cannot have the full generality of a Cstructure or a C++ class, but they are powerful enough for handling most common interface issues.Methods SWIG provides constructor and destructor procedures, which allow you to allocate and free C structuresfrom Perl space.You can convert basic C structures to objects in Perl space with a primitive called%addmethods.Ordinary functionsSWIG creates function wrappers that look pretty similar to their C equivalents.Each parameter can beoptionally typemapped, but since a typemap provides a translation in isolation (from other parameters),the number of parameters cannot be changed.This is not a constraint in XS.In other words, with SWIG you cannot map the C functionchar ** permute(char *string); // returns permutations of stringto@array = permute ($str);because one parameter, char**, needs conversion to a variable number of scalars (to be assigned to@array).You can instead write a typemap to convert the char** to an array and return its reference, so inPerl space, it is accessible this way:$rarray = permute ($str);print join(' ', @$rarray);Of course, you can always write a wrapper Perl function and insert it in the.pm file created automaticallyby SWIG:sub fancy_permute {@{permute($_[0])}; # dereferences array}Default and optional parametersParameters can have default values but, as in C++, can be applied only to the rightmost parameters.Thisis how you specify the function signature in the interface file:draw_mandel (file,width,height,orig_real,orig_imag,range,depth=30);This allows you to optionally skip the last parameter when calling from Perl.Centralized exception handlingSWIG provides a %except directive to wrap all external library calls inside a generic exception handler.This way you can trap all user-defined errors and C++ exceptions in one central place and translate theminto Perl exceptions.Please see the SWIG documentation for examples.Shadow classesSWIG optionally creates wrapper Perl code that allows you to access member attributes and functions ofC or C++ objects using the Perl hash notation, $person->{age}.This mechanism is built on top of theattribute accessor functions mentioned earlier.Nested structuresAn embedded structure gets the same treatment as an outermost structure - accessor functions and supportfrom shadow classes.The following interface file shows an example of using classes, accessing methods, and creating shadow classes:%module Graphics class Shape {public:int x, y; // originint w, h; // width, ht (defines bounding area)draw();};class Polygon : public Shape {public:Polygon(int x, int y, int w, int h);draw();};We invoke SWIG with the -c++ option, since it is not enabled by default, and the -shadow option for creatingshadow classes:% swig -c++ -shadow Graphics [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • andsol.htw.pl