summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSwati Sharma <itawswati@gmail.com>2012-07-23 19:26:45 +0000
committerSwati Sharma <itawswati@gmail.com>2012-07-23 19:26:45 +0000
commitaf1cca22cd734835d2cb7a991316f4fab771fd03 (patch)
treecd99302dfd695b73895c00c65e9015748502d0d1
parentc7b506796fdb82b07cfa1a7fc0f721792edada1a (diff)
downloadswig-af1cca22cd734835d2cb7a991316f4fab771fd03.tar.gz
Changed Examples runme.m files for Refactored Objective C Module
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-objc@13393 626c5289-ae23-0410-ae9c-e8d60b6d4f22
-rw-r--r--Examples/objc/callback/runme.m31
-rw-r--r--Examples/objc/check.list4
-rw-r--r--Examples/objc/class/runme.m68
-rw-r--r--Examples/objc/constants/example.i2
-rw-r--r--Examples/objc/enum/example.i2
-rw-r--r--Examples/objc/enum/runme.m16
-rw-r--r--Examples/objc/funcptr/runme.m20
-rw-r--r--Examples/objc/pointer/example.i26
-rw-r--r--Examples/objc/pointer/runme.m47
-rw-r--r--Examples/objc/reference/runme.m81
-rw-r--r--Examples/objc/simple/runme.m17
-rw-r--r--Examples/objc/templates/runme.m58
-rw-r--r--Examples/objc/variables/example.c12
-rw-r--r--Examples/objc/variables/example.h2
-rw-r--r--Examples/objc/variables/example.i9
-rw-r--r--Examples/objc/variables/runme.m37
16 files changed, 303 insertions, 129 deletions
diff --git a/Examples/objc/callback/runme.m b/Examples/objc/callback/runme.m
index 353e761fd..01d427178 100644
--- a/Examples/objc/callback/runme.m
+++ b/Examples/objc/callback/runme.m
@@ -1,15 +1,40 @@
#import "example_proxy.h"
+@interface ObjCCallback: Callback
+- (id)init;
+- (void)run;
+@end
+@implementation ObjCCallback
+- (id)init {
+ self = [super init];
+ return self;
+}
+- (void)run {
+ NSLog(@"ObjCCallback run()");
+}
+@end
int main(int argc, char* argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- Caller *caller=[[Caller alloc]init];
+ NSLog(@"Adding and calling a normal C++ callback\n");
+ NSLog(@"----------------------------------------\n");
+
+ Caller *caller=[[Caller alloc]init];
Callback *callback=[[Callback alloc]init];
[caller setCallback:callback];
[caller call];
[caller delCallback];
-
- [callback release];
+
+ NSLog(@"\n");
+ NSLog(@"Adding and calling an ObjectiveC callback\n");
+ NSLog(@"----------------------------------------\n");
+
+ Callback *objcallback=[[ObjCCallback alloc] init];
+ [caller setCallback:objcallback];
+ [caller call];
+ [caller delCallback];
+
+ [caller release];
[pool drain];
return 0;
diff --git a/Examples/objc/check.list b/Examples/objc/check.list
index dd5dd8454..6b9472171 100644
--- a/Examples/objc/check.list
+++ b/Examples/objc/check.list
@@ -3,9 +3,7 @@ callback
class
constants
enum
-#extend
-#funcptr
-#native
+funcptr
pointer
reference
simple
diff --git a/Examples/objc/class/runme.m b/Examples/objc/class/runme.m
index 4e8704131..e51a2e8bb 100644
--- a/Examples/objc/class/runme.m
+++ b/Examples/objc/class/runme.m
@@ -2,16 +2,60 @@
#import "example_proxy.h"
int main(int argc, char* argv[]) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- Circle *c = [[Circle alloc] initWithR: 2.0];
- NSLog(@"Area of circle is %f\n", [c area]);
- [c move:4 dy:5];
-
- Square *s = [[Square alloc] initWithW: 3.0];
- NSLog(@"Perimiter of the square is %f\n", [s perimeter]);
- NSLog(@"Number of shapes created = %d", [Shape getNshapes]);
- [c release];
- [s release];
- [pool release];
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+ // ----- Object creation -----
+ NSLog(@"Creating some objects:" );
+ Circle *c = [[Circle alloc] initWithR: 10];
+ NSLog(@"Created circle %@", c );
+
+ Square *s = [[Square alloc] initWithW: 10];
+ NSLog(@"Created square %@", s );
+
+ // ----- Access a static member -----
+ NSLog(@"\nA total of %d shapes were created", [Shape getNshapes]);
+
+
+ // ----- Member data access -----
+ // Notice how we can do this using functions specific to
+ // the 'Circle' class.
+ [c setX: 20];
+ [c setY: 30];
+
+
+ // Now use the same functions in the base class
+ Shape *shape = s;
+ [shape setX: -10];
+ [shape setY: 5];
+
+ NSLog(@"\nHere is their current position:" );
+ NSLog(@"Circle = (%f, %f)",[c getX], [c getY]);
+ NSLog(@"Square = (%f, %f)",[s getX], [s getY]);
+
+ // ----- Call some methods -----
+ NSLog(@"\nHere are some properties of the shapes:" );
+ NSMutableArray *shapes = [[NSMutableArray alloc] init];
+ [shapes addObject: c];
+ [shapes addObject: s];
+
+ // Notice how the area() and perimeter() functions really
+ // invoke the appropriate virtual method on each object.
+ for (Shape* obj in shapes)
+ {
+ NSLog(@"Shape: %@", obj);
+ NSLog(@"area = %f", [obj area]);
+ NSLog(@"perimeter = %f", [obj perimeter]);
+ }
+
+ // ----- Delete everything -----
+ // Note: this invokes the virtual destructor
+ // You could leave this to the garbage collector
+ [c release];
+ [s release];
+ NSLog(@"%d shapes remain", [Shape getNshapes]);
+
+ [shapes release];
+ [pool release];
return 0;
-}
+ }
+
diff --git a/Examples/objc/constants/example.i b/Examples/objc/constants/example.i
index ce03afe29..5ddb1f43f 100644
--- a/Examples/objc/constants/example.i
+++ b/Examples/objc/constants/example.i
@@ -2,7 +2,7 @@
%module example
/* Force the generated Objective-C code to use the C constant values rather than making an intermediate call */
-%objcconst(2);
+//%objcconst(2);
/* A few preprocessor macros */
diff --git a/Examples/objc/enum/example.i b/Examples/objc/enum/example.i
index fb3f636a6..dab9237dd 100644
--- a/Examples/objc/enum/example.i
+++ b/Examples/objc/enum/example.i
@@ -6,7 +6,7 @@
%}
/* Force the generated Objective-C code to use the C enum values rather than making a method call */
-%objcconst(1);
+//%objcconst(1);
/* Let's just grab the original header file here */
diff --git a/Examples/objc/enum/runme.m b/Examples/objc/enum/runme.m
index 91b25caa1..73429839f 100644
--- a/Examples/objc/enum/runme.m
+++ b/Examples/objc/enum/runme.m
@@ -13,18 +13,18 @@ int main(int argc, char* argv[]) {
NSLog(@"speed::WARP =%d", WARP);
NSLog(@"speed::LUDICROUS =%d", LUDICROUS);
- NSLog(@"\nTesting use of enums with functions\n");
-
- enum_test(RED, IMPULSE);
- enum_test(BLUE, WARP);
- enum_test(GREEN, LUDICROUS);
-
+ NSLog(@"\nTesting use of enums with global function\n");
+ ObjcEnum_test(RED, IMPULSE);
+ ObjcEnum_test(BLUE, WARP);
+ ObjcEnum_test(GREEN, LUDICROUS);
+
NSLog(@"\nTesting use of enum with class method" );
Foo* f = [[Foo alloc] init];
[f enum_test:IMPULSE];
[f enum_test:WARP];
[f enum_test:LUDICROUS];
- [f release];
- return 0;
+ [f release];
+
+ return 0;
}
diff --git a/Examples/objc/funcptr/runme.m b/Examples/objc/funcptr/runme.m
index 42d40be3e..a6875bb81 100644
--- a/Examples/objc/funcptr/runme.m
+++ b/Examples/objc/funcptr/runme.m
@@ -2,5 +2,21 @@
#import "example_proxy.h"
int main(int argc, char* argv[]) {
- return 0;
-} \ No newline at end of file
+
+ int a = 37;
+ int b = 42;
+
+ // Now call our C function with a bunch of callbacks
+ NSLog(@"Trying some C callback functions" );
+ NSLog(@"a = ", a );
+ NSLog(@"b = ", b );
+ NSLog(@"ADD(a,b) = ", ObjcDo_op(a,b,example.ADD) );
+ NSLog(@"SUB(a,b) = ", ObjcDo_op(a,b,example.SUB) );
+ NSLog(@"MUL(a,b) = ", ObjcDo_op(a,b,example.MUL) );
+
+ NSLog(@"Here is what the C callback function classes are called in Java" );
+ NSLog(@"ADD = ", example.ADD.getClass().getName() );
+ NSLog(@"SUB = ", example.SUB.getClass().getName() );
+ NSLog(@"MUL = ", example.MUL.getClass().getName() );
+ return 0;
+}
diff --git a/Examples/objc/pointer/example.i b/Examples/objc/pointer/example.i
index e08637ad4..a49c8bf4d 100644
--- a/Examples/objc/pointer/example.i
+++ b/Examples/objc/pointer/example.i
@@ -2,11 +2,37 @@
%module example
%{
+#ifdef __cplusplus
+extern "C" {
+#endif
+
extern void add(int *, int *, int *);
extern void sub(int *, int *, int *);
extern int divide(int, int, int *);
+
+#ifdef __cplusplus
+}
+#endif
%}
+/* This example illustrates a couple of different techniques
+ for manipulating C pointers */
+
+/* First we'll use the pointer library */
+extern void add(int *x, int *y, int *result);
+%include cpointer.i
+%pointer_functions(int, intp);
+
+/* Next we'll use some typemaps */
+
+%include typemaps.i
+extern void sub(int *INPUT, int *INPUT, int *OUTPUT);
+
+/* Next we'll use typemaps and the %apply directive */
+
+%apply int *OUTPUT { int *r };
+extern int divide(int n, int d, int *r);
+
diff --git a/Examples/objc/pointer/runme.m b/Examples/objc/pointer/runme.m
index 42d40be3e..148fd760c 100644
--- a/Examples/objc/pointer/runme.m
+++ b/Examples/objc/pointer/runme.m
@@ -2,5 +2,48 @@
#import "example_proxy.h"
int main(int argc, char* argv[]) {
- return 0;
-} \ No newline at end of file
+
+NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
+
+ // First create some objects using the pointer library.
+ NSLog(@"Testing the pointer library");
+ int* a = ObjcNew_intp();
+ int* b = ObjcNew_intp();
+ int* c = ObjcNew_intp();
+ ObjcIntp_assign(a,37);
+ ObjcIntp_assign(b,42);
+
+ // Note that getCPtr() has package access by default
+ NSLog(@" a =%p",a );
+ NSLog(@" b =%p",b);
+ NSLog(@" c =%p",c);
+
+ // Call the add() function with some pointers
+ ObjcAdd(a,b,c);
+
+ // Now get the result
+ int res = ObjcIntp_value(c);
+ NSLog(@" 37 + 42 = %d", res);
+
+ // Clean up the pointers
+ ObjcDelete_intp(a);
+ ObjcDelete_intp(b);
+ ObjcDelete_intp(c);
+
+ // Now try the typemap library
+ // Now it is no longer necessary to manufacture pointers.
+ // Instead we use a single element array which in Java is modifiable.
+
+ NSLog(@"Trying the typemap library");
+ int r[] = {0};
+ ObjcSub(37,42,r);
+ NSLog(@" 37 - 42 = %d", r[0]);
+
+ // Now try the version with return value
+
+ NSLog(@"Testing return value");
+ int q = ObjcDivide(42,37,r);
+ NSLog(@" 42/37 =%d and remainder=%d", q, r[0]);
+ [pool drain];
+ return 0;
+}
diff --git a/Examples/objc/reference/runme.m b/Examples/objc/reference/runme.m
index 4c0e175b4..368097c4a 100644
--- a/Examples/objc/reference/runme.m
+++ b/Examples/objc/reference/runme.m
@@ -3,45 +3,44 @@
int main(int argc, char* argv[]) {
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
- //Object creation
-Vector *v1 =[[Vector alloc]initWithX:3.0 y:4.0 z:5.0];
-Vector *v2 =[[Vector alloc]initWithX:10.0 y:11.0 z:12.0];
-NSLog(@"Created v1 %@",[v1 print]);
-NSLog(@"Created v2 %@",[v2 print]);
-
- //Call an overloaded operator
-
-NSLog(@"Adding a+b \n");
-Vector *v3 =addv(v1 ,v2);
-NSLog(@"a+b = %@",[v3 print]);
-
-
- //Create a vector Array
-NSLog(@"Creating an array of vectors\n");
-VectorArray *va=[[VectorArray alloc]initWithMaxsize:10];
-NSLog(@" va:size %i",[va size]);
-
- //copy the value of v1 and v2 to a vector array
-[va set:0 a:v1];
-[va set:1 a:v2];
-[va set:2 a:addv(v1,v2)];
-
- //Get some values from the array
-
-NSLog(@"Getting some array values \n");
-int i =0;
-for(i=0; i<5 ;i++)
-{
-Vector *v4=[va get:i];
-NSLog(@"Array element %@",[v4 print]);
-}
-
- //---Clean up
-[v1 release];
-[v2 release];
-[va release];
-[pool drain];
-
-return 0;
-} \ No newline at end of file
+
+ // Object creation
+ Vector *v1 =[[Vector alloc]initWithX:3.0 y:4.0 z:5.0];
+ Vector *v2 =[[Vector alloc]initWithX:10.0 y:11.0 z:12.0];
+
+ NSLog(@"Created v1 %@",[v1 print]);
+ NSLog(@"Created v2 %@",[v2 print]);
+
+ // Call an overloaded operator
+ NSLog(@"Adding a+b \n");
+ Vector *v3 =ObjcAddv(v1 ,v2);
+ NSLog(@"a+b = %@",[v3 print]);
+
+ // Create a vector Array
+ NSLog(@"Creating an array of vectors\n");
+ VectorArray *va=[[VectorArray alloc]initWithMaxsize:10];
+ NSLog(@" va:size %i",[va size]);
+
+ // Copy the value of v1 and v2 to a vector array
+ [va set:0 a:v1];
+ [va set:1 a:v2];
+ [va set:2 a:ObjcAddv(v1,v2)];
+
+ // Get some values from the array
+ NSLog(@"Getting some array values \n");
+ int i =0;
+ for(i=0; i<5 ;i++)
+ {
+ Vector *v4=[va get:i];
+ NSLog(@"Array element %@",[v4 print]);
+ }
+
+ // Clean up
+ [v1 release];
+ [v2 release];
+ [va release];
+ [pool drain];
+
+ return 0;
+ }
diff --git a/Examples/objc/simple/runme.m b/Examples/objc/simple/runme.m
index e7db04799..7599901f2 100644
--- a/Examples/objc/simple/runme.m
+++ b/Examples/objc/simple/runme.m
@@ -2,6 +2,21 @@
#import "example_proxy.h"
int main(int argc, char* argv[]) {
- printf("result is %d\n", gcd(18,6));
+ // Call the gcd() function
+ int x = 42;
+ int y = 105;
+ int g = ObjcGcd(x,y);
+ NSLog(@"The gcd of %d and %d is %d\n", x, y, g);
+
+ // Manipulate the Foo global variable
+ // Output its current value
+ NSLog(@"Foo = %f \n", getFoo());
+
+ // Change its value
+ setFoo(3.1415926);
+
+ // See if the change took effect
+ NSLog(@"Changed Foo = %f \n", getFoo());
+
return 0;
}
diff --git a/Examples/objc/templates/runme.m b/Examples/objc/templates/runme.m
index 83e971cf3..530a3c149 100644
--- a/Examples/objc/templates/runme.m
+++ b/Examples/objc/templates/runme.m
@@ -5,27 +5,37 @@ int main(int argc, char* argv[]) {
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init];
//Call some templated functions
- NSLog(@"The value returned by maxint is %i",maxint(3,7));
- NSLog(@"The value returned by maxdouble is %g",maxdouble(3.14,2.18));
-
-
- //Create some class
- vecint *iv =[[vecint alloc]initWith_sz:100];
- vecdouble *dv=[[vecdouble alloc]initWith_sz:1000];
- int i=0;
- for (i=0;i<100;i++)
- [iv setitem:i val:2*i];
-
- for (i=0;i<1000;i++)
- [dv setitem:i val:1.0/(i+1)];
-
- int sum =0;
- for (i=0;i<100;i++)
- sum=sum+[iv getitem:i];
- NSLog(@"The sum is %i",sum);
-
- [iv release];
- [dv release];
- [pool drain];
- return 0;
-}
+ NSLog(@"The value returned by maxint is %i",ObjcMaxint(3,7));
+ NSLog(@"The value returned by maxdouble is %g",ObjcMaxdouble(3.14,2.18));
+
+
+ //Create some class
+ vecint *iv =[[vecint alloc]initWith_sz:100];
+ vecdouble *dv=[[vecdouble alloc]initWith_sz:1000];
+ int i=0;
+ for (i=0;i<100;i++)
+ [iv setitem:i val:2*i];
+
+ for (i=0;i<1000;i++)
+ [dv setitem:i val:1.0/(i+1)];
+
+ {
+ int sum =0;
+ for (i=0;i<100;i++)
+ sum=sum+[iv getitem:i];
+ NSLog(@"The sum of integers: %i",sum);
+ }
+
+ {
+ double sum =0;
+ for (i=0;i<1000;i++)
+ sum=sum+[dv getitem:i];
+ NSLog(@"The sum of doubles: %f",sum);
+ }
+
+ [iv release];
+ [dv release];
+ [pool drain];
+
+ return 0;
+ }
diff --git a/Examples/objc/variables/example.c b/Examples/objc/variables/example.c
index f8c7fa07f..d5b33e13e 100644
--- a/Examples/objc/variables/example.c
+++ b/Examples/objc/variables/example.c
@@ -30,8 +30,8 @@ char path[256] = "/home/beazley";
/* Global variables involving a structure */
-_Point *ptptr = 0;
-_Point pt = { 10, 20 };
+TPoint *ptptr = 0;
+TPoint pt = { 10, 20 };
/* A variable that we will make read-only in the interface */
int status = 1;
@@ -67,16 +67,16 @@ int *new_int(int value) {
return ip;
}
-/* A function to create a _Point */
+/* A function to create a Point */
-_Point *new_Point(int x, int y) {
- _Point *p = (_Point *) malloc(sizeof(_Point));
+TPoint *new_TPoint(int x, int y) {
+ TPoint *p = (TPoint *) malloc(sizeof(TPoint));
p->x = x;
p->y = y;
return p;
}
-char * Point_print(_Point *p) {
+char * TPoint_print(TPoint *p) {
static char buffer[256];
if (p) {
sprintf(buffer,"(%d,%d)", p->x,p->y);
diff --git a/Examples/objc/variables/example.h b/Examples/objc/variables/example.h
index f0b4c105f..86a5952f4 100644
--- a/Examples/objc/variables/example.h
+++ b/Examples/objc/variables/example.h
@@ -2,5 +2,5 @@
typedef struct {
int x,y;
-} _Point;
+} TPoint;
diff --git a/Examples/objc/variables/example.i b/Examples/objc/variables/example.i
index 80eb4e6a2..09b43f368 100644
--- a/Examples/objc/variables/example.i
+++ b/Examples/objc/variables/example.i
@@ -18,11 +18,12 @@ extern char cvar;
extern float fvar;
extern double dvar;
extern char *strvar;
+//extern const char cstrvar[];
extern int *iptrvar;
extern char name[256];
-extern _Point *ptptr;
-extern _Point pt;
+extern TPoint *ptptr;
+extern TPoint pt;
%}
@@ -41,8 +42,8 @@ extern char path[256];
%inline %{
extern void print_vars();
extern int *new_int(int value);
-extern _Point *new_Point(int x, int y);
-extern char *Point_print(_Point *p);
+extern TPoint *new_TPoint(int x, int y);
+extern char *TPoint_print(TPoint *p);
extern void pt_print();
%}
diff --git a/Examples/objc/variables/runme.m b/Examples/objc/variables/runme.m
index 054471087..2639e2fd5 100644
--- a/Examples/objc/variables/runme.m
+++ b/Examples/objc/variables/runme.m
@@ -16,8 +16,8 @@ int main(int argc, char* argv[]) {
setCvar('S');
setFvar(3.14159);
setStrvar(@"Hello World");
- setIptrvar(new_int(37));
- setPtptr(new_Point(37,42));
+ setIptrvar(ObjcNew_int(37));
+ setPtptr(ObjcNew_TPoint(37,42));
setName(@"Bill");
//Now print out the values of variables
@@ -36,33 +36,30 @@ int main(int argc, char* argv[]) {
NSLog(@"dvar = %g\n", getDvar());
NSLog(@"cvar = %c\n", getCvar());
NSLog(@"strvar = %@\n", getStrvar());
- getIptrvar();
+ NSLog(@"iptrvar =%p\n", getIptrvar());
NSLog(@"name = %@\n", getName());
- NSLog(@"ptptr = %@\n", Point_print(getPtptr()));
+ NSLog(@"ptptr = %p %@\n", getPtptr(), ObjcTPoint_print(getPtptr()));
+ NSLog(@"pt ="); ObjcPt_print();
+ NSLog(@"status = %d\n", getStatus());
NSLog(@"\nVariables (values printed from C)\n");
- print_vars();
-
- NSLog(@"\nNow I'm going to try and modify some read only variables\n");
- NSLog(@" Tring to set 'path'\n");
-
- NSLog(@" 'path is' %@",getPath());
-
-
- NSLog(@" Trying to set 'status '\n");
- NSLog(@" 'Status is' %i",getStatus());
+ ObjcPrint_vars();
+ // This line would not compile: since status is marked with
+ // %immutable, there is no SetStatus function.
+ // NSLog(@"\nNow I'm going to try and modify some read only variables")
+ // setStatus(0)
+
NSLog(@"\nI'm going to try and update a structure variable.\n");
- setPt(getPtptr());
+ setPt((struct SWIGTYPE_TPoint*)getPtptr());
NSLog(@"The new value is \n");
- pt_print();
-
- NSLog(@"You should see the value %@",Point_print(getPtptr()));
-
- [pool drain];
+ ObjcPt_print();
+ NSLog(@"You should see the value %@",ObjcTPoint_print(getPtptr()));
+
+ [pool drain];
return 0;
}