From beb11b3823cd55c4c76f8ac93b205bc8d7aafc06 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Sun, 17 Feb 2008 21:50:15 +0000 Subject: [PATCH] Add a sample C API based on functions operating over structures in a object oriented way; goal is to support this API in the future (not yet done) --- examples/c-hello/hello.c | 75 ++++++++++++++++++++++++++++++++++++++++ examples/c-hello/hello.h | 19 ++++++++-- examples/c-hello/wscript | 2 +- 3 files changed, 93 insertions(+), 3 deletions(-) diff --git a/examples/c-hello/hello.c b/examples/c-hello/hello.c index 417a022..5ccd046 100644 --- a/examples/c-hello/hello.c +++ b/examples/c-hello/hello.c @@ -1,5 +1,7 @@ #include "hello.h" #include +#include +#include void hello_print_message(const char *message) { @@ -10,3 +12,76 @@ double hello_sum(double x, double y) { return x + y; } + + +struct _HelloFoo +{ + int refcount; + char *data; +}; + +HelloFoo* +hello_foo_new(void) +{ + HelloFoo *foo; + foo = (HelloFoo *) malloc(sizeof(HelloFoo)); + foo->refcount = 1; + foo->data = NULL; + return foo; +} + +HelloFoo* +hello_foo_new_from_data(const char *data) +{ + HelloFoo* foo; + + foo = hello_foo_new(); + hello_foo_set_data(foo, data); + return foo; +} + +HelloFoo* +hello_foo_new_with_spaces (int num_spaces) +{ + int i; + HelloFoo *foo; + foo = hello_foo_new(); + foo->data = malloc(num_spaces + 1); + for (i = 0; i < num_spaces; i++) + foo->data[i] = ' '; + foo->data[i] = '\0'; + return foo; +} + +void +hello_foo_ref(HelloFoo *foo) +{ + foo->refcount++; +} + +void +hello_foo_unref(HelloFoo *foo) +{ + if (--foo->refcount > 0) + return; + + if (foo->data) + free(foo->data); + free(foo); +} + +void +hello_foo_set_data(HelloFoo *foo, + const char *data) +{ + if (foo->data) + free(foo->data); + foo->data = strdup(data); +} + +const char * +hello_foo_get_data(HelloFoo *foo) +{ + return foo->data; +} + diff --git a/examples/c-hello/hello.h b/examples/c-hello/hello.h index c8903d0..661ce50 100644 --- a/examples/c-hello/hello.h +++ b/examples/c-hello/hello.h @@ -7,8 +7,23 @@ extern "C" #endif -void hello_print_message(const char *message); -double hello_sum(double x, double y); +void hello_print_message (const char *message); +double hello_sum (double x, + double y); + + +typedef struct _HelloFoo HelloFoo; + + +HelloFoo* hello_foo_new (void); +HelloFoo* hello_foo_new_from_data (const char *data); +HelloFoo* hello_foo_new_with_spaces (int num_spaces); +void hello_foo_ref (HelloFoo *foo); +void hello_foo_unref (HelloFoo *foo); +void hello_foo_set_data (HelloFoo *foo, + const char *data); +const char * hello_foo_get_data (HelloFoo *foo); + #ifdef __cplusplus diff --git a/examples/c-hello/wscript b/examples/c-hello/wscript index 7681cff..1af2072 100644 --- a/examples/c-hello/wscript +++ b/examples/c-hello/wscript @@ -18,7 +18,7 @@ def build(bld): bindgen.command_is_external = True bindgen.prio = 55 # must run after the 'py' objects in ../../pybindgen - obj = bld.create_obj('cpp', 'plugin', 'pyext') + obj = bld.create_obj('cc', 'plugin', 'pyext') obj.source = [ 'hello.c', 'hellomodule.c'