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)

This commit is contained in:
Gustavo J. A. M. Carneiro committed 2008-02-17 21:50:15 +00:00
1 parent 08624a0f85
commit beb11b3823
3 files changed
+93 -3

No files matched your search

+75
View File
@@ -1,5 +1,7 @@
#include "hello.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
+17 -2
View File
@@ -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
+1 -1
View File
@@ -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'