Created
May 19, 2012 08:32
-
-
Save masaori335/2730090 to your computer and use it in GitHub Desktop.
An Example of Gauche Extension Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "example.h" | |
ScmClass* ExampleClass; | |
static void example_class_print (ScmObj obj, ScmPort* out, ScmWriteContext* ctx) | |
{ | |
struct example* e = EXAMPLE_UNBOX(obj); | |
Scm_Printf(out, "#<example-class \"%p\">", e); | |
} | |
static void example_class_cleanup (ScmObj obj) | |
{ | |
struct example* e = EXAMPLE_UNBOX(obj); | |
free(e); | |
} | |
/* | |
* Module initialization function. | |
*/ | |
extern void Scm_Init_examplelib (ScmModule*); | |
void Scm_Init_example (void) | |
{ | |
ScmModule *mod; | |
/* Register this DSO to Gauche */ | |
SCM_INIT_EXTENSION(example); | |
/* Create the module if it doesn't exist yet. */ | |
mod = SCM_MODULE(SCM_FIND_MODULE ("example", TRUE)); | |
ExampleClass = | |
Scm_MakeForeignPointerClass(mod, | |
"<example-class>", | |
example_class_print, | |
example_class_cleanup, | |
SCM_FOREIGN_POINTER_KEEP_IDENTITY | SCM_FOREIGN_POINTER_MAP_NULL); | |
/* Register stub-generated procedures */ | |
Scm_Init_examplelib (mod); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Prologue */ | |
#ifndef GAUCHE_EXAMPLE_H | |
#define GAUCHE_EXAMPLE_H | |
#include <gauche.h> | |
#include <gauche/extend.h> | |
SCM_DECL_BEGIN | |
struct example { | |
int x; | |
}; | |
extern ScmClass *ExampleClass; | |
#define EXAMPLE_P(obj) SCM_XTYPEP (obj, ExampleClass) | |
#define EXAMPLE_UNBOX(obj) SCM_FOREIGN_POINTER_REF (struct example*, obj) | |
#define EXAMPLE_BOX(obj) Scm_MakeForeignPointer (ExampleClass, obj) | |
/* Epilogue */ | |
SCM_DECL_END | |
#endif /* GAUCHE_EXAMPLE_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-module example | |
(export <example-class> | |
)) | |
(select-module example) | |
;; Loads extension | |
(dynamic-load "example") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" | |
#include \"example.h\" | |
" | |
(define-type <example-class> "struct example*" "example class" "EXAMPLE_P" "EXAMPLE_UNBOX" "EXAMPLE_BOX") | |
;; Local variables: | |
;; mode: scheme | |
;; end: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment