update swig on windows

This commit is contained in:
Bassem Girgis
2019-08-04 21:38:19 -05:00
parent 8414b7136b
commit 76ad52be58
5943 changed files with 91790 additions and 71892 deletions

View File

@@ -0,0 +1,97 @@
/* -----------------------------------------------------------------------------
* cdata.i
*
* SWIG library file containing macros for manipulating raw C data as strings.
* ----------------------------------------------------------------------------- */
%{
typedef struct SWIGCDATA {
char *data;
intgo len;
} SWIGCDATA;
%}
%fragment("cdata", "header") %{
struct swigcdata {
intgo size;
void *data;
};
%}
%typemap(gotype) SWIGCDATA "[]byte"
%typemap(imtype) SWIGCDATA "uint64"
%typemap(out, fragment="cdata") SWIGCDATA(struct swigcdata *swig_out) %{
swig_out = (struct swigcdata *)malloc(sizeof(*swig_out));
if (swig_out) {
swig_out->size = $1.len;
swig_out->data = malloc(swig_out->size);
if (swig_out->data) {
memcpy(swig_out->data, $1.data, swig_out->size);
}
}
$result = *(long long *)(void **)&swig_out;
%}
%typemap(goout) SWIGCDATA %{
{
type swigcdata struct { size int; data uintptr }
p := (*swigcdata)(unsafe.Pointer(uintptr($1)))
if p == nil || p.data == 0 {
$result = nil
} else {
b := make([]byte, p.size)
a := (*[0x7fffffff]byte)(unsafe.Pointer(p.data))[:p.size]
copy(b, a)
Swig_free(p.data)
Swig_free(uintptr(unsafe.Pointer(p)))
$result = b
}
}
%}
/* -----------------------------------------------------------------------------
* %cdata(TYPE [, NAME])
*
* Convert raw C data to a binary string.
* ----------------------------------------------------------------------------- */
%define %cdata(TYPE,NAME...)
%insert("header") {
#if #NAME == ""
static SWIGCDATA cdata_##TYPE(TYPE *ptr, int nelements) {
#else
static SWIGCDATA cdata_##NAME(TYPE *ptr, int nelements) {
#endif
SWIGCDATA d;
d.data = (char *) ptr;
#if #TYPE != "void"
d.len = nelements*sizeof(TYPE);
#else
d.len = nelements;
#endif
return d;
}
}
%typemap(default) int nelements "$1 = 1;"
#if #NAME == ""
SWIGCDATA cdata_##TYPE(TYPE *ptr, int nelements);
#else
SWIGCDATA cdata_##NAME(TYPE *ptr, int nelements);
#endif
%enddef
%typemap(default) int nelements;
%rename(cdata) ::cdata_void(void *ptr, int nelements);
%cdata(void);
/* Memory move function. Due to multi-argument typemaps this appears
to be wrapped as
void memmove(void *data, const char *s); */
void memmove(void *data, char *indata, int inlen);

View File

@@ -0,0 +1,80 @@
/* -----------------------------------------------------------------------------
* director.swg
*
* This file contains support for director classes so that Go proxy
* methods can be called from C++.
* ----------------------------------------------------------------------------- */
#include <exception>
#include <map>
namespace Swig {
class DirectorException : public std::exception {
};
}
/* Handle memory management for directors. */
namespace {
struct GCItem {
virtual ~GCItem() {}
};
struct GCItem_var {
GCItem_var(GCItem *item = 0) : _item(item) {
}
GCItem_var& operator=(GCItem *item) {
GCItem *tmp = _item;
_item = item;
delete tmp;
return *this;
}
~GCItem_var() {
delete _item;
}
GCItem* operator->() {
return _item;
}
private:
GCItem *_item;
};
template <typename Type>
struct GCItem_T : GCItem {
GCItem_T(Type *ptr) : _ptr(ptr) {
}
virtual ~GCItem_T() {
delete _ptr;
}
private:
Type *_ptr;
};
}
class Swig_memory {
public:
template <typename Type>
void swig_acquire_pointer(Type* vptr) {
if (vptr) {
swig_owner[vptr] = new GCItem_T<Type>(vptr);
}
}
private:
typedef std::map<void *, GCItem_var> swig_ownership_map;
swig_ownership_map swig_owner;
};
template <typename Type>
static void swig_acquire_pointer(Swig_memory** pmem, Type* ptr) {
if (!pmem) {
*pmem = new Swig_memory;
}
(*pmem)->swig_acquire_pointer(ptr);
}

View File

@@ -0,0 +1,7 @@
%typemap(throws,noblock=1) (...) {
SWIG_exception(SWIG_RuntimeError,"unknown exception");
}
%insert("runtime") %{
#define SWIG_exception(code, msg) _swig_gopanic(msg)
%}

View File

@@ -0,0 +1,705 @@
/* ------------------------------------------------------------
* go.swg
*
* Go configuration module.
* ------------------------------------------------------------ */
%include <gostring.swg>
/* Code insertion directives */
#define %go_import(...) %insert(go_imports) %{__VA_ARGS__%}
/* Basic types */
%typemap(gotype) bool, const bool & "bool"
%typemap(gotype) char, const char & "byte"
%typemap(gotype) signed char, const signed char & "int8"
%typemap(gotype) unsigned char, const unsigned char & "byte"
%typemap(gotype) short, const short & "int16"
%typemap(gotype) unsigned short, const unsigned short & "uint16"
%typemap(gotype) int, const int & "int"
%typemap(gotype) unsigned int, const unsigned int & "uint"
%typemap(gotype) long, const long & "int64"
%typemap(gotype) unsigned long, const unsigned long & "uint64"
%typemap(gotype) long long, const long long & "int64"
%typemap(gotype) unsigned long long, const unsigned long long & "uint64"
%typemap(gotype) float, const float & "float32"
%typemap(gotype) double, const double & "float64"
%typemap(in) bool,
char,
signed char,
unsigned char,
short,
unsigned short,
int,
unsigned int,
long,
unsigned long,
long long,
unsigned long long,
float,
double
%{ $1 = ($1_ltype)$input; %}
%typemap(in) const bool &,
const char &,
const signed char &,
const unsigned char &,
const short &,
const unsigned short &,
const int &,
const unsigned int &,
const long long &,
const unsigned long long &,
const float &,
const double &
%{ $1 = ($1_ltype)&$input; %}
%typemap(in) const long & ($*1_ltype temp),
const unsigned long & ($*1_ltype temp)
%{ temp = ($*1_ltype)$input;
$1 = ($1_ltype)&temp; %}
%typemap(out) bool,
char,
signed char,
unsigned char,
short,
unsigned short,
int,
unsigned int,
long,
unsigned long,
long long,
unsigned long long,
float,
double
%{ $result = $1; %}
%typemap(goout) bool,
char,
signed char,
unsigned char,
short,
unsigned short,
int,
unsigned int,
long,
unsigned long,
long long,
unsigned long long,
float,
double
""
%typemap(out) const bool &,
const char &,
const signed char &,
const unsigned char &,
const short &,
const unsigned short &,
const int &,
const unsigned int &,
const long &,
const unsigned long &,
const long long &,
const unsigned long long &,
const float &,
const double &
%{ $result = ($*1_ltype)*$1; %}
%typemap(goout) const bool &,
const char &,
const signed char &,
const unsigned char &,
const short &,
const unsigned short &,
const int &,
const unsigned int &,
const long &,
const unsigned long &,
const long long &,
const unsigned long long &,
const float &,
const double &
""
%typemap(out) void ""
%typemap(goout) void ""
%typemap(directorin) bool,
char,
signed char,
unsigned char,
short,
unsigned short,
int,
unsigned int,
long,
unsigned long,
long long,
unsigned long long,
float,
double
%{ $input = ($1_ltype)$1; %}
%typemap(godirectorin) bool,
char,
signed char,
unsigned char,
short,
unsigned short,
int,
unsigned int,
long,
unsigned long,
long long,
unsigned long long,
float,
double
""
%typemap(directorin) const bool &,
const char &,
const signed char &,
const unsigned char &,
const short &,
const unsigned short &,
const int &,
const unsigned int &,
const long &,
const unsigned long &,
const long long &,
const unsigned long long &,
const float &,
const double &
%{ $input = ($*1_ltype)$1; %}
%typemap(godirectorin) const bool &,
const char &,
const signed char &,
const unsigned char &,
const short &,
const unsigned short &,
const int &,
const unsigned int &,
const long &,
const unsigned long &,
const long long &,
const unsigned long long &,
const float &,
const double &
""
%typemap(directorout) bool,
char,
signed char,
unsigned char,
short,
unsigned short,
int,
unsigned int,
long,
unsigned long,
long long,
unsigned long long,
float,
double
%{ $result = ($1_ltype)$input; %}
%typemap(directorout,warning=SWIGWARN_TYPEMAP_DIRECTOROUT_PTR_MSG) const bool &,
const char &,
const signed char &,
const unsigned char &,
const short &,
const unsigned short &,
const int &,
const unsigned int &,
const long &,
const unsigned long &,
const long long &,
const unsigned long long &,
const float &,
const double &
%{
$result = new $*1_ltype($input);
swig_acquire_pointer(&swig_mem, $result);
%}
/* The size_t type. */
%typemap(gotype) size_t, const size_t & %{int64%}
%typemap(in) size_t
%{ $1 = (size_t)$input; %}
%typemap(in) const size_t &
%{ $1 = ($1_ltype)&$input; %}
%typemap(out) size_t
%{ $result = $1; %}
%typemap(goout) size_t ""
%typemap(out) const size_t &
%{ $result = ($*1_ltype)*$1; %}
%typemap(goout) const size_t & ""
%typemap(directorin) size_t
%{ $input = (size_t)$1; %}
%typemap(godirectorin) size_t ""
%typemap(directorin) const size_t &
%{ $input = ($*1_ltype)$1; %}
%typemap(godirectorin) const size_t & ""
%typemap(directorout) size_t
%{ $result = ($1_ltype)$input; %}
%typemap(directorout,warning=SWIGWARN_TYPEMAP_DIRECTOROUT_PTR_MSG) const size_t &
%{
$result = new $*1_ltype($input);
swig_acquire_pointer(&swig_mem, $result);
%}
/* Member pointers. */
%typemap(gotype) SWIGTYPE (CLASS::*)
%{$gotypename%}
%typemap(in) SWIGTYPE (CLASS::*)
%{ $1 = *($&1_ltype)$input; %}
%typemap(out) SWIGTYPE (CLASS::*)
%{
struct swig_out_type { intgo size; void* val; } *swig_out;
swig_out = (struct swig_out_type*)malloc(sizeof(*swig_out));
if (swig_out) {
swig_out->size = sizeof($1_ltype);
swig_out->val = malloc(swig_out->size);
if (swig_out->val) {
*($&1_ltype)(swig_out->val) = $1;
}
}
$result = swig_out;
%}
%typemap(goout) SWIGTYPE (CLASS::*)
%{
{
type swig_out_type struct { size int; val uintptr }
p := (*swig_out_type)(unsafe.Pointer($1))
if p == nil || p.val == 0 {
$result = nil
} else {
m := make([]byte, p.size)
a := (*[1024]byte)(unsafe.Pointer(p.val))[:p.size]
copy(m, a)
Swig_free(p.val)
Swig_free(uintptr(unsafe.Pointer(p)))
$result = &m[0]
}
}
%}
%typemap(directorin) SWIGTYPE (CLASS::*)
%{ $input = *($&1_ltype)$1; %}
%typemap(godirectorin) SWIGTYPE (CLASS::*) ""
%typemap(directorout) SWIGTYPE (CLASS::*)
%{
$result = new $1_ltype($input);
swig_acquire_pointer(&swig_mem, $result);
%}
/* Pointers. */
/* We can't translate pointers using a typemap, so that is handled in
the C++ code. */
%typemap(gotype) SWIGTYPE *
%{$gotypename%}
%typemap(in) SWIGTYPE *
%{ $1 = *($&1_ltype)&$input; %}
%typemap(out) SWIGTYPE *
%{ *($&1_ltype)&$result = ($1_ltype)$1; %}
%typemap(goout) SWIGTYPE * ""
%typemap(directorin) SWIGTYPE *
%{ *($&1_ltype)&$input = ($1_ltype)$1; %}
%typemap(godirectorin) SWIGTYPE * ""
%typemap(directorout) SWIGTYPE *
%{ $result = *($&1_ltype)&$input; %}
/* Pointer references. */
%typemap(gotype) SWIGTYPE *const&
%{$gotypename%}
%typemap(in) SWIGTYPE *const& ($*1_ltype temp = 0)
%{
temp = *($1_ltype)&$input;
$1 = ($1_ltype)&temp;
%}
%typemap(out) SWIGTYPE *const&
%{ *($1_ltype)&$result = *$1; %}
%typemap(goout) SWIGTYPE *const& ""
/* References. */
/* Converting a C++ reference to Go has to be handled in the C++
code. */
%typemap(gotype) SWIGTYPE &
%{$gotypename%}
%typemap(in) SWIGTYPE &
%{ $1 = *($&1_ltype)&$input; %}
%typemap(out) SWIGTYPE &
%{ *($&1_ltype)&$result = $1; %}
%typemap(goout) SWIGTYPE & ""
%typemap(directorin) SWIGTYPE &
%{ $input = ($1_ltype)&$1; %}
%typemap(godirectorin) SWIGTYPE & ""
%typemap(directorout) SWIGTYPE &
%{ *($&1_ltype)&$result = $input; %}
%typemap(directorout, warning=SWIGWARN_TYPEMAP_DIRECTOROUT_PTR_MSG) SWIGTYPE *const&
%{ static $*1_ltype swig_temp;
swig_temp = *($1_ltype)&$input;
$result = &swig_temp; %}
%typemap(gotype) SWIGTYPE &&
%{$gotypename%}
%typemap(in) SWIGTYPE &&
%{ $1 = *($&1_ltype)&$input; %}
%typemap(out) SWIGTYPE &&
%{ *($&1_ltype)&$result = $1; %}
%typemap(goout) SWIGTYPE && ""
%typemap(directorin) SWIGTYPE &&
%{ $input = ($1_ltype)&$1_name; %}
%typemap(godirectorin) SWIGTYPE && ""
%typemap(directorout) SWIGTYPE &&
%{ *($&1_ltype)&$result = $input; %}
/* C arrays turn into Go pointers. If we know the length we can use a
slice. */
%typemap(gotype) SWIGTYPE []
%{$gotypename%}
%typemap(in) SWIGTYPE []
%{ $1 = *($&1_ltype)&$input; %}
%typemap(out) SWIGTYPE []
%{ *($&1_ltype)&$result = $1; %}
%typemap(goout) SWIGTYPE [] ""
%typemap(directorin) SWIGTYPE []
%{ $input = *($1_ltype)&$1; %}
%typemap(godirectorin) SWIGTYPE [] ""
%typemap(directorout) SWIGTYPE []
%{ *($&1_ltype)&$result = $input; %}
/* Strings. */
%typemap(gotype)
char *, char *&, char[ANY], char[] "string"
/* Needed to avoid confusion with the way the go module handles
references. */
%typemap(gotype) char&, unsigned char& "*byte"
%typemap(gotype) signed char& "*int8"
%typemap(in)
char *, char[ANY], char[]
%{
$1 = ($1_ltype)malloc($input.n + 1);
memcpy($1, $input.p, $input.n);
$1[$input.n] = '\0';
%}
%typemap(in) char *& (char *temp)
%{
temp = (char *)malloc($input.n + 1);
memcpy(temp, $input.p, $input.n);
temp[$input.n] = '\0';
$1 = ($1_ltype)&temp;
%}
%typemap(freearg)
char *, char *&, char[ANY], char[]
%{ free($1); %}
%typemap(out,fragment="AllocateString")
char *, char *&, char[ANY], char[]
%{ $result = Swig_AllocateString((char*)$1, $1 ? strlen((char*)$1) : 0); %}
%typemap(goout,fragment="CopyString")
char *, char *&, char[ANY], char[]
%{ $result = swigCopyString($1) %}
%typemap(directorin,fragment="AllocateString")
char *, char *&, char[ANY], char[]
%{
$input = Swig_AllocateString((char*)$1, $1 ? strlen((char*)$1) : 0);
%}
%typemap(godirectorin,fragment="CopyString")
char *, char *&, char[ANY], char[]
%{
$result = swigCopyString($input)
%}
%typemap(godirectorout)
char *, char *&, char[ANY], char[]
%{
{
p := Swig_malloc(len($input) + 1)
s := (*[1<<30]byte)(unsafe.Pointer(p))[:len($input) + 1]
copy(s, $input)
s[len($input)] = 0
$result = *(*string)(unsafe.Pointer(&s))
}
%}
%typemap(directorout, warning=SWIGWARN_TYPEMAP_DIRECTOROUT_PTR_MSG)
char *, char *&, char[ANY], char[]
%{ $result = ($1_ltype)$input.p; %}
/* String & length */
%typemap(gotype) (char *STRING, size_t LENGTH) "string"
%typemap(in) (char *STRING, size_t LENGTH)
%{
$1 = ($1_ltype)$input.p;
$2 = ($2_ltype)$input.n;
%}
%typemap(out,fragment="AllocateString") (char *STRING, size_t LENGTH)
%{ $result = Swig_AllocateString((char*)$1, (size_t)$2); %}
%typemap(goout,fragment="CopyString") (char *STRING, size_t LENGTH)
%{ $result = swigCopyString($1) %}
%typemap(directorin,fragment="AllocateString") (char *STRING, size_t LENGTH)
%{ $input = Swig_AllocateString((char*)$1, $2); %}
%typemap(godirectorin,fragment="CopyString") (char *STRING, size_t LENGTH)
%{ $result = swigCopyString($input) %}
%typemap(directorout) (char *STRING, size_t LENGTH)
%{
$1 = ($1_ltype)$input.p;
$2 = ($2_ltype)$input.n;
%}
/* Enums. We can't do the right thing for enums in typemap(gotype) so
we deliberately don't define them. The right thing would be to
capitalize the name. This is instead done in go.cxx. */
%typemap(gotype) enum SWIGTYPE
%{$gotypename%}
%typemap(in) enum SWIGTYPE
%{ $1 = ($1_ltype)$input; %}
%typemap(out) enum SWIGTYPE
%{ $result = (intgo)$1; %}
%typemap(goout) enum SWIGTYPE ""
%typemap(directorin) enum SWIGTYPE
%{ $input = (intgo)$1; %}
%typemap(godirectorin) enum SWIGTYPE ""
%typemap(directorout) enum SWIGTYPE
%{ $result = ($1_ltype)$input; %}
%typemap(directorin) enum SWIGTYPE & (intgo e)
%{
e = (intgo)$1;
$input = ($1_ltype)&e;
%}
%typemap(godirectorin) enum SWIGTYPE & ""
%typemap(directorout) enum SWIGTYPE &
%{
$*1_ltype f = ($*1_ltype)*$input;
$result = ($1_ltype)&f;
%}
/* Arbitrary type. This is a type passed by value in the C/C++ code.
We convert it to a pointer for the Go code. Note that all basic
types are explicitly handled above. */
%typemap(gotype) SWIGTYPE
%{$gotypename%}
%typemap(in) SWIGTYPE ($&1_type argp)
%{
argp = ($&1_ltype)$input;
if (argp == NULL) {
_swig_gopanic("Attempt to dereference null $1_type");
}
$1 = ($1_ltype)*argp;
%}
%typemap(out) SWIGTYPE
#ifdef __cplusplus
%{ *($&1_ltype*)&$result = new $1_ltype($1); %}
#else
{
$&1_ltype $1ptr = ($&1_ltype)malloc(sizeof($1_ltype));
memmove($1ptr, &$1, sizeof($1_type));
*($&1_ltype*)&$result = $1ptr;
}
#endif
%typemap(goout) SWIGTYPE ""
%typemap(directorin) SWIGTYPE
%{ $input = new $1_ltype((const $1_ltype &)$1); %}
%typemap(godirectorin) SWIGTYPE ""
%typemap(directorout) SWIGTYPE
%{ $result = *($&1_ltype)$input; %}
/* Exception handling */
%typemap(throws) char *
%{ _swig_gopanic($1); %}
%typemap(throws) SWIGTYPE, SWIGTYPE &, SWIGTYPE &&, SWIGTYPE *, SWIGTYPE [], SWIGTYPE [ANY]
%{
(void)$1;
_swig_gopanic("C++ $1_type exception thrown");
%}
/* Typecheck typemaps. The purpose of these is merely to issue a
warning for overloaded C++ functions that cannot be overloaded in
Go as more than one C++ type maps to a single Go type. */
%typecheck(SWIG_TYPECHECK_BOOL) /* Go bool */
bool,
const bool &
""
%typecheck(SWIG_TYPECHECK_CHAR) /* Go byte */
char,
const char &,
unsigned char,
const unsigned char &
""
%typecheck(SWIG_TYPECHECK_INT8) /* Go int8 */
signed char,
const signed char &
""
%typecheck(SWIG_TYPECHECK_INT16) /* Go int16 */
short,
const short &
""
%typecheck(SWIG_TYPECHECK_INT16) /* Go uint16 */
unsigned short,
const unsigned short &
""
%typecheck(SWIG_TYPECHECK_INT32) /* Go int */
int,
const int &
""
%typecheck(SWIG_TYPECHECK_INT32) /* Go uint */
unsigned int,
const unsigned int &
""
%typecheck(SWIG_TYPECHECK_INT64) /* Go int64 */
long,
const long &,
long long,
const long long &
""
%typecheck(SWIG_TYPECHECK_INT64) /* Go uint64 */
unsigned long,
const unsigned long &,
unsigned long long,
const unsigned long long &
""
%typecheck(SWIG_TYPECHECK_FLOAT) /* Go float32 */
float,
const float &
""
%typecheck(SWIG_TYPECHECK_DOUBLE) /* Go float64 */
double,
const double &
""
%typecheck(SWIG_TYPECHECK_STRING) /* Go string */
char *,
char *&,
char[ANY],
char [],
signed char *,
signed char *&,
signed char[ANY],
signed char [],
unsigned char *,
unsigned char *&,
unsigned char[ANY],
unsigned char []
""
%typecheck(SWIG_TYPECHECK_POINTER)
SWIGTYPE,
SWIGTYPE *,
SWIGTYPE &,
SWIGTYPE &&,
SWIGTYPE *const&,
SWIGTYPE [],
SWIGTYPE (CLASS::*)
""
%apply SWIGTYPE * { SWIGTYPE *const }
%apply SWIGTYPE (CLASS::*) { SWIGTYPE (CLASS::*const) }
%apply SWIGTYPE & { SWIGTYPE (CLASS::*const&) }
/* Go keywords. */
%include <gokw.swg>
%include <goruntime.swg>

View File

@@ -0,0 +1,33 @@
/* Rename keywords. */
#define GOKW(x) %keywordwarn("'" `x` "' is a Go keyword, renaming to 'X"`x`"'",rename="X%s") `x`
#define GOBN(x) %builtinwarn("'" `x` "' conflicts with a built-in name in Go") "::"`x`
GOKW(break);
GOKW(case);
GOKW(chan);
GOKW(const);
GOKW(continue);
GOKW(default);
GOKW(defer);
GOKW(else);
GOKW(fallthrough);
GOKW(for);
GOKW(func);
GOKW(go);
GOKW(goto);
GOKW(if);
GOKW(import);
GOKW(interface);
GOKW(package);
GOKW(range);
GOKW(return);
GOKW(select);
GOKW(struct);
GOKW(switch);
GOKW(type);
GOKW(var);
GOBN(map);
#undef GOKW

View File

@@ -0,0 +1,402 @@
/* ------------------------------------------------------------
* goruntime.swg
*
* Go runtime code for the various generated files.
* ------------------------------------------------------------ */
%inline %{
static void Swig_free(void* p) {
free(p);
}
static void* Swig_malloc(int c) {
return malloc(c);
}
%}
%insert(runtime) %{
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
%}
#if SWIGGO_CGO
%insert(cgo_comment_typedefs) %{
#include <stdint.h>
%}
#endif
#if SWIGGO_INTGO_SIZE == 32
%insert(runtime) %{
typedef int intgo;
typedef unsigned int uintgo;
%}
#if SWIGGO_CGO
%insert(cgo_comment_typedefs) %{
typedef int intgo;
typedef unsigned int uintgo;
%}
#endif
#elif SWIGGO_INTGO_SIZE == 64
%insert(runtime) %{
typedef long long intgo;
typedef unsigned long long uintgo;
%}
#if SWIGGO_CGO
%insert(cgo_comment_typedefs) %{
typedef long long intgo;
typedef unsigned long long uintgo;
%}
#endif
#else
%insert(runtime) %{
typedef ptrdiff_t intgo;
typedef size_t uintgo;
%}
#if SWIGGO_CGO
%insert(cgo_comment_typedefs) %{
typedef ptrdiff_t intgo;
typedef size_t uintgo;
%}
#endif
#endif
#ifndef SWIGGO_GCCGO
// Set the host compiler struct attribute that will be
// used to match gc's struct layout. For example, on 386 Windows,
// gcc wants to 8-align int64s, but gc does not.
// Use __gcc_struct__ to work around http://gcc.gnu.org/PR52991 on x86,
// and https://golang.org/issue/5603.
// See: https://github.com/golang/go/blob/fcbf04f9b93b4cd8addd05c2ed784118eb50a46c/src/cmd/cgo/out.go#L663
%insert(runtime) %{
# if !defined(__clang__) && (defined(__i386__) || defined(__x86_64__))
# define SWIGSTRUCTPACKED __attribute__((__packed__, __gcc_struct__))
# else
# define SWIGSTRUCTPACKED __attribute__((__packed__))
# endif
%}
#else
# define SWIGSTRUCTPACKED
#endif
%insert(runtime) %{
typedef struct { char *p; intgo n; } _gostring_;
typedef struct { void* array; intgo len; intgo cap; } _goslice_;
%}
#ifdef SWIGGO_CGO
%insert(cgo_comment_typedefs) %{
typedef struct { char *p; intgo n; } _gostring_;
typedef struct { void* array; intgo len; intgo cap; } _goslice_;
%}
#endif
#ifndef SWIGGO_GCCGO
/* Boilerplate for C/C++ code when using 6g/8g. This code is compiled
with gcc. */
%insert(runtime) %{
#define swiggo_size_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2+1];
#define swiggo_size_assert(t, n) swiggo_size_assert_eq(sizeof(t), n, swiggo_sizeof_##t##_is_not_##n)
swiggo_size_assert(char, 1)
swiggo_size_assert(short, 2)
swiggo_size_assert(int, 4)
typedef long long swiggo_long_long;
swiggo_size_assert(swiggo_long_long, 8)
swiggo_size_assert(float, 4)
swiggo_size_assert(double, 8)
#ifdef __cplusplus
extern "C" {
#endif
extern void crosscall2(void (*fn)(void *, int), void *, int);
extern char* _cgo_topofstack(void) __attribute__ ((weak));
extern void _cgo_allocate(void *, int);
extern void _cgo_panic(void *, int);
#ifdef __cplusplus
}
#endif
static char *_swig_topofstack() {
if (_cgo_topofstack) {
return _cgo_topofstack();
} else {
return 0;
}
}
static void _swig_gopanic(const char *p) {
struct {
const char *p;
} SWIGSTRUCTPACKED a;
a.p = p;
crosscall2(_cgo_panic, &a, (int) sizeof a);
}
%}
#if !SWIGGO_CGO
/* This is here for backward compatibility, but it will not work
with Go 1.5 or later. Do not use it in new code. */
%insert(runtime) %{
static void *_swig_goallocate(size_t len) {
struct {
size_t len;
void *ret;
} SWIGSTRUCTPACKED a;
a.len = len;
crosscall2(_cgo_allocate, &a, (int) sizeof a);
return a.ret;
}
%}
#endif
#if !SWIGGO_CGO
/* Boilerplate for C code when using 6g/8g. This code is compiled
with 6c/8c. */
%insert(gc_header) %{
#include "runtime.h"
#include "cgocall.h"
#pragma dataflag 16
static void *cgocall = runtime·cgocall;
#pragma dataflag 16
void *·_cgo_runtime_cgocall = &cgocall;
%}
#endif
#else
/* Boilerplate for C/C++ code when using gccgo. */
%insert(runtime) %{
#define SWIGGO_GCCGO
#ifdef __cplusplus
extern "C" {
#endif
extern void *_cgo_allocate(size_t);
extern void _cgo_panic(const char *);
#ifdef __cplusplus
}
#endif
#define _swig_goallocate _cgo_allocate
#define _swig_gopanic _cgo_panic
%}
#if !SWIGGO_CGO
%insert(runtime) %{
/* Implementations of SwigCgocall and friends for different versions
of gccgo. The Go code will call these functions using C names with
a prefix of the module name. The implementations here call the
routine in libgo. The routines to call vary depending on the gccgo
version. We assume that the version of gcc used to compile this
file is the same as the version of gccgo. */
#ifdef __cplusplus
extern "C" {
#endif
#define SWIG_GCC_VERSION \
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#if SWIG_GCC_VERSION < 40700
#define SwigDoCgocall()
#define SwigDoCgocallDone()
#define SwigDoCgocallBack()
#define SwigDoCgocallBackDone()
#elif SWIG_GCC_VERSION == 40700
void SwigDoCgocall(void) __asm__("libgo_syscall.syscall.Entersyscall");
void SwigDoCgocallDone(void) __asm__("libgo_syscall.syscall.Exitsyscall");
void SwigDoCgocallBack(void) __asm__("libgo_syscall.syscall.Exitsyscall");
void SwigDoCgocallBackDone(void) __asm__("libgo_syscall.syscall.Entersyscall");
#else
void SwigDoCgocall(void) __asm__("syscall.Cgocall");
void SwigDoCgocallDone(void) __asm__("syscall.CgocallDone");
void SwigDoCgocallBack(void) __asm__("syscall.CgocallBack");
void SwigDoCgocallBackDone(void) __asm__("syscall.CgocallBackDone");
#endif
#define SWIGSTRINGIFY2(s) #s
#define SWIGSTRINGIFY(s) SWIGSTRINGIFY2(s)
void SwigCgocall()
__asm__(SWIGSTRINGIFY(SWIGGO_PREFIX) ".SwigCgocall");
void SwigCgocall() {
SwigDoCgocall();
}
void SwigCgocallDone()
__asm__(SWIGSTRINGIFY(SWIGGO_PREFIX) ".SwigCgocallDone");
void SwigCgocallDone() {
SwigDoCgocallDone();
}
void SwigCgocallBack()
__asm__(SWIGSTRINGIFY(SWIGGO_PREFIX) ".SwigCgocallBack");
void SwigCgocallBack() {
SwigDoCgocallBack();
}
void SwigCgocallBackDone()
__asm__(SWIGSTRINGIFY(SWIGGO_PREFIX) ".SwigCgocallBackDone");
void SwigCgocallBackDone() {
SwigDoCgocallBackDone();
}
#undef SWIGSTRINGIFY
#undef SWIGSTRINGIFY2
#ifdef __cplusplus
}
#endif
%}
#endif
#endif
#if !SWIGGO_CGO
%insert(runtime) %{
/* This is here for backward compatibility, but it will not work
with Go 1.5 or later. Do not use it in new code. */
static _gostring_ _swig_makegostring(const char *p, size_t l) {
_gostring_ ret;
ret.p = (char*)_swig_goallocate(l + 1);
memcpy(ret.p, p, l);
ret.n = l;
return ret;
}
%}
#endif
%insert(runtime) %{
#define SWIG_contract_assert(expr, msg) \
if (!(expr)) { _swig_gopanic(msg); } else
%}
#ifndef SWIGGO_GCCGO
%go_import("unsafe", _ "runtime/cgo")
#if !SWIGGO_CGO
%insert(go_header) %{
var _cgo_runtime_cgocall func(unsafe.Pointer, uintptr)
%}
#endif
#else
%go_import("syscall", "unsafe")
%insert(go_header) %{
type _ syscall.Sockaddr
%}
#endif
%insert(go_header) %{
type _ unsafe.Pointer
%}
/* Swig_always_false is used to conditionally assign parameters to
Swig_escape_val so that the compiler thinks that they escape. We
only assign them if Swig_always_false is true, which it never is.
We export the variable so that the compiler doesn't realize that it
is never set. */
%insert(go_header) %{
var Swig_escape_always_false bool
var Swig_escape_val interface{}
%}
/* Function pointers are translated by the code in go.cxx into
_swig_fnptr. Member pointers are translated to _swig_memberptr. */
%insert(go_header) %{
type _swig_fnptr *byte
type _swig_memberptr *byte
%}
/* For directors we need C++ to track a Go pointer. Since we can't
pass a Go pointer into C++, we use a map to track the pointers on
the Go side. */
%go_import("sync")
%insert(go_header) %{
type _ sync.Mutex
%}
%insert(go_director) %{
var swigDirectorTrack struct {
sync.Mutex
m map[int]interface{}
c int
}
func swigDirectorAdd(v interface{}) int {
swigDirectorTrack.Lock()
defer swigDirectorTrack.Unlock()
if swigDirectorTrack.m == nil {
swigDirectorTrack.m = make(map[int]interface{})
}
swigDirectorTrack.c++
ret := swigDirectorTrack.c
swigDirectorTrack.m[ret] = v
return ret
}
func swigDirectorLookup(c int) interface{} {
swigDirectorTrack.Lock()
defer swigDirectorTrack.Unlock()
ret := swigDirectorTrack.m[c]
if ret == nil {
panic("C++ director pointer not found (possible use-after-free)")
}
return ret
}
func swigDirectorDelete(c int) {
swigDirectorTrack.Lock()
defer swigDirectorTrack.Unlock()
if swigDirectorTrack.m[c] == nil {
if c > swigDirectorTrack.c {
panic("C++ director pointer invalid (possible memory corruption")
} else {
panic("C++ director pointer not found (possible use-after-free)")
}
}
delete(swigDirectorTrack.m, c)
}
%}

View File

@@ -0,0 +1,29 @@
/* ------------------------------------------------------------
* gostring.swg
*
* Support for returning strings from C to Go.
* ------------------------------------------------------------ */
// C/C++ code to convert a memory buffer into a Go string allocated in
// C/C++ memory.
%fragment("AllocateString", "runtime") %{
static _gostring_ Swig_AllocateString(const char *p, size_t l) {
_gostring_ ret;
ret.p = (char*)malloc(l);
memcpy(ret.p, p, l);
ret.n = l;
return ret;
}
%}
// Go code to convert a string allocated in C++ memory to one
// allocated in Go memory.
%fragment("CopyString", "go_runtime") %{
type swig_gostring struct { p uintptr; n int }
func swigCopyString(s string) string {
p := *(*swig_gostring)(unsafe.Pointer(&s))
r := string((*[0x7fffffff]byte)(unsafe.Pointer(p.p))[:p.n])
Swig_free(p.p)
return r
}
%}

View File

@@ -0,0 +1,4 @@
%include <std_except.i>
%apply size_t { std::size_t };
%apply const size_t& { const std::size_t& };

View File

@@ -0,0 +1 @@
%include <std/_std_deque.i>

View File

@@ -0,0 +1,31 @@
/* -----------------------------------------------------------------------------
* std_except.i
*
* Typemaps used by the STL wrappers that throw exceptions.
* These typemaps are used when methods are declared with an STL exception specification, such as
* size_t at() const throw (std::out_of_range);
* ----------------------------------------------------------------------------- */
%{
#include <typeinfo>
#include <stdexcept>
%}
namespace std
{
%ignore exception;
struct exception {};
}
%typemap(throws) std::bad_cast %{_swig_gopanic($1.what());%}
%typemap(throws) std::bad_exception %{_swig_gopanic($1.what());%}
%typemap(throws) std::domain_error %{_swig_gopanic($1.what());%}
%typemap(throws) std::exception %{_swig_gopanic($1.what());%}
%typemap(throws) std::invalid_argument %{_swig_gopanic($1.what());%}
%typemap(throws) std::length_error %{_swig_gopanic($1.what());%}
%typemap(throws) std::logic_error %{_swig_gopanic($1.what());%}
%typemap(throws) std::out_of_range %{_swig_gopanic($1.what());%}
%typemap(throws) std::overflow_error %{_swig_gopanic($1.what());%}
%typemap(throws) std::range_error %{_swig_gopanic($1.what());%}
%typemap(throws) std::runtime_error %{_swig_gopanic($1.what());%}
%typemap(throws) std::underflow_error %{_swig_gopanic($1.what());%}

View File

@@ -0,0 +1,41 @@
/* -----------------------------------------------------------------------------
* std_list.i
* ----------------------------------------------------------------------------- */
%{
#include <list>
#include <stdexcept>
%}
namespace std {
template<class T>
class list {
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
list();
list(const list& other);
size_type size() const;
bool empty() const;
%rename(isEmpty) empty;
void clear();
void push_front(const value_type& x);
void pop_front();
void push_back(const value_type& x);
void pop_back();
void remove(value_type x);
void reverse();
void unique();
void sort();
void merge(list& x);
};
}

View File

@@ -0,0 +1,66 @@
/* -----------------------------------------------------------------------------
* std_map.i
*
* SWIG typemaps for std::map
* ----------------------------------------------------------------------------- */
%include <std_common.i>
// ------------------------------------------------------------------------
// std::map
// ------------------------------------------------------------------------
%{
#include <map>
#include <algorithm>
#include <stdexcept>
%}
// exported class
namespace std {
template<class K, class T, class C = std::less<K> > class map {
// add typemaps here
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef K key_type;
typedef T mapped_type;
typedef std::pair< const K, T > value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
map();
map(const map& other);
unsigned int size() const;
bool empty() const;
void clear();
%extend {
const T& get(const K& key) throw (std::out_of_range) {
std::map< K, T, C >::iterator i = self->find(key);
if (i != self->end())
return i->second;
else
throw std::out_of_range("key not found");
}
void set(const K& key, const T& x) {
(*self)[key] = x;
}
void del(const K& key) throw (std::out_of_range) {
std::map< K, T, C >::iterator i = self->find(key);
if (i != self->end())
self->erase(i);
else
throw std::out_of_range("key not found");
}
bool has_key(const K& key) {
std::map< K, T, C >::iterator i = self->find(key);
return i != self->end();
}
}
};
}

View File

@@ -0,0 +1,36 @@
/* -----------------------------------------------------------------------------
* std_pair.i
*
* SWIG typemaps for std::pair
* ----------------------------------------------------------------------------- */
%include <std_common.i>
%include <exception.i>
// ------------------------------------------------------------------------
// std::pair
// ------------------------------------------------------------------------
%{
#include <utility>
%}
namespace std {
template<class T, class U> struct pair {
typedef T first_type;
typedef U second_type;
pair();
pair(T first, U second);
pair(const pair& other);
template <class U1, class U2> pair(const pair<U1, U2> &other);
T first;
U second;
};
// add specializations here
}

View File

@@ -0,0 +1,91 @@
/* -----------------------------------------------------------------------------
* std_string.i
*
* Typemaps for std::string and const std::string&
* These are mapped to a Go string and are passed around by value.
*
* To use non-const std::string references use the following %apply. Note
* that they are passed by value.
* %apply const std::string & {std::string &};
* ----------------------------------------------------------------------------- */
%{
#include <string>
%}
namespace std {
%naturalvar string;
class string;
%typemap(gotype) string, const string & "string"
%typemap(in) string
%{ $1.assign($input.p, $input.n); %}
%typemap(godirectorout) string
%{
{
p := Swig_malloc(len($input))
s := (*[1<<30]byte)(unsafe.Pointer(p))[:len($input)]
copy(s, $input)
$result = *(*string)(unsafe.Pointer(&s))
}
%}
%typemap(directorout) string
%{
$result.assign($input.p, $input.n);
free($input.p);
%}
%typemap(out,fragment="AllocateString") string
%{ $result = Swig_AllocateString($1.data(), $1.length()); %}
%typemap(goout,fragment="CopyString") string
%{ $result = swigCopyString($1) %}
%typemap(directorin,fragment="AllocateString") string
%{ $input = Swig_AllocateString($1.data(), $1.length()); %}
%typemap(godirectorin,fragment="CopyString") string
%{ $result = swigCopyString($input) %}
%typemap(in) const string &
%{
$*1_ltype $1_str($input.p, $input.n);
$1 = &$1_str;
%}
%typemap(godirectorout) const string &
%{
{
p := Swig_malloc(len($input))
s := (*[1<<30]byte)(unsafe.Pointer(p))[:len($input)]
copy(s, $input)
$result = *(*string)(unsafe.Pointer(&s))
}
%}
%typemap(directorout,warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const string &
%{
static $*1_ltype $1_str;
$1_str.assign($input.p, $input.n);
free($input.p);
$result = &$1_str;
%}
%typemap(out,fragment="AllocateString") const string &
%{ $result = Swig_AllocateString((*$1).data(), (*$1).length()); %}
%typemap(goout,fragment="CopyString") const string &
%{ $result = swigCopyString($1) %}
%typemap(directorin,fragment="AllocateString") const string &
%{ $input = Swig_AllocateString($1.data(), $1.length()); %}
%typemap(godirectorin,fragment="CopyString") const string &
%{ $result = swigCopyString($input) %}
}

View File

@@ -0,0 +1,92 @@
/* -----------------------------------------------------------------------------
* std_vector.i
* ----------------------------------------------------------------------------- */
%{
#include <vector>
#include <stdexcept>
%}
namespace std {
template<class T> class vector {
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
vector();
vector(size_type n);
vector(const vector& other);
size_type size() const;
size_type capacity() const;
void reserve(size_type n);
%rename(isEmpty) empty;
bool empty() const;
void clear();
%rename(add) push_back;
void push_back(const value_type& x);
%extend {
const_reference get(int i) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
return (*self)[i];
else
throw std::out_of_range("vector index out of range");
}
void set(int i, const value_type& val) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
(*self)[i] = val;
else
throw std::out_of_range("vector index out of range");
}
}
};
// bool specialization
template<> class vector<bool> {
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef bool value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef bool const_reference;
vector();
vector(size_type n);
vector(const vector& other);
size_type size() const;
size_type capacity() const;
void reserve(size_type n);
%rename(isEmpty) empty;
bool empty() const;
void clear();
%rename(add) push_back;
void push_back(const value_type& x);
%extend {
bool get(int i) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
return (*self)[i];
else
throw std::out_of_range("vector index out of range");
}
void set(int i, const value_type& val) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
(*self)[i] = val;
else
throw std::out_of_range("vector index out of range");
}
}
};
}

View File

@@ -0,0 +1,10 @@
/* -----------------------------------------------------------------------------
* stl.i
* ----------------------------------------------------------------------------- */
%include <std_common.i>
%include <std_string.i>
%include <std_vector.i>
%include <std_map.i>
%include <std_pair.i>

View File

@@ -0,0 +1,298 @@
/* -----------------------------------------------------------------------------
* typemaps.i
*
* Pointer and reference handling typemap library
*
* These mappings provide support for input/output arguments and common
* uses for C/C++ pointers and C++ references.
* ----------------------------------------------------------------------------- */
/*
INPUT typemaps
--------------
These typemaps remap a C pointer or C++ reference to be an "INPUT" value which is
passed by value instead of reference.
The following typemaps can be applied to turn a pointer or reference into a simple
input value. That is, instead of passing a pointer or reference to an object,
you would use a real value instead.
bool *INPUT, bool &INPUT
signed char *INPUT, signed char &INPUT
unsigned char *INPUT, unsigned char &INPUT
short *INPUT, short &INPUT
unsigned short *INPUT, unsigned short &INPUT
int *INPUT, int &INPUT
unsigned int *INPUT, unsigned int &INPUT
long *INPUT, long &INPUT
unsigned long *INPUT, unsigned long &INPUT
long long *INPUT, long long &INPUT
unsigned long long *INPUT, unsigned long long &INPUT
float *INPUT, float &INPUT
double *INPUT, double &INPUT
To use these, suppose you had a C function like this :
double fadd(double *a, double *b) {
return *a+*b;
}
You could wrap it with SWIG as follows :
%include <typemaps.i>
double fadd(double *INPUT, double *INPUT);
or you can use the %apply directive :
%include <typemaps.i>
%apply double *INPUT { double *a, double *b };
double fadd(double *a, double *b);
In Go you could then use it like this:
answer := modulename.Fadd(10.0, 20.0)
There are no char *INPUT typemaps, however you can apply the signed
char * typemaps instead:
%include <typemaps.i>
%apply signed char *INPUT {char *input};
void f(char *input);
*/
%define INPUT_TYPEMAP(TYPE, GOTYPE)
%typemap(gotype) TYPE *INPUT, TYPE &INPUT "GOTYPE"
%typemap(in) TYPE *INPUT, TYPE &INPUT
%{ $1 = ($1_ltype)&$input; %}
%typemap(out) TYPE *INPUT, TYPE &INPUT ""
%typemap(goout) TYPE *INPUT, TYPE &INPUT ""
%typemap(freearg) TYPE *INPUT, TYPE &INPUT ""
%typemap(argout) TYPE *INPUT, TYPE &INPUT ""
// %typemap(typecheck) TYPE *INPUT = TYPE;
// %typemap(typecheck) TYPE &INPUT = TYPE;
%enddef
INPUT_TYPEMAP(bool, bool);
INPUT_TYPEMAP(signed char, int8);
INPUT_TYPEMAP(char, byte);
INPUT_TYPEMAP(unsigned char, byte);
INPUT_TYPEMAP(short, int16);
INPUT_TYPEMAP(unsigned short, uint16);
INPUT_TYPEMAP(int, int);
INPUT_TYPEMAP(unsigned int, uint);
INPUT_TYPEMAP(long, int64);
INPUT_TYPEMAP(unsigned long, uint64);
INPUT_TYPEMAP(long long, int64);
INPUT_TYPEMAP(unsigned long long, uint64);
INPUT_TYPEMAP(float, float32);
INPUT_TYPEMAP(double, float64);
#undef INPUT_TYPEMAP
// OUTPUT typemaps. These typemaps are used for parameters that
// are output only. An array replaces the c pointer or reference parameter.
// The output value is returned in this array passed in.
/*
OUTPUT typemaps
---------------
The following typemaps can be applied to turn a pointer or reference
into an "output" value. When calling a function, no input value would
be given for a parameter, but an output value would be returned. This
works by a Go slice being passed as a parameter where a c pointer or
reference is required. As with any Go function, the array is passed
by reference so that any modifications to the array will be picked up
in the calling function. Note that the array passed in MUST have at
least one element, but as the c function does not require any input,
the value can be set to anything.
bool *OUTPUT, bool &OUTPUT
signed char *OUTPUT, signed char &OUTPUT
unsigned char *OUTPUT, unsigned char &OUTPUT
short *OUTPUT, short &OUTPUT
unsigned short *OUTPUT, unsigned short &OUTPUT
int *OUTPUT, int &OUTPUT
unsigned int *OUTPUT, unsigned int &OUTPUT
long *OUTPUT, long &OUTPUT
unsigned long *OUTPUT, unsigned long &OUTPUT
long long *OUTPUT, long long &OUTPUT
unsigned long long *OUTPUT, unsigned long long &OUTPUT
float *OUTPUT, float &OUTPUT
double *OUTPUT, double &OUTPUT
For example, suppose you were trying to wrap the modf() function in the
C math library which splits x into integral and fractional parts (and
returns the integer part in one of its parameters):
double modf(double x, double *ip);
You could wrap it with SWIG as follows :
%include <typemaps.i>
double modf(double x, double *OUTPUT);
or you can use the %apply directive :
%include <typemaps.i>
%apply double *OUTPUT { double *ip };
double modf(double x, double *ip);
The Go output of the function would be the function return value and the
value in the single element array. In Go you would use it like this:
ptr := []float64{0.0}
fraction := modulename.Modf(5.0,ptr)
There are no char *OUTPUT typemaps, however you can apply the signed
char * typemaps instead:
%include <typemaps.i>
%apply signed char *OUTPUT {char *output};
void f(char *output);
*/
%define OUTPUT_TYPEMAP(TYPE, GOTYPE)
%typemap(gotype) TYPE *OUTPUT, TYPE &OUTPUT %{[]GOTYPE%}
%typemap(in) TYPE *OUTPUT($*1_ltype temp), TYPE &OUTPUT($*1_ltype temp)
{
if ($input.len == 0) {
_swig_gopanic("array must contain at least 1 element");
}
$1 = &temp;
}
%typemap(out) TYPE *OUTPUT, TYPE &OUTPUT ""
%typemap(goout) TYPE *INPUT, TYPE &INPUT ""
%typemap(freearg) TYPE *OUTPUT, TYPE &OUTPUT ""
%typemap(argout) TYPE *OUTPUT, TYPE &OUTPUT
{
TYPE* a = (TYPE *) $input.array;
a[0] = temp$argnum;
}
%enddef
OUTPUT_TYPEMAP(bool, bool);
OUTPUT_TYPEMAP(signed char, int8);
OUTPUT_TYPEMAP(char, byte);
OUTPUT_TYPEMAP(unsigned char, byte);
OUTPUT_TYPEMAP(short, int16);
OUTPUT_TYPEMAP(unsigned short, uint16);
OUTPUT_TYPEMAP(int, int);
OUTPUT_TYPEMAP(unsigned int, uint);
OUTPUT_TYPEMAP(long, int64);
OUTPUT_TYPEMAP(unsigned long, uint64);
OUTPUT_TYPEMAP(long long, int64);
OUTPUT_TYPEMAP(unsigned long long, uint64);
OUTPUT_TYPEMAP(float, float32);
OUTPUT_TYPEMAP(double, float64);
#undef OUTPUT_TYPEMAP
/*
INOUT typemaps
--------------
Mappings for a parameter that is both an input and an output parameter
The following typemaps can be applied to make a function parameter both
an input and output value. This combines the behavior of both the
"INPUT" and "OUTPUT" typemaps described earlier. Output values are
returned as an element in a Go slice.
bool *INOUT, bool &INOUT
signed char *INOUT, signed char &INOUT
unsigned char *INOUT, unsigned char &INOUT
short *INOUT, short &INOUT
unsigned short *INOUT, unsigned short &INOUT
int *INOUT, int &INOUT
unsigned int *INOUT, unsigned int &INOUT
long *INOUT, long &INOUT
unsigned long *INOUT, unsigned long &INOUT
long long *INOUT, long long &INOUT
unsigned long long *INOUT, unsigned long long &INOUT
float *INOUT, float &INOUT
double *INOUT, double &INOUT
For example, suppose you were trying to wrap the following function :
void neg(double *x) {
*x = -(*x);
}
You could wrap it with SWIG as follows :
%include <typemaps.i>
void neg(double *INOUT);
or you can use the %apply directive :
%include <typemaps.i>
%apply double *INOUT { double *x };
void neg(double *x);
This works similarly to C in that the mapping directly modifies the
input value - the input must be an array with a minimum of one element.
The element in the array is the input and the output is the element in
the array.
x := []float64{5.0}
Neg(x);
The implementation of the OUTPUT and INOUT typemaps is different to
other languages in that other languages will return the output value
as part of the function return value. This difference is due to Go
being a typed language.
There are no char *INOUT typemaps, however you can apply the signed
char * typemaps instead:
%include <typemaps.i>
%apply signed char *INOUT {char *inout};
void f(char *inout);
*/
%define INOUT_TYPEMAP(TYPE, GOTYPE)
%typemap(gotype) TYPE *INOUT, TYPE &INOUT %{[]GOTYPE%}
%typemap(in) TYPE *INOUT, TYPE &INOUT {
if ($input.len == 0) {
_swig_gopanic("array must contain at least 1 element");
}
$1 = ($1_ltype) $input.array;
}
%typemap(out) TYPE *INOUT, TYPE &INOUT ""
%typemap(goout) TYPE *INOUT, TYPE &INOUT ""
%typemap(freearg) TYPE *INOUT, TYPE &INOUT ""
%typemap(argout) TYPE *INOUT, TYPE &INOUT ""
%enddef
INOUT_TYPEMAP(bool, bool);
INOUT_TYPEMAP(signed char, int8);
INOUT_TYPEMAP(char, byte);
INOUT_TYPEMAP(unsigned char, byte);
INOUT_TYPEMAP(short, int16);
INOUT_TYPEMAP(unsigned short, uint16);
INOUT_TYPEMAP(int, int);
INOUT_TYPEMAP(unsigned int, uint);
INOUT_TYPEMAP(long, int64);
INOUT_TYPEMAP(unsigned long, uint64);
INOUT_TYPEMAP(long long, int64);
INOUT_TYPEMAP(unsigned long long, uint64);
INOUT_TYPEMAP(float, float32);
INOUT_TYPEMAP(double, float64);
#undef INOUT_TYPEMAP