From 40e008cec50b80d9fde89ca026dfebfa34d7bd33 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Sat, 15 Mar 2003 22:51:46 +0000 Subject: [PATCH] Usual SWIG variables example - uses properties for global variable access git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4542 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Examples/csharp/variables/.cvsignore | 14 +++++ Examples/csharp/variables/Makefile | 20 +++++++ Examples/csharp/variables/example.c | 86 ++++++++++++++++++++++++++++ Examples/csharp/variables/example.h | 6 ++ Examples/csharp/variables/example.i | 41 +++++++++++++ Examples/csharp/variables/runme.cs | 83 +++++++++++++++++++++++++++ 6 files changed, 250 insertions(+) create mode 100644 Examples/csharp/variables/.cvsignore create mode 100644 Examples/csharp/variables/Makefile create mode 100644 Examples/csharp/variables/example.c create mode 100644 Examples/csharp/variables/example.h create mode 100644 Examples/csharp/variables/example.i create mode 100644 Examples/csharp/variables/runme.cs diff --git a/Examples/csharp/variables/.cvsignore b/Examples/csharp/variables/.cvsignore new file mode 100644 index 000000000..fccb2721d --- /dev/null +++ b/Examples/csharp/variables/.cvsignore @@ -0,0 +1,14 @@ +runme +*_wrap.c +*_wrap.cxx +*.iltmp +*.cs +*.dll +*.dsw +*.exp +*.lib +*.ncb +*.opt +*.plg +Release +Debug diff --git a/Examples/csharp/variables/Makefile b/Examples/csharp/variables/Makefile new file mode 100644 index 000000000..08ce054a0 --- /dev/null +++ b/Examples/csharp/variables/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../swig +SRCS = example.c +TARGET = example +INTERFACE = example.i +SWIGOPT = +CSHARPSRCS = *.cs +CSHARPFLAGS= -o runme + +all:: csharp + +csharp:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp + $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile + +clean:: + $(MAKE) -f $(TOP)/Makefile csharp_clean + +check: all diff --git a/Examples/csharp/variables/example.c b/Examples/csharp/variables/example.c new file mode 100644 index 000000000..37e9feb33 --- /dev/null +++ b/Examples/csharp/variables/example.c @@ -0,0 +1,86 @@ +/* File : example.c */ + +/* I'm a file containing some C global variables */ + +#include +#include +#include "example.h" + +int ivar = 0; +short svar = 0; +long lvar = 0; +unsigned int uivar = 0; +unsigned short usvar = 0; +unsigned long ulvar = 0; +signed char scvar = 0; +unsigned char ucvar = 0; +char cvar = 0; +float fvar = 0; +double dvar = 0; +char *strvar = 0; +const char *cstrvar = 0; +int *iptrvar = 0; +char name[256] = "Dave"; +char path[256] = "/home/beazley"; + + +/* Global variables involving a structure */ +Point *ptptr = 0; +Point pt = { 10, 20 }; + +/* A variable that we will make read-only in the interface */ +int status = 1; + +/* A debugging function to print out their values */ + +void print_vars() { + printf("ivar = %d\n", ivar); + printf("svar = %d\n", svar); + printf("lvar = %ld\n", lvar); + printf("uivar = %u\n", uivar); + printf("usvar = %u\n", usvar); + printf("ulvar = %lu\n", ulvar); + printf("scvar = %d\n", scvar); + printf("ucvar = %u\n", ucvar); + printf("fvar = %g\n", fvar); + printf("dvar = %g\n", dvar); + printf("cvar = %c\n", cvar); + printf("strvar = %s\n", strvar ? strvar : "(null)"); + printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)"); + printf("iptrvar = %x\n", iptrvar); + printf("name = %s\n", name); + printf("ptptr = %x (%d, %d)\n", ptptr, ptptr ? ptptr->x : 0, ptptr ? ptptr->y : 0); + printf("pt = (%d, %d)\n", pt.x, pt.y); + printf("status = %d\n", status); +} + +/* A function to create an integer (to test iptrvar) */ + +int *new_int(int value) { + int *ip = (int *) malloc(sizeof(int)); + *ip = value; + return ip; +} + +/* A function to create a point */ + +Point *new_Point(int x, int y) { + Point *p = (Point *) malloc(sizeof(Point)); + p->x = x; + p->y = y; + return p; +} + +char * Point_print(Point *p) { + static char buffer[256]; + if (p) { + sprintf(buffer,"(%d,%d)", p->x,p->y); + } else { + sprintf(buffer,"null"); + } + return buffer; +} + +void pt_print() { + printf("(%d, %d)\n", pt.x, pt.y); +} diff --git a/Examples/csharp/variables/example.h b/Examples/csharp/variables/example.h new file mode 100644 index 000000000..0f7e89594 --- /dev/null +++ b/Examples/csharp/variables/example.h @@ -0,0 +1,6 @@ +/* File: example.h */ + +typedef struct { + int x,y; +} Point; + diff --git a/Examples/csharp/variables/example.i b/Examples/csharp/variables/example.i new file mode 100644 index 000000000..51a28ba36 --- /dev/null +++ b/Examples/csharp/variables/example.i @@ -0,0 +1,41 @@ +/* File : example.i */ +%module example +%{ +#include "example.h" +%} + +/* Some global variable declarations */ +extern int ivar; +extern short svar; +extern long lvar; +extern unsigned int uivar; +extern unsigned short usvar; +extern unsigned long ulvar; +extern signed char scvar; +extern unsigned char ucvar; +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; + + +/* Some read-only variables */ + +%immutable; +extern int status; +extern char path[256]; +%mutable; + +/* Some helper functions to make it easier to test */ +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 void pt_print(); + diff --git a/Examples/csharp/variables/runme.cs b/Examples/csharp/variables/runme.cs new file mode 100644 index 000000000..0dbe5b19f --- /dev/null +++ b/Examples/csharp/variables/runme.cs @@ -0,0 +1,83 @@ +// This example illustrates global variable access from C#. + +using System; +using System.Reflection; + +public class runme { + + public static void Main() { + + // Try to set the values of some global variables + + example.ivar = 42; + example.svar = -31000; + example.lvar = 65537; + example.uivar = 123456; + example.usvar = 61000; + example.ulvar = 654321; + example.scvar = -13; + example.ucvar = 251; + example.cvar = 'S'; + example.fvar = (float)3.14159; + example.dvar = 2.1828; + example.strvar = "Hello World"; + example.cstrvar = "Goodbye"; + example.iptrvar= example.new_int(37); + example.ptptr = example.new_Point(37,42); + example.name = "Bill"; + + // Now print out the values of the variables + + Console.WriteLine( "Variables (values printed from C#)" ); + + Console.WriteLine( "ivar =" + example.ivar ); + Console.WriteLine( "svar =" + example.svar ); + Console.WriteLine( "lvar =" + example.lvar ); + Console.WriteLine( "uivar =" + example.uivar ); + Console.WriteLine( "usvar =" + example.usvar ); + Console.WriteLine( "ulvar =" + example.ulvar ); + Console.WriteLine( "scvar =" + (int)example.scvar ); // cast for pnet compiler bug + Console.WriteLine( "ucvar =" + (int)example.ucvar ); // cast for pnet compiler bug + Console.WriteLine( "fvar =" + example.fvar ); + Console.WriteLine( "dvar =" + example.dvar ); + Console.WriteLine( "cvar =" + example.cvar ); + Console.WriteLine( "strvar =" + example.strvar ); + Console.WriteLine( "cstrvar =" + example.cstrvar ); + Console.WriteLine( "iptrvar =" + example.iptrvar ); + Console.WriteLine( "name =" + example.name ); + Console.WriteLine( "ptptr =" + example.ptptr + example.Point_print(example.ptptr) ); + Console.WriteLine( "pt =" + example.pt + example.Point_print(example.pt) ); + + Console.WriteLine( "\nVariables (values printed from C)" ); + + example.print_vars(); + + Console.WriteLine( "\nNow I'm going to try and modify some read only variables" ); + Console.WriteLine( "\nChecking that the read only variables are readonly..." ); + + example ex = new example(); + Type t = ex.GetType(); + + Console.WriteLine( " 'path'" ); + PropertyInfo pi = t.GetProperty("path"); + if (pi.CanWrite) + Console.WriteLine("Oh dear this variable is not read only\n"); + else + Console.WriteLine("Good."); + + Console.WriteLine( " 'status'" ); + pi = t.GetProperty("status"); + if (pi.CanWrite) + Console.WriteLine("Oh dear this variable is not read only"); + else + Console.WriteLine("Good."); + + Console.WriteLine( "\nI'm going to try and update a structure variable.\n" ); + + example.pt = example.ptptr; + + Console.WriteLine( "The new value is" ); + example.pt_print(); + Console.WriteLine( "You should see the value" + example.Point_print(example.ptptr) ); + } +}